Commit 09216794 authored by Collin Jackson's avatar Collin Jackson

Add support for saving settings in fitness app

parent 7f4aa210
...@@ -21,8 +21,17 @@ part 'measurement.dart'; ...@@ -21,8 +21,17 @@ part 'measurement.dart';
part 'settings.dart'; part 'settings.dart';
class UserData { class UserData {
UserData();
List<FitnessItem> _items = []; List<FitnessItem> _items = [];
BackupMode _backupSetting;
BackupMode get backupSetting => _backupSetting;
void setBackupModeAndSave(BackupMode value) {
_backupSetting = 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,17 +54,37 @@ class UserData { ...@@ -45,17 +54,37 @@ class UserData {
save(); 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 { class FitnessApp extends App {
NavigationState _navigationState; NavigationState _navigationState;
final UserData _userData = new UserData(); UserData _userData = new UserData();
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"));
} }
...@@ -90,7 +119,11 @@ class FitnessApp extends App { ...@@ -90,7 +119,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,
_userData.backupSetting,
settingsUpdater
)
), ),
]); ]);
super.initState(); super.initState();
...@@ -112,12 +145,10 @@ class FitnessApp extends App { ...@@ -112,12 +145,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);
}); });
} }
......
...@@ -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 UserData.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(UserData 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