From 7ea0e2ac102bc92c67ec0a567ca334ab71943af3 Mon Sep 17 00:00:00 2001 From: mark-cachy Date: Thu, 4 Jun 2026 14:21:27 +0200 Subject: [PATCH] changed version check --- lib/main.dart | 80 +++++++++++++++++++++++++++++++++++++++------------ pubspec.lock | 32 +++++++++++++++++++++ pubspec.yaml | 3 +- 3 files changed, 95 insertions(+), 20 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index bd684ae..2e6d37e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:io'; import 'package:firebase_core/firebase_core.dart'; @@ -47,6 +48,7 @@ import 'package:flux/features/master_data/store/data/store_repository.dart'; import 'package:flux/features/settings/ui/settings.dart'; import 'package:flutter_web_plugins/url_strategy.dart'; import 'package:url_launcher/url_launcher.dart'; +import 'package:universal_html/html.dart' as html; String? initialRecoveryFragment; void main() async { @@ -304,21 +306,71 @@ class GlobalUpdateChecker extends StatefulWidget { State createState() => _GlobalUpdateCheckerState(); } -class _GlobalUpdateCheckerState extends State { +class _GlobalUpdateCheckerState extends State + with WidgetsBindingObserver { bool _mustUpdate = false; String? _updateUrl; + StreamSubscription? _versionSubscription; @override void initState() { super.initState(); - _checkVersionAndBlock(); + // 🥷 1. Registriamo questo widget per ascoltare i cicli vitali dell'app + WidgetsBinding.instance.addObserver(this); + _startRealtimeVersionCheck(); } - Future _checkVersionAndBlock() async { + @override + void dispose() { + // 🥷 2. Rimuoviamo l'observer quando il widget muore (mai, in questo caso, ma è buona norma) + WidgetsBinding.instance.removeObserver(this); + _stopRealtimeVersionCheck(); + super.dispose(); + } + + // 🥷 3. IL VERO GESTORE DEL CICLO VITALE + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + super.didChangeAppLifecycleState(state); + + if (state == AppLifecycleState.resumed) { + // L'app è tornata attiva: riaccendiamo il radar + _startRealtimeVersionCheck(); + } else if (state == AppLifecycleState.paused || + state == AppLifecycleState.hidden) { + // L'app è andata in background: spegniamo il socket per evitare crash + _stopRealtimeVersionCheck(); + } + } + + void _startRealtimeVersionCheck() { + // Sicurezza: cancelliamo eventuali vecchi abbonamenti rimasti appesi + _stopRealtimeVersionCheck(); + + // Facciamo un check immediato non appena rientriamo in app + _checkVersion(); + + // Riapriamo il rubinetto di Supabase + _versionSubscription = GetIt.I() + .from( + 'app_versions', + ) // <-- Sostituisci col nome reale della tua tabella + .stream(primaryKey: ['id']) + .listen((_) { + _checkVersion(); + }); + } + + void _stopRealtimeVersionCheck() { + // Chiudiamo gentilmente il socket + _versionSubscription?.cancel(); + _versionSubscription = null; + } + + Future _checkVersion() async { final updateUrl = await VersionCheckService().checkForceUpdate(); if (updateUrl != null && mounted) { - // Invece di aprire un dialog, cambiamo lo stato e attiviamo lo "Scudo" setState(() { _mustUpdate = true; _updateUrl = updateUrl; @@ -328,21 +380,15 @@ class _GlobalUpdateCheckerState extends State { @override Widget build(BuildContext context) { - // 1. Se l'app è aggiornata, mostriamo solo l'app normale if (!_mustUpdate) return widget.child; - // 2. Se l'app è vecchia, sovrapponiamo il blocco con uno Stack return Stack( children: [ - // L'app sotto continua ad esistere, ma è inaccessibile widget.child, - - // IL BLOCCO INVALICABILE SOPRA A TUTTO Positioned.fill( child: Container( - color: Colors.black.withValues(alpha: 0.85), // Sfondo oscurante + color: Colors.black.withValues(alpha: 0.85), child: Center( - // Usiamo Material per ereditare correttamente temi, font e colori child: Material( color: Colors.transparent, child: Container( @@ -386,7 +432,7 @@ class _GlobalUpdateCheckerState extends State { const SizedBox(height: 16), Text( kIsWeb - ? "È stata rilasciata una nuova versione dell'applicazione. Ricarica la pagina per continuare." + ? "È stata rilasciata una nuova versione dell'applicazione. Ricarica la pagina per scaricare il nuovo codice." : "Per continuare ad utilizzare l'applicazione è necessario scaricare e installare l'ultimo aggiornamento.", textAlign: TextAlign.center, style: const TextStyle(fontSize: 16), @@ -399,18 +445,14 @@ class _GlobalUpdateCheckerState extends State { style: FilledButton.styleFrom( minimumSize: const Size(double.infinity, 50), ), - onPressed: () async { - // Trick cross-platform per fare il reload - await launchUrl( - Uri.parse(Uri.base.toString()), - webOnlyWindowName: '_self', - ); + onPressed: () { + // Hard reload aggirando la cache! + html.window.location.reload(); }, ) else FilledButton.icon( icon: const Icon(Icons.download), - label: const Text("SCARICA AGGIORNAMENTO"), style: FilledButton.styleFrom( minimumSize: const Size(double.infinity, 50), diff --git a/pubspec.lock b/pubspec.lock index aa77f86..3d942d6 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -105,6 +105,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.4.1" + charcode: + dependency: transitive + description: + name: charcode + sha256: fb0f1107cac15a5ea6ef0a6ef71a807b9e4267c713bb93e00e92d737cc8dbd8a + url: "https://pub.dev" + source: hosted + version: "1.4.0" checked_yaml: dependency: transitive description: @@ -169,6 +177,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.0.7" + csslib: + dependency: transitive + description: + name: csslib + sha256: "09bad715f418841f976c77db72d5398dc1253c21fb9c0c7f0b0b985860b2d58e" + url: "https://pub.dev" + source: hosted + version: "1.0.2" dart_jsonwebtoken: dependency: transitive description: @@ -461,6 +477,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.2" + html: + dependency: transitive + description: + name: html + sha256: "6d1264f2dffa1b1101c25a91dff0dc2daee4c18e87cd8538729773c073dbf602" + url: "https://pub.dev" + source: hosted + version: "0.15.6" http: dependency: transitive description: @@ -1131,6 +1155,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.0" + universal_html: + dependency: "direct main" + description: + name: universal_html + sha256: c0bcae5c733c60f26c7dfc88b10b0fd27cbcc45cb7492311cdaa6067e21c9cd4 + url: "https://pub.dev" + source: hosted + version: "2.3.0" universal_io: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 44eed8d..315b615 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: flux description: "Gestione attività negozio di telefonia" publish_to: 'none' -version: 1.1.17+35 +version: 1.1.18+36 environment: sdk: ^3.11.3 @@ -41,6 +41,7 @@ dependencies: flutter_staggered_grid_view: ^0.7.0 firebase_core: ^4.9.0 firebase_messaging: ^16.2.2 + universal_html: ^2.3.0 dependency_overrides: pdfx: