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
58296881
Commit
58296881
authored
Aug 20, 2015
by
Collin Jackson
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #691 from collinjackson/settings_save
Add support for saving settings in fitness app
parents
8a3285b6
c890dde7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
25 deletions
+53
-25
main.dart
examples/fitness/lib/main.dart
+45
-13
settings.dart
examples/fitness/lib/settings.dart
+4
-4
user_data.dart
examples/fitness/lib/user_data.dart
+4
-8
No files found.
examples/fitness/lib/main.dart
View file @
58296881
...
...
@@ -20,9 +20,23 @@ part 'meal.dart';
part
'measurement.dart'
;
part
'settings.dart'
;
class
UserData
{
abstract
class
UserData
{
BackupMode
get
backupMode
;
List
<
FitnessItem
>
get
items
;
}
class
UserDataImpl
extends
UserData
{
UserDataImpl
();
List
<
FitnessItem
>
_items
=
[];
BackupMode
_backupMode
;
BackupMode
get
backupMode
=>
_backupMode
;
void
setBackupModeAndSave
(
BackupMode
value
)
{
_backupMode
=
value
;
save
();
}
List
<
FitnessItem
>
get
items
=>
_items
;
void
set
items
(
List
<
FitnessItem
>
newItems
)
{
_items
=
[];
...
...
@@ -45,24 +59,40 @@ class UserData {
save
();
}
Future
save
()
=>
saveFitnessData
(
_items
);
Future
save
()
=>
saveFitnessData
(
this
);
UserDataImpl
.
fromJson
(
Map
json
)
{
json
[
'items'
].
forEach
((
item
)
{
_items
.
add
(
new
Measurement
.
fromJson
(
item
));
});
try
{
_backupMode
=
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'
]
=
_backupMode
.
toString
();
return
json
;
}
}
class
FitnessApp
extends
App
{
NavigationState
_navigationState
;
final
UserData
_userData
=
new
UserData
();
UserDataImpl
_userData
=
new
UserDataImpl
();
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
"
));
}
void
save
()
{
_userData
.
save
().
catchError
((
e
)
=>
print
(
"Failed to load data:
$e
"
));
}
void
initState
()
{
_navigationState
=
new
NavigationState
([
new
Route
(
...
...
@@ -90,7 +120,11 @@ class FitnessApp extends App {
),
new
Route
(
name:
'/settings'
,
builder:
(
navigator
,
route
)
=>
new
SettingsFragment
(
navigator
,
backupSetting
,
settingsUpdater
)
builder:
(
navigator
,
route
)
=>
new
SettingsFragment
(
navigator:
navigator
,
userData:
_userData
as
UserData
,
updater:
settingsUpdater
)
),
]);
super
.
initState
();
...
...
@@ -112,12 +146,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/settings.dart
View file @
58296881
...
...
@@ -10,10 +10,10 @@ typedef void SettingsUpdater({
class
SettingsFragment
extends
Component
{
SettingsFragment
(
this
.
navigator
,
this
.
backup
,
this
.
updater
);
SettingsFragment
(
{
this
.
navigator
,
this
.
userData
,
this
.
updater
}
);
final
Navigator
navigator
;
final
BackupMode
backup
;
final
UserData
userData
;
final
SettingsUpdater
updater
;
void
_handleBackupChanged
(
bool
value
)
{
...
...
@@ -38,10 +38,10 @@ class SettingsFragment extends Component {
padding:
const
EdgeDims
.
symmetric
(
vertical:
20.0
),
child:
new
Block
([
new
DrawerItem
(
onPressed:
()
{
_handleBackupChanged
(!(
backup
==
BackupMode
.
enabled
));
},
onPressed:
()
{
_handleBackupChanged
(!(
userData
.
backupMode
==
BackupMode
.
enabled
));
},
children:
[
new
Flexible
(
child:
new
Text
(
'Back up data to the cloud'
)),
new
Switch
(
value:
backup
==
BackupMode
.
enabled
,
onChanged:
_handleBackupChanged
)
new
Switch
(
value:
userData
.
backupMode
==
BackupMode
.
enabled
,
onChanged:
_handleBackupChanged
)
]
)
])
...
...
examples/fitness/lib/user_data.dart
View file @
58296881
...
...
@@ -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
UserDataImpl
.
fromJson
(
data
);
}
// Intentionally synchronous for execution just before shutdown.
Future
saveFitnessData
(
List
<
Measurement
>
data
)
async
{
Future
saveFitnessData
(
UserDataImpl
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