More redux work in fires page

This commit is contained in:
vjrj 2018-06-28 10:42:59 +02:00
parent 5d947f9577
commit 7dddf03e32
13 changed files with 210 additions and 54 deletions

View file

@ -4,6 +4,8 @@ abstract class AppActions {}
class FetchYourLocationsAction extends AppActions {}
class PersistAppStateAction extends AppActions {}
class FetchYourLocationsSucceededAction extends AppActions {
final List<YourLocation> fetchedYourLocations;

10
lib/redux/appReducer.dart Normal file
View file

@ -0,0 +1,10 @@
import 'actions.dart';
import '../models/user.dart';
import '../models/appState.dart';
AppState appReducer(AppState state, action) {
if (action is FetchYourLocationsSucceededAction) {
return state.copyWith(yourLocations: action.fetchedYourLocations);
}
return state;
}

View file

@ -1,11 +1,10 @@
import 'package:bson_objectid/bson_objectid.dart';
import 'package:fires_flutter/models/yourLocation.dart';
import 'package:redux/redux.dart';
import '../globals.dart' as globals;
import '../models/appState.dart';
import '../models/firesApi.dart';
import 'package:fires_flutter/models/yourLocation.dart';
import '../models/yourLocationPersist.dart';
import 'actions.dart';
@ -36,28 +35,52 @@ void fetchYourLocationsMiddleware(
createUser(store, store.state.user.lang, action.token);
}
if (action is FetchYourLocationsAction) {
final api = globals.getIt.get<FiresApi>();
if (action is AddYourLocationAction) {
if (action.loc.subscribed) {
subscribeViaApi(store, action.loc,
(sub) => store.dispatch(new AddedYourLocationAction(sub)));
} else {
// No subscribed (only local)
store.dispatch(new AddedYourLocationAction(action.loc));
persistYourLocations(store.state.yourLocations);
}
}
if (action is DeleteYourLocationAction) {
unsubsViaApi(store, action.id,
() => store.dispatch(new DeletedYourLocationAction(action.id)));
}
if (action is ToggleSubscriptionAction) {
if (action.loc.subscribed) {
subscribeViaApi(store, action.loc,
(sub) => store.dispatch(new ToggledSubscriptionAction(sub)));
} else {
unsubsViaApi(store, action.loc.id,
() => store.dispatch(new ToggledSubscriptionAction(action.loc)));
}
}
if (action is FetchYourLocationsAction) {
// Use the api to fetch the YourLocations
api.fetchYourLocations(store.state).then((List<YourLocation> yourSubs) {
api
.fetchYourLocations(store.state)
.then((List<YourLocation> subscribedLocations) {
// If it succeeds, dispatch a success action with the YourLocations.
// Our reducer will then update the State using these YourLocations.
// print('Subscribed to: ${subscribedLocations.length}');
loadYourLocations().then((localLocations) {
// unsubscribe all locally to sync the subs state
localLocations.forEach((location) => location.subscribed = false);
yourSubs.forEach((loc) {
// print('Local persisted: ${localLocations.length}');
subscribedLocations.forEach((subsLoc) {
localLocations.firstWhere(
(localLocation) => localLocation.id == loc.id, orElse: () {
localLocations.add(loc);
(localLocation) => localLocation.id == subsLoc.id, orElse: () {
localLocations.add(subsLoc);
}).subscribed = true;
});
persistYourLocations(localLocations);
store.dispatch(new FetchYourLocationsSucceededAction(localLocations));
persistYourLocations(localLocations);
});
}).catchError((Exception error) {
// If it fails, dispatch a failure action. The reducer will
@ -65,11 +88,28 @@ void fetchYourLocationsMiddleware(
store.dispatch(new FetchYourLocationsFailedAction(error));
});
}
// Make sure our actions continue on to the reducer.
next(action);
}
void unsubsViaApi(Store<AppState> store, ObjectId id, onUnsubs) {
api.unsubscribe(store.state, id.toHexString()).then((res) {
onUnsubs();
persistYourLocations(store.state.yourLocations);
});
}
void subscribeViaApi(Store<AppState> store, YourLocation loc, onSubs) {
api.subscribe(store.state, loc).then((subsId) {
YourLocation sub = loc;
if (loc.id != subsId) {
sub.id = new ObjectId.fromHexString(subsId);
}
onSubs(sub);
persistYourLocations(store.state.yourLocations);
});
}
void createUser(store, lang, token) {
assert(token != null, "User lang is null");
assert(token != null, "User mobile token is null");

View file

@ -5,9 +5,14 @@ import 'loadedReducer.dart';
import 'loadingReducer.dart';
import 'userReducer.dart';
import 'yourLocationsReducer.dart';
import 'actions.dart';
import 'appReducer.dart';
// We create the State reducer by combining many smaller reducers into one!
AppState appStateReducer(AppState state, action) {
AppState appStateReducer(AppState prevState, action) {
var state = prevState;
if (action is AppActions)
state = appReducer(prevState, action);
return AppState(
yourLocations: yourLocationsReducer(state.yourLocations, action),
user: userReducer(state.user, action),

View file

@ -11,12 +11,25 @@ class AddYourLocationAction extends YourLocationActions {
AddYourLocationAction(this.loc);
}
class AddedYourLocationAction extends YourLocationActions {
YourLocation loc;
AddedYourLocationAction(this.loc);
}
class DeleteYourLocationAction extends YourLocationActions {
ObjectId id;
DeleteYourLocationAction(this.id);
}
class DeletedYourLocationAction extends YourLocationActions {
ObjectId id;
DeletedYourLocationAction(this.id);
}
class UpdateYourLocationAction extends YourLocationActions {
ObjectId id;
YourLocation loc;
@ -25,9 +38,15 @@ class UpdateYourLocationAction extends YourLocationActions {
}
class ToggleSubscriptionAction extends YourLocationActions {
ObjectId id;
YourLocation loc;
ToggleSubscriptionAction(this.id);
ToggleSubscriptionAction(this.loc);
}
class ToggledSubscriptionAction extends YourLocationActions {
YourLocation loc;
ToggledSubscriptionAction(this.loc);
}
class SubscribeAction extends YourLocationActions {

View file

@ -4,24 +4,25 @@ import 'package:redux/redux.dart';
import 'actions.dart';
final yourLocationsReducer = combineReducers<List<YourLocation>>([
new TypedReducer<List<YourLocation>, AddYourLocationAction>(_addYourLocation),
new TypedReducer<List<YourLocation>, DeleteYourLocationAction>(
_deleteYourLocation),
new TypedReducer<List<YourLocation>, AddedYourLocationAction>(_addedYourLocation),
new TypedReducer<List<YourLocation>, DeletedYourLocationAction>(
_deletedYourLocation),
new TypedReducer<List<YourLocation>, UpdateYourLocationAction>(
_updateYourLocation),
new TypedReducer<List<YourLocation>, ToggledSubscriptionAction>(_toggledSubscriptionAction)
/* new TypedReducer<List<YourLocation>, ToggleSubscriptionAction>(_toggleSubscriptionAction),
new TypedReducer<List<YourLocation>, SubscribeAction>(_setLoadedYourLocations),
new TypedReducer<List<YourLocation>, UnSubscribeAction>(_setNoYourLocations), */
]);
List<YourLocation> _addYourLocation(
List<YourLocation> yourLocations, AddYourLocationAction action) {
List<YourLocation> _addedYourLocation(
List<YourLocation> yourLocations, AddedYourLocationAction action) {
return new List.from(yourLocations)..add(action.loc);
}
List<YourLocation> _deleteYourLocation(
List<YourLocation> yourLocations, DeleteYourLocationAction action) {
List<YourLocation> _deletedYourLocation(
List<YourLocation> yourLocations, DeletedYourLocationAction action) {
return yourLocations
.where((yourLocation) => yourLocation.id != action.id)
.toList();
@ -34,13 +35,13 @@ List<YourLocation> _updateYourLocation(
yourLocation.id == action.id ? action.loc : yourLocation)
.toList();
}
/*
List<YourLocation> _toggleSubscriptionAction(List<YourLocation> yourLocations, UpdateYourLocationAction action) {
List<YourLocation> _toggledSubscriptionAction(List<YourLocation> yourLocations, ToggledSubscriptionAction action) {
return yourLocations
.map((yourLocation) => yourLocation.id == action.id ? action.loc : yourLocation)
.map((yourLocation) => yourLocation.id == action.loc.id ? action.loc : yourLocation)
.toList();
}
/*
List<YourLocation> _setLoadedYourLocations(List<YourLocation> yourLocations, YourLocationsLoadedAction action) {
return action.yourLocations;
}