From b22df7a8e239b8e1485bb108736a405faa78428c Mon Sep 17 00:00:00 2001 From: vjrj Date: Sat, 30 Jun 2018 08:17:45 +0200 Subject: [PATCH] YourLocations persist refactor --- lib/mainCommon.dart | 15 +++++-------- lib/models/yourLocationPersist.dart | 33 ++++++++++++----------------- 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/lib/mainCommon.dart b/lib/mainCommon.dart index a52bdad..be0755d 100644 --- a/lib/mainCommon.dart +++ b/lib/mainCommon.dart @@ -1,42 +1,39 @@ import 'dart:async'; import 'package:comunes_flutter/comunes_flutter.dart'; +import 'package:fires_flutter/models/yourLocationPersist.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_simple_dependency_injection/injector.dart'; import 'package:redux/redux.dart'; import 'firebaseMessagingConf.dart'; import 'firesApp.dart'; -import 'globals.dart' as globals; import 'models/appState.dart'; import 'models/firesApi.dart'; import 'redux/fetchDataMiddleware.dart'; import 'redux/reducers.dart'; -import 'package:fires_flutter/models/yourLocationPersist.dart'; -import 'package:flutter_simple_dependency_injection/injector.dart'; Future> loadSecrets() async { return await SecretLoader(secretPath: 'assets/private-settings.json').load(); } void mainCommon(List> otherMiddleware) { - final injector = Injector.getInjector(); injector.map(FiresApi, (i) => new FiresApi(), isSingleton: true); loadSecrets().then((secrets) { - final store = new Store(appStateReducer, initialState: new AppState( gmapKey: secrets['gmapKey'], firesApiKey: secrets['firesApiKey'], 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.firesApiKey, key: "firesApiKey"); injector.map(String, (i) => store.state.gmapKey, key: "gmapKey"); - globals.prefs.then((prefs) { - loadYourLocationsWithPrefs(prefs); + loadYourLocations().then((yl) { firebaseConfig(store); // Run baby run! @@ -46,7 +43,5 @@ void mainCommon(List> otherMiddleware) { // Listen to store changes, and re-render when the state is updated // store.onChange.listen((state) { // }); - }); - } diff --git a/lib/models/yourLocationPersist.dart b/lib/models/yourLocationPersist.dart index 7b9973f..4540d83 100644 --- a/lib/models/yourLocationPersist.dart +++ b/lib/models/yourLocationPersist.dart @@ -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> loadYourLocations() async { - return await globals.prefs.then((prefs) { - return loadYourLocationsWithPrefs(prefs); - }); -} - -persistYourLocations(List yl) { - globals.prefs.then((prefs) { - List yourLocationsAsString = []; - yl.forEach((location) { - yourLocationsAsString.add(json.encode(location.toJson())); - }); - prefs.setStringList(locationKey, yourLocationsAsString); - }); -} - -Future> loadYourLocationsWithPrefs( - SharedPreferences prefs) async { return await globals.prefs.then((prefs) { List yourLocations = prefs.getStringList(locationKey); + List persistedList = List(); if (yourLocations == null) { yourLocations = []; - persistYourLocations([]); + // first run, init with empty list + persistYourLocations(persistedList); } - List persistedList = List(); yourLocations.forEach((locationString) { Map locationMap = json.decode(locationString); persistedList.add(YourLocation.fromJson(locationMap)); @@ -40,3 +23,13 @@ Future> loadYourLocationsWithPrefs( return persistedList; }); } + +persistYourLocations(List yl) { + globals.prefs.then((prefs) { + List ylAsString = []; + yl.forEach((location) { + ylAsString.add(json.encode(location.toJson())); + }); + prefs.setStringList(locationKey, ylAsString); + }); +}