You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
4.5 KiB
150 lines
4.5 KiB
import 'package:flutter/material.dart';
|
|
import '../home/home_screen.dart';
|
|
import '../immich/immich_screen.dart';
|
|
import '../nostr_events/nostr_events_screen.dart';
|
|
import '../session/session_screen.dart';
|
|
import '../settings/settings_screen.dart';
|
|
import '../../core/service_locator.dart';
|
|
|
|
/// Main navigation scaffold with bottom navigation bar.
|
|
class MainNavigationScaffold extends StatefulWidget {
|
|
const MainNavigationScaffold({super.key});
|
|
|
|
@override
|
|
State<MainNavigationScaffold> createState() => _MainNavigationScaffoldState();
|
|
}
|
|
|
|
class _MainNavigationScaffoldState extends State<MainNavigationScaffold> {
|
|
int _currentIndex = 0;
|
|
int _loginStateVersion = 0; // Increment when login state changes
|
|
int? _pendingProtectedRoute; // Track if user was trying to access a protected route
|
|
|
|
void _onItemTapped(int index) {
|
|
setState(() {
|
|
_currentIndex = index;
|
|
// If accessing a protected route (Immich=1, Nostr=2) while not logged in, remember it
|
|
final sessionService = ServiceLocator.instance.sessionService;
|
|
if ((index == 1 || index == 2) && !(sessionService?.isLoggedIn ?? false)) {
|
|
_pendingProtectedRoute = index;
|
|
} else {
|
|
_pendingProtectedRoute = null;
|
|
}
|
|
});
|
|
}
|
|
|
|
/// Callback to notify that login state may have changed
|
|
void _onSessionStateChanged() {
|
|
setState(() {
|
|
_loginStateVersion++; // Force rebuild when login state changes
|
|
|
|
// If user just logged in and was trying to access a protected route, navigate there
|
|
final sessionService = ServiceLocator.instance.sessionService;
|
|
if (sessionService?.isLoggedIn == true && _pendingProtectedRoute != null) {
|
|
_currentIndex = _pendingProtectedRoute!;
|
|
_pendingProtectedRoute = null;
|
|
}
|
|
});
|
|
}
|
|
|
|
Widget _buildScreen(int index) {
|
|
final locator = ServiceLocator.instance;
|
|
final sessionService = locator.sessionService;
|
|
|
|
switch (index) {
|
|
case 0:
|
|
return const HomeScreen();
|
|
case 1:
|
|
// Check auth guard for Immich
|
|
if (!(sessionService?.isLoggedIn ?? false)) {
|
|
return _buildLoginRequiredScreen();
|
|
}
|
|
return const ImmichScreen();
|
|
case 2:
|
|
// Check auth guard for Nostr Events
|
|
if (!(sessionService?.isLoggedIn ?? false)) {
|
|
return _buildLoginRequiredScreen();
|
|
}
|
|
return const NostrEventsScreen();
|
|
case 3:
|
|
return SessionScreen(
|
|
onSessionChanged: _onSessionStateChanged,
|
|
);
|
|
case 4:
|
|
return const SettingsScreen();
|
|
default:
|
|
return const SizedBox();
|
|
}
|
|
}
|
|
|
|
Widget _buildLoginRequiredScreen() {
|
|
return Scaffold(
|
|
body: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Icon(
|
|
Icons.lock_outline,
|
|
size: 64,
|
|
color: Colors.grey,
|
|
),
|
|
const SizedBox(height: 16),
|
|
const Text(
|
|
'Please login to access this feature',
|
|
style: TextStyle(fontSize: 16),
|
|
),
|
|
const SizedBox(height: 24),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
_currentIndex = 3; // Navigate to Session tab
|
|
});
|
|
},
|
|
child: const Text('Go to Login'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// Use login state version to force IndexedStack rebuild when login changes
|
|
return Scaffold(
|
|
body: IndexedStack(
|
|
key: ValueKey('nav_$_currentIndex\_v$_loginStateVersion'),
|
|
index: _currentIndex,
|
|
children: List.generate(5, (index) => _buildScreen(index)),
|
|
),
|
|
bottomNavigationBar: BottomNavigationBar(
|
|
type: BottomNavigationBarType.fixed,
|
|
currentIndex: _currentIndex,
|
|
onTap: _onItemTapped,
|
|
items: const [
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.home),
|
|
label: 'Home',
|
|
),
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.photo_library),
|
|
label: 'Immich',
|
|
),
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.cloud),
|
|
label: 'Nostr',
|
|
),
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.person),
|
|
label: 'Session',
|
|
),
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.settings),
|
|
label: 'Settings',
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|