From 5e9e5af6908a8506ddad39752dd351b2cc806380 Mon Sep 17 00:00:00 2001 From: Hugo Pointcheval Date: Tue, 14 Nov 2023 18:56:52 +0100 Subject: [PATCH] feat: update app template using wyatt_go_router --- apps/wyatt_app_template/brickgen.yaml | 2 +- .../lib/core/enums/page_protection.dart | 10 ----- .../lib/core/extensions/go_route_guard.dart | 37 ------------------- .../extensions/go_route_list_extension.dart | 21 ----------- .../core/extensions/route_base_extension.dart | 20 ---------- .../lib/core/routes/app_router.dart | 35 +++--------------- .../core/routes/go_router_refresh_stream.dart | 24 ------------ .../starting_template/pubspec.yaml | 9 ++++- .../lib/core/enums/page_protection.dart | 10 ----- .../lib/core/extensions/go_route_guard.dart | 37 ------------------- .../extensions/go_route_list_extension.dart | 21 ----------- .../core/extensions/route_base_extension.dart | 20 ---------- .../__brick__/lib/core/routes/app_router.dart | 35 +++--------------- .../core/routes/go_router_refresh_stream.dart | 24 ------------ .../wyatt_app_template/__brick__/pubspec.yaml | 9 ++++- bricks/wyatt_app_template/brick.yaml | 2 +- 16 files changed, 28 insertions(+), 288 deletions(-) delete mode 100644 apps/wyatt_app_template/starting_template/lib/core/enums/page_protection.dart delete mode 100644 apps/wyatt_app_template/starting_template/lib/core/extensions/go_route_guard.dart delete mode 100644 apps/wyatt_app_template/starting_template/lib/core/extensions/go_route_list_extension.dart delete mode 100644 apps/wyatt_app_template/starting_template/lib/core/extensions/route_base_extension.dart delete mode 100644 apps/wyatt_app_template/starting_template/lib/core/routes/go_router_refresh_stream.dart delete mode 100644 bricks/wyatt_app_template/__brick__/lib/core/enums/page_protection.dart delete mode 100644 bricks/wyatt_app_template/__brick__/lib/core/extensions/go_route_guard.dart delete mode 100644 bricks/wyatt_app_template/__brick__/lib/core/extensions/go_route_list_extension.dart delete mode 100644 bricks/wyatt_app_template/__brick__/lib/core/extensions/route_base_extension.dart delete mode 100644 bricks/wyatt_app_template/__brick__/lib/core/routes/go_router_refresh_stream.dart diff --git a/apps/wyatt_app_template/brickgen.yaml b/apps/wyatt_app_template/brickgen.yaml index 9d7c417..01468b7 100644 --- a/apps/wyatt_app_template/brickgen.yaml +++ b/apps/wyatt_app_template/brickgen.yaml @@ -17,7 +17,7 @@ name: wyatt_app_template description: New app template for Wyatt Studio projects. -version: 0.2.1 +version: 0.2.2 vars: display_name: diff --git a/apps/wyatt_app_template/starting_template/lib/core/enums/page_protection.dart b/apps/wyatt_app_template/starting_template/lib/core/enums/page_protection.dart deleted file mode 100644 index 4d63018..0000000 --- a/apps/wyatt_app_template/starting_template/lib/core/enums/page_protection.dart +++ /dev/null @@ -1,10 +0,0 @@ -enum PageProtection { - /// The page can be accessed without authentication. - public, - - /// The page can only be accessed with authentication. - protected, - - /// The page protection is unknown, and the default one should be used. - none, -} diff --git a/apps/wyatt_app_template/starting_template/lib/core/extensions/go_route_guard.dart b/apps/wyatt_app_template/starting_template/lib/core/extensions/go_route_guard.dart deleted file mode 100644 index c9148d1..0000000 --- a/apps/wyatt_app_template/starting_template/lib/core/extensions/go_route_guard.dart +++ /dev/null @@ -1,37 +0,0 @@ -import 'package:go_router/go_router.dart'; -import 'package:starting_template/core/enums/page_protection.dart'; - -/// Defines if a GoRoute is public or not. -/// -/// By default, all routes are in the [PageProtection.none] state. -extension GoRouteGuard on GoRoute { - static final _guard = Expando(); - - /// Returns `true` if the route is public. - bool get isPublic => _guard[this] == PageProtection.public; - - /// Returns `true` if the route is protected. - bool get isProtected => _guard[this] == PageProtection.protected; - - /// Returns `true` if the route is neither public nor protected. - /// This is the default state. - bool get isNone => _guard[this] == PageProtection.none; - - /// Sets the route to be public. - /// This is useful for routes that should be accessible - /// without authentication. - /// ```dart - /// GoRoute( - /// path: '/sign_in', - /// ... - /// )..setPublic(), - /// ``` - void setPublic() => _guard[this] = PageProtection.public; - - /// Sets the route to be protected. - /// This is useful for routes that should only be accessible - /// with authentication. - void setProtected() => _guard[this] = PageProtection.protected; - - PageProtection get guard => _guard[this] ?? PageProtection.none; -} diff --git a/apps/wyatt_app_template/starting_template/lib/core/extensions/go_route_list_extension.dart b/apps/wyatt_app_template/starting_template/lib/core/extensions/go_route_list_extension.dart deleted file mode 100644 index b7a9fd6..0000000 --- a/apps/wyatt_app_template/starting_template/lib/core/extensions/go_route_list_extension.dart +++ /dev/null @@ -1,21 +0,0 @@ -import 'package:go_router/go_router.dart'; -import 'package:starting_template/core/extensions/go_route_guard.dart'; - -extension GoRouteListExtension on Iterable { - /// Returns a list of public routes. - List get publicRoutes => - where((route) => route.isPublic).toList(growable: false); - - /// Returns a list of protected routes. - List get protectedRoutes => - where((route) => route.isProtected).toList(growable: false); - - /// Returns a list of routes that are neither public nor protected. - List get noneRoutes => - where((route) => route.isNone).toList(growable: false); - - GoRoute fromName(String name) => firstWhere((route) => route.name == name); - GoRoute fromPath(String path) => firstWhere((route) => route.path == path); - - GoRoute operator [](String name) => fromName(name); -} diff --git a/apps/wyatt_app_template/starting_template/lib/core/extensions/route_base_extension.dart b/apps/wyatt_app_template/starting_template/lib/core/extensions/route_base_extension.dart deleted file mode 100644 index 9866537..0000000 --- a/apps/wyatt_app_template/starting_template/lib/core/extensions/route_base_extension.dart +++ /dev/null @@ -1,20 +0,0 @@ -import 'package:go_router/go_router.dart'; - -/// Flatten the [GoRoute] tree. -extension RouteBaseExtension on RouteBase { - List flatten() { - final List routes = []; - if (this is GoRoute) { - routes.add(this as GoRoute); - } else if (this is ShellRoute) { - routes.addAll( - (this as ShellRoute) - .routes - .map((route) => route.flatten()) - .expand((routeList) => routeList), - ); - } - - return routes; - } -} diff --git a/apps/wyatt_app_template/starting_template/lib/core/routes/app_router.dart b/apps/wyatt_app_template/starting_template/lib/core/routes/app_router.dart index 63b0ae8..5f132c9 100644 --- a/apps/wyatt_app_template/starting_template/lib/core/routes/app_router.dart +++ b/apps/wyatt_app_template/starting_template/lib/core/routes/app_router.dart @@ -1,10 +1,6 @@ import 'package:flutter/cupertino.dart'; -import 'package:go_router/go_router.dart'; -import 'package:starting_template/core/enums/page_protection.dart'; -import 'package:starting_template/core/extensions/go_route_guard.dart'; -import 'package:starting_template/core/extensions/route_base_extension.dart'; -import 'package:starting_template/core/routes/go_router_refresh_stream.dart'; import 'package:starting_template/presentation/features/home/home.dart'; +import 'package:wyatt_go_router/wyatt_go_router.dart'; abstract class AppRouter { /// Default transition for all pages @@ -52,34 +48,15 @@ abstract class AppRouter { /// Define the default guard /// This is the guard that will be used if the route /// does not have a guard set. (It is set to [PageProtection.none]) - const defaultGuard = PageProtection.protected; - - /// Recursively get all routes and subroutes in a one-dimensional list - /// This will work for any level of nesting - final routes = - AppRouter.routes.map((route) => route.flatten()).expand( - (routeList) => routeList, - ); - - /// Matches is a list of all routes that match the current uri - final matches = _router?.configuration - .findMatch( - state.uri.toString(), - ) - .routes - .map((route) => route.flatten()) - .expand( - (routeList) => routeList, - ) ?? - routes; + const defaultProtection = PageProtection.protected; /// Get the current route - final currentRoute = matches.last; + final currentRoute = context.currentRoute; /// Get the guard of the current route - final guard = (currentRoute.guard == PageProtection.none) - ? defaultGuard - : currentRoute.guard; + final _ = (currentRoute.protection == PageProtection.none) + ? defaultProtection + : currentRoute.protection; return null; }, diff --git a/apps/wyatt_app_template/starting_template/lib/core/routes/go_router_refresh_stream.dart b/apps/wyatt_app_template/starting_template/lib/core/routes/go_router_refresh_stream.dart deleted file mode 100644 index 269555c..0000000 --- a/apps/wyatt_app_template/starting_template/lib/core/routes/go_router_refresh_stream.dart +++ /dev/null @@ -1,24 +0,0 @@ -import 'dart:async'; - -import 'package:flutter/foundation.dart'; - -/// {@template go_router_refresh_stream} -/// A [ChangeNotifier] that notifies its listeners when a stream emits a value. -/// {@endtemplate} -class GoRouterRefreshStream extends ChangeNotifier { - /// {@macro go_router_refresh_stream} - GoRouterRefreshStream(Stream stream) { - notifyListeners(); - _subscription = stream.asBroadcastStream().listen( - (dynamic _) => notifyListeners(), - ); - } - - late final StreamSubscription _subscription; - - @override - void dispose() { - _subscription.cancel(); - super.dispose(); - } -} diff --git a/apps/wyatt_app_template/starting_template/pubspec.yaml b/apps/wyatt_app_template/starting_template/pubspec.yaml index 4efc88d..02d6208 100644 --- a/apps/wyatt_app_template/starting_template/pubspec.yaml +++ b/apps/wyatt_app_template/starting_template/pubspec.yaml @@ -15,9 +15,9 @@ dependencies: freezed_annotation: ^2.4.1 gap: ^3.0.1 get_it: ^7.6.4 - go_router: ^10.1.2 intl: ^0.18.1 json_annotation: ^4.8.1 + logging: ^1.2.0 flutter: sdk: flutter @@ -41,7 +41,12 @@ dependencies: hosted: name: wyatt_type_utils url: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub/ - logging: ^1.2.0 + + wyatt_go_router: + version: ^1.0.0 + hosted: + name: wyatt_go_router + url: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub/ dev_dependencies: build_runner: ^2.4.6 diff --git a/bricks/wyatt_app_template/__brick__/lib/core/enums/page_protection.dart b/bricks/wyatt_app_template/__brick__/lib/core/enums/page_protection.dart deleted file mode 100644 index 4d63018..0000000 --- a/bricks/wyatt_app_template/__brick__/lib/core/enums/page_protection.dart +++ /dev/null @@ -1,10 +0,0 @@ -enum PageProtection { - /// The page can be accessed without authentication. - public, - - /// The page can only be accessed with authentication. - protected, - - /// The page protection is unknown, and the default one should be used. - none, -} diff --git a/bricks/wyatt_app_template/__brick__/lib/core/extensions/go_route_guard.dart b/bricks/wyatt_app_template/__brick__/lib/core/extensions/go_route_guard.dart deleted file mode 100644 index e5e8ecd..0000000 --- a/bricks/wyatt_app_template/__brick__/lib/core/extensions/go_route_guard.dart +++ /dev/null @@ -1,37 +0,0 @@ -import 'package:go_router/go_router.dart'; -import 'package:{{#snakeCase}}{{project_name}}{{/snakeCase}}/core/enums/page_protection.dart'; - -/// Defines if a GoRoute is public or not. -/// -/// By default, all routes are in the [PageProtection.none] state. -extension GoRouteGuard on GoRoute { - static final _guard = Expando(); - - /// Returns `true` if the route is public. - bool get isPublic => _guard[this] == PageProtection.public; - - /// Returns `true` if the route is protected. - bool get isProtected => _guard[this] == PageProtection.protected; - - /// Returns `true` if the route is neither public nor protected. - /// This is the default state. - bool get isNone => _guard[this] == PageProtection.none; - - /// Sets the route to be public. - /// This is useful for routes that should be accessible - /// without authentication. - /// ```dart - /// GoRoute( - /// path: '/sign_in', - /// ... - /// )..setPublic(), - /// ``` - void setPublic() => _guard[this] = PageProtection.public; - - /// Sets the route to be protected. - /// This is useful for routes that should only be accessible - /// with authentication. - void setProtected() => _guard[this] = PageProtection.protected; - - PageProtection get guard => _guard[this] ?? PageProtection.none; -} diff --git a/bricks/wyatt_app_template/__brick__/lib/core/extensions/go_route_list_extension.dart b/bricks/wyatt_app_template/__brick__/lib/core/extensions/go_route_list_extension.dart deleted file mode 100644 index f1eb6ab..0000000 --- a/bricks/wyatt_app_template/__brick__/lib/core/extensions/go_route_list_extension.dart +++ /dev/null @@ -1,21 +0,0 @@ -import 'package:go_router/go_router.dart'; -import 'package:{{#snakeCase}}{{project_name}}{{/snakeCase}}/core/extensions/go_route_guard.dart'; - -extension GoRouteListExtension on Iterable { - /// Returns a list of public routes. - List get publicRoutes => - where((route) => route.isPublic).toList(growable: false); - - /// Returns a list of protected routes. - List get protectedRoutes => - where((route) => route.isProtected).toList(growable: false); - - /// Returns a list of routes that are neither public nor protected. - List get noneRoutes => - where((route) => route.isNone).toList(growable: false); - - GoRoute fromName(String name) => firstWhere((route) => route.name == name); - GoRoute fromPath(String path) => firstWhere((route) => route.path == path); - - GoRoute operator [](String name) => fromName(name); -} diff --git a/bricks/wyatt_app_template/__brick__/lib/core/extensions/route_base_extension.dart b/bricks/wyatt_app_template/__brick__/lib/core/extensions/route_base_extension.dart deleted file mode 100644 index 9866537..0000000 --- a/bricks/wyatt_app_template/__brick__/lib/core/extensions/route_base_extension.dart +++ /dev/null @@ -1,20 +0,0 @@ -import 'package:go_router/go_router.dart'; - -/// Flatten the [GoRoute] tree. -extension RouteBaseExtension on RouteBase { - List flatten() { - final List routes = []; - if (this is GoRoute) { - routes.add(this as GoRoute); - } else if (this is ShellRoute) { - routes.addAll( - (this as ShellRoute) - .routes - .map((route) => route.flatten()) - .expand((routeList) => routeList), - ); - } - - return routes; - } -} diff --git a/bricks/wyatt_app_template/__brick__/lib/core/routes/app_router.dart b/bricks/wyatt_app_template/__brick__/lib/core/routes/app_router.dart index 0a63502..f6f5d19 100644 --- a/bricks/wyatt_app_template/__brick__/lib/core/routes/app_router.dart +++ b/bricks/wyatt_app_template/__brick__/lib/core/routes/app_router.dart @@ -1,10 +1,6 @@ import 'package:flutter/cupertino.dart'; -import 'package:go_router/go_router.dart'; -import 'package:{{#snakeCase}}{{project_name}}{{/snakeCase}}/core/enums/page_protection.dart'; -import 'package:{{#snakeCase}}{{project_name}}{{/snakeCase}}/core/extensions/go_route_guard.dart'; -import 'package:{{#snakeCase}}{{project_name}}{{/snakeCase}}/core/extensions/route_base_extension.dart'; -import 'package:{{#snakeCase}}{{project_name}}{{/snakeCase}}/core/routes/go_router_refresh_stream.dart'; import 'package:{{#snakeCase}}{{project_name}}{{/snakeCase}}/presentation/features/home/home.dart'; +import 'package:wyatt_go_router/wyatt_go_router.dart'; abstract class AppRouter { /// Default transition for all pages @@ -52,34 +48,15 @@ abstract class AppRouter { /// Define the default guard /// This is the guard that will be used if the route /// does not have a guard set. (It is set to [PageProtection.none]) - const defaultGuard = PageProtection.protected; - - /// Recursively get all routes and subroutes in a one-dimensional list - /// This will work for any level of nesting - final routes = - AppRouter.routes.map((route) => route.flatten()).expand( - (routeList) => routeList, - ); - - /// Matches is a list of all routes that match the current uri - final matches = _router?.configuration - .findMatch( - state.uri.toString(), - ) - .routes - .map((route) => route.flatten()) - .expand( - (routeList) => routeList, - ) ?? - routes; + const defaultProtection = PageProtection.protected; /// Get the current route - final currentRoute = matches.last; + final currentRoute = context.currentRoute; /// Get the guard of the current route - final guard = (currentRoute.guard == PageProtection.none) - ? defaultGuard - : currentRoute.guard; + final _ = (currentRoute.protection == PageProtection.none) + ? defaultProtection + : currentRoute.protection; return null; }, diff --git a/bricks/wyatt_app_template/__brick__/lib/core/routes/go_router_refresh_stream.dart b/bricks/wyatt_app_template/__brick__/lib/core/routes/go_router_refresh_stream.dart deleted file mode 100644 index 269555c..0000000 --- a/bricks/wyatt_app_template/__brick__/lib/core/routes/go_router_refresh_stream.dart +++ /dev/null @@ -1,24 +0,0 @@ -import 'dart:async'; - -import 'package:flutter/foundation.dart'; - -/// {@template go_router_refresh_stream} -/// A [ChangeNotifier] that notifies its listeners when a stream emits a value. -/// {@endtemplate} -class GoRouterRefreshStream extends ChangeNotifier { - /// {@macro go_router_refresh_stream} - GoRouterRefreshStream(Stream stream) { - notifyListeners(); - _subscription = stream.asBroadcastStream().listen( - (dynamic _) => notifyListeners(), - ); - } - - late final StreamSubscription _subscription; - - @override - void dispose() { - _subscription.cancel(); - super.dispose(); - } -} diff --git a/bricks/wyatt_app_template/__brick__/pubspec.yaml b/bricks/wyatt_app_template/__brick__/pubspec.yaml index 2454d01..221fa8a 100644 --- a/bricks/wyatt_app_template/__brick__/pubspec.yaml +++ b/bricks/wyatt_app_template/__brick__/pubspec.yaml @@ -15,9 +15,9 @@ dependencies: freezed_annotation: ^2.4.1 gap: ^3.0.1 get_it: ^7.6.4 - go_router: ^10.1.2 intl: ^0.18.1 json_annotation: ^4.8.1 + logging: ^1.2.0 flutter: sdk: flutter @@ -41,7 +41,12 @@ dependencies: hosted: name: wyatt_type_utils url: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub/ - logging: ^1.2.0 + + wyatt_go_router: + version: ^1.0.0 + hosted: + name: wyatt_go_router + url: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub/ dev_dependencies: build_runner: ^2.4.6 diff --git a/bricks/wyatt_app_template/brick.yaml b/bricks/wyatt_app_template/brick.yaml index d184f99..aab4a03 100644 --- a/bricks/wyatt_app_template/brick.yaml +++ b/bricks/wyatt_app_template/brick.yaml @@ -1,7 +1,7 @@ name: wyatt_app_template description: New app template for Wyatt Studio projects. -version: 0.2.1 +version: 0.2.2 vars: display_name: