YourLocations persist refactor
This commit is contained in:
parent
a9329e3824
commit
b22df7a8e2
2 changed files with 18 additions and 30 deletions
|
|
@ -2,37 +2,20 @@ import 'dart:async';
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:fires_flutter/models/yourLocation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../globals.dart' as globals;
|
||||
|
||||
final String locationKey = 'yourlocations';
|
||||
|
||||
Future<List<YourLocation>> loadYourLocations() async {
|
||||
return await globals.prefs.then((prefs) {
|
||||
return loadYourLocationsWithPrefs(prefs);
|
||||
});
|
||||
}
|
||||
|
||||
persistYourLocations(List<YourLocation> yl) {
|
||||
globals.prefs.then((prefs) {
|
||||
List<String> yourLocationsAsString = [];
|
||||
yl.forEach((location) {
|
||||
yourLocationsAsString.add(json.encode(location.toJson()));
|
||||
});
|
||||
prefs.setStringList(locationKey, yourLocationsAsString);
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<YourLocation>> loadYourLocationsWithPrefs(
|
||||
SharedPreferences prefs) async {
|
||||
return await globals.prefs.then((prefs) {
|
||||
List<String> yourLocations = prefs.getStringList(locationKey);
|
||||
List<YourLocation> persistedList = List<YourLocation>();
|
||||
if (yourLocations == null) {
|
||||
yourLocations = [];
|
||||
persistYourLocations(<YourLocation>[]);
|
||||
// first run, init with empty list
|
||||
persistYourLocations(persistedList);
|
||||
}
|
||||
List<YourLocation> persistedList = List<YourLocation>();
|
||||
yourLocations.forEach((locationString) {
|
||||
Map locationMap = json.decode(locationString);
|
||||
persistedList.add(YourLocation.fromJson(locationMap));
|
||||
|
|
@ -40,3 +23,13 @@ Future<List<YourLocation>> loadYourLocationsWithPrefs(
|
|||
return persistedList;
|
||||
});
|
||||
}
|
||||
|
||||
persistYourLocations(List<YourLocation> yl) {
|
||||
globals.prefs.then((prefs) {
|
||||
List<String> ylAsString = [];
|
||||
yl.forEach((location) {
|
||||
ylAsString.add(json.encode(location.toJson()));
|
||||
});
|
||||
prefs.setStringList(locationKey, ylAsString);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue