diff --git a/lib/core/layout/app_shell.dart b/lib/core/layout/app_shell.dart index 1175940..c0333ce 100644 --- a/lib/core/layout/app_shell.dart +++ b/lib/core/layout/app_shell.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flux/core/routes/routes.dart'; import 'package:flux/core/utils/extensions.dart'; +import 'package:flux/core/widgets/app_version_label.dart'; import 'package:go_router/go_router.dart'; // ========================================== @@ -197,6 +198,12 @@ class _AppMenuState extends State { ), ), + // --- VERSIONE APP CON HYPERLINK --- + Padding( + padding: const EdgeInsets.symmetric(vertical: 4.0), + child: AppVersionLabel(isCollapsed: effectivelyCollapsed), + ), + // --- PULSANTE TOGGLE (Solo Desktop) --- if (!widget.isDrawer) Padding( diff --git a/lib/core/utils/update_prompt_helper.dart b/lib/core/utils/update_prompt_helper.dart new file mode 100644 index 0000000..c0d2f5c --- /dev/null +++ b/lib/core/utils/update_prompt_helper.dart @@ -0,0 +1,282 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:flux/core/utils/auto_update_service.dart'; +import 'package:flux/core/utils/version_check_service.dart'; +import 'package:universal_html/html.dart' as html; +import 'package:url_launcher/url_launcher.dart'; + +class UpdatePromptHelper { + /// Avvia una verifica manuale degli aggiornamenti con feedback visivo immediato + static Future showManualUpdateCheck(BuildContext context) async { + // Feedback immediato tramite SnackBar + final messenger = ScaffoldMessenger.of(context); + messenger.hideCurrentSnackBar(); + messenger.showSnackBar( + const SnackBar( + content: Row( + children: [ + SizedBox( + width: 16, + height: 16, + child: CircularProgressIndicator( + strokeWidth: 2, + color: Colors.white, + ), + ), + SizedBox(width: 12), + Text("Verifica aggiornamenti in corso..."), + ], + ), + duration: Duration(seconds: 2), + ), + ); + + try { + final currentVersion = await VersionCheckService.getShortVersionString(); + final updateInfo = await VersionCheckService().checkUpdate(); + + if (!context.mounted) return; + messenger.hideCurrentSnackBar(); + + if (updateInfo != null && updateInfo.hasUpdate) { + // Nuova versione disponibile: mostra il dialog informativo e di azione + showDialog( + context: context, + barrierDismissible: !updateInfo.isMandatory, + builder: (dialogContext) => _ManualUpdateDialog( + updateInfo: updateInfo, + currentVersion: currentVersion, + ), + ); + } else { + // Nessun aggiornamento: l'app è già all'ultima versione + messenger.showSnackBar( + SnackBar( + backgroundColor: Colors.green.shade700, + content: Row( + children: [ + const Icon(Icons.check_circle_outline, color: Colors.white), + const SizedBox(width: 12), + Expanded( + child: Text( + "Flux è aggiornato all'ultima versione disponibile ($currentVersion).", + style: const TextStyle(color: Colors.white), + ), + ), + ], + ), + duration: const Duration(seconds: 4), + ), + ); + } + } catch (e) { + if (!context.mounted) return; + messenger.hideCurrentSnackBar(); + messenger.showSnackBar( + SnackBar( + backgroundColor: Colors.red.shade700, + content: Row( + children: [ + const Icon(Icons.error_outline, color: Colors.white), + const SizedBox(width: 12), + Expanded( + child: Text( + "Impossibile verificare gli aggiornamenti: $e", + style: const TextStyle(color: Colors.white), + ), + ), + ], + ), + duration: const Duration(seconds: 4), + ), + ); + } + } +} + +class _ManualUpdateDialog extends StatefulWidget { + final UpdateInfo updateInfo; + final String currentVersion; + + const _ManualUpdateDialog({ + required this.updateInfo, + required this.currentVersion, + }); + + @override + State<_ManualUpdateDialog> createState() => _ManualUpdateDialogState(); +} + +class _ManualUpdateDialogState extends State<_ManualUpdateDialog> { + bool _isUpdating = false; + double _downloadProgress = 0.0; + String _statusMessage = ""; + String? _errorMessage; + + Future _startUpdate() async { + setState(() { + _isUpdating = true; + _errorMessage = null; + _downloadProgress = 0.0; + _statusMessage = "Avvio aggiornamento..."; + }); + + try { + await AutoUpdateService.downloadAndApplyUpdate( + downloadUrl: widget.updateInfo.downloadUrl, + onProgress: (progress, status) { + if (mounted) { + setState(() { + _downloadProgress = progress; + _statusMessage = status; + }); + } + }, + ); + } catch (e) { + if (mounted) { + setState(() { + _isUpdating = false; + _errorMessage = "Errore durante l'aggiornamento: $e"; + }); + } + } + } + + @override + Widget build(BuildContext context) { + final theme = Theme.of(context); + final updateInfo = widget.updateInfo; + + return AlertDialog( + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)), + title: Row( + children: [ + Icon( + updateInfo.isMandatory + ? Icons.warning_amber_rounded + : (kIsWeb ? Icons.cached : Icons.system_update), + color: updateInfo.isMandatory ? Colors.amber[700] : Colors.blue, + size: 28, + ), + const SizedBox(width: 12), + Expanded( + child: Text( + updateInfo.isMandatory + ? "Aggiornamento Obbligatorio" + : "Nuova Versione Disponibile", + style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + ), + ), + ], + ), + content: ConstrainedBox( + constraints: const BoxConstraints(maxWidth: 400), + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text.rich( + TextSpan( + style: const TextStyle(fontSize: 14), + children: [ + const TextSpan(text: "Versione installata: "), + TextSpan( + text: widget.currentVersion, + style: const TextStyle(fontWeight: FontWeight.bold), + ), + const TextSpan(text: "\nNuova versione disponibile: "), + TextSpan( + text: "v${updateInfo.latestVersion}", + style: TextStyle( + fontWeight: FontWeight.bold, + color: theme.colorScheme.primary, + ), + ), + ], + ), + ), + const SizedBox(height: 12), + Text( + kIsWeb + ? "È stata rilasciata una nuova versione. Ricarica la pagina per applicarla." + : (updateInfo.isMandatory + ? "La versione attuale non è più supportata. Aggiorna all'ultima versione per continuare a lavorare." + : "Vuoi scaricare e installare l'aggiornamento adesso?"), + style: TextStyle( + fontSize: 14, + color: theme.colorScheme.onSurface.withValues(alpha: 0.8), + ), + ), + if (_errorMessage != null) ...[ + const SizedBox(height: 16), + Container( + padding: const EdgeInsets.all(10), + decoration: BoxDecoration( + color: Colors.red.withValues(alpha: 0.1), + borderRadius: BorderRadius.circular(8), + border: Border.all(color: Colors.red.withValues(alpha: 0.3)), + ), + child: Text( + _errorMessage!, + style: const TextStyle(color: Colors.red, fontSize: 13), + ), + ), + ], + if (_isUpdating) ...[ + const SizedBox(height: 16), + ClipRRect( + borderRadius: BorderRadius.circular(8), + child: LinearProgressIndicator( + value: _downloadProgress > 0 ? _downloadProgress : null, + minHeight: 8, + ), + ), + const SizedBox(height: 8), + Text( + _statusMessage, + style: TextStyle( + fontSize: 12, + color: theme.colorScheme.onSurface.withValues(alpha: 0.7), + ), + ), + ], + ], + ), + ), + actions: _isUpdating + ? [] + : [ + if (!updateInfo.isMandatory) + TextButton( + onPressed: () => Navigator.of(context).pop(), + child: const Text("PIÙ TARDI"), + ), + if (kIsWeb) + FilledButton.icon( + icon: const Icon(Icons.refresh), + label: const Text("RICARICA ORA"), + onPressed: () { + html.window.location.reload(); + }, + ) + else if (AutoUpdateService.isAutoUpdateSupported) + FilledButton.icon( + icon: const Icon(Icons.system_update_alt), + label: const Text("AGGIORNA ORA"), + onPressed: _startUpdate, + ) + else + FilledButton.icon( + icon: const Icon(Icons.open_in_browser), + label: const Text("SCARICA"), + onPressed: () async { + final url = Uri.parse(updateInfo.downloadUrl); + await launchUrl(url, mode: LaunchMode.externalApplication); + if (context.mounted) Navigator.of(context).pop(); + }, + ), + ], + ); + } +} diff --git a/lib/core/utils/version_check_service.dart b/lib/core/utils/version_check_service.dart index 4ce9123..f0fe522 100644 --- a/lib/core/utils/version_check_service.dart +++ b/lib/core/utils/version_check_service.dart @@ -97,6 +97,28 @@ class VersionCheckService { return info?.isMandatory == true ? info?.downloadUrl : null; } + static Future getAppVersionString() async { + try { + final info = await PackageInfo.fromPlatform(); + return info.buildNumber.isNotEmpty + ? 'v${info.version}+${info.buildNumber}' + : 'v${info.version}'; + } catch (_) { + return ''; + } + } + + static Future getFullVersionString() => getAppVersionString(); + + static Future getShortVersionString() async { + try { + final info = await PackageInfo.fromPlatform(); + return 'v${info.version}'; + } catch (_) { + return ''; + } + } + String _cleanVersion(String raw) { return raw.split('+').first.replaceAll('v', '').trim(); } diff --git a/lib/core/widgets/app_version_label.dart b/lib/core/widgets/app_version_label.dart new file mode 100644 index 0000000..13abbf3 --- /dev/null +++ b/lib/core/widgets/app_version_label.dart @@ -0,0 +1,99 @@ +import 'package:flutter/material.dart'; +import 'package:flux/core/utils/update_prompt_helper.dart'; +import 'package:flux/core/utils/version_check_service.dart'; + +class AppVersionLabel extends StatefulWidget { + final bool isCollapsed; + final bool showIcon; + + const AppVersionLabel({ + super.key, + this.isCollapsed = false, + this.showIcon = true, + }); + + @override + State createState() => _AppVersionLabelState(); +} + +class _AppVersionLabelState extends State { + String _version = ""; + + @override + void initState() { + super.initState(); + _loadVersion(); + } + + Future _loadVersion() async { + final v = await VersionCheckService.getShortVersionString(); + if (mounted) { + setState(() { + _version = v; + }); + } + } + + @override + Widget build(BuildContext context) { + if (_version.isEmpty) return const SizedBox.shrink(); + + final theme = Theme.of(context); + final textMuted = theme.colorScheme.onSurface.withValues(alpha: 0.5); + + if (widget.isCollapsed) { + return Tooltip( + message: "Flux $_version\nClicca per verificare aggiornamenti", + child: InkWell( + borderRadius: BorderRadius.circular(8), + onTap: () => UpdatePromptHelper.showManualUpdateCheck(context), + child: Container( + height: 36, + width: 36, + alignment: Alignment.center, + child: Icon( + Icons.info_outline, + size: 16, + color: textMuted, + ), + ), + ), + ); + } + + return Tooltip( + message: "Clicca per verificare se ci sono aggiornamenti", + child: InkWell( + borderRadius: BorderRadius.circular(6), + onTap: () => UpdatePromptHelper.showManualUpdateCheck(context), + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + if (widget.showIcon) ...[ + Icon( + Icons.info_outline, + size: 13, + color: textMuted, + ), + const SizedBox(width: 6), + ], + Text( + "Flux $_version", + style: TextStyle( + fontSize: 11, + fontWeight: FontWeight.w500, + color: textMuted, + decoration: TextDecoration.underline, + decorationColor: textMuted.withValues(alpha: 0.4), + decorationStyle: TextDecorationStyle.dotted, + ), + ), + ], + ), + ), + ), + ); + } +} diff --git a/lib/features/settings/ui/settings_screen.dart b/lib/features/settings/ui/settings_screen.dart index 3421cb8..951aa2e 100644 --- a/lib/features/settings/ui/settings_screen.dart +++ b/lib/features/settings/ui/settings_screen.dart @@ -4,6 +4,8 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flux/core/blocs/session/session_cubit.dart'; import 'package:flux/core/routes/routes.dart'; import 'package:flux/core/theme/theme.dart'; +import 'package:flux/core/utils/update_prompt_helper.dart'; +import 'package:flux/core/utils/version_check_service.dart'; import 'package:flux/features/settings/blocs/settings_cubit.dart'; import 'package:get_it/get_it.dart'; import 'package:go_router/go_router.dart'; @@ -91,6 +93,22 @@ class SettingsScreen extends StatelessWidget { context: context, onTap: () => context.pushNamed(Routes.themeSettings), ), + const Divider(height: 30), + FutureBuilder( + future: VersionCheckService.getFullVersionString(), + builder: (context, snapshot) { + final versionText = snapshot.data ?? '...'; + return _settingsTile( + icon: Icons.info_outline, + title: 'Versione Flux', + subtitle: versionText.isNotEmpty + ? '$versionText • Tocca per verificare aggiornamenti' + : 'Verifica disponibilità aggiornamenti', + context: context, + onTap: () => UpdatePromptHelper.showManualUpdateCheck(context), + ); + }, + ), ]), const SizedBox(height: 24), diff --git a/pubspec.yaml b/pubspec.yaml index 2230c14..0393c3e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: flux description: "Gestione attività negozio di telefonia" publish_to: 'none' -version: 2.0.11+67 +version: 2.0.12+68 environment: sdk: ^3.11.3