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';
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);
});
}
......
......@@ -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();
......
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