import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:hive_flutter/hive_flutter.dart'; import '../data/local/hive_boxes.dart'; import '../data/local/models/app_settings.dart'; import '../core/widgets/bottom_nav_bar.dart'; import '../features/dashboard/presentation/dashboard_screen.dart'; import '../features/imsakiyah/presentation/imsakiyah_screen.dart'; import '../features/checklist/presentation/checklist_screen.dart'; import '../features/laporan/presentation/laporan_screen.dart'; import '../features/tools/presentation/tools_screen.dart'; import '../features/dzikir/presentation/dzikir_screen.dart'; import '../features/qibla/presentation/qibla_screen.dart'; import '../features/quran/presentation/quran_screen.dart'; import '../features/quran/presentation/quran_reading_screen.dart'; import '../features/quran/presentation/quran_murattal_screen.dart'; import '../features/quran/presentation/quran_bookmarks_screen.dart'; import '../features/settings/presentation/settings_screen.dart'; /// Navigation key for the shell navigator (bottom-nav screens). final _rootNavigatorKey = GlobalKey(); final _shellNavigatorKey = GlobalKey(); /// GoRouter configuration per PRD §5.2. final GoRouter appRouter = GoRouter( navigatorKey: _rootNavigatorKey, initialLocation: '/', routes: [ // ── Shell route (bottom nav persists) ── ShellRoute( navigatorKey: _shellNavigatorKey, builder: (context, state, child) => _ScaffoldWithNav(child: child), routes: [ GoRoute( path: '/', pageBuilder: (context, state) => const NoTransitionPage( child: DashboardScreen(), ), routes: [ GoRoute( path: 'qibla', parentNavigatorKey: _rootNavigatorKey, builder: (context, state) => const QiblaScreen(), ), ], ), GoRoute( path: '/imsakiyah', pageBuilder: (context, state) => const NoTransitionPage( child: ImsakiyahScreen(), ), ), GoRoute( path: '/checklist', pageBuilder: (context, state) => const NoTransitionPage( child: ChecklistScreen(), ), ), GoRoute( path: '/laporan', pageBuilder: (context, state) => const NoTransitionPage( child: LaporanScreen(), ), ), GoRoute( path: '/tools', pageBuilder: (context, state) => const NoTransitionPage( child: ToolsScreen(), ), routes: [ GoRoute( path: 'dzikir', parentNavigatorKey: _rootNavigatorKey, builder: (context, state) => const DzikirScreen(), ), GoRoute( path: 'quran', parentNavigatorKey: _rootNavigatorKey, builder: (context, state) => const QuranScreen(), routes: [ GoRoute( path: 'bookmarks', parentNavigatorKey: _rootNavigatorKey, builder: (context, state) => const QuranBookmarksScreen(), ), GoRoute( path: ':surahId', parentNavigatorKey: _rootNavigatorKey, builder: (context, state) { final surahId = state.pathParameters['surahId']!; final startVerse = int.tryParse(state.uri.queryParameters['startVerse'] ?? ''); return QuranReadingScreen(surahId: surahId, initialVerse: startVerse); }, routes: [ GoRoute( path: 'murattal', parentNavigatorKey: _rootNavigatorKey, builder: (context, state) { final surahId = state.pathParameters['surahId']!; final qariId = state.uri.queryParameters['qariId']; final autoplay = state.uri.queryParameters['autoplay'] == 'true'; return QuranMurattalScreen( surahId: surahId, initialQariId: qariId, autoPlay: autoplay, ); }, ), ], ), ], ), GoRoute( path: 'qibla', parentNavigatorKey: _rootNavigatorKey, builder: (context, state) => const QiblaScreen(), ), ], ), // Simple Mode Tab: Zikir GoRoute( path: '/dzikir', builder: (context, state) => const DzikirScreen(isSimpleModeTab: true), ), // Simple Mode Tab: Tilawah GoRoute( path: '/quran', builder: (context, state) => const QuranScreen(isSimpleModeTab: true), routes: [ GoRoute( path: 'bookmarks', builder: (context, state) => const QuranBookmarksScreen(), ), GoRoute( path: ':surahId', builder: (context, state) { final surahId = state.pathParameters['surahId']!; final startVerse = int.tryParse(state.uri.queryParameters['startVerse'] ?? ''); return QuranReadingScreen(surahId: surahId, initialVerse: startVerse, isSimpleModeTab: true); }, routes: [ GoRoute( path: 'murattal', parentNavigatorKey: _rootNavigatorKey, builder: (context, state) { final surahId = state.pathParameters['surahId']!; final qariId = state.uri.queryParameters['qariId']; final autoplay = state.uri.queryParameters['autoplay'] == 'true'; return QuranMurattalScreen( surahId: surahId, initialQariId: qariId, autoPlay: autoplay, isSimpleModeTab: true, ); }, ), ], ), ], ), ], ), // ── Settings (pushed, no bottom nav) ── GoRoute( path: '/settings', parentNavigatorKey: _rootNavigatorKey, builder: (context, state) => const SettingsScreen(), ), ], ); /// Scaffold wrapper that provides the persistent bottom nav bar. class _ScaffoldWithNav extends StatelessWidget { const _ScaffoldWithNav({required this.child}); final Widget child; /// Maps route locations to bottom nav indices. int _currentIndex(BuildContext context) { final location = GoRouterState.of(context).uri.toString(); final box = Hive.box(HiveBoxes.settings); final isSimpleMode = box.get('default')?.simpleMode ?? false; if (isSimpleMode) { if (location.startsWith('/imsakiyah')) return 1; if (location.startsWith('/quran') && !location.contains('/murattal')) return 2; if (location.contains('/murattal')) return 3; if (location.startsWith('/dzikir')) return 4; return 0; } else { if (location.startsWith('/imsakiyah')) return 1; if (location.startsWith('/checklist')) return 2; if (location.startsWith('/laporan')) return 3; if (location.startsWith('/tools')) return 4; return 0; } } void _onTap(BuildContext context, int index) { final box = Hive.box(HiveBoxes.settings); final isSimpleMode = box.get('default')?.simpleMode ?? false; if (isSimpleMode) { switch (index) { case 0: context.go('/'); break; case 1: context.go('/imsakiyah'); break; case 2: context.go('/quran'); break; case 3: context.push('/quran/1/murattal'); break; case 4: context.go('/dzikir'); break; } } else { switch (index) { case 0: context.go('/'); break; case 1: context.go('/imsakiyah'); break; case 2: context.go('/checklist'); break; case 3: context.go('/laporan'); break; case 4: context.go('/tools'); break; } } } @override Widget build(BuildContext context) { return ValueListenableBuilder>( valueListenable: Hive.box(HiveBoxes.settings).listenable(), builder: (context, box, _) { return Scaffold( body: child, bottomNavigationBar: AppBottomNavBar( currentIndex: _currentIndex(context), onTap: (i) => _onTap(context, i), ), ); }, ); } }