60 lines
1.9 KiB
Dart
60 lines
1.9 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:pyramid/features/auth/login_notifier.dart';
|
|
import 'package:pyramid/features/auth/login_page.dart';
|
|
import 'package:pyramid/features/auth/server_page.dart';
|
|
import 'package:pyramid/features/chat/chat_page.dart';
|
|
import 'package:pyramid/features/rooms/rooms_page.dart';
|
|
import 'package:pyramid/features/settings/settings_page.dart';
|
|
import 'package:pyramid/layout/shell_page.dart';
|
|
|
|
final routerProvider = Provider<GoRouter>((ref) {
|
|
final isLoggedIn = ref.watch(isLoggedInProvider).valueOrNull ?? false;
|
|
final hasServer = ref.watch(homeserverProvider) != null;
|
|
|
|
return GoRouter(
|
|
initialLocation: '/rooms',
|
|
redirect: (context, state) {
|
|
final loc = state.matchedLocation;
|
|
final onAuth = loc == '/login' || loc == '/server';
|
|
|
|
if (!isLoggedIn) {
|
|
if (!hasServer && loc != '/server') return '/server';
|
|
if (hasServer && loc != '/login' && !onAuth) return '/login';
|
|
return null;
|
|
}
|
|
|
|
if (isLoggedIn && onAuth) return '/rooms';
|
|
return null;
|
|
},
|
|
routes: [
|
|
GoRoute(
|
|
path: '/server',
|
|
builder: (context, state) => const ServerPage(),
|
|
),
|
|
GoRoute(
|
|
path: '/login',
|
|
builder: (context, state) => const LoginPage(),
|
|
),
|
|
ShellRoute(
|
|
builder: (context, state, child) => ShellPage(child: child),
|
|
routes: [
|
|
GoRoute(
|
|
path: '/rooms',
|
|
builder: (context, state) => const RoomsPage(),
|
|
),
|
|
GoRoute(
|
|
path: '/chat/:roomId',
|
|
builder: (context, state) =>
|
|
ChatPage(roomId: state.pathParameters['roomId']!),
|
|
),
|
|
GoRoute(
|
|
path: '/settings',
|
|
builder: (context, state) => const SettingsPage(),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
});
|