Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
09216794
Commit
09216794
authored
Aug 19, 2015
by
Collin Jackson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for saving settings in fitness app
parent
7f4aa210
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
16 deletions
+43
-16
main.dart
examples/fitness/lib/main.dart
+39
-8
user_data.dart
examples/fitness/lib/user_data.dart
+4
-8
No files found.
examples/fitness/lib/main.dart
View file @
09216794
...
...
@@ -21,8 +21,17 @@ part 'measurement.dart';
part
'settings.dart'
;
class
UserData
{
UserData
();
List
<
FitnessItem
>
_items
=
[];
BackupMode
_backupSetting
;
BackupMode
get
backupSetting
=>
_backupSetting
;
void
setBackupModeAndSave
(
BackupMode
value
)
{
_backupSetting
=
value
;
save
();
}
List
<
FitnessItem
>
get
items
=>
_items
;
void
set
items
(
List
<
FitnessItem
>
newItems
)
{
_items
=
[];
...
...
@@ -45,17 +54,37 @@ class UserData {
save
();
}
Future
save
()
=>
saveFitnessData
(
_items
);
Future
save
()
=>
saveFitnessData
(
this
);
UserData
.
fromJson
(
Map
json
)
{
json
[
'items'
].
forEach
((
item
)
{
_items
.
add
(
new
Measurement
.
fromJson
(
item
));
});
try
{
_backupSetting
=
BackupMode
.
values
.
firstWhere
((
BackupMode
mode
)
{
return
mode
.
toString
()
==
json
[
'backupMode'
];
});
}
catch
(
e
)
{
print
(
"Failed to load backup mode:
${e}
"
);
}
}
Map
toJson
()
{
Map
json
=
new
Map
();
json
[
'items'
]
=
_items
.
map
((
item
)
=>
item
.
toJson
()).
toList
();
json
[
'backupMode'
]
=
_backupSetting
.
toString
();
return
json
;
}
}
class
FitnessApp
extends
App
{
NavigationState
_navigationState
;
final
UserData
_userData
=
new
UserData
();
UserData
_userData
=
new
UserData
();
void
didMount
()
{
super
.
didMount
();
loadFitnessData
().
then
((
List
<
Measurement
>
list
)
{
setState
(()
=>
_userData
.
items
=
list
);
loadFitnessData
().
then
((
UserData
data
)
{
setState
(()
=>
_userData
=
data
);
}).
catchError
((
e
)
=>
print
(
"Failed to load data:
$e
"
));
}
...
...
@@ -90,7 +119,11 @@ class FitnessApp extends App {
),
new
Route
(
name:
'/settings'
,
builder:
(
navigator
,
route
)
=>
new
SettingsFragment
(
navigator
,
backupSetting
,
settingsUpdater
)
builder:
(
navigator
,
route
)
=>
new
SettingsFragment
(
navigator
,
_userData
.
backupSetting
,
settingsUpdater
)
),
]);
super
.
initState
();
...
...
@@ -112,12 +145,10 @@ class FitnessApp extends App {
setState
(()
=>
_userData
.
removeAndSave
(
item
));
}
BackupMode
backupSetting
=
BackupMode
.
disabled
;
void
settingsUpdater
({
BackupMode
backup
})
{
setState
(()
{
if
(
backup
!=
null
)
backupSetting
=
backup
;
_userData
.
setBackupModeAndSave
(
backup
)
;
});
}
...
...
examples/fitness/lib/user_data.dart
View file @
09216794
...
...
@@ -21,20 +21,16 @@ Future<String> dataFilePath() async {
return
cachedDataFilePath
;
}
Future
<
List
<
Measurement
>>
loadFitnessData
()
async
{
List
<
Measurement
>
items
=
[];
Future
<
UserData
>
loadFitnessData
()
async
{
String
dataPath
=
await
dataFilePath
();
print
(
"Loading from
$dataPath
"
);
JsonDecoder
decoder
=
new
JsonDecoder
();
var
data
=
await
decoder
.
convert
(
await
new
File
(
dataPath
).
readAsString
());
data
.
forEach
((
item
)
{
items
.
add
(
new
Measurement
.
fromJson
(
item
));
});
return
items
;
Map
data
=
await
decoder
.
convert
(
await
new
File
(
dataPath
).
readAsString
());
return
new
UserData
.
fromJson
(
data
);
}
// Intentionally synchronous for execution just before shutdown.
Future
saveFitnessData
(
List
<
Measurement
>
data
)
async
{
Future
saveFitnessData
(
UserData
data
)
async
{
String
dataPath
=
await
dataFilePath
();
print
(
"Saving to
$dataPath
"
);
JsonEncoder
encoder
=
new
JsonEncoder
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment