601 lines
18 KiB
Dart
601 lines
18 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:sentry_flutter/sentry_flutter.dart';
|
|
|
|
import '../l10n/app_localizations.dart';
|
|
import '../models/app_error.dart';
|
|
import '../models/settings.dart';
|
|
import '../utils/app_update_util.dart';
|
|
import '../utils/navigation.dart';
|
|
import '../viewmodels/admin_pin_view_model.dart';
|
|
import '../viewmodels/pin_lock_view_model.dart';
|
|
import '../viewmodels/settings_view_model.dart';
|
|
import 'pin_lock_view.dart';
|
|
|
|
class SettingsScreenView extends StatefulWidget {
|
|
const SettingsScreenView({super.key});
|
|
|
|
@override
|
|
State<SettingsScreenView> createState() => _SettingsScreenViewState();
|
|
}
|
|
|
|
class _SettingsScreenViewState extends State<SettingsScreenView> {
|
|
String _appVersion = '';
|
|
Timer? _versionHoldTimer;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
context.read<SettingsViewModel>().ensureLoaded();
|
|
context.read<PinLockViewModel>().ensureLoaded();
|
|
context.read<AdminPinViewModel>().ensureLoaded();
|
|
_loadVersion();
|
|
});
|
|
}
|
|
|
|
Future<String> currentVersion() async {
|
|
final info = await PackageInfo.fromPlatform();
|
|
debugPrint('Current app version: ${info.version}');
|
|
return info.version;
|
|
}
|
|
|
|
void _startVersionHold() {
|
|
_versionHoldTimer?.cancel();
|
|
_versionHoldTimer = Timer(const Duration(seconds: 1), () {
|
|
_versionHoldTimer = null;
|
|
if (mounted) context.push('/dev');
|
|
});
|
|
}
|
|
|
|
void _leave() {
|
|
final adminPinViewModel = context.read<AdminPinViewModel>();
|
|
context.popOrGo('/bar');
|
|
adminPinViewModel.lock();
|
|
}
|
|
|
|
void _cancelVersionHold() {
|
|
_versionHoldTimer?.cancel();
|
|
_versionHoldTimer = null;
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_cancelVersionHold();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _loadVersion() async {
|
|
final version = await currentVersion();
|
|
|
|
if (!mounted) return;
|
|
|
|
setState(() {
|
|
_appVersion = version;
|
|
});
|
|
}
|
|
|
|
/// Shows the PIN keypad full screen; true once the PIN step succeeded.
|
|
Future<bool> _showPinEntry({
|
|
required PinEntryMode mode,
|
|
bool isAdmin = false,
|
|
String? title,
|
|
Future<bool> Function(String)? onVerify,
|
|
Future<bool> Function(String)? onSet,
|
|
Future<bool> Function(String, String)? onChange,
|
|
AppError? Function()? error,
|
|
}) async {
|
|
final succeeded = await Navigator.of(context).push<bool>(
|
|
MaterialPageRoute(
|
|
fullscreenDialog: true,
|
|
builder: (routeContext) => PinEntryView(
|
|
mode: mode,
|
|
isAdmin: isAdmin,
|
|
title: title,
|
|
onVerify: onVerify,
|
|
onSet: onSet,
|
|
onChange: onChange,
|
|
error: error,
|
|
onSuccess: () => Navigator.pop(routeContext, true),
|
|
onClose: () => Navigator.pop(routeContext, false),
|
|
),
|
|
),
|
|
);
|
|
|
|
return succeeded ?? false;
|
|
}
|
|
|
|
Future<T?> _pickOption<T>(
|
|
BuildContext context, {
|
|
required List<T> options,
|
|
required T selected,
|
|
required String Function(T) label,
|
|
}) {
|
|
return showModalBottomSheet<T>(
|
|
context: context,
|
|
builder: (sheetContext) => SafeArea(
|
|
child: RadioGroup<T>(
|
|
groupValue: selected,
|
|
onChanged: (value) => Navigator.pop(sheetContext, value),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
for (final option in options)
|
|
RadioListTile<T>(value: option, title: Text(label(option))),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showMessage(String message) {
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(message)));
|
|
}
|
|
|
|
Future<void> _enablePin() async {
|
|
final l10n = AppLocalizations.of(context);
|
|
final pinLockViewModel = context.read<PinLockViewModel>();
|
|
final settingsViewModel = context.read<SettingsViewModel>();
|
|
|
|
if (!await _showPinEntry(mode: PinEntryMode.create)) return;
|
|
|
|
pinLockViewModel.setPinRequired(true);
|
|
await settingsViewModel.updatePinRequired(true);
|
|
_showMessage(l10n.pinSaved);
|
|
}
|
|
|
|
Future<void> _changePin() async {
|
|
final l10n = AppLocalizations.of(context);
|
|
|
|
if (await _showPinEntry(mode: PinEntryMode.change)) {
|
|
_showMessage(l10n.pinSaved);
|
|
}
|
|
}
|
|
|
|
Future<void> _disablePin() async {
|
|
final l10n = AppLocalizations.of(context);
|
|
final pinLockViewModel = context.read<PinLockViewModel>();
|
|
final settingsViewModel = context.read<SettingsViewModel>();
|
|
|
|
final disabled = await _showPinEntry(
|
|
mode: PinEntryMode.unlock,
|
|
title: l10n.enterCurrentPin,
|
|
onVerify: pinLockViewModel.disablePin,
|
|
);
|
|
if (!disabled) return;
|
|
|
|
pinLockViewModel.setPinRequired(false);
|
|
await settingsViewModel.updatePinRequired(false);
|
|
}
|
|
|
|
Future<void> _configureAdminPin() async {
|
|
final l10n = AppLocalizations.of(context);
|
|
final adminPinViewModel = context.read<AdminPinViewModel>();
|
|
|
|
final saved = adminPinViewModel.isPinSet
|
|
? await _showPinEntry(
|
|
mode: PinEntryMode.change,
|
|
isAdmin: true,
|
|
onVerify: adminPinViewModel.verify,
|
|
onChange: (currentPin, newPin) => adminPinViewModel.changePin(
|
|
currentPin: currentPin,
|
|
newPin: newPin,
|
|
),
|
|
error: () => adminPinViewModel.error,
|
|
)
|
|
: await _showPinEntry(
|
|
mode: PinEntryMode.create,
|
|
isAdmin: true,
|
|
onSet: adminPinViewModel.setPin,
|
|
error: () => adminPinViewModel.error,
|
|
);
|
|
|
|
if (saved) _showMessage(l10n.pinSaved);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context);
|
|
final settingsViewModel = context.watch<SettingsViewModel>();
|
|
final pinLockViewModel = context.watch<PinLockViewModel>();
|
|
final adminPinViewModel = context.watch<AdminPinViewModel>();
|
|
|
|
return PopScope(
|
|
onPopInvokedWithResult: (didPop, result) {
|
|
if (didPop) context.read<AdminPinViewModel>().lock();
|
|
},
|
|
child: Scaffold(
|
|
appBar: AppBar(
|
|
leading: IconButton(
|
|
onPressed: _leave,
|
|
icon: const Icon(Icons.arrow_back),
|
|
),
|
|
title: Text(l10n.settingsTitle),
|
|
),
|
|
body: Builder(
|
|
builder: (context) {
|
|
final isLoading =
|
|
settingsViewModel.isLoading ||
|
|
pinLockViewModel.isLoading ||
|
|
adminPinViewModel.isLoading;
|
|
final hasLoaded =
|
|
settingsViewModel.hasLoaded &&
|
|
pinLockViewModel.hasLoaded &&
|
|
adminPinViewModel.hasLoaded;
|
|
|
|
if (isLoading && !hasLoaded) {
|
|
return const Center(
|
|
child: CircularProgressIndicator(strokeWidth: 2.5),
|
|
);
|
|
}
|
|
|
|
final settings = settingsViewModel.settings;
|
|
|
|
return ListView(
|
|
padding: const EdgeInsets.symmetric(vertical: 12),
|
|
children: [
|
|
_SettingsSection(
|
|
title: l10n.security,
|
|
children: [
|
|
_SettingsSwitchTile(
|
|
icon: Icons.lock_outline_rounded,
|
|
title: l10n.pinRequired,
|
|
value: settings.pinRequired,
|
|
onChanged: (value) {
|
|
if (value) {
|
|
_enablePin();
|
|
} else {
|
|
_disablePin();
|
|
}
|
|
},
|
|
),
|
|
_SettingsSwitchTile(
|
|
icon: Icons.lock_clock_rounded,
|
|
title: l10n.autoLockOnExit,
|
|
value: settings.autoLockEnabled,
|
|
onChanged: (value) {
|
|
settingsViewModel.updateAutoLockEnabled(value);
|
|
},
|
|
),
|
|
if (settings.pinRequired)
|
|
_SettingsTile(
|
|
icon: Icons.pin_rounded,
|
|
title: l10n.changePin,
|
|
onTap: _changePin,
|
|
),
|
|
_SettingsTile(
|
|
icon: Icons.admin_panel_settings_outlined,
|
|
title: l10n.adminPin,
|
|
subtitle: adminPinViewModel.isPinSet
|
|
? l10n.adminPinConfigured
|
|
: l10n.adminPinNotSet,
|
|
onTap: _configureAdminPin,
|
|
),
|
|
],
|
|
),
|
|
_SettingsSection(
|
|
title: l10n.appearance,
|
|
children: [
|
|
_SettingsTile(
|
|
icon: Icons.language_rounded,
|
|
title: l10n.language,
|
|
subtitle: switch (settings.language) {
|
|
AppLanguage.system => l10n.languageSystem,
|
|
AppLanguage.english => l10n.languageEnglish,
|
|
AppLanguage.dutch => l10n.languageDutch,
|
|
},
|
|
onTap: () async {
|
|
final selected = await _pickOption(
|
|
context,
|
|
options: AppLanguage.values,
|
|
selected: settings.language,
|
|
label: (language) => switch (language) {
|
|
AppLanguage.system => l10n.languageSystem,
|
|
AppLanguage.english => l10n.languageEnglish,
|
|
AppLanguage.dutch => l10n.languageDutch,
|
|
},
|
|
);
|
|
|
|
if (selected != null) {
|
|
await settingsViewModel.updateLanguage(selected);
|
|
}
|
|
},
|
|
),
|
|
_SettingsTile(
|
|
icon: Icons.grid_view_rounded,
|
|
title: l10n.productGridRows,
|
|
subtitle: l10n.rowsCount(settings.barGridRows),
|
|
onTap: () async {
|
|
final selected = await _pickOption(
|
|
context,
|
|
options: const [2, 3, 4, 5, 6],
|
|
selected: settings.barGridRows,
|
|
label: l10n.rowsCount,
|
|
);
|
|
|
|
if (selected != null) {
|
|
await settingsViewModel.updateBarGridRows(selected);
|
|
}
|
|
},
|
|
),
|
|
_SettingsTile(
|
|
icon: Icons.brightness_6_rounded,
|
|
title: l10n.theme,
|
|
subtitle: switch (settings.themeMode) {
|
|
AppThemeMode.system => l10n.system,
|
|
AppThemeMode.light => l10n.light,
|
|
AppThemeMode.dark => l10n.dark,
|
|
AppThemeMode.ugly => l10n.ugly,
|
|
},
|
|
onTap: () async {
|
|
final selected = await _pickOption(
|
|
context,
|
|
options: AppThemeMode.values,
|
|
selected: settings.themeMode,
|
|
label: (mode) => switch (mode) {
|
|
AppThemeMode.system => l10n.system,
|
|
AppThemeMode.light => l10n.light,
|
|
AppThemeMode.dark => l10n.dark,
|
|
AppThemeMode.ugly => l10n.ugly,
|
|
},
|
|
);
|
|
|
|
if (selected != null) {
|
|
await settingsViewModel.updateThemeMode(selected);
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
if (Platform.isAndroid)
|
|
_SettingsSection(
|
|
title: l10n.updates,
|
|
children: [
|
|
_SettingsTile(
|
|
icon: Icons.system_update_alt_rounded,
|
|
title: settingsViewModel.checkingForUpdates
|
|
? l10n.checkingForUpdates
|
|
: l10n.checkForUpdates,
|
|
onTap: settingsViewModel.checkingForUpdates
|
|
? null
|
|
: _checkForUpdates,
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 40),
|
|
GestureDetector(
|
|
onTapDown: (_) => _startVersionHold(),
|
|
onTapUp: (_) => _cancelVersionHold(),
|
|
onTapCancel: _cancelVersionHold,
|
|
child: Center(
|
|
child: Text(
|
|
l10n.version(_appVersion),
|
|
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
|
color: Theme.of(
|
|
context,
|
|
).colorScheme.onSurface.withValues(alpha: 0.5),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _checkForUpdates() async {
|
|
if (!Platform.isAndroid) return;
|
|
final l10n = AppLocalizations.of(context);
|
|
final vm = context.read<SettingsViewModel>();
|
|
|
|
try {
|
|
final update = await vm.checkForUpdates();
|
|
|
|
if (!mounted) return;
|
|
|
|
if (update == null) {
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(l10n.latestVersion)));
|
|
return;
|
|
}
|
|
|
|
_showUpdateDialog(update);
|
|
} catch (e, stack) {
|
|
unawaited(Sentry.captureException(e, stackTrace: stack));
|
|
if (!mounted) return;
|
|
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(l10n.updateCheckFailed)));
|
|
}
|
|
}
|
|
|
|
void _showUpdateDialog(UpdateInfo update) {
|
|
final l10n = AppLocalizations.of(context);
|
|
|
|
showDialog(
|
|
context: context,
|
|
barrierDismissible: !update.mandatory,
|
|
builder: (dialogContext) => AlertDialog(
|
|
title: Text(l10n.updateAvailable),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(l10n.versionAvailable(update.version)),
|
|
const SizedBox(height: 12),
|
|
Text(update.notes),
|
|
],
|
|
),
|
|
actions: [
|
|
if (!update.mandatory)
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(dialogContext),
|
|
child: Text(l10n.later),
|
|
),
|
|
FilledButton(
|
|
onPressed: () {
|
|
Navigator.pop(dialogContext);
|
|
context.push('/update-progress', extra: update);
|
|
},
|
|
child: Text(l10n.update),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _SettingsSection extends StatelessWidget {
|
|
final String title;
|
|
final List<Widget> children;
|
|
|
|
const _SettingsSection({required this.title, required this.children});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final scheme = Theme.of(context).colorScheme;
|
|
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
title.toUpperCase(),
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.w700,
|
|
letterSpacing: 0.5,
|
|
color: scheme.onSurface.withValues(alpha: 0.5),
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
color: scheme.onSurface.withValues(alpha: 0.04),
|
|
borderRadius: BorderRadius.circular(14),
|
|
border: Border.all(
|
|
color: scheme.onSurface.withValues(alpha: 0.06),
|
|
),
|
|
),
|
|
clipBehavior: Clip.antiAlias,
|
|
child: Column(children: children),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _SettingsTile extends StatelessWidget {
|
|
final IconData icon;
|
|
final String title;
|
|
final String? subtitle;
|
|
final VoidCallback? onTap;
|
|
|
|
const _SettingsTile({
|
|
required this.icon,
|
|
required this.title,
|
|
this.subtitle,
|
|
this.onTap,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final scheme = Theme.of(context).colorScheme;
|
|
|
|
return Material(
|
|
color: Colors.transparent,
|
|
child: InkWell(
|
|
onTap: onTap,
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
child: Row(
|
|
children: [
|
|
Icon(
|
|
icon,
|
|
size: 22,
|
|
color: scheme.onSurface.withValues(alpha: 0.7),
|
|
),
|
|
const SizedBox(width: 14),
|
|
Expanded(
|
|
child: Text(
|
|
title,
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.w600,
|
|
color: scheme.onSurface,
|
|
),
|
|
),
|
|
),
|
|
if (subtitle != null) ...[
|
|
Text(subtitle!, style: Theme.of(context).textTheme.bodySmall),
|
|
const SizedBox(width: 4),
|
|
],
|
|
if (onTap != null)
|
|
Icon(
|
|
Icons.chevron_right_rounded,
|
|
color: scheme.onSurface.withValues(alpha: 0.3),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _SettingsSwitchTile extends StatelessWidget {
|
|
final IconData icon;
|
|
final String title;
|
|
final bool value;
|
|
final ValueChanged<bool> onChanged;
|
|
|
|
const _SettingsSwitchTile({
|
|
required this.icon,
|
|
required this.title,
|
|
required this.value,
|
|
required this.onChanged,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final scheme = Theme.of(context).colorScheme;
|
|
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
|
|
child: Row(
|
|
children: [
|
|
Icon(icon, size: 22, color: scheme.onSurface.withValues(alpha: 0.7)),
|
|
const SizedBox(width: 14),
|
|
Expanded(
|
|
child: Text(
|
|
title,
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.w600,
|
|
color: scheme.onSurface,
|
|
),
|
|
),
|
|
),
|
|
Switch(value: value, onChanged: onChanged),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|