More redux and api work

This commit is contained in:
vjrj 2018-06-27 16:26:06 +02:00
parent 866e776389
commit 2c67b68512
17 changed files with 256 additions and 11 deletions

View file

@ -8,6 +8,9 @@ import 'homePage.dart';
import 'introPage.dart'; import 'introPage.dart';
import 'sandbox.dart'; import 'sandbox.dart';
import 'theme.dart'; import 'theme.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import 'models/appState.dart';
class FiresApp extends StatelessWidget { class FiresApp extends StatelessWidget {
static final WidgetBuilder introWidget = (context) => new IntroPage(); static final WidgetBuilder introWidget = (context) => new IntroPage();
@ -20,12 +23,15 @@ class FiresApp extends StatelessWidget {
Sandbox.routeName: (BuildContext context) => new Sandbox(), Sandbox.routeName: (BuildContext context) => new Sandbox(),
}; };
final Store<AppState> store;
// globals.getIt.registerSingleton // globals.getIt.registerSingleton
FiresApp(); FiresApp(this.store);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new MaterialApp( return new StoreProvider(store: this.store, child:
new MaterialApp(
localizationsDelegates: [ localizationsDelegates: [
S.delegate, S.delegate,
GlobalMaterialLocalizations.delegate, GlobalMaterialLocalizations.delegate,
@ -38,6 +44,6 @@ class FiresApp extends StatelessWidget {
introWidget, continueWidget, 'showInitialWizard-2018-06-27-01'), introWidget, continueWidget, 'showInitialWizard-2018-06-27-01'),
onGenerateTitle: (context) => S.of(context).appName, onGenerateTitle: (context) => S.of(context).appName,
theme: firesTheme, theme: firesTheme,
routes: routes); routes: routes));
} }
} }

View file

@ -2,17 +2,31 @@ import 'dart:async';
import 'package:comunes_flutter/comunes_flutter.dart'; import 'package:comunes_flutter/comunes_flutter.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:redux/redux.dart';
import 'firebaseMessagingConf.dart'; import 'firebaseMessagingConf.dart';
import 'firesApp.dart'; import 'firesApp.dart';
import 'globals.dart' as globals; import 'globals.dart' as globals;
import 'models/appState.dart';
import 'redux/reducers.dart';
import 'yourLocationPersist.dart'; import 'yourLocationPersist.dart';
import 'models/firesApi.dart';
import 'package:get_it/get_it.dart';
Future<Map<String, dynamic>> loadSecrets() async { Future<Map<String, dynamic>> loadSecrets() async {
return await SecretLoader(secretPath: 'assets/private-settings.json').load(); return await SecretLoader(secretPath: 'assets/private-settings.json').load();
} }
void mainCommon() { void mainCommon(List<Middleware<AppState>> otherMiddleware) {
final store = new Store<AppState>(
appStateReducer,
initialState: new AppState(),
middleware: otherMiddleware,
);
globals.getIt.registerSingleton<FiresApi>(new FiresApi());
loadSecrets().then((secrets) { loadSecrets().then((secrets) {
globals.gmapKey = secrets['gmapKey']; globals.gmapKey = secrets['gmapKey'];
globals.firesApiKey = secrets['firesApiKey']; globals.firesApiKey = secrets['firesApiKey'];
@ -22,7 +36,10 @@ void mainCommon() {
firebaseConfig(); firebaseConfig();
// Run baby run! // Run baby run!
runApp(new FiresApp()); runApp(new FiresApp(store));
}); });
}); });
// Listen to store changes, and re-render when the state is updated
// store.onChange.listen(render);
} }

View file

@ -1,7 +1,14 @@
import 'package:redux_logging/redux_logging.dart';
import 'globals.dart' as globals; import 'globals.dart' as globals;
import 'mainCommon.dart'; import 'mainCommon.dart';
void main() { void main() {
globals.isDevelopment = true; globals.isDevelopment = true;
mainCommon();
List devMiddlewares = [
new LoggingMiddleware(formatter: LoggingMiddleware.multiLineFormatter)
];
mainCommon(devMiddlewares);
} }

View file

@ -3,5 +3,5 @@ import 'mainCommon.dart';
void main() { void main() {
globals.isDevelopment = false; globals.isDevelopment = false;
mainCommon(); mainCommon([]);
} }

14
lib/models/firesApi.dart Normal file
View file

@ -0,0 +1,14 @@
import 'yourLocation.dart';
import 'dart:async';
import 'package:jaguar_resty/jaguar_resty.dart' as resty;
class FiresApi {
Future<String> createUser(String token) async {
}
Future<List<YourLocation>> fetchYourLocations() async {
}
}

2
lib/redux/actions.dart Normal file
View file

@ -0,0 +1,2 @@
export 'appActions.dart';
export 'yourLocationActions.dart';

29
lib/redux/appActions.dart Normal file
View file

@ -0,0 +1,29 @@
import '../models/yourLocation.dart';
abstract class AppActions {}
class FetchYourLocationsAction extends AppActions {}
class FetchYourLocationsSucceededAction extends AppActions {
final List<YourLocation> fetchedYourLocations;
FetchYourLocationsSucceededAction(this.fetchedYourLocations);
}
class FetchYourLocationsFailedAction extends AppActions {
final Exception error;
FetchYourLocationsFailedAction(this.error);
}
class OnUserTokenAction extends AppActions {
final String token;
OnUserTokenAction(this.token);
}
class OnUserCreatedAction extends AppActions {
final String userId;
OnUserCreatedAction(this.userId);
}

View file

@ -0,0 +1,3 @@
String errorReducer(String error, action) {
return error;
}

View file

@ -0,0 +1,36 @@
import 'package:redux/redux.dart';
import 'actions.dart';
import '../models/yourLocation.dart';
import '../models/appState.dart';
import '../globals.dart' as globals;
import '../models/firesApi.dart';
// A middleware takes in 3 parameters: your Store, which you can use to
// read state or dispatch new actions, the action that was dispatched,
// and a `next` function. The first two you know about, and the `next`
// function is responsible for sending the action to your Reducer, or
// the next Middleware if you provide more than one.
//
// Middleware do not return any values themselves. They simply forward
// actions on to the Reducer or swallow actions in some special cases.
void fetchYourLocationsMiddleware(Store<AppState> store, action, NextDispatcher next) {
// If our Middleware encounters a `FetchYourLocationAction`
if (action is FetchYourLocationsAction) {
final api = globals.getIt.get<FiresApi>();
// Use the api to fetch the YourLocations
api.fetchYourLocations().then((List<YourLocation> YourLocations) {
// If it succeeds, dispatch a success action with the YourLocations.
// Our reducer will then update the State using these YourLocations.
store.dispatch(new FetchYourLocationsSucceededAction(YourLocations));
}).catchError((Exception error) {
// If it fails, dispatch a failure action. The reducer will
// update the state with the error.
store.dispatch(new FetchYourLocationsFailedAction(error));
});
}
// Make sure our actions continue on to the reducer.
next(action);
}

View file

@ -0,0 +1,5 @@
import '../models/fireMapState.dart';
FireMapState fireMapReducer(FireMapState state, action) {
return state;
}

View file

@ -0,0 +1,3 @@
bool loadedReducer(isLoaded, action) {
return isLoaded;
}

View file

@ -0,0 +1,3 @@
bool loadingReducer(isLoading, action) {
return isLoading;
}

22
lib/redux/reducers.dart Normal file
View file

@ -0,0 +1,22 @@
import '../models/appState.dart';
import 'yourLocationsReducer.dart';
import 'loadedReducer.dart';
import 'loadingReducer.dart';
import 'errorReducer.dart';
import 'userIdReducer.dart';
import 'userTokenReducer.dart';
import 'fireMapReducer.dart';
// We create the State reducer by combining many smaller reducers into one!
AppState appStateReducer(AppState state, action) {
return AppState(
yourLocations: yourLocationsReducer(state.yourLocations, action),
userId: userReducer(state.userId, action),
token: userTokenReducer(state.token, action),
isLoading: loadingReducer(state.isLoading, action),
isLoaded: loadedReducer(state.isLoaded, action),
error: errorReducer(state.error, action),
fireMapState: fireMapReducer(state.fireMapState, action)
);
}

View file

@ -0,0 +1,3 @@
String userReducer(String userId, action) {
return userId;
}

View file

@ -0,0 +1,3 @@
String userTokenReducer(String userToken, action) {
return userToken;
}

View file

@ -0,0 +1,41 @@
import 'package:bson_objectid/bson_objectid.dart';
import '../models/yourLocation.dart';
abstract class YourLocationActions {}
class AddYourLocationAction extends YourLocationActions {
YourLocation loc;
AddYourLocationAction(this.loc);
}
class DeleteYourLocationAction extends YourLocationActions {
ObjectId id;
DeleteYourLocationAction(this.id);
}
class UpdateYourLocationAction extends YourLocationActions {
ObjectId id;
YourLocation loc;
UpdateYourLocationAction(this.id, this.loc);
}
class ToggleSubscriptionAction extends YourLocationActions {
ObjectId id;
ToggleSubscriptionAction(this.id);
}
class SubscribeAction extends YourLocationActions {
ObjectId id;
SubscribeAction(this.id);
}
class UnSubscribeAction extends YourLocationActions {
ObjectId id;
UnSubscribeAction(this.id);
}

View file

@ -0,0 +1,51 @@
import 'package:redux/redux.dart';
import '../models/yourLocation.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>, UpdateYourLocationAction>(
_updateYourLocation),
/* 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) {
return new List.from(yourLocations)..add(action.loc);
}
List<YourLocation> _deleteYourLocation(
List<YourLocation> yourLocations, DeleteYourLocationAction action) {
return yourLocations
.where((yourLocation) => yourLocation.id != action.id)
.toList();
}
List<YourLocation> _updateYourLocation(
List<YourLocation> yourLocations, UpdateYourLocationAction action) {
return yourLocations
.map((yourLocation) =>
yourLocation.id == action.id ? action.loc : yourLocation)
.toList();
}
/*
List<YourLocation> _toggleSubscriptionAction(List<YourLocation> yourLocations, UpdateYourLocationAction action) {
return yourLocations
.map((yourLocation) => yourLocation.id == action.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 [];
}
*/