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.
basedFood-app/lib/ui/navigation/main_navigation_scaffold.dart

179 lines
5.4 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 '../../data/session/session_service.dart';
import '../../data/local/local_storage_service.dart';
import '../../data/nostr/nostr_service.dart';
import '../../data/sync/sync_engine.dart';
import '../../data/firebase/firebase_service.dart';
import '../../data/immich/immich_service.dart';
/// Main navigation scaffold with bottom navigation bar.
class MainNavigationScaffold extends StatefulWidget {
final SessionService? sessionService;
final LocalStorageService? localStorageService;
final NostrService? nostrService;
final SyncEngine? syncEngine;
final FirebaseService? firebaseService;
final ImmichService? immichService;
const MainNavigationScaffold({
super.key,
this.sessionService,
this.localStorageService,
this.nostrService,
this.syncEngine,
this.firebaseService,
this.immichService,
});
@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
if ((index == 1 || index == 2) && !(widget.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
if (widget.sessionService?.isLoggedIn == true && _pendingProtectedRoute != null) {
_currentIndex = _pendingProtectedRoute!;
_pendingProtectedRoute = null;
}
});
}
Widget _buildScreen(int index) {
switch (index) {
case 0:
return HomeScreen(
localStorageService: widget.localStorageService,
);
case 1:
// Check auth guard for Immich
if (!(widget.sessionService?.isLoggedIn ?? false)) {
return _buildLoginRequiredScreen();
}
return ImmichScreen(
localStorageService: widget.localStorageService,
immichService: widget.immichService,
);
case 2:
// Check auth guard for Nostr Events
if (!(widget.sessionService?.isLoggedIn ?? false)) {
return _buildLoginRequiredScreen();
}
return NostrEventsScreen(
nostrService: widget.nostrService,
syncEngine: widget.syncEngine,
);
case 3:
return SessionScreen(
sessionService: widget.sessionService,
firebaseService: widget.firebaseService,
onSessionChanged: _onSessionStateChanged,
);
case 4:
return SettingsScreen(
firebaseService: widget.firebaseService,
nostrService: widget.nostrService,
syncEngine: widget.syncEngine,
);
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',
),
],
),
);
}
}

Powered by TurnKey Linux.