Fire notifications

This commit is contained in:
Vicente J. Ruiz Jurado 2018-07-26 19:13:12 +02:00
parent c680482b08
commit 5ecc3c3401
22 changed files with 524 additions and 31 deletions

View file

@ -1,3 +1,4 @@
export 'appActions.dart';
export 'yourLocationActions.dart';
export 'fireMapActions.dart';
export 'fireMapActions.dart';
export 'fireNotificationActions.dart';

View file

@ -1,4 +1,5 @@
import 'package:fires_flutter/models/yourLocation.dart';
import 'package:fires_flutter/models/fireNotification.dart';
abstract class AppActions {}
@ -12,6 +13,8 @@ class FetchYourLocationsSucceededAction extends AppActions {
FetchYourLocationsSucceededAction(this.fetchedYourLocations);
}
class FetchFireNotificationsAction extends AppActions {}
class FetchYourLocationsFailedAction extends AppActions {
final Exception error;
@ -35,3 +38,9 @@ class OnUserLangAction extends AppActions {
OnUserLangAction(this.lang);
}
class FetchFireNotificationsSucceededAction extends AppActions {
final List<FireNotification> fetchedFireNotifications;
FetchFireNotificationsSucceededAction(this.fetchedFireNotifications);
}

View file

@ -5,5 +5,8 @@ AppState appReducer(AppState state, action) {
if (action is FetchYourLocationsSucceededAction) {
return state.copyWith(yourLocations: action.fetchedYourLocations);
}
if (action is FetchFireNotificationsSucceededAction) {
return state.copyWith(fireNotifications: action.fetchedFireNotifications);
}
return state;
}

View file

@ -5,6 +5,7 @@ import 'package:just_debounce_it/just_debounce_it.dart';
import 'package:redux/redux.dart';
import '../models/appState.dart';
import '../models/fireNotificationsPersist.dart';
import '../models/firesApi.dart';
import '../models/yourLocationPersist.dart';
import 'actions.dart';
@ -20,8 +21,7 @@ import 'actions.dart';
FiresApi api = Injector.getInjector().get<FiresApi>();
void fetchYourLocationsMiddleware(
Store<AppState> store, action, NextDispatcher next) {
void fetchDataMiddleware(Store<AppState> store, action, NextDispatcher next) {
// If our Middleware encounters a `FetchYourLocationAction`
if (action is OnUserLangAction) {
@ -46,11 +46,10 @@ void fetchYourLocationsMiddleware(
if (action is AddYourLocationAction) {
if (action.loc.subscribed) {
subscribeViaApi(store, action.loc,
(sub) {
store.dispatch(new AddedYourLocationAction(sub));
persistYourLocations(store.state.yourLocations);
});
subscribeViaApi(store, action.loc, (sub) {
store.dispatch(new AddedYourLocationAction(sub));
persistYourLocations(store.state.yourLocations);
});
} else {
// No subscribed (only local)
store.dispatch(new AddedYourLocationAction(action.loc));
@ -69,6 +68,21 @@ void fetchYourLocationsMiddleware(
}
}
if (action is DeleteFireNotificationAction) {
store.dispatch(new DeletedFireNotificationAction(action.notif));
persistFireNotifications(store.state.fireNotifications);
}
if (action is DeleteAllFireNotificationAction) {
store.dispatch(new DeletedAllFireNotificationAction());
persistFireNotifications(store.state.fireNotifications);
}
if (action is AddFireNotificationAction) {
store.dispatch(new AddedFireNotificationAction(action.notif));
persistFireNotifications(store.state.fireNotifications);
}
if (action is ShowYourLocationMapAction) {
api
.getYourLocationFireStats(store.state, action.loc)
@ -139,6 +153,15 @@ void fetchYourLocationsMiddleware(
store.dispatch(new FetchYourLocationsFailedAction(error));
});
}
if (action is FetchFireNotificationsAction) {
loadFireNotifications().then((fireNotifications) {
store.dispatch(
new FetchFireNotificationsSucceededAction(fireNotifications));
persistFireNotifications(fireNotifications);
});
}
// Make sure our actions continue on to the reducer.
next(action);
}
@ -167,5 +190,6 @@ void createUser(store, lang, token) {
api.createUser(store.state, token, lang).then((userId) {
store.dispatch(new OnUserCreatedAction(userId));
store.dispatch(new FetchYourLocationsAction());
store.dispatch(new FetchFireNotificationsAction());
});
}

View file

@ -0,0 +1,35 @@
import 'package:fires_flutter/models/fireNotification.dart';
abstract class FireNotificationActions {}
class DeleteFireNotificationAction extends FireNotificationActions {
final FireNotification notif;
DeleteFireNotificationAction(this.notif);
}
class DeleteAllFireNotificationAction extends FireNotificationActions {
DeleteAllFireNotificationAction();
}
class AddFireNotificationAction extends FireNotificationActions {
final FireNotification notif;
AddFireNotificationAction(this.notif);
}
class DeletedFireNotificationAction extends FireNotificationActions {
final FireNotification notif;
DeletedFireNotificationAction(this.notif);
}
class DeletedAllFireNotificationAction extends FireNotificationActions {
DeletedAllFireNotificationAction();
}
class AddedFireNotificationAction extends FireNotificationActions {
final FireNotification notif;
AddedFireNotificationAction(this.notif);
}

View file

@ -0,0 +1,28 @@
import 'package:redux/redux.dart';
import 'actions.dart';
import 'package:fires_flutter/models/fireNotification.dart';
final fireNotificationReducer = combineReducers<List<FireNotification>>([
new TypedReducer<List<FireNotification>, AddedFireNotificationAction>(
_receivedFireNotification),
new TypedReducer<List<FireNotification>, DeletedFireNotificationAction>(
_deletedFireNotification),
new TypedReducer<List<FireNotification>, DeletedAllFireNotificationAction>(
_deletedAllFireNotifications)
]);
List<FireNotification> _receivedFireNotification(
List<FireNotification> yourLocations, AddedFireNotificationAction action) {
return new List.from(yourLocations)..add(action.notif);
}
List<FireNotification> _deletedFireNotification(
List<FireNotification> notifications, DeletedFireNotificationAction action) {
return new List.from(notifications)..remove(action.notif);
}
List<FireNotification> _deletedAllFireNotifications(
List<FireNotification> notifications, DeletedAllFireNotificationAction action) {
return new List<FireNotification>();
}

View file

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