YourLocations persist refactor
This commit is contained in:
parent
a9329e3824
commit
b22df7a8e2
2 changed files with 18 additions and 30 deletions
|
|
@ -1,42 +1,39 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:comunes_flutter/comunes_flutter.dart';
|
import 'package:comunes_flutter/comunes_flutter.dart';
|
||||||
|
import 'package:fires_flutter/models/yourLocationPersist.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_simple_dependency_injection/injector.dart';
|
||||||
import 'package:redux/redux.dart';
|
import 'package:redux/redux.dart';
|
||||||
|
|
||||||
import 'firebaseMessagingConf.dart';
|
import 'firebaseMessagingConf.dart';
|
||||||
import 'firesApp.dart';
|
import 'firesApp.dart';
|
||||||
import 'globals.dart' as globals;
|
|
||||||
import 'models/appState.dart';
|
import 'models/appState.dart';
|
||||||
import 'models/firesApi.dart';
|
import 'models/firesApi.dart';
|
||||||
import 'redux/fetchDataMiddleware.dart';
|
import 'redux/fetchDataMiddleware.dart';
|
||||||
import 'redux/reducers.dart';
|
import 'redux/reducers.dart';
|
||||||
import 'package:fires_flutter/models/yourLocationPersist.dart';
|
|
||||||
import 'package:flutter_simple_dependency_injection/injector.dart';
|
|
||||||
|
|
||||||
Future<Map<String, dynamic>> loadSecrets() async {
|
Future<Map<String, dynamic>> loadSecrets() async {
|
||||||
return await SecretLoader(secretPath: 'assets/private-settings.json').load();
|
return await SecretLoader(secretPath: 'assets/private-settings.json').load();
|
||||||
}
|
}
|
||||||
|
|
||||||
void mainCommon(List<Middleware<AppState>> otherMiddleware) {
|
void mainCommon(List<Middleware<AppState>> otherMiddleware) {
|
||||||
|
|
||||||
final injector = Injector.getInjector();
|
final injector = Injector.getInjector();
|
||||||
injector.map(FiresApi, (i) => new FiresApi(), isSingleton: true);
|
injector.map(FiresApi, (i) => new FiresApi(), isSingleton: true);
|
||||||
loadSecrets().then((secrets) {
|
loadSecrets().then((secrets) {
|
||||||
|
|
||||||
final store = new Store<AppState>(appStateReducer,
|
final store = new Store<AppState>(appStateReducer,
|
||||||
initialState: new AppState(
|
initialState: new AppState(
|
||||||
gmapKey: secrets['gmapKey'],
|
gmapKey: secrets['gmapKey'],
|
||||||
firesApiKey: secrets['firesApiKey'],
|
firesApiKey: secrets['firesApiKey'],
|
||||||
firesApiUrl: secrets['firesApiUrl'] + "api/v1/"),
|
firesApiUrl: secrets['firesApiUrl'] + "api/v1/"),
|
||||||
middleware: List.from(otherMiddleware)..add(fetchYourLocationsMiddleware));
|
middleware: List.from(otherMiddleware)
|
||||||
|
..add(fetchYourLocationsMiddleware));
|
||||||
|
|
||||||
injector.map(String, (i) => store.state.firesApiUrl, key: "firesApiUrl");
|
injector.map(String, (i) => store.state.firesApiUrl, key: "firesApiUrl");
|
||||||
injector.map(String, (i) => store.state.firesApiKey, key: "firesApiKey");
|
injector.map(String, (i) => store.state.firesApiKey, key: "firesApiKey");
|
||||||
injector.map(String, (i) => store.state.gmapKey, key: "gmapKey");
|
injector.map(String, (i) => store.state.gmapKey, key: "gmapKey");
|
||||||
|
|
||||||
globals.prefs.then((prefs) {
|
loadYourLocations().then((yl) {
|
||||||
loadYourLocationsWithPrefs(prefs);
|
|
||||||
firebaseConfig(store);
|
firebaseConfig(store);
|
||||||
|
|
||||||
// Run baby run!
|
// Run baby run!
|
||||||
|
|
@ -46,7 +43,5 @@ void mainCommon(List<Middleware<AppState>> otherMiddleware) {
|
||||||
// Listen to store changes, and re-render when the state is updated
|
// Listen to store changes, and re-render when the state is updated
|
||||||
// store.onChange.listen((state) {
|
// store.onChange.listen((state) {
|
||||||
// });
|
// });
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,37 +2,20 @@ import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:fires_flutter/models/yourLocation.dart';
|
import 'package:fires_flutter/models/yourLocation.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
|
|
||||||
import '../globals.dart' as globals;
|
import '../globals.dart' as globals;
|
||||||
|
|
||||||
final String locationKey = 'yourlocations';
|
final String locationKey = 'yourlocations';
|
||||||
|
|
||||||
Future<List<YourLocation>> loadYourLocations() async {
|
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) {
|
return await globals.prefs.then((prefs) {
|
||||||
List<String> yourLocations = prefs.getStringList(locationKey);
|
List<String> yourLocations = prefs.getStringList(locationKey);
|
||||||
|
List<YourLocation> persistedList = List<YourLocation>();
|
||||||
if (yourLocations == null) {
|
if (yourLocations == null) {
|
||||||
yourLocations = [];
|
yourLocations = [];
|
||||||
persistYourLocations(<YourLocation>[]);
|
// first run, init with empty list
|
||||||
|
persistYourLocations(persistedList);
|
||||||
}
|
}
|
||||||
List<YourLocation> persistedList = List<YourLocation>();
|
|
||||||
yourLocations.forEach((locationString) {
|
yourLocations.forEach((locationString) {
|
||||||
Map locationMap = json.decode(locationString);
|
Map locationMap = json.decode(locationString);
|
||||||
persistedList.add(YourLocation.fromJson(locationMap));
|
persistedList.add(YourLocation.fromJson(locationMap));
|
||||||
|
|
@ -40,3 +23,13 @@ Future<List<YourLocation>> loadYourLocationsWithPrefs(
|
||||||
return persistedList;
|
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