Files
companion/lib/router.dart
T
2026-05-07 18:35:58 +02:00

93 lines
2.9 KiB
Dart

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'features/connection/server_discovery_screen.dart';
import 'features/connection/login_screen.dart';
import 'features/onboarding/onboarding_screen.dart';
import 'features/shell/main_shell.dart';
import 'features/floorplan/floor_plan_screen.dart';
import 'features/sensors/sensor_list_screen.dart';
import 'features/sensors/sensor_detail_screen.dart';
import 'features/tags/tag_list_screen.dart';
import 'features/tags/tag_detail_screen.dart';
import 'features/settings/settings_screen.dart';
import 'providers.dart';
const _unauthenticated = {'/connect', '/login', '/onboarding'};
final routerProvider = Provider<GoRouter>((ref) {
return GoRouter(
initialLocation: '/connect',
redirect: (context, state) {
final hasConfig = ref.read(serverConfigProvider) != null;
final hasToken = ref.read(authTokenProvider) != null;
final loc = state.matchedLocation;
if (!hasConfig && loc != '/connect') return '/connect';
if (hasConfig && !hasToken && !_unauthenticated.contains(loc)) {
return '/login';
}
return null;
},
routes: [
GoRoute(
path: '/connect',
builder: (context, state) => const ServerDiscoveryScreen(),
),
GoRoute(
path: '/login',
builder: (context, state) => const LoginScreen(),
),
GoRoute(
path: '/onboarding',
builder: (context, state) => const OnboardingScreen(),
),
StatefulShellRoute.indexedStack(
builder: (context, state, shell) => MainShell(shell: shell),
branches: [
StatefulShellBranch(routes: [
GoRoute(
path: '/floorplan',
builder: (context, state) => const FloorPlanScreen(),
),
]),
StatefulShellBranch(routes: [
GoRoute(
path: '/sensors',
builder: (context, state) => const SensorListScreen(),
routes: [
GoRoute(
path: ':id',
builder: (context, state) => SensorDetailScreen(
sensorId: state.pathParameters['id']!,
),
),
],
),
]),
StatefulShellBranch(routes: [
GoRoute(
path: '/tags',
builder: (context, state) => const TagListScreen(),
routes: [
GoRoute(
path: ':id',
builder: (context, state) => TagDetailScreen(
tagId: state.pathParameters['id']!,
),
),
],
),
]),
StatefulShellBranch(routes: [
GoRoute(
path: '/settings',
builder: (context, state) => const SettingsScreen(),
),
]),
],
),
],
);
});