Files
mobile-app/lib/routing/app_router.dart

168 lines
6.1 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:real_estate_mobile/core/constants/app_colors.dart';
import 'package:real_estate_mobile/core/widgets/app_shell.dart';
import 'package:real_estate_mobile/features/auth/presentation/providers/auth_provider.dart';
import 'package:real_estate_mobile/features/auth/presentation/screens/login_screen.dart';
import 'package:real_estate_mobile/features/auth/presentation/screens/signup_screen.dart';
import 'package:real_estate_mobile/features/agents/presentation/screens/agent_detail_screen.dart';
import 'package:real_estate_mobile/features/agents/presentation/screens/agent_search_screen.dart';
import 'package:real_estate_mobile/features/faq/presentation/screens/faq_screen.dart';
import 'package:real_estate_mobile/features/home/presentation/screens/home_screen.dart';
import 'package:real_estate_mobile/features/messaging/presentation/screens/conversations_screen.dart';
import 'package:real_estate_mobile/features/messaging/presentation/screens/chat_screen.dart';
import 'package:real_estate_mobile/features/agents/presentation/screens/agent_home_screen.dart';
import 'package:real_estate_mobile/features/contact/presentation/screens/contact_screen.dart';
import 'package:real_estate_mobile/features/about/presentation/screens/about_screen.dart';
import 'package:real_estate_mobile/features/agents/presentation/screens/agent_network_screen.dart';
import 'package:real_estate_mobile/features/profile/presentation/screens/profile_settings_screen.dart';
final routerProvider = Provider<GoRouter>((ref) {
final authRefreshNotifier = _AuthRefreshNotifier();
ref.listen(authProvider, (previous, next) {
authRefreshNotifier.notify();
});
ref.onDispose(() {
authRefreshNotifier.dispose();
});
// Public routes accessible without authentication (matching web middleware)
const publicRoutes = ['/home', '/agents/search', '/agents/detail', '/faq', '/contact', '/about'];
const authRoutes = ['/login', '/signup'];
return GoRouter(
initialLocation: '/home',
refreshListenable: authRefreshNotifier,
redirect: (context, state) {
final authState = ref.read(authProvider);
final isAuthenticated = authState.status == AuthStatus.authenticated;
final isLoading = authState.status == AuthStatus.loading;
final location = state.matchedLocation;
final isAuthRoute = authRoutes.contains(location);
final isPublicRoute = publicRoutes.any(
(route) => location == route || location.startsWith('$route/'),
);
// While checking auth status, don't redirect
if (isLoading) return null;
// If authenticated and on auth page (login/signup), go to home
if (isAuthenticated && isAuthRoute) return '/home';
// Public routes are always accessible
if (isPublicRoute || isAuthRoute) return null;
// Protected routes require authentication — redirect to login
if (!isAuthenticated) return '/login';
return null;
},
routes: [
// Auth routes (no shell — full screen)
GoRoute(
path: '/login',
builder: (context, state) => const LoginScreen(),
),
GoRoute(
path: '/signup',
builder: (context, state) => const SignupScreen(),
),
// All main app routes wrapped in AppShell (shared header + bottom nav)
ShellRoute(
builder: (context, state, child) => AppShell(child: child),
routes: [
GoRoute(
path: '/home',
builder: (context, state) => const _HomeRouteWrapper(),
),
GoRoute(
path: '/agents/search',
builder: (context, state) {
final query = state.uri.queryParameters['q'];
return AgentSearchScreen(initialQuery: query);
},
),
GoRoute(
path: '/agents/detail/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
return AgentDetailScreen(agentId: id);
},
),
GoRoute(
path: '/messages',
builder: (context, state) => const ConversationsScreen(),
),
GoRoute(
path: '/messages/chat/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
return ChatScreen(conversationId: id);
},
),
GoRoute(
path: '/faq',
builder: (context, state) => const FaqScreen(),
),
GoRoute(
path: '/contact',
builder: (context, state) => const ContactScreen(),
),
GoRoute(
path: '/about',
builder: (context, state) => const AboutScreen(),
),
GoRoute(
path: '/agent/network',
builder: (context, state) => const AgentNetworkScreen(),
),
GoRoute(
path: '/profile',
builder: (context, state) => const ProfileSettingsScreen(),
),
],
),
],
);
});
/// Reactively switches between HomeScreen and AgentHomeScreen based on auth.
/// Shows a loading indicator while auth status is being determined to prevent
/// flashing the wrong screen on refresh.
class _HomeRouteWrapper extends ConsumerWidget {
const _HomeRouteWrapper();
@override
Widget build(BuildContext context, WidgetRef ref) {
final authState = ref.watch(authProvider);
// Show loader while auth is initializing or loading
if (authState.status == AuthStatus.initial ||
authState.status == AuthStatus.loading) {
return const Center(
child: CircularProgressIndicator(
color: AppColors.accentOrange,
),
);
}
if (authState.status == AuthStatus.authenticated &&
authState.user?.role == 'AGENT') {
return const AgentHomeScreen();
}
return const HomeScreen();
}
}
/// Notifier that triggers GoRouter refresh when auth state changes.
class _AuthRefreshNotifier extends ChangeNotifier {
void notify() {
notifyListeners();
}
}