Skip to content

Commit c808dda

Browse files
authored
Add previous ViewModel to onWillChange (#163)
1 parent 9e47a65 commit c808dda

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

lib/flutter_redux.dart

+15-8
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,14 @@ typedef IgnoreChangeTest<S> = bool Function(S state);
144144
/// This function is passed the `ViewModel`, and if `distinct` is `true`,
145145
/// it will only be called if the `ViewModel` changes.
146146
///
147-
/// This can be useful for imperative calls to things like Navigator,
148-
/// TabController, etc
149-
typedef OnWillChangeCallback<ViewModel> = void Function(ViewModel viewModel);
147+
/// This is useful for making calls to other classes, such as a
148+
/// `Navigator` or `TabController`, in response to state changes.
149+
/// It can also be used to trigger an action based on the previous
150+
/// state.
151+
typedef OnWillChangeCallback<ViewModel> = void Function(
152+
ViewModel previousViewModel,
153+
ViewModel newViewModel,
154+
);
150155

151156
/// A function that will be run on State change, after the build method.
152157
///
@@ -237,7 +242,8 @@ class StoreConnector<S, ViewModel> extends StatelessWidget {
237242
/// it will only be called if the `ViewModel` changes.
238243
///
239244
/// This can be useful for imperative calls to things like Navigator,
240-
/// TabController, etc
245+
/// TabController, etc. This can also be useful for triggering actions
246+
/// based on the previous state.
241247
final OnWillChangeCallback<ViewModel> onWillChange;
242248

243249
/// A function that will be run on State change, after the Widget is built.
@@ -337,7 +343,8 @@ class StoreBuilder<S> extends StatelessWidget {
337343
/// A function that will be run on State change, before the Widget is built.
338344
///
339345
/// This can be useful for imperative calls to things like Navigator,
340-
/// TabController, etc
346+
/// TabController, etc. This can also be useful for triggering actions
347+
/// based on the previous state.
341348
final OnWillChangeCallback<Store<S>> onWillChange;
342349

343350
/// A function that will be run on State change, after the Widget is built.
@@ -509,12 +516,12 @@ class _StoreStreamListenerState<S, ViewModel>
509516
}
510517

511518
void _handleChange(ViewModel vm, EventSink<ViewModel> sink) {
512-
latestValue = vm;
513-
514519
if (widget.onWillChange != null) {
515-
widget.onWillChange(latestValue);
520+
widget.onWillChange(latestValue, vm);
516521
}
517522

523+
latestValue = vm;
524+
518525
if (widget.onDidChange != null) {
519526
WidgetsBinding.instance.addPostFrameCallback((_) {
520527
widget.onDidChange(latestValue);

test/flutter_redux_test.dart

+11-5
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ void main() {
341341
Widget widget() => StoreProvider<String>(
342342
store: store,
343343
child: StoreConnector<String, String>(
344-
onWillChange: (_) => states.add(BuildState.before),
344+
onWillChange: (_, __) => states.add(BuildState.before),
345345
converter: (store) => store.state,
346346
builder: (context, latest) {
347347
states.add(BuildState.during);
@@ -578,13 +578,15 @@ void main() {
578578
);
579579
testWidgets(
580580
'onWillChange works as expected',
581-
(WidgetTester tester) async {
581+
(WidgetTester tester) async {
582582
String currentState;
583583
final store = Store<String>(
584584
identityReducer,
585585
initialState: 'I',
586586
);
587-
Widget widget([void Function(String viewModel) onWillChange]) {
587+
Widget widget([
588+
void Function(String prev, String current) onWillChange,
589+
]) {
588590
return StoreProvider<String>(
589591
store: store,
590592
child: StoreConnector<String, String>(
@@ -605,7 +607,7 @@ void main() {
605607
expect(currentState, isNull);
606608

607609
// Build the widget with a new onWillChange
608-
final newWidget = widget((_) => currentState = 'S');
610+
final newWidget = widget((_, __) => currentState = 'S');
609611
await tester.pumpWidget(newWidget);
610612

611613
// Dispatch a new value, which should cause onWillChange to run
@@ -837,10 +839,14 @@ String identityReducer(String state, dynamic action) {
837839

838840
class CallCounter<S> {
839841
final List<S> states = [];
842+
final List<S> states2 = [];
840843

841844
int get callCount => states.length;
842845

843-
void call(S state) => states.add(state);
846+
void call(S s1, [S s2]) {
847+
states.add(s1);
848+
states2.add(s2);
849+
}
844850
}
845851

846852
enum BuildState { before, during, after }

0 commit comments

Comments
 (0)