More redux work

This commit is contained in:
vjrj 2018-06-29 00:36:43 +02:00
parent 657fecdf17
commit eedd39f6fc
11 changed files with 346 additions and 181 deletions

View file

@ -1,5 +1,6 @@
import 'package:bson_objectid/bson_objectid.dart';
import 'package:fires_flutter/models/yourLocation.dart';
import 'package:just_debounce_it/just_debounce_it.dart';
import 'package:redux/redux.dart';
import '../globals.dart' as globals;
@ -47,8 +48,43 @@ void fetchYourLocationsMiddleware(
}
if (action is DeleteYourLocationAction) {
unsubsViaApi(store, action.id,
() => store.dispatch(new DeletedYourLocationAction(action.id)));
store.dispatch(new DeletedYourLocationAction(action.loc.id));
if (action.loc.subscribed) {
unsubsViaApi(store, action.loc.id, () {
persistYourLocations(store.state.yourLocations);
});
} else {
persistYourLocations(store.state.yourLocations);
}
}
if (action is ShowYourLocationMapAction) {
api
.getYourLocationFireStats(store.state, action.loc)
.then((result) => store.dispatch(result));
}
if (action is UpdateLocalYourLocationAction) {
if (action.loc.subscribed)
Debounce.seconds(
2,
() => api
.getYourLocationFireStats(store.state, action.loc)
.then((result) => store.dispatch(result)));
}
if (action is SubscribeConfirmAction) {
subscribeViaApi(store, action.loc, (sub) {
store.dispatch(new UpdateLocalYourLocationAction(action.loc));
persistYourLocations(store.state.yourLocations);
});
}
if (action is UnSubscribeAction) {
unsubsViaApi(store, action.loc.id, () {
store.dispatch(new UpdateLocalYourLocationAction(action.loc));
persistYourLocations(store.state.yourLocations);
});
}
if (action is ToggleSubscriptionAction) {

View file

@ -6,15 +6,44 @@ import 'actions.dart';
final fireMapReducer = combineReducers<FireMapState>([
new TypedReducer<FireMapState, ShowYourLocationMapAction>(
_showYourLocationMap),
new TypedReducer<FireMapState, UpdateYourLocationMapStatsAction>(
_updateYourLocationMapStats),
new TypedReducer<FireMapState, SubscribeAction>(_subscribeYourLocationMap),
new TypedReducer<FireMapState, SubscribeConfirmAction>(
_subscribeConfirmYourLocationMap),
new TypedReducer<FireMapState, UnSubscribeAction>(
_unsubscribeYourLocationMap),
]);
FireMapState _updateYourLocationMapStats(
FireMapState state, UpdateYourLocationMapStatsAction action) {
return state.copyWith(
numFires: action.numFires,
fires: action.fires,
falsePos: action.falsePos,
industries: action.industries);
}
FireMapState _showYourLocationMap(
FireMapState state, ShowYourLocationMapAction action) {
if (action.loc.subscribed) {
return state.copyWith(
status: action.loc.subscribed
? FireMapStatus.unsubscribe
: FireMapStatus.view,
yourLocation: action.loc);
}
return state.copyWith(
status: action.loc.subscribed
? FireMapStatus.unsubscribe
: FireMapStatus.view,
yourLocation: action.loc);
}
FireMapState _subscribeYourLocationMap(
FireMapState state, SubscribeAction action) {
return state.copyWith(status: FireMapStatus.subscriptionConfirm);
}
FireMapState _subscribeConfirmYourLocationMap(
FireMapState state, SubscribeConfirmAction action) {
return state.copyWith(status: FireMapStatus.unsubscribe);
}
FireMapState _unsubscribeYourLocationMap(
FireMapState state, UnSubscribeAction action) {
return state.copyWith(status: FireMapStatus.view);
}

View file

@ -1,6 +1,6 @@
import 'package:bson_objectid/bson_objectid.dart';
import 'package:fires_flutter/models/yourLocation.dart';
import 'package:meta/meta.dart';
abstract class YourLocationActions {}
@ -22,10 +22,23 @@ class ShowYourLocationMapAction extends YourLocationActions {
ShowYourLocationMapAction(this.loc);
}
class DeleteYourLocationAction extends YourLocationActions {
ObjectId id;
class UpdateYourLocationMapStatsAction extends YourLocationActions {
int numFires;
List<dynamic> fires = [];
List<dynamic> falsePos = [];
List<dynamic> industries = [];
DeleteYourLocationAction(this.id);
UpdateYourLocationMapStatsAction(
{@required this.numFires,
@required this.fires,
@required this.falsePos,
@required this.industries});
}
class DeleteYourLocationAction extends YourLocationActions {
YourLocation loc;
DeleteYourLocationAction(this.loc);
}
class DeletedYourLocationAction extends YourLocationActions {
@ -34,12 +47,10 @@ class DeletedYourLocationAction extends YourLocationActions {
DeletedYourLocationAction(this.id);
}
class UpdateYourLocationAction extends YourLocationActions {
ObjectId id;
class UpdateLocalYourLocationAction extends YourLocationActions {
YourLocation loc;
UpdateYourLocationAction(this.id, this.loc);
UpdateLocalYourLocationAction(this.loc);
}
class ToggleSubscriptionAction extends YourLocationActions {
@ -55,13 +66,18 @@ class ToggledSubscriptionAction extends YourLocationActions {
}
class SubscribeAction extends YourLocationActions {
ObjectId id;
SubscribeAction();
}
SubscribeAction(this.id);
class SubscribeConfirmAction extends YourLocationActions {
YourLocation loc;
SubscribeConfirmAction(this.loc);
}
class UnSubscribeAction extends YourLocationActions {
ObjectId id;
YourLocation loc;
UnSubscribeAction(this.id);
UnSubscribeAction(this.loc);
}

View file

@ -4,16 +4,14 @@ import 'package:redux/redux.dart';
import 'actions.dart';
final yourLocationsReducer = combineReducers<List<YourLocation>>([
new TypedReducer<List<YourLocation>, AddedYourLocationAction>(_addedYourLocation),
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), */
new TypedReducer<List<YourLocation>, UpdateLocalYourLocationAction>(
_updateLocalYourLocation),
new TypedReducer<List<YourLocation>, ToggledSubscriptionAction>(
_toggledSubscriptionAction)
]);
List<YourLocation> _addedYourLocation(
@ -28,25 +26,18 @@ List<YourLocation> _deletedYourLocation(
.toList();
}
List<YourLocation> _updateYourLocation(
List<YourLocation> yourLocations, UpdateYourLocationAction action) {
List<YourLocation> _updateLocalYourLocation(
List<YourLocation> yourLocations, UpdateLocalYourLocationAction action) {
return yourLocations
.map((yourLocation) =>
yourLocation.id == action.id ? action.loc : yourLocation)
yourLocation.id == action.loc.id ? action.loc : yourLocation)
.toList();
}
List<YourLocation> _toggledSubscriptionAction(List<YourLocation> yourLocations, ToggledSubscriptionAction action) {
List<YourLocation> _toggledSubscriptionAction(
List<YourLocation> yourLocations, ToggledSubscriptionAction action) {
return yourLocations
.map((yourLocation) => yourLocation.id == action.loc.id ? action.loc : yourLocation)
.toList();
.map((yourLocation) =>
yourLocation.id == action.loc.id ? action.loc : yourLocation)
.toList();
}
/*
List<YourLocation> _setLoadedYourLocations(List<YourLocation> yourLocations, YourLocationsLoadedAction action) {
return action.yourLocations;
}
List<YourLocation> _setNoYourLocations(List<YourLocation> yourLocations, YourLocationsNotLoadedAction action) {
return [];
}
*/