Persist your saved locations

This commit is contained in:
vjrj 2018-06-27 08:59:23 +02:00
parent c9941429f0
commit 0ad2fce4f3
5 changed files with 19 additions and 33 deletions

View file

@ -1,36 +0,0 @@
import 'dart:convert';
import 'basicLocation.dart';
import 'globals.dart' as globals;
import 'package:shared_preferences/shared_preferences.dart';
final String locationKey = 'yourlocations';
void loadYourLocations() {
globals.prefs.then((prefs) {
loadYourLocationsWithPrefs(prefs);
});
}
persistYourLocations() {
globals.prefs.then((prefs) {
List<String> yourLocationsAsString = [];
globals.yourLocations.forEach((location) {
yourLocationsAsString.add(json.encode(location.toJson()));
});
prefs.setStringList(locationKey, yourLocationsAsString);
});
}
void loadYourLocationsWithPrefs(SharedPreferences prefs) {
globals.prefs.then((prefs) {
List<String> yourLocations = prefs.getStringList(locationKey);
if (yourLocations == null) {
yourLocations = [];
persistYourLocations();
}
yourLocations.forEach((locationString) {
Map locationMap = json.decode(locationString);
globals.yourLocations.add(BasicLocation.fromJson(locationMap));
});
});
}