Commit 58296881 authored by Collin Jackson's avatar Collin Jackson

Merge pull request #691 from collinjackson/settings_save

Add support for saving settings in fitness app
parents 8a3285b6 c890dde7
...@@ -20,9 +20,23 @@ part 'meal.dart'; ...@@ -20,9 +20,23 @@ part 'meal.dart';
part 'measurement.dart'; part 'measurement.dart';
part 'settings.dart'; part 'settings.dart';
class UserData { abstract class UserData {
BackupMode get backupMode;
List<FitnessItem> get items;
}
class UserDataImpl extends UserData {
UserDataImpl();
List<FitnessItem> _items = []; List<FitnessItem> _items = [];
BackupMode _backupMode;
BackupMode get backupMode => _backupMode;
void setBackupModeAndSave(BackupMode value) {
_backupMode = value;
save();
}
List<FitnessItem> get items => _items; List<FitnessItem> get items => _items;
void set items(List<FitnessItem> newItems) { void set items(List<FitnessItem> newItems) {
_items = []; _items = [];
...@@ -45,24 +59,40 @@ class UserData { ...@@ -45,24 +59,40 @@ class UserData {
save(); 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 { class FitnessApp extends App {
NavigationState _navigationState; NavigationState _navigationState;
final UserData _userData = new UserData(); UserDataImpl _userData = new UserDataImpl();
void didMount() { void didMount() {
super.didMount(); super.didMount();
loadFitnessData().then((List<Measurement> list) { loadFitnessData().then((UserData data) {
setState(() => _userData.items = list); setState(() => _userData = data);
}).catchError((e) => print("Failed to load data: $e")); }).catchError((e) => print("Failed to load data: $e"));
} }
void save() {
_userData.save().catchError((e) => print("Failed to load data: $e"));
}
void initState() { void initState() {
_navigationState = new NavigationState([ _navigationState = new NavigationState([
new Route( new Route(
...@@ -90,7 +120,11 @@ class FitnessApp extends App { ...@@ -90,7 +120,11 @@ class FitnessApp extends App {
), ),
new Route( new Route(
name: '/settings', 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(); super.initState();
...@@ -112,12 +146,10 @@ class FitnessApp extends App { ...@@ -112,12 +146,10 @@ class FitnessApp extends App {
setState(() => _userData.removeAndSave(item)); setState(() => _userData.removeAndSave(item));
} }
BackupMode backupSetting = BackupMode.disabled;
void settingsUpdater({ BackupMode backup }) { void settingsUpdater({ BackupMode backup }) {
setState(() { setState(() {
if (backup != null) if (backup != null)
backupSetting = backup; _userData.setBackupModeAndSave(backup);
}); });
} }
......
...@@ -10,10 +10,10 @@ typedef void SettingsUpdater({ ...@@ -10,10 +10,10 @@ typedef void SettingsUpdater({
class SettingsFragment extends Component { class SettingsFragment extends Component {
SettingsFragment(this.navigator, this.backup, this.updater); SettingsFragment({ this.navigator, this.userData, this.updater });
final Navigator navigator; final Navigator navigator;
final BackupMode backup; final UserData userData;
final SettingsUpdater updater; final SettingsUpdater updater;
void _handleBackupChanged(bool value) { void _handleBackupChanged(bool value) {
...@@ -38,10 +38,10 @@ class SettingsFragment extends Component { ...@@ -38,10 +38,10 @@ class SettingsFragment extends Component {
padding: const EdgeDims.symmetric(vertical: 20.0), padding: const EdgeDims.symmetric(vertical: 20.0),
child: new Block([ child: new Block([
new DrawerItem( new DrawerItem(
onPressed: () { _handleBackupChanged(!(backup == BackupMode.enabled)); }, onPressed: () { _handleBackupChanged(!(userData.backupMode == BackupMode.enabled)); },
children: [ children: [
new Flexible(child: new Text('Back up data to the cloud')), 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)
] ]
) )
]) ])
......
...@@ -21,20 +21,16 @@ Future<String> dataFilePath() async { ...@@ -21,20 +21,16 @@ Future<String> dataFilePath() async {
return cachedDataFilePath; return cachedDataFilePath;
} }
Future<List<Measurement>> loadFitnessData() async { Future<UserData> loadFitnessData() async {
List<Measurement> items = [];
String dataPath = await dataFilePath(); String dataPath = await dataFilePath();
print("Loading from $dataPath"); print("Loading from $dataPath");
JsonDecoder decoder = new JsonDecoder(); JsonDecoder decoder = new JsonDecoder();
var data = await decoder.convert(await new File(dataPath).readAsString()); Map data = await decoder.convert(await new File(dataPath).readAsString());
data.forEach((item) { return new UserDataImpl.fromJson(data);
items.add(new Measurement.fromJson(item));
});
return items;
} }
// Intentionally synchronous for execution just before shutdown. // Intentionally synchronous for execution just before shutdown.
Future saveFitnessData(List<Measurement> data) async { Future saveFitnessData(UserDataImpl data) async {
String dataPath = await dataFilePath(); String dataPath = await dataFilePath();
print("Saving to $dataPath"); print("Saving to $dataPath");
JsonEncoder encoder = new JsonEncoder(); JsonEncoder encoder = new JsonEncoder();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment