More work with redux and load/persist data

This commit is contained in:
vjrj 2018-06-27 20:58:10 +02:00
parent 2dfa745c6a
commit 69428c5d96
7 changed files with 82 additions and 49 deletions

View file

@ -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}';
}
}

View file

@ -12,18 +12,18 @@ class FiresApi {
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.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<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;
}
}

View 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;
});
}