Skip to content

Commit

Permalink
refactor: migrate router provider
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckUVeryX committed Nov 29, 2023
1 parent b52ca26 commit 6fadbd1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 61 deletions.
46 changes: 39 additions & 7 deletions lib/app/router/router.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
import 'package:evlve/app/router/router_listenable.dart';
import 'package:evlve/app/router/routes.dart';
import 'package:evlve/modules/auth/auth.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'router.g.dart';

@riverpod
Raw<GoRouter> router(RouterRef ref) {
final notifier = ref.watch(routerListenableProvider.notifier);
GoRouter router(RouterRef ref) {
final authState = ValueNotifier<AsyncValue<AuthState>>(const AsyncLoading());
ref
..onDispose(authState.dispose)
..listen(authControllerProvider, (_, value) {
authState.value = value;
});

final router = GoRouter(
routes: $appRoutes,
navigatorKey: rootKey,
debugLogDiagnostics: true,
redirect: notifier.redirect,
refreshListenable: notifier,
refreshListenable: authState,
initialLocation: SplashRoute.path,
debugLogDiagnostics: true,
routes: $appRoutes,
redirect: (context, state) {
if (authState.value.unwrapPrevious().hasError) {
return const LoginRoute().location;
}
if (authState.value.isLoading || !authState.value.hasValue) {
return const SplashRoute().location;
}
final auth = authState.value.requireValue;
final isSplash = state.uri.path == const SplashRoute().location;
if (isSplash) {
return auth.map(
loggedIn: (_) {
final currentRoute = state.uri.toString();
final isLoggingIn =
currentRoute.startsWith(const LoginRoute().location);
return isLoggingIn ? const ScheduleRoute().location : null;
},
loggedOut: (_) => const LoginRoute().location,
requireOtp: (_) => const OtpRoute().location,
);
}

return null;
},
);

ref.onDispose(router.dispose);

return router;
}
54 changes: 0 additions & 54 deletions lib/app/router/router_listenable.dart

This file was deleted.

0 comments on commit 6fadbd1

Please sign in to comment.