-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrouter_state_mixin.dart
68 lines (64 loc) · 2.52 KB
/
router_state_mixin.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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:example/src/feature/shop/data/shop_tabs_cache_service.dart';
import 'package:flutter/widgets.dart'
show DefaultTransitionDelegate, 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);
// Observe all errors.
errorsObserver =
ValueNotifier<List<({Object error, StackTrace stackTrace})>>(
<({Object error, StackTrace stackTrace})>[],
);
// Create cache for shop tabs.
final shopTabCache = ShopTabsCacheService(
sharedPreferences: dependencies.sharedPreferences,
);
// Create router.
router = Octopus(
routes: Routes.values,
defaultRoute: Routes.home,
transitionDelegate: const DefaultTransitionDelegate<void>(),
guards: <IOctopusGuard>[
// Check authentication.
AuthenticationGuard(
// Get current user from authentication controller.
getUser: () => dependencies.authenticationController.state.user,
// Available routes for non authenticated user.
routes: <String>{
Routes.signin.name,
Routes.signup.name,
},
// Default route for non authenticated user.
signinNavigation: OctopusState.single(Routes.signin.node()),
// Default route for authenticated user.
homeNavigation: OctopusState.single(Routes.home.node()),
// Check authentication on every authentication controller state change.
refresh: dependencies.authenticationController,
),
// Home route should be always on top.
HomeGuard(),
// Check shop tabs with nested navigation.
ShopGuard(cache: shopTabCache),
],
onError: (error, stackTrace) =>
errorsObserver.value = <({Object error, StackTrace stackTrace})>[
(error: error, stackTrace: stackTrace),
...errorsObserver.value,
],
/* observers: <NavigatorObserver>[
HeroController(),
], */
);
super.initState();
}
}