diff --git a/lib/activeFires.dart b/lib/activeFires.dart index 3605257..5337e9d 100644 --- a/lib/activeFires.dart +++ b/lib/activeFires.dart @@ -13,7 +13,7 @@ import 'locationUtils.dart'; import 'mainDrawer.dart'; import 'placesAutocompleteUtils.dart'; import 'package:fires_flutter/models/yourLocation.dart'; -import 'yourLocationPersist.dart'; +import 'package:fires_flutter/models/yourLocationPersist.dart'; class ActiveFiresPage extends StatefulWidget { static const String routeName = '/fires'; diff --git a/lib/mainCommon.dart b/lib/mainCommon.dart index 0249d78..86b7c61 100644 --- a/lib/mainCommon.dart +++ b/lib/mainCommon.dart @@ -11,7 +11,7 @@ import 'models/appState.dart'; import 'models/firesApi.dart'; import 'redux/fetchDataMiddleware.dart'; import 'redux/reducers.dart'; -import 'yourLocationPersist.dart'; +import 'package:fires_flutter/models/yourLocationPersist.dart'; import 'package:redux_logging/redux_logging.dart'; Future> loadSecrets() async { return await SecretLoader(secretPath: 'assets/private-settings.json').load(); diff --git a/lib/models/appState.dart b/lib/models/appState.dart index 0d052cd..9679aa8 100644 --- a/lib/models/appState.dart +++ b/lib/models/appState.dart @@ -66,6 +66,6 @@ class AppState extends Object with _$AppStateSerializerMixin { @override String toString() { - return 'AppState{\nuser: ${user}\nisLoading: $isLoading\nisLoaded: $isLoaded\napiKey: ${ellipse(firesApiKey, 8)}\napiUrl: ${ellipse(firesApiUrl, 8)}'; + return 'AppState{\nuser: ${user}\nisLoading: $isLoading\nisLoaded: $isLoaded\napiKey: ${ellipse(firesApiKey, 8)}\napiUrl: ${ellipse(firesApiUrl, 8)}\nyourLocations: ${yourLocations}'; } } diff --git a/lib/models/firesApi.dart b/lib/models/firesApi.dart index f746249..df7b703 100644 --- a/lib/models/firesApi.dart +++ b/lib/models/firesApi.dart @@ -12,18 +12,18 @@ class FiresApi { resty.globalClient = new ht.IOClient(); } - Future createUser(AppState state, String token, String lang) async { + Future createUser(AppState state, String mobileToken, String lang) async { assert(state.firesApiUrl != null); assert(state.firesApiKey != null); - assert(token != null); + assert(mobileToken != null); assert(lang != null); final params = { "token": state.firesApiKey, - "mobileToken": token, + "mobileToken": mobileToken, "lang": lang }; - final url = '${state.firesApiUrl}mobile/users'; + final String url = '${state.firesApiUrl}mobile/users'; /* print(url); print(params); */ String resp = await resty.post(url).json(params).go().then((response) { @@ -35,5 +35,16 @@ class FiresApi { return resp; } - Future> fetchYourLocations() async {} + Future> fetchYourLocations(AppState state) async { + final apiKey = state.firesApiKey; + final mobileToken = state.user.token; + final String url = '${state.firesApiUrl}mobile/subscriptions/all/$apiKey/$mobileToken'; + List resp = await resty.get(url).go().then((response) { + if (response.statusCode == 200) { + // print(response.body); + print(json.decode(response.body)['data']['subscriptions']); return []; + } + }); + return resp; + } } diff --git a/lib/models/yourLocationPersist.dart b/lib/models/yourLocationPersist.dart new file mode 100644 index 0000000..863d17e --- /dev/null +++ b/lib/models/yourLocationPersist.dart @@ -0,0 +1,41 @@ +import 'dart:convert'; +import '../globals.dart' as globals; +import 'dart:async'; +import 'yourLocation.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +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); + if (yourLocations == null) { + yourLocations = []; + persistYourLocations([]); + } + List persistedList = List(); + yourLocations.forEach((locationString) { + Map locationMap = json.decode(locationString); + persistedList.add(YourLocation.fromJson(locationMap)); + }); + return persistedList; + }); + +} diff --git a/lib/redux/fetchDataMiddleware.dart b/lib/redux/fetchDataMiddleware.dart index 8787fa6..10e419c 100644 --- a/lib/redux/fetchDataMiddleware.dart +++ b/lib/redux/fetchDataMiddleware.dart @@ -4,6 +4,7 @@ import '../globals.dart' as globals; import '../models/appState.dart'; import '../models/firesApi.dart'; import '../models/yourLocation.dart'; +import '../models/yourLocationPersist.dart'; import 'actions.dart'; // A middleware takes in 3 parameters: your Store, which you can use to @@ -37,10 +38,25 @@ void fetchYourLocationsMiddleware( final api = globals.getIt.get(); // Use the api to fetch the YourLocations - api.fetchYourLocations().then((List YourLocations) { + api.fetchYourLocations(store.state).then((List yourSubs) { // If it succeeds, dispatch a success action with the YourLocations. // Our reducer will then update the State using these YourLocations. - store.dispatch(new FetchYourLocationsSucceededAction(YourLocations)); + + loadYourLocations().then((localLocations) { + // unsubscribe all locally to sync the subs state + localLocations.forEach((location) => location.subscribed = false); + + yourSubs.forEach((loc) { + localLocations.firstWhere( + (localLocation) => localLocation.id == loc.id, orElse: () { + localLocations.add(loc); + }).subscribed = true; + }); + + persistYourLocations(localLocations); + + store.dispatch(new FetchYourLocationsSucceededAction(localLocations)); + }); }).catchError((Exception error) { // If it fails, dispatch a failure action. The reducer will // update the state with the error. @@ -55,7 +71,8 @@ void fetchYourLocationsMiddleware( void createUser(store, lang, token) { assert(token != null, "User lang is null"); assert(token != null, "User mobile token is null"); - api - .createUser(store.state, token, lang) - .then((userId) => store.dispatch(new OnUserCreatedAction(userId))); + api.createUser(store.state, token, lang).then((userId) { + store.dispatch(new OnUserCreatedAction(userId)); + store.dispatch(new FetchYourLocationsAction()); + }); } diff --git a/lib/yourLocationPersist.dart b/lib/yourLocationPersist.dart deleted file mode 100644 index 4fb37fb..0000000 --- a/lib/yourLocationPersist.dart +++ /dev/null @@ -1,36 +0,0 @@ -import 'dart:convert'; -import 'package:fires_flutter/models/yourLocation.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 yourLocationsAsString = []; - globals.yourLocations.forEach((location) { - yourLocationsAsString.add(json.encode(location.toJson())); - }); - prefs.setStringList(locationKey, yourLocationsAsString); - }); -} - -void loadYourLocationsWithPrefs(SharedPreferences prefs) { - globals.prefs.then((prefs) { - List yourLocations = prefs.getStringList(locationKey); - if (yourLocations == null) { - yourLocations = []; - persistYourLocations(); - } - yourLocations.forEach((locationString) { - Map locationMap = json.decode(locationString); - globals.yourLocations.add(YourLocation.fromJson(locationMap)); - }); - }); -}