initial commit
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import '../../features/backups/backups_screen.dart';
|
||||
import '../../features/dashboard/dashboard_screen.dart';
|
||||
import '../../features/fleet/fleet_screen.dart';
|
||||
import '../../features/services/services_screen.dart';
|
||||
import '../../features/settings/settings_screen.dart';
|
||||
import '../../features/updates/updates_screen.dart';
|
||||
import '../connections/connection_providers.dart';
|
||||
import '../widgets/nav_rail_shell.dart';
|
||||
import 'route_paths.dart';
|
||||
|
||||
part 'app_router.g.dart';
|
||||
|
||||
/// Bridges Riverpod's [activeConnectionProvider] to go_router's
|
||||
/// [Listenable]-based `refreshListenable` — so removing/adding the active
|
||||
/// connection re-evaluates the top-level redirect immediately, not just on
|
||||
/// the next navigation.
|
||||
class _ConnectionRefreshListenable extends ChangeNotifier {
|
||||
_ConnectionRefreshListenable(Ref ref) {
|
||||
ref.listen(activeConnectionProvider, (previous, next) => notifyListeners());
|
||||
}
|
||||
}
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
GoRouter appRouter(Ref ref) {
|
||||
return GoRouter(
|
||||
initialLocation: RoutePaths.dashboard,
|
||||
refreshListenable: _ConnectionRefreshListenable(ref),
|
||||
redirect: (context, state) {
|
||||
final hasConnection = ref.read(activeConnectionProvider) != null;
|
||||
final onSettings = state.matchedLocation == RoutePaths.settings;
|
||||
if (!hasConnection && !onSettings) return RoutePaths.settings;
|
||||
return null;
|
||||
},
|
||||
routes: [
|
||||
ShellRoute(
|
||||
builder: (context, state, child) {
|
||||
return NavRailShell(currentPath: state.uri.path, child: child);
|
||||
},
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: RoutePaths.dashboard,
|
||||
builder: (context, state) => const DashboardScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: RoutePaths.services,
|
||||
builder: (context, state) => ServicesScreen(serviceId: state.uri.queryParameters['id']),
|
||||
),
|
||||
GoRoute(
|
||||
path: RoutePaths.backups,
|
||||
builder: (context, state) => const BackupsScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: RoutePaths.fleet,
|
||||
builder: (context, state) => const FleetScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: RoutePaths.updates,
|
||||
builder: (context, state) => const UpdatesScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: RoutePaths.settings,
|
||||
builder: (context, state) => const SettingsScreen(),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user