Skip to content

Commit

Permalink
4.14, redux middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
trance128 committed Sep 13, 2020
1 parent 5ea6761 commit 1ddca82
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
7 changes: 4 additions & 3 deletions redux_tut/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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<AppState> _store =
Store<AppState>(reducers, initialState: AppState.initial());
Store<AppState>(reducers, initialState: AppState.initial(), middleware: [appStateMiddleware]);

runApp(MyApp(store: _store));
}
Expand Down
14 changes: 14 additions & 0 deletions redux_tut/lib/redux/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
37 changes: 37 additions & 0 deletions redux_tut/lib/redux/middleware.dart
Original file line number Diff line number Diff line change
@@ -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<AppState> 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);
}
23 changes: 9 additions & 14 deletions redux_tut/lib/redux/reducers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 1ddca82

Please sign in to comment.