feat: update app template using wyatt_go_router
This commit is contained in:
		
							parent
							
								
									0d2605717a
								
							
						
					
					
						commit
						5e9e5af690
					
				| @ -17,7 +17,7 @@ | |||||||
| name: wyatt_app_template | name: wyatt_app_template | ||||||
| description: New app template for Wyatt Studio projects. | description: New app template for Wyatt Studio projects. | ||||||
| 
 | 
 | ||||||
| version: 0.2.1 | version: 0.2.2 | ||||||
| 
 | 
 | ||||||
| vars: | vars: | ||||||
|   display_name: |   display_name: | ||||||
|  | |||||||
| @ -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, |  | ||||||
| } |  | ||||||
| @ -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<PageProtection>(); |  | ||||||
| 
 |  | ||||||
|   /// 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; |  | ||||||
| } |  | ||||||
| @ -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<GoRoute> { |  | ||||||
|   /// Returns a list of public routes. |  | ||||||
|   List<GoRoute> get publicRoutes => |  | ||||||
|       where((route) => route.isPublic).toList(growable: false); |  | ||||||
| 
 |  | ||||||
|   /// Returns a list of protected routes. |  | ||||||
|   List<GoRoute> get protectedRoutes => |  | ||||||
|       where((route) => route.isProtected).toList(growable: false); |  | ||||||
| 
 |  | ||||||
|   /// Returns a list of routes that are neither public nor protected. |  | ||||||
|   List<GoRoute> 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); |  | ||||||
| } |  | ||||||
| @ -1,20 +0,0 @@ | |||||||
| import 'package:go_router/go_router.dart'; |  | ||||||
| 
 |  | ||||||
| /// Flatten the [GoRoute] tree. |  | ||||||
| extension RouteBaseExtension on RouteBase { |  | ||||||
|   List<GoRoute> flatten() { |  | ||||||
|     final List<GoRoute> 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; |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -1,10 +1,6 @@ | |||||||
| import 'package:flutter/cupertino.dart'; | 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:starting_template/presentation/features/home/home.dart'; | ||||||
|  | import 'package:wyatt_go_router/wyatt_go_router.dart'; | ||||||
| 
 | 
 | ||||||
| abstract class AppRouter { | abstract class AppRouter { | ||||||
|   /// Default transition for all pages |   /// Default transition for all pages | ||||||
| @ -52,34 +48,15 @@ abstract class AppRouter { | |||||||
|           /// Define the default guard |           /// Define the default guard | ||||||
|           /// This is the guard that will be used if the route |           /// This is the guard that will be used if the route | ||||||
|           /// does not have a guard set. (It is set to [PageProtection.none]) |           /// does not have a guard set. (It is set to [PageProtection.none]) | ||||||
|           const defaultGuard = PageProtection.protected; |           const defaultProtection = 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; |  | ||||||
| 
 | 
 | ||||||
|           /// Get the current route |           /// Get the current route | ||||||
|           final currentRoute = matches.last; |           final currentRoute = context.currentRoute; | ||||||
| 
 | 
 | ||||||
|           /// Get the guard of the current route |           /// Get the guard of the current route | ||||||
|           final guard = (currentRoute.guard == PageProtection.none) |           final _ = (currentRoute.protection == PageProtection.none) | ||||||
|               ? defaultGuard |               ? defaultProtection | ||||||
|               : currentRoute.guard; |               : currentRoute.protection; | ||||||
| 
 | 
 | ||||||
|           return null; |           return 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<dynamic> stream) { |  | ||||||
|     notifyListeners(); |  | ||||||
|     _subscription = stream.asBroadcastStream().listen( |  | ||||||
|           (dynamic _) => notifyListeners(), |  | ||||||
|         ); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   late final StreamSubscription<dynamic> _subscription; |  | ||||||
| 
 |  | ||||||
|   @override |  | ||||||
|   void dispose() { |  | ||||||
|     _subscription.cancel(); |  | ||||||
|     super.dispose(); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -15,9 +15,9 @@ dependencies: | |||||||
|   freezed_annotation: ^2.4.1 |   freezed_annotation: ^2.4.1 | ||||||
|   gap: ^3.0.1 |   gap: ^3.0.1 | ||||||
|   get_it: ^7.6.4 |   get_it: ^7.6.4 | ||||||
|   go_router: ^10.1.2 |  | ||||||
|   intl: ^0.18.1 |   intl: ^0.18.1 | ||||||
|   json_annotation: ^4.8.1 |   json_annotation: ^4.8.1 | ||||||
|  |   logging: ^1.2.0 | ||||||
| 
 | 
 | ||||||
|   flutter: |   flutter: | ||||||
|     sdk: flutter |     sdk: flutter | ||||||
| @ -41,7 +41,12 @@ dependencies: | |||||||
|     hosted: |     hosted: | ||||||
|       name: wyatt_type_utils |       name: wyatt_type_utils | ||||||
|       url: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub/ |       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: | dev_dependencies: | ||||||
|   build_runner: ^2.4.6 |   build_runner: ^2.4.6 | ||||||
|  | |||||||
| @ -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, |  | ||||||
| } |  | ||||||
| @ -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<PageProtection>(); |  | ||||||
| 
 |  | ||||||
|   /// 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; |  | ||||||
| } |  | ||||||
| @ -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<GoRoute> { |  | ||||||
|   /// Returns a list of public routes. |  | ||||||
|   List<GoRoute> get publicRoutes => |  | ||||||
|       where((route) => route.isPublic).toList(growable: false); |  | ||||||
| 
 |  | ||||||
|   /// Returns a list of protected routes. |  | ||||||
|   List<GoRoute> get protectedRoutes => |  | ||||||
|       where((route) => route.isProtected).toList(growable: false); |  | ||||||
| 
 |  | ||||||
|   /// Returns a list of routes that are neither public nor protected. |  | ||||||
|   List<GoRoute> 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); |  | ||||||
| } |  | ||||||
| @ -1,20 +0,0 @@ | |||||||
| import 'package:go_router/go_router.dart'; |  | ||||||
| 
 |  | ||||||
| /// Flatten the [GoRoute] tree. |  | ||||||
| extension RouteBaseExtension on RouteBase { |  | ||||||
|   List<GoRoute> flatten() { |  | ||||||
|     final List<GoRoute> 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; |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -1,10 +1,6 @@ | |||||||
| import 'package:flutter/cupertino.dart'; | 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:{{#snakeCase}}{{project_name}}{{/snakeCase}}/presentation/features/home/home.dart'; | ||||||
|  | import 'package:wyatt_go_router/wyatt_go_router.dart'; | ||||||
| 
 | 
 | ||||||
| abstract class AppRouter { | abstract class AppRouter { | ||||||
|   /// Default transition for all pages |   /// Default transition for all pages | ||||||
| @ -52,34 +48,15 @@ abstract class AppRouter { | |||||||
|           /// Define the default guard |           /// Define the default guard | ||||||
|           /// This is the guard that will be used if the route |           /// This is the guard that will be used if the route | ||||||
|           /// does not have a guard set. (It is set to [PageProtection.none]) |           /// does not have a guard set. (It is set to [PageProtection.none]) | ||||||
|           const defaultGuard = PageProtection.protected; |           const defaultProtection = 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; |  | ||||||
| 
 | 
 | ||||||
|           /// Get the current route |           /// Get the current route | ||||||
|           final currentRoute = matches.last; |           final currentRoute = context.currentRoute; | ||||||
| 
 | 
 | ||||||
|           /// Get the guard of the current route |           /// Get the guard of the current route | ||||||
|           final guard = (currentRoute.guard == PageProtection.none) |           final _ = (currentRoute.protection == PageProtection.none) | ||||||
|               ? defaultGuard |               ? defaultProtection | ||||||
|               : currentRoute.guard; |               : currentRoute.protection; | ||||||
| 
 | 
 | ||||||
|           return null; |           return 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<dynamic> stream) { |  | ||||||
|     notifyListeners(); |  | ||||||
|     _subscription = stream.asBroadcastStream().listen( |  | ||||||
|           (dynamic _) => notifyListeners(), |  | ||||||
|         ); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   late final StreamSubscription<dynamic> _subscription; |  | ||||||
| 
 |  | ||||||
|   @override |  | ||||||
|   void dispose() { |  | ||||||
|     _subscription.cancel(); |  | ||||||
|     super.dispose(); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -15,9 +15,9 @@ dependencies: | |||||||
|   freezed_annotation: ^2.4.1 |   freezed_annotation: ^2.4.1 | ||||||
|   gap: ^3.0.1 |   gap: ^3.0.1 | ||||||
|   get_it: ^7.6.4 |   get_it: ^7.6.4 | ||||||
|   go_router: ^10.1.2 |  | ||||||
|   intl: ^0.18.1 |   intl: ^0.18.1 | ||||||
|   json_annotation: ^4.8.1 |   json_annotation: ^4.8.1 | ||||||
|  |   logging: ^1.2.0 | ||||||
| 
 | 
 | ||||||
|   flutter: |   flutter: | ||||||
|     sdk: flutter |     sdk: flutter | ||||||
| @ -41,7 +41,12 @@ dependencies: | |||||||
|     hosted: |     hosted: | ||||||
|       name: wyatt_type_utils |       name: wyatt_type_utils | ||||||
|       url: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub/ |       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: | dev_dependencies: | ||||||
|   build_runner: ^2.4.6 |   build_runner: ^2.4.6 | ||||||
|  | |||||||
| @ -1,7 +1,7 @@ | |||||||
| name: wyatt_app_template | name: wyatt_app_template | ||||||
| description: New app template for Wyatt Studio projects. | description: New app template for Wyatt Studio projects. | ||||||
| 
 | 
 | ||||||
| version: 0.2.1 | version: 0.2.2 | ||||||
| 
 | 
 | ||||||
| vars: | vars: | ||||||
|   display_name: |   display_name: | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user