From 1ddca82c7073dc4edc344b189e9fd7e68769667b Mon Sep 17 00:00:00 2001 From: Ovidius Mazuru Date: Sun, 13 Sep 2020 15:55:58 +0100 Subject: [PATCH] 4.14, redux middleware --- redux_tut/lib/main.dart | 7 +++--- redux_tut/lib/redux/actions.dart | 14 +++++++++++ redux_tut/lib/redux/middleware.dart | 37 +++++++++++++++++++++++++++++ redux_tut/lib/redux/reducers.dart | 23 +++++++----------- 4 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 redux_tut/lib/redux/middleware.dart diff --git a/redux_tut/lib/main.dart b/redux_tut/lib/main.dart index 6b85446..b3c79f1 100644 --- a/redux_tut/lib/main.dart +++ b/redux_tut/lib/main.dart @@ -1,14 +1,15 @@ import 'package:flutter/material.dart'; import 'package:flutter_redux/flutter_redux.dart'; import 'package:redux/redux.dart'; -import 'package:redux_tut/redux/reducers.dart'; -import 'package:redux_tut/redux/store.dart'; +import 'redux/middleware.dart'; +import 'redux/reducers.dart'; +import 'redux/store.dart'; import 'screens/home_screen.dart'; void main() { Store _store = - Store(reducers, initialState: AppState.initial()); + Store(reducers, initialState: AppState.initial(), middleware: [appStateMiddleware]); runApp(MyApp(store: _store)); } diff --git a/redux_tut/lib/redux/actions.dart b/redux_tut/lib/redux/actions.dart index f23521f..700002c 100644 --- a/redux_tut/lib/redux/actions.dart +++ b/redux_tut/lib/redux/actions.dart @@ -9,3 +9,17 @@ class UpdateKm extends Action { this.payload, ); } + +class UpdateKmCleaned extends Action { + String payload; + + UpdateKmCleaned( + this.payload, + ); +} + +class UpdateMiles extends Action { + String payload; + + UpdateMiles(this.payload); +} \ No newline at end of file diff --git a/redux_tut/lib/redux/middleware.dart b/redux_tut/lib/redux/middleware.dart new file mode 100644 index 0000000..65e08a8 --- /dev/null +++ b/redux_tut/lib/redux/middleware.dart @@ -0,0 +1,37 @@ +import 'package:redux/redux.dart'; +import 'package:redux_tut/redux/actions.dart'; +import 'store.dart'; + +bool isNumeric(String c) { + /// isNumeric takes a string of length 1 + /// returns true if string is numeric, returns false otherwise + + return c.compareTo('0') >= 0 && c.compareTo('9') <= 0; +} + +void appStateMiddleware(Store store, dynamic action, NextDispatcher next) { + print("In the middleware"); + + if (action is UpdateKm) { + String cleanedString = ''; + for (String c in action.payload.split('')) { + if (isNumeric(c)) { + print("Adding $c to cleanedString"); + cleanedString += c; + print("cleanedString is $cleanedString"); + } else { + print("$c is not recognized as numeric"); + } + } + + store.dispatch(UpdateKmCleaned(cleanedString)); + } else if (action is Convert) { + print("Action is convert"); + double kmAsDouble = double.parse(store.state.km); + double milesAsDouble = kmAsDouble * 0.621371; + + store.dispatch(UpdateMiles(milesAsDouble.toString())); + } + + next(action); +} \ No newline at end of file diff --git a/redux_tut/lib/redux/reducers.dart b/redux_tut/lib/redux/reducers.dart index 14b2db7..7cf5ce1 100644 --- a/redux_tut/lib/redux/reducers.dart +++ b/redux_tut/lib/redux/reducers.dart @@ -2,21 +2,16 @@ import 'actions.dart'; import 'store.dart'; AppState reducers(AppState prevState, dynamic action) { - AppState newState; + print("-------------"); + print("In the reducer"); - if (action is UpdateKm) { - newState = AppState.copyWith(prev: prevState, km: action.payload); - } else if (action is Convert) { + if (action is UpdateKmCleaned) { print(action); - //! THE FOLLOW CODE BELONGS IN MIDDLEWARE - //! NOT IN REDUCERS!! - double kmAsDouble = double.parse(prevState.km); - double milesAsDouble = kmAsDouble * 0.621371; - - print(milesAsDouble); - - newState = AppState.copyWith(prev: prevState, miles: milesAsDouble.toString()); + return AppState.copyWith(prev: prevState, km: action.payload); + } else if (action is UpdateMiles) { + print(action); + return AppState.copyWith(prev: prevState, miles: action.payload); + } else { + return AppState(miles: prevState.miles, km: prevState.km); } - - return newState; } \ No newline at end of file