import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:provider/provider.dart'; import '../utils/app_update_util.dart'; import '../utils/navigation.dart'; import '../viewmodels/dev_menu_view_model.dart'; import '../l10n/app_localizations.dart'; import '../l10n/app_localizations_helpers.dart'; class DevMenuView extends StatefulWidget { const DevMenuView({super.key}); @override State createState() => _DevMenuViewState(); } class _DevMenuViewState extends State { DevMenuViewModel? _vm; @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { if (!mounted) return; _vm = context.read(); _vm!.addListener(_onVmChange); }); } void _onVmChange() { if (!mounted) return; final vm = _vm; if (vm == null) return; final message = vm.lastAction; if (message == null) return; final l10n = AppLocalizations.of(context); ScaffoldMessenger.of( context, ).showSnackBar(SnackBar(content: Text(l10n.devAction(message)))); } @override void dispose() { _vm?.removeListener(_onVmChange); super.dispose(); } @override Widget build(BuildContext context) { final vm = context.watch(); final l10n = AppLocalizations.of(context); return Scaffold( appBar: AppBar( leading: IconButton( onPressed: () => context.popOrGo('/bar'), icon: const Icon(Icons.arrow_back), ), title: Text(l10n.devMenu), centerTitle: true, ), body: ListView( padding: const EdgeInsets.symmetric(vertical: 12), children: [ const Text("Hallo Matteo", style: TextStyle(color: Colors.pinkAccent),), _Section( title: l10n.dataManagement, children: [ _Tile( icon: Icons.delete_sweep_rounded, title: l10n.clearOpenTabs, onTap: vm.isLoading ? null : () => vm.clearOpenTabs(), ), _Tile( icon: Icons.history_rounded, title: l10n.clearClosedHistory, onTap: vm.isLoading ? null : () => vm.clearClosedTabHistory(), ), _Tile( icon: Icons.add_box_rounded, title: l10n.seedDefaultProducts, subtitle: l10n.onlySeedsWhenEmpty, onTap: vm.isLoading ? null : () => vm.seedDefaultProducts(), ), _Tile( icon: Icons.restart_alt_rounded, title: l10n.clearAndReseedProducts, subtitle: l10n.wipesAndReseeds, onTap: vm.isLoading ? null : () => vm.clearAndReseedProducts(), ), _Tile( icon: Icons.delete_forever_rounded, title: l10n.clearAllProducts, onTap: vm.isLoading ? null : () => vm.clearAllProducts(), ), _Tile( icon: Icons.image_rounded, title: l10n.clearProductImages, subtitle: l10n.deletesProductImages, onTap: vm.isLoading ? null : () => vm.clearProductImages(), ), ], ), _Section( title: l10n.debugging, children: [ _Tile( icon: Icons.lock_reset_rounded, title: l10n.resetPin, subtitle: l10n.removePinLock, onTap: vm.isLoading ? null : () => vm.resetPin(), ), _Tile( icon: Icons.bug_report_rounded, title: l10n.toggleDebugOverlay, subtitle: l10n.showFpsMemoryWidgets, onTap: () => _showDebugOverlayInfo(), ), _Tile( icon: Icons.error_outline_rounded, title: l10n.errorScreenMenu, subtitle: l10n.viewErrorScreen, onTap: () => context.push('/error'), ), ], ), _Section( title: l10n.featureToggles, children: [ _Tile( icon: Icons.update_rounded, title: l10n.simulateUpdateDownload, subtitle: l10n.openDownloadProgress, onTap: () => _showSimulateUpdateDialog(), ), ], ), _Section( title: l10n.testingHelpers, children: [ _Tile( icon: Icons.receipt_long_rounded, title: l10n.createTestTab, subtitle: l10n.addRandomItems, onTap: vm.isLoading ? null : () => vm.createTestTab(), ), _Tile( icon: Icons.grid_view_rounded, title: l10n.addTestProducts, subtitle: l10n.stressTestGrid, onTap: vm.isLoading ? null : () => vm.addTestProducts(), ), _Tile( icon: Icons.warning_rounded, title: l10n.simulateLowStock, subtitle: l10n.setProductsBelowThreshold, onTap: vm.isLoading ? null : () => vm.simulateLowStock(), ), _Tile( icon: Icons.history_rounded, title: l10n.generateMockOrders, subtitle: l10n.randomCustomersItemsAmounts, onTap: vm.isLoading ? null : () => vm.generateMockOrders(count: 100), ), ], ), _Section( title: l10n.performance, children: [ _Tile( icon: Icons.image_rounded, title: l10n.clearImageCache, onTap: vm.isLoading ? null : () => vm.clearImageCache(), ), _Tile( icon: Icons.refresh_rounded, title: l10n.reloadProductImages, onTap: vm.isLoading ? null : () => vm.reloadProductImages(), ), ], ), if (vm.isLoading) const Padding( padding: EdgeInsets.all(20), child: Center(child: CircularProgressIndicator()), ), ], ), ); } void _showDebugOverlayInfo() { final l10n = AppLocalizations.of(context); showDialog( context: context, builder: (context) => AlertDialog( title: Text(l10n.debugOverlay), content: Text(l10n.debugOverlayDescription), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: Text(l10n.ok), ), ], ), ); } void _showSimulateUpdateDialog() { final l10n = AppLocalizations.of(context); final fakeUpdate = UpdateInfo( update: true, version: '99.0.0', notes: l10n.simulatedUpdateNotes, mandatory: false, sha256: 'abc123', download: 'https://example.com/fake-update.apk', ); showDialog( context: context, builder: (dialogContext) => AlertDialog( title: Text(l10n.simulateUpdate), content: Column( mainAxisSize: MainAxisSize.min, children: [ Text(l10n.chooseSimulationMode), const SizedBox(height: 16), _SimOption( icon: Icons.download_rounded, label: l10n.successfulDownload, description: l10n.progressThenInstall, onTap: () { Navigator.pop(dialogContext); context.push( '/update-progress?simulate=true', extra: fakeUpdate, ); }, ), const SizedBox(height: 8), _SimOption( icon: Icons.error_outline_rounded, label: l10n.downloadError, description: l10n.failsWithNetworkTimeout, onTap: () { Navigator.pop(dialogContext); context.push( '/update-progress?simulate=true&simulateError=true', extra: fakeUpdate, ); }, ), const SizedBox(height: 8), _SimOption( icon: Icons.wifi_off_rounded, label: l10n.realDownloadWillFail, description: l10n.attemptsFakeUrl, onTap: () { Navigator.pop(dialogContext); context.push('/update-progress', extra: fakeUpdate); }, ), ], ), actions: [ TextButton( onPressed: () => Navigator.pop(dialogContext), child: Text(l10n.cancel), ), ], ), ); } } class _SimOption extends StatelessWidget { final IconData icon; final String label; final String description; final VoidCallback onTap; const _SimOption({ required this.icon, required this.label, required this.description, required this.onTap, }); @override Widget build(BuildContext context) { return Material( color: Theme.of(context).colorScheme.surfaceContainerHighest, borderRadius: BorderRadius.circular(12), child: InkWell( borderRadius: BorderRadius.circular(12), onTap: onTap, child: Padding( padding: const EdgeInsets.all(12), child: Row( children: [ Icon(icon, size: 28), const SizedBox(width: 12), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(label, style: Theme.of(context).textTheme.titleSmall), Text( description, style: Theme.of(context).textTheme.bodySmall?.copyWith( color: Theme.of( context, ).colorScheme.onSurface.withValues(alpha: 0.6), ), ), ], ), ), const Icon(Icons.chevron_right_rounded), ], ), ), ), ); } } class _Section extends StatelessWidget { final String title; final List children; const _Section({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 _Tile extends StatelessWidget { final IconData icon; final String title; final String? subtitle; final VoidCallback? onTap; const _Tile({ 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: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( title, style: TextStyle( fontWeight: FontWeight.w600, color: scheme.onSurface, ), ), if (subtitle != null) Text( subtitle!, style: Theme.of(context).textTheme.bodySmall?.copyWith( color: scheme.onSurface.withValues(alpha: 0.5), ), ), ], ), ), if (onTap != null) Icon( Icons.chevron_right_rounded, color: scheme.onSurface.withValues(alpha: 0.3), ), ], ), ), ), ); } }