feat: add admin page
This commit is contained in:
+55
-10
@@ -2,11 +2,14 @@ import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:sentry_flutter/sentry_flutter.dart';
|
||||
|
||||
import '../viewmodels/admin_pin_view_model.dart';
|
||||
import '../viewmodels/pin_lock_view_model.dart';
|
||||
import '../views/admin_screen_view.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/import_names_view.dart';
|
||||
import '../views/pin_lock_view.dart';
|
||||
import '../views/product_form_view.dart';
|
||||
import '../views/product_list_view.dart';
|
||||
@@ -16,26 +19,45 @@ import '../utils/app_update_util.dart';
|
||||
|
||||
final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();
|
||||
|
||||
GoRouter createAppRouter(PinLockViewModel pinLockViewModel) {
|
||||
GoRouter createAppRouter(
|
||||
PinLockViewModel pinLockViewModel, {
|
||||
required AdminPinViewModel adminPinViewModel,
|
||||
}) {
|
||||
return GoRouter(
|
||||
initialLocation: '/bar',
|
||||
observers: [routeObserver, SentryNavigatorObserver()],
|
||||
refreshListenable: pinLockViewModel,
|
||||
refreshListenable: Listenable.merge([pinLockViewModel, adminPinViewModel]),
|
||||
redirect: (context, state) {
|
||||
if (!pinLockViewModel.hasLoaded) return null;
|
||||
if (!pinLockViewModel.hasLoaded || !adminPinViewModel.hasLoaded) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final needsPinSetup =
|
||||
!pinLockViewModel.isPinSet && pinLockViewModel.pinRequired;
|
||||
final isLocked = pinLockViewModel.isPinSet &&
|
||||
final isLocked =
|
||||
pinLockViewModel.isPinSet &&
|
||||
!pinLockViewModel.isUnlocked &&
|
||||
pinLockViewModel.pinRequired;
|
||||
final mustBeOnLock = needsPinSetup || isLocked;
|
||||
|
||||
final goingToLock = state.matchedLocation == '/lock';
|
||||
final location = state.uri.path;
|
||||
final goingToLock = location == '/lock';
|
||||
final goingToAdminLock = location == '/admin/lock';
|
||||
final isAdminArea =
|
||||
location == '/admin' ||
|
||||
location.startsWith('/admin/') ||
|
||||
location == '/products' ||
|
||||
location.startsWith('/products/');
|
||||
|
||||
if (mustBeOnLock && !goingToLock) return '/lock';
|
||||
if (!mustBeOnLock && goingToLock) return '/bar';
|
||||
|
||||
if (isAdminArea && !goingToAdminLock && !adminPinViewModel.isUnlocked) {
|
||||
return '/admin/lock';
|
||||
}
|
||||
|
||||
if (goingToAdminLock && adminPinViewModel.isUnlocked) return '/admin';
|
||||
|
||||
return null;
|
||||
},
|
||||
routes: [
|
||||
@@ -54,6 +76,31 @@ GoRouter createAppRouter(PinLockViewModel pinLockViewModel) {
|
||||
|
||||
GoRoute(path: '/bar', builder: (context, state) => const BarScreenView()),
|
||||
|
||||
GoRoute(
|
||||
path: '/admin/lock',
|
||||
builder: (context, state) => PinEntryView(
|
||||
mode: adminPinViewModel.isPinSet
|
||||
? PinEntryMode.unlock
|
||||
: PinEntryMode.create,
|
||||
isAdmin: true,
|
||||
onVerify: adminPinViewModel.verify,
|
||||
onSet: adminPinViewModel.setPin,
|
||||
errorMessage: () => adminPinViewModel.errorMessage,
|
||||
onSuccess: () => context.go('/admin'),
|
||||
),
|
||||
),
|
||||
|
||||
GoRoute(
|
||||
path: '/admin',
|
||||
builder: (context, state) => const AdminScreenView(),
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: 'import',
|
||||
builder: (context, state) => const ImportNamesView(),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
GoRoute(
|
||||
path: '/products',
|
||||
builder: (context, state) => const ProductListView(),
|
||||
@@ -87,7 +134,8 @@ GoRouter createAppRouter(PinLockViewModel pinLockViewModel) {
|
||||
builder: (context, state) {
|
||||
final update = state.extra as UpdateInfo;
|
||||
final simulate = state.uri.queryParameters['simulate'] == 'true';
|
||||
final simulateError = state.uri.queryParameters['simulateError'] == 'true';
|
||||
final simulateError =
|
||||
state.uri.queryParameters['simulateError'] == 'true';
|
||||
return UpdateProgressView(
|
||||
update: update,
|
||||
simulate: simulate,
|
||||
@@ -96,10 +144,7 @@ GoRouter createAppRouter(PinLockViewModel pinLockViewModel) {
|
||||
},
|
||||
),
|
||||
|
||||
GoRoute(
|
||||
path: '/dev',
|
||||
builder: (context, state) => const DevMenuView(),
|
||||
),
|
||||
GoRoute(path: '/dev', builder: (context, state) => const DevMenuView()),
|
||||
GoRoute(
|
||||
path: '/error',
|
||||
builder: (context, state) => const ErrorScreenView(),
|
||||
|
||||
Reference in New Issue
Block a user