This commit is contained in:
Christien Rioux 2023-07-23 23:13:21 -04:00
parent f06657d700
commit 8907ce04ac
72 changed files with 539 additions and 224 deletions

View file

@ -5,12 +5,14 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../pages/pages.dart';
import 'router_notifier.dart';
part 'router.g.dart';
final _key = GlobalKey<NavigatorState>(debugLabel: 'routerKey');
/// This simple provider caches our GoRouter.
final routerProvider = Provider.autoDispose<GoRouter>((ref) {
@riverpod
GoRouter router(RouterRef ref) {
final notifier = ref.watch(routerNotifierProvider.notifier);
return GoRouter(
navigatorKey: _key,
refreshListenable: notifier,
@ -19,4 +21,4 @@ final routerProvider = Provider.autoDispose<GoRouter>((ref) {
routes: notifier.routes,
redirect: notifier.redirect,
);
});
}

25
lib/router/router.g.dart Normal file
View file

@ -0,0 +1,25 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'router.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$routerHash() => r'2273f69a347c52bbb53358ac9034ab0ea760ecce';
/// This simple provider caches our GoRouter.
///
/// Copied from [router].
@ProviderFor(router)
final routerProvider = AutoDisposeProvider<GoRouter>.internal(
router,
name: r'routerProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product') ? null : _$routerHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef RouterRef = AutoDisposeProviderRef<GoRouter>;
// ignore_for_file: unnecessary_raw_strings, subtype_of_sealed_class, invalid_use_of_internal_member, do_not_use_environment, prefer_const_constructors, public_member_api_docs, avoid_private_typedef_functions

View file

@ -1,23 +1,25 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../pages/pages.dart';
import '../providers/logins.dart';
import '../providers/local_accounts.dart';
class RouterNotifier extends AutoDisposeAsyncNotifier<void>
implements Listenable {
part 'router_notifier.g.dart';
@riverpod
class RouterNotifier extends _$RouterNotifier implements Listenable {
/// GoRouter listener
VoidCallback? routerListener;
/// Router state for redirect
bool hasActiveUserLogin = false;
/// Do we need to make or import an account immediately?
bool hasAnyAccount = false;
/// AsyncNotifier build
@override
Future<void> build() async {
hasActiveUserLogin = await ref.watch(
loginsProvider.selectAsync((data) => data.activeUserLogin != null),
hasAnyAccount = await ref.watch(
localAccountsProvider.selectAsync((data) => data.isNotEmpty),
);
// When this notifier's state changes, inform GoRouter
@ -33,11 +35,9 @@ class RouterNotifier extends AutoDisposeAsyncNotifier<void>
switch (state.location) {
case IndexPage.path:
return hasActiveUserLogin ? HomePage.path : LoginPage.path;
case LoginPage.path:
return hasActiveUserLogin ? HomePage.path : null;
return hasAnyAccount ? HomePage.path : NewAccountPage.path;
default:
return hasActiveUserLogin ? null : LoginPage.path;
return hasAnyAccount ? null : NewAccountPage.path;
}
}
@ -50,38 +50,6 @@ class RouterNotifier extends AutoDisposeAsyncNotifier<void>
GoRoute(
path: HomePage.path,
builder: (context, state) => const HomePage(),
// redirect: (context, state) async {
// if (state.location == HomePage.path) return null;
// // final roleListener = ProviderScope.containerOf(context).listen(
// // permissionsProvider.select((value) => value.valueOrNull),
// // (previous, next) {},
// // );
// // final userRole = roleListener.read();
// // final redirectTo = userRole?.redirectBasedOn(state.location);
// // roleListener.close();
// // return redirectTo;
// },
// routes: [
// GoRoute(
// path: AdminPage.path,
// builder: (context, state) => const AdminPage(),
// ),
// GoRoute(
// path: UserPage.path,
// builder: (context, state) => const UserPage(),
// ),
// GoRoute(
// path: GuestPage.path,
// builder: (context, state) => const GuestPage(),
// )
// ]
),
GoRoute(
path: LoginPage.path,
builder: (context, state) => const LoginPage(),
),
GoRoute(
path: NewAccountPage.path,
@ -112,27 +80,3 @@ class RouterNotifier extends AutoDisposeAsyncNotifier<void>
routerListener = null;
}
}
final routerNotifierProvider =
AutoDisposeAsyncNotifierProvider<RouterNotifier, void>(() {
return RouterNotifier();
});
/// A simple extension to determine wherever should we redirect our users
// extension RedirecttionBasedOnRole on UserRole {
// /// Redirects the users based on [this] and its current [location]
// String? redirectBasedOn(String location) {
// switch (this) {
// case UserRole.admin:
// return null;
// case UserRole.verifiedUser:
// case UserRole.unverifiedUser:
// if (location == AdminPage.path) return HomePage.path;
// return null;
// case UserRole.guest:
// case UserRole.none:
// if (location != HomePage.path) return HomePage.path;
// return null;
// }
// }
// }

View file

@ -0,0 +1,25 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'router_notifier.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$routerNotifierHash() => r'00de1dd715945e96b49507ea55d7b97a78366adc';
/// See also [RouterNotifier].
@ProviderFor(RouterNotifier)
final routerNotifierProvider =
AutoDisposeAsyncNotifierProvider<RouterNotifier, void>.internal(
RouterNotifier.new,
name: r'routerNotifierProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
? null
: _$routerNotifierHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef _$RouterNotifier = AutoDisposeAsyncNotifier<void>;
// ignore_for_file: unnecessary_raw_strings, subtype_of_sealed_class, invalid_use_of_internal_member, do_not_use_environment, prefer_const_constructors, public_member_api_docs, avoid_private_typedef_functions