refactor and move stuff

This commit is contained in:
John Smith 2023-01-10 21:04:18 -05:00
parent 8c22bf8cc0
commit b54868cc55
28 changed files with 500 additions and 94 deletions

View file

@ -1,12 +1,9 @@
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 '../entities/user_role.dart';
import '../pages/pages.dart';
import '../state/auth.dart';
import '../state/permissions.dart';
/// This notifier is meant to implement the [Listenable] our [GoRouter] needs.
///
@ -45,9 +42,9 @@ class RouterNotifier extends AutoDisposeAsyncNotifier<void>
String? redirect(BuildContext context, GoRouterState state) {
if (this.state.isLoading || this.state.hasError) return null;
final isSplash = state.location == IndexPage.path;
final isIndex = state.location == IndexPage.path;
if (isSplash) {
if (isIndex) {
return isAuth ? HomePage.path : LoginPage.path;
}
@ -57,47 +54,52 @@ class RouterNotifier extends AutoDisposeAsyncNotifier<void>
return isAuth ? null : IndexPage.path;
}
/// Our application routes. Obtained through code generation
/// Our application routes
List<GoRoute> get routes => [
GoRoute(
path: IndexPage.path,
builder: (context, state) => const IndexPage(),
),
GoRoute(
path: HomePage.path,
builder: (context, state) => const HomePage(),
redirect: (context, state) async {
if (state.location == HomePage.path) return null;
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 roleListener = ProviderScope.containerOf(context).listen(
// // permissionsProvider.select((value) => value.valueOrNull),
// // (previous, next) {},
// // );
final userRole = roleListener.read();
final redirectTo = userRole?.redirectBasedOn(state.location);
// // 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(),
)
]),
// // 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,
builder: (context, state) => const NewAccountPage(),
),
];
/// Adds [GoRouter]'s listener as specified by its [Listenable].
@ -127,20 +129,20 @@ final routerNotifierProvider =
});
/// 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;
}
}
}
// 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;
// }
// }
// }