Persist your saved locations

This commit is contained in:
vjrj 2018-06-27 08:59:23 +02:00
parent c9941429f0
commit 0ad2fce4f3
5 changed files with 19 additions and 33 deletions

View file

@ -13,6 +13,7 @@ import 'locationUtils.dart';
import 'mainDrawer.dart';
import 'placesAutocompleteUtils.dart';
import 'yourLocation.dart';
import 'yourLocationPersist.dart';
class ActiveFiresPage extends StatefulWidget {
static const String routeName = '/fires';
@ -60,9 +61,9 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
onPressed: () {
loc.subscribed = !loc.subscribed;
int i = globals.yourLocations.indexOf(loc);
YourLocationRepository.repo.update(i, loc);
globals.yourLocations.removeAt(i);
globals.yourLocations.insert(i, loc);
persistYourLocations();
setState(() {});
}),
title: new Text(loc.description),
@ -102,7 +103,8 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
void handleUndo(YourLocation item) {
setState(() {
YourLocationRepository.repo.save(item);
globals.yourLocations.add(item);
persistYourLocations();
});
}
@ -114,8 +116,7 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
onDismissed: (DismissDirection direction) {
setState(() {
globals.yourLocations.remove(item);
int i = globals.yourLocations.indexOf(item);
YourLocationRepository.repo.remove(i);
persistYourLocations();
});
_scaffoldKey.currentState.showSnackBar(new SnackBar(
@ -207,7 +208,7 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
} else
this.setState(() {
globals.yourLocations.add(newLocation);
YourLocationRepository.repo.save(newLocation);
persistYourLocations();
new Timer(new Duration(milliseconds: 1000), () {
showLocationMap(newLocation);
});

View file

@ -2,6 +2,8 @@ library fires.globals;
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
import 'yourLocation.dart';
@ -14,7 +16,7 @@ final Widget appMediumIcon =
Image.asset('images/logo-200.png', width: 60.0, height: 60.0);
final Widget appIcon =
Image.asset('images/logo-200.png', width: 24.0, height: 24.0);
// final Future<SharedPreferences> prefs = SharedPreferences.getInstance();
final Future<SharedPreferences> prefs = SharedPreferences.getInstance();
List<YourLocation> yourLocations = [];
final GetIt getIt = new GetIt();
bool isDevelopment = false;

View file

@ -7,6 +7,8 @@ import 'firebaseMessagingConf.dart';
import 'firesApp.dart';
import 'globals.dart' as globals;
import 'yourLocation.dart';
import 'yourLocationPersist.dart';
Future<Map<String, dynamic>> loadSecrets() async {
return await SecretLoader(secretPath: 'assets/private-settings.json').load();
}
@ -16,13 +18,12 @@ void mainCommon() {
globals.gmapKey = secrets['gmapKey'];
globals.firesApiKey = secrets['firesApiKey'];
globals.firesApiUrl = secrets['firesApiUrl'] + "api/v1/";
YourLocationRepository.repo.remove(0);
YourLocationRepository.repo.getAll().then((yourLocations) {
globals.yourLocations = yourLocations;
firebaseConfig();
globals.prefs.then((prefs) {
loadYourLocationsWithPrefs(prefs);
firebaseConfig();
// Run baby run!
runApp(new FiresApp());
// Run baby run!
runApp(new FiresApp());
});
});
});
}

View file

@ -58,21 +58,3 @@ class YourLocation extends Object with _$YourLocationSerializerMixin {
description.hashCode ^
subscribed.hashCode;
}
class YourLocationRepository extends PreferencesRepository<YourLocation> {
static final repo = new YourLocationRepository();
YourLocationRepository() : super();
@override
YourLocation deserialize(String s) {
return YourLocation.fromJson(json.decode(s));
}
@override
String serialize(YourLocation y) {
return json.encode(y.toJson());
}
}

View file

@ -1,5 +1,5 @@
import 'dart:convert';
import 'basicLocation.dart';
import 'yourLocation.dart';
import 'globals.dart' as globals;
import 'package:shared_preferences/shared_preferences.dart';
@ -30,7 +30,7 @@ void loadYourLocationsWithPrefs(SharedPreferences prefs) {
}
yourLocations.forEach((locationString) {
Map locationMap = json.decode(locationString);
globals.yourLocations.add(BasicLocation.fromJson(locationMap));
globals.yourLocations.add(YourLocation.fromJson(locationMap));
});
});
}