-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
71 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'package:example/src/common/model/dependencies.dart'; | ||
import 'package:example/src/common/router/authentication_guard.dart'; | ||
import 'package:example/src/common/router/home_guard.dart'; | ||
import 'package:example/src/common/router/routes.dart'; | ||
import 'package:example/src/common/router/shop_guard.dart'; | ||
import 'package:flutter/widgets.dart' show State, StatefulWidget, ValueNotifier; | ||
import 'package:octopus/octopus.dart'; | ||
|
||
mixin RouterStateMixin<T extends StatefulWidget> on State<T> { | ||
late final Octopus router; | ||
late final ValueNotifier<List<({Object error, StackTrace stackTrace})>> | ||
errorsObserver; | ||
|
||
@override | ||
void initState() { | ||
final dependencies = Dependencies.of(context); | ||
errorsObserver = | ||
ValueNotifier<List<({Object error, StackTrace stackTrace})>>( | ||
<({Object error, StackTrace stackTrace})>[], | ||
); | ||
router = Octopus( | ||
routes: Routes.values, | ||
defaultRoute: Routes.home, | ||
guards: <IOctopusGuard>[ | ||
AuthenticationGuard( | ||
getUser: () => dependencies.authenticationController.state.user, | ||
routes: <String>{ | ||
Routes.signin.name, | ||
Routes.signup.name, | ||
}, | ||
signinNavigation: OctopusState.single(Routes.signin.node()), | ||
homeNavigation: OctopusState.single(Routes.home.node()), | ||
refresh: dependencies.authenticationController, | ||
), | ||
HomeGuard(), | ||
ShopGuard(), | ||
], | ||
onError: (error, stackTrace) => | ||
errorsObserver.value = <({Object error, StackTrace stackTrace})>[ | ||
(error: error, stackTrace: stackTrace), | ||
...errorsObserver.value, | ||
], | ||
/* observers: <NavigatorObserver>[ | ||
HeroController(), | ||
], */ | ||
); | ||
super.initState(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:example/src/common/router/routes.dart'; | ||
import 'package:octopus/octopus.dart'; | ||
|
||
/// Do not allow any nested routes at `shop` inderectly except of `*-tab`. | ||
class ShopGuard extends OctopusGuard { | ||
ShopGuard(); | ||
|
||
@override | ||
FutureOr<OctopusState?> call( | ||
List<OctopusHistoryEntry> history, | ||
OctopusState state, | ||
Map<String, Object?> context, | ||
) => | ||
state | ||
..find((node) => node.name == Routes.shop.name) | ||
?.children | ||
.removeWhere((node) => !node.name.endsWith('-tab')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters