import 'package:flutter/material.dart'; import '../client/plex_client.dart'; import '../models/plex_user_profile.dart'; import '../utils/app_logger.dart'; import '../main.dart'; import '../mixins/refreshable.dart'; import 'discover_screen.dart'; import 'libraries_screen.dart'; import 'search_screen.dart'; class MainScreen extends StatefulWidget { final PlexClient client; final PlexUserProfile? userProfile; const MainScreen({super.key, required this.client, this.userProfile}); @override State createState() => _MainScreenState(); } class _MainScreenState extends State with RouteAware { int _currentIndex = 0; late final List _screens; final GlobalKey> _discoverKey = GlobalKey(); @override void initState() { super.initState(); _screens = [ DiscoverScreen( key: _discoverKey, client: widget.client, userProfile: widget.userProfile, onBecameVisible: _onDiscoverBecameVisible, ), LibrariesScreen(client: widget.client, userProfile: widget.userProfile), SearchScreen(client: widget.client, userProfile: widget.userProfile), ]; } @override void didChangeDependencies() { super.didChangeDependencies(); routeObserver.subscribe(this, ModalRoute.of(context) as PageRoute); } @override void dispose() { routeObserver.unsubscribe(this); super.dispose(); } @override void didPush() { // Called when this route has been pushed (initial navigation) if (_currentIndex == 0) { _onDiscoverBecameVisible(); } } @override void didPopNext() { // Called when returning to this route from a child route (e.g., from video player) if (_currentIndex == 0) { _onDiscoverBecameVisible(); } } void _onDiscoverBecameVisible() { appLogger.d('Navigated to home'); // Refresh content when returning to discover page final discoverState = _discoverKey.currentState; if (discoverState != null && discoverState is Refreshable) { (discoverState as Refreshable).refresh(); } } @override Widget build(BuildContext context) { return Scaffold( body: IndexedStack(index: _currentIndex, children: _screens), bottomNavigationBar: NavigationBar( selectedIndex: _currentIndex, onDestinationSelected: (index) { setState(() { _currentIndex = index; }); // Notify discover screen when it becomes visible via tab switch if (index == 0) { _onDiscoverBecameVisible(); } }, destinations: const [ NavigationDestination( icon: Icon(Icons.home_outlined), selectedIcon: Icon(Icons.home), label: 'Home', ), NavigationDestination( icon: Icon(Icons.video_library_outlined), selectedIcon: Icon(Icons.video_library), label: 'Libraries', ), NavigationDestination( icon: Icon(Icons.search), selectedIcon: Icon(Icons.search), label: 'Search', ), ], ), ); } }