More work with redux and load/persist data
This commit is contained in:
parent
2dfa745c6a
commit
69428c5d96
7 changed files with 82 additions and 49 deletions
|
|
@ -13,7 +13,7 @@ import 'locationUtils.dart';
|
||||||
import 'mainDrawer.dart';
|
import 'mainDrawer.dart';
|
||||||
import 'placesAutocompleteUtils.dart';
|
import 'placesAutocompleteUtils.dart';
|
||||||
import 'package:fires_flutter/models/yourLocation.dart';
|
import 'package:fires_flutter/models/yourLocation.dart';
|
||||||
import 'yourLocationPersist.dart';
|
import 'package:fires_flutter/models/yourLocationPersist.dart';
|
||||||
|
|
||||||
class ActiveFiresPage extends StatefulWidget {
|
class ActiveFiresPage extends StatefulWidget {
|
||||||
static const String routeName = '/fires';
|
static const String routeName = '/fires';
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ 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 'yourLocationPersist.dart';
|
import 'package:fires_flutter/models/yourLocationPersist.dart';
|
||||||
import 'package:redux_logging/redux_logging.dart';
|
import 'package:redux_logging/redux_logging.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();
|
||||||
|
|
|
||||||
|
|
@ -66,6 +66,6 @@ class AppState extends Object with _$AppStateSerializerMixin {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() {
|
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}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,18 +12,18 @@ class FiresApi {
|
||||||
resty.globalClient = new ht.IOClient();
|
resty.globalClient = new ht.IOClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String> createUser(AppState state, String token, String lang) async {
|
Future<String> createUser(AppState state, String mobileToken, String lang) async {
|
||||||
assert(state.firesApiUrl != null);
|
assert(state.firesApiUrl != null);
|
||||||
assert(state.firesApiKey != null);
|
assert(state.firesApiKey != null);
|
||||||
assert(token != null);
|
assert(mobileToken != null);
|
||||||
assert(lang != null);
|
assert(lang != null);
|
||||||
|
|
||||||
final params = {
|
final params = {
|
||||||
"token": state.firesApiKey,
|
"token": state.firesApiKey,
|
||||||
"mobileToken": token,
|
"mobileToken": mobileToken,
|
||||||
"lang": lang
|
"lang": lang
|
||||||
};
|
};
|
||||||
final url = '${state.firesApiUrl}mobile/users';
|
final String url = '${state.firesApiUrl}mobile/users';
|
||||||
/* print(url);
|
/* print(url);
|
||||||
print(params); */
|
print(params); */
|
||||||
String resp = await resty.post(url).json(params).go().then((response) {
|
String resp = await resty.post(url).json(params).go().then((response) {
|
||||||
|
|
@ -35,5 +35,16 @@ class FiresApi {
|
||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<YourLocation>> fetchYourLocations() async {}
|
Future<List<YourLocation>> fetchYourLocations(AppState state) async {
|
||||||
|
final apiKey = state.firesApiKey;
|
||||||
|
final mobileToken = state.user.token;
|
||||||
|
final String url = '${state.firesApiUrl}mobile/subscriptions/all/$apiKey/$mobileToken';
|
||||||
|
List<YourLocation> 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
41
lib/models/yourLocationPersist.dart
Normal file
41
lib/models/yourLocationPersist.dart
Normal file
|
|
@ -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<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);
|
||||||
|
if (yourLocations == null) {
|
||||||
|
yourLocations = [];
|
||||||
|
persistYourLocations(<YourLocation>[]);
|
||||||
|
}
|
||||||
|
List<YourLocation> persistedList = List<YourLocation>();
|
||||||
|
yourLocations.forEach((locationString) {
|
||||||
|
Map locationMap = json.decode(locationString);
|
||||||
|
persistedList.add(YourLocation.fromJson(locationMap));
|
||||||
|
});
|
||||||
|
return persistedList;
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@ import '../globals.dart' as globals;
|
||||||
import '../models/appState.dart';
|
import '../models/appState.dart';
|
||||||
import '../models/firesApi.dart';
|
import '../models/firesApi.dart';
|
||||||
import '../models/yourLocation.dart';
|
import '../models/yourLocation.dart';
|
||||||
|
import '../models/yourLocationPersist.dart';
|
||||||
import 'actions.dart';
|
import 'actions.dart';
|
||||||
|
|
||||||
// A middleware takes in 3 parameters: your Store, which you can use to
|
// A middleware takes in 3 parameters: your Store, which you can use to
|
||||||
|
|
@ -37,10 +38,25 @@ void fetchYourLocationsMiddleware(
|
||||||
final api = globals.getIt.get<FiresApi>();
|
final api = globals.getIt.get<FiresApi>();
|
||||||
|
|
||||||
// Use the api to fetch the YourLocations
|
// Use the api to fetch the YourLocations
|
||||||
api.fetchYourLocations().then((List<YourLocation> YourLocations) {
|
api.fetchYourLocations(store.state).then((List<YourLocation> yourSubs) {
|
||||||
// If it succeeds, dispatch a success action with the YourLocations.
|
// If it succeeds, dispatch a success action with the YourLocations.
|
||||||
// Our reducer will then update the State using these 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) {
|
}).catchError((Exception error) {
|
||||||
// If it fails, dispatch a failure action. The reducer will
|
// If it fails, dispatch a failure action. The reducer will
|
||||||
// update the state with the error.
|
// update the state with the error.
|
||||||
|
|
@ -55,7 +71,8 @@ void fetchYourLocationsMiddleware(
|
||||||
void createUser(store, lang, token) {
|
void createUser(store, lang, token) {
|
||||||
assert(token != null, "User lang is null");
|
assert(token != null, "User lang is null");
|
||||||
assert(token != null, "User mobile token is null");
|
assert(token != null, "User mobile token is null");
|
||||||
api
|
api.createUser(store.state, token, lang).then((userId) {
|
||||||
.createUser(store.state, token, lang)
|
store.dispatch(new OnUserCreatedAction(userId));
|
||||||
.then((userId) => store.dispatch(new OnUserCreatedAction(userId)));
|
store.dispatch(new FetchYourLocationsAction());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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<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(YourLocation.fromJson(locationMap));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue