94 lines
2.7 KiB
Dart
94 lines
2.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:kooltab2/views/settings_view.dart';
|
|
import 'package:sentry_flutter/sentry_flutter.dart';
|
|
|
|
import '../viewmodels/pin_lock_view_model.dart';
|
|
import '../views/bar_screen_view.dart';
|
|
import '../views/dev_menu_view.dart';
|
|
import '../views/error_screen_view.dart';
|
|
import '../views/history_screen_view.dart';
|
|
import '../views/pin_lock_view.dart';
|
|
import '../views/product_form_view.dart';
|
|
import '../views/product_list_view.dart';
|
|
|
|
final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();
|
|
|
|
GoRouter createAppRouter(PinLockViewModel pinLockViewModel) {
|
|
return GoRouter(
|
|
initialLocation: '/bar',
|
|
observers: [routeObserver, SentryNavigatorObserver()],
|
|
refreshListenable: pinLockViewModel,
|
|
redirect: (context, state) {
|
|
if (!pinLockViewModel.hasLoaded) return null;
|
|
|
|
final needsPinSetup =
|
|
!pinLockViewModel.isPinSet && pinLockViewModel.pinRequired;
|
|
final isLocked = pinLockViewModel.isPinSet &&
|
|
!pinLockViewModel.isUnlocked &&
|
|
pinLockViewModel.pinRequired;
|
|
final mustBeOnLock = needsPinSetup || isLocked;
|
|
|
|
final goingToLock = state.matchedLocation == '/lock';
|
|
|
|
if (mustBeOnLock && !goingToLock) return '/lock';
|
|
if (!mustBeOnLock && goingToLock) return '/bar';
|
|
|
|
return null;
|
|
},
|
|
routes: [
|
|
GoRoute(path: '/', redirect: (context, state) => '/bar'),
|
|
GoRoute(
|
|
path: '/lock',
|
|
builder: (context, state) => PinEntryView(
|
|
mode: pinLockViewModel.isPinSet
|
|
? PinEntryMode.unlock
|
|
: PinEntryMode.create,
|
|
onSuccess: () {
|
|
context.go("/bar");
|
|
},
|
|
),
|
|
),
|
|
|
|
GoRoute(path: '/bar', builder: (context, state) => const BarScreenView()),
|
|
|
|
GoRoute(
|
|
path: '/products',
|
|
builder: (context, state) => const ProductListView(),
|
|
routes: [
|
|
GoRoute(
|
|
path: 'new',
|
|
builder: (context, state) => const ProductFormView(),
|
|
),
|
|
GoRoute(
|
|
path: ':id/edit',
|
|
builder: (context, state) {
|
|
final productId = state.pathParameters['id']!;
|
|
|
|
return ProductFormView(productId: productId);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
GoRoute(
|
|
path: '/history',
|
|
builder: (context, state) => const HistoryScreenView(),
|
|
),
|
|
|
|
GoRoute(
|
|
path: '/settings',
|
|
builder: (context, state) => const SettingsScreenView(),
|
|
),
|
|
|
|
GoRoute(
|
|
path: '/dev',
|
|
builder: (context, state) => const DevMenuView(),
|
|
),
|
|
GoRoute(
|
|
path: '/error',
|
|
builder: (context, state) => const ErrorScreenView(),
|
|
),
|
|
],
|
|
);
|
|
}
|