Skip to content

Commit

Permalink
Add RouterStateMixin and ShopGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Dec 18, 2023
1 parent bbbd882 commit 15ad188
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 45 deletions.
49 changes: 49 additions & 0 deletions example/lib/src/common/router/router_state_mixin.dart
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();
}
}
20 changes: 20 additions & 0 deletions example/lib/src/common/router/shop_guard.dart
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'));
}
47 changes: 2 additions & 45 deletions example/lib/src/common/widget/app.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import 'package:example/src/common/constant/config.dart';
import 'package:example/src/common/localization/localization.dart';
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/router_state_mixin.dart';
import 'package:example/src/common/widget/router_state_observer.dart';
import 'package:example/src/feature/authentication/widget/authentication_scope.dart';
import 'package:example/src/feature/shop/widget/shop_scope.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:octopus/octopus.dart';

/// {@template app}
/// App widget.
Expand All @@ -22,46 +18,7 @@ class App extends StatefulWidget {
State<App> createState() => _AppState();
}

class _AppState extends State<App> {
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(),
],
onError: (error, stackTrace) =>
errorsObserver.value = <({Object error, StackTrace stackTrace})>[
(error: error, stackTrace: stackTrace),
...errorsObserver.value,
],
/* observers: <NavigatorObserver>[
HeroController(),
], */
);
super.initState();
}

class _AppState extends State<App> with RouterStateMixin {
@override
Widget build(BuildContext context) => MaterialApp.router(
title: 'Octopus: example',
Expand Down

0 comments on commit 15ad188

Please sign in to comment.