flutter/examples/fitness/lib/user_data.dart
Adam Barth 7064551a45 Move mojo frontend into services.dart
What's important about this code is that it's presenting services outside the
VM, not the particular technology used to present the services.
2015-09-16 19:13:50 -07:00

41 lines
1.2 KiB
Dart

// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'dart:io';
import 'dart:async';
import 'package:path/path.dart' as path;
import 'main.dart';
import 'package:sky/services.dart';
String cachedDataFilePath = null;
Future<String> dataFilePath() async {
if (cachedDataFilePath == null) {
String dataDir = await getFilesDir();
cachedDataFilePath = path.join(dataDir, 'data.json');
}
return cachedDataFilePath;
}
Future<UserData> loadFitnessData() async {
String dataPath = await dataFilePath();
print("Loading from $dataPath");
JsonDecoder decoder = new JsonDecoder();
Map data = await decoder.convert(await new File(dataPath).readAsString());
return new UserDataImpl.fromJson(data);
}
// Intentionally synchronous for execution just before shutdown.
Future saveFitnessData(UserDataImpl data) async {
String dataPath = await dataFilePath();
print("Saving to $dataPath");
JsonEncoder encoder = new JsonEncoder();
String contents = await encoder.convert(data);
File dataFile = await new File(dataPath).writeAsString(contents);
print("Success! $dataFile");
}