108 lines
3.5 KiB
Dart
108 lines
3.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
|
|
import '../../features/auth/presentation/screens/forgot_password_screen.dart';
|
|
import '../../features/auth/presentation/screens/login_screen.dart';
|
|
import '../../features/auth/presentation/screens/signup_screen.dart';
|
|
import '../../features/admin/presentation/screens/admin_screen.dart';
|
|
import '../../features/discover/presentation/pages/discover_page.dart';
|
|
import '../../features/feed/presentation/screens/feed_screen.dart';
|
|
import '../../features/feed/presentation/screens/upload_post_screen.dart';
|
|
import '../../features/music/presentation/pages/music_page.dart';
|
|
import '../../features/profile/presentation/pages/profile_page.dart';
|
|
import '../../features/splash/presentation/pages/splash_page.dart';
|
|
import '../../features/theme_preview/presentation/pages/riotz_theme_preview_page.dart';
|
|
import '../supabase/supabase_providers.dart';
|
|
import 'app_routes.dart';
|
|
|
|
final appRouterProvider = Provider<GoRouter>((ref) {
|
|
final client = ref.watch(supabaseProvider);
|
|
final authStateStream = client.auth.onAuthStateChange;
|
|
|
|
return GoRouter(
|
|
initialLocation: AppRoutes.splash,
|
|
refreshListenable: GoRouterRefreshStream(authStateStream),
|
|
routes: [
|
|
GoRoute(
|
|
path: AppRoutes.splash,
|
|
builder: (context, state) => const SplashPage(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.login,
|
|
builder: (context, state) => const LoginScreen(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.signup,
|
|
builder: (context, state) => const SignupScreen(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.forgotPassword,
|
|
builder: (context, state) => const ForgotPasswordScreen(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.home,
|
|
builder: (context, state) => const FeedScreen(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.uploadPost,
|
|
builder: (context, state) => const UploadPostScreen(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.profile,
|
|
builder: (context, state) => const ProfilePage(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.music,
|
|
builder: (context, state) => const MusicPage(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.discover,
|
|
builder: (context, state) => const DiscoverPage(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.admin,
|
|
builder: (context, state) => const AdminScreen(),
|
|
),
|
|
GoRoute(
|
|
path: AppRoutes.themePreview,
|
|
builder: (context, state) => const RiotzThemePreviewPage(),
|
|
),
|
|
],
|
|
redirect: (context, state) {
|
|
final isLoggedIn = client.auth.currentUser != null;
|
|
final isSplash = state.matchedLocation == AppRoutes.splash;
|
|
final isAuthRoute = state.matchedLocation == AppRoutes.login ||
|
|
state.matchedLocation == AppRoutes.signup ||
|
|
state.matchedLocation == AppRoutes.forgotPassword;
|
|
|
|
if (!isLoggedIn && !isSplash && !isAuthRoute) {
|
|
return AppRoutes.login;
|
|
}
|
|
|
|
if (isLoggedIn && (isSplash || isAuthRoute)) {
|
|
return AppRoutes.home;
|
|
}
|
|
|
|
return null;
|
|
},
|
|
);
|
|
});
|
|
|
|
class GoRouterRefreshStream extends ChangeNotifier {
|
|
GoRouterRefreshStream(Stream<AuthState> stream) {
|
|
_subscription = stream.asBroadcastStream().listen((_) => notifyListeners());
|
|
}
|
|
|
|
late final StreamSubscription<AuthState> _subscription;
|
|
|
|
@override
|
|
void dispose() {
|
|
_subscription.cancel();
|
|
super.dispose();
|
|
}
|
|
}
|