Skip to content

Commit

Permalink
{beamer_delegate}: [add] updateListenable
Browse files Browse the repository at this point in the history
	- closes #467
  • Loading branch information
slovnicki committed Feb 5, 2022
1 parent 46d60c8 commit f0ccfd7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
10 changes: 10 additions & 0 deletions package/lib/src/beamer_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BeamerDelegate extends RouterDelegate<RouteInformation>
this.initialPath = '/',
this.routeListener,
this.buildListener,
this.updateListenable,
@Deprecated(
'No longer used by this package, please remove any references to it. '
'This feature was deprecated after v1.0.0.',
Expand All @@ -42,6 +43,8 @@ class BeamerDelegate extends RouterDelegate<RouteInformation>
);

configuration = RouteInformation(location: initialPath);

updateListenable?.addListener(_update);
}

/// A state of this delegate. This is the `routeInformation` that goes into
Expand Down Expand Up @@ -136,6 +139,10 @@ class BeamerDelegate extends RouterDelegate<RouteInformation>
/// are updated. it receives a reference to this delegate.
final void Function(BuildContext, BeamerDelegate)? buildListener;

/// A Listenable to which an update listener will be added, i.e.
/// [update] will be called when listeners are notified.
final Listenable? updateListenable;

@Deprecated(
'No longer used by this package, please remove any references to it. '
'This feature was deprecated after v1.0.0.',
Expand Down Expand Up @@ -979,6 +986,8 @@ class BeamerDelegate extends RouterDelegate<RouteInformation>
_beamLocationCandidate = location;
}

void _update() => update();

void _updateFromParent({bool rebuild = true}) {
update(
configuration: _parent!.configuration.copyWith(),
Expand Down Expand Up @@ -1009,6 +1018,7 @@ class BeamerDelegate extends RouterDelegate<RouteInformation>
_parent?.removeListener(_updateFromParent);
_disposeBeamLocation(currentBeamLocation);
currentBeamLocation.dispose();
updateListenable?.removeListener(_update);
super.dispose();
}
}
34 changes: 34 additions & 0 deletions package/test/beamer_delegate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -619,4 +619,38 @@ void main() {
expect(delegate.beamingHistory.last.history.length, 1);
});
});

testWidgets('updateListenable', (tester) async {
final guardCheck = ValueNotifier<bool>(true);

final delegate = BeamerDelegate(
initialPath: '/r1',
locationBuilder: RoutesLocationBuilder(
routes: {
'/r1': (context, state, data) => Container(),
'/r2': (context, state, data) => Container(),
},
),
guards: [
BeamGuard(
pathPatterns: ['/r1'],
check: (_, __) => guardCheck.value,
beamToNamed: (_, __) => '/r2',
),
],
updateListenable: guardCheck,
);
await tester.pumpWidget(
MaterialApp.router(
routeInformationParser: BeamerParser(),
routerDelegate: delegate,
),
);

expect(delegate.configuration.location, '/r1');

guardCheck.value = false;

expect(delegate.configuration.location, '/r2');
});
}

0 comments on commit f0ccfd7

Please sign in to comment.