diff --git a/lib/core/enums_and_consts/consts.dart b/lib/core/enums_and_consts/consts.dart index dad0841..b7458db 100644 --- a/lib/core/enums_and_consts/consts.dart +++ b/lib/core/enums_and_consts/consts.dart @@ -1,4 +1,5 @@ class Tables { + static const String appConfig = 'app_config'; static const String attachments = 'attachments'; static const String brands = 'brands'; static const String campaigns = 'campaigns'; diff --git a/lib/core/utils/version_check_service.dart b/lib/core/utils/version_check_service.dart index 8dea48b..2a4f051 100644 --- a/lib/core/utils/version_check_service.dart +++ b/lib/core/utils/version_check_service.dart @@ -1,67 +1,94 @@ +import 'dart:io'; import 'package:flutter/foundation.dart'; -import 'dart:io' show Platform; import 'package:package_info_plus/package_info_plus.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; class VersionCheckService { - final _supabase = Supabase.instance.client; - - /// Controlla se l'app corrente deve essere bloccata o aggiornata. - /// Ritorna il link di download se l'aggiornamento è obbligatorio, altrimenti null. Future checkForceUpdate() async { try { - // 1. Determiniamo la piattaforma corrente - String platformKey = 'web'; - if (!kIsWeb) { - if (Platform.isAndroid) platformKey = 'android'; - if (Platform.isWindows) platformKey = 'windows'; + // 1. Capiamo su che piattaforma sta girando l'app in questo istante + String currentPlatform = _getCurrentPlatform(); + + // 2. Recuperiamo SOLO la riga corrispondente alla nostra piattaforma + final dbResponse = await Supabase.instance.client + .from('app_config') + .select('min_version, download_url') + .eq('platform', currentPlatform) + .maybeSingle(); // Usiamo maybeSingle così se non c'è la riga non crasha + + if (dbResponse == null) { + return null; // Nessuna regola per questa piattaforma } - // 2. Recuperiamo la configurazione minima da Supabase - final data = await _supabase - .from('app_config') - .select() - .eq('platform', platformKey) - .maybeSingle(); + String minVersionFromDb = dbResponse['min_version'] as String; + String downloadUrl = dbResponse['download_url'] as String; - if (data == null) return null; + // 3. Recuperiamo la versione locale di Flutter + PackageInfo packageInfo = await PackageInfo.fromPlatform(); + String localVersionRaw = packageInfo.version; - final String minVersion = data['min_version']; - final String downloadUrl = data['download_url']; + // 🥷 TRUCCO 1: Pulizia totale dai build number (+37) o tag "v" + String cleanLocal = localVersionRaw + .split('+') + .first + .replaceAll('v', '') + .trim(); + String cleanDb = minVersionFromDb + .split('+') + .first + .replaceAll('v', '') + .trim(); - // 3. Recuperiamo la versione attuale dell'app dal pubspec.yaml - final packageInfo = await PackageInfo.fromPlatform(); - final String currentVersion = packageInfo.version; - - // 4. Confronto matematico semantico (es. 1.2.3 vs 1.1.9) - if (_isVersionLower(currentVersion, minVersion)) { - return downloadUrl; // Aggiornamento obbligatorio richiesto! + // 🥷 TRUCCO 2: Confronto Semantico Reale + if (_isVersionLower(current: cleanLocal, minimum: cleanDb)) { + // Ritorna il link VERO per questa specifica piattaforma preso dal CSV! + return downloadUrl; } return null; } catch (e) { - debugPrint('Errore controllo versione: $e'); - return null; // In caso di errore non blocchiamo l'utente + debugPrint("Errore durante il check versione: $e"); + return null; } } - bool _isVersionLower(String current, String min) { + // Helper ninja per mappare le piattaforme in base alle stringhe del tuo DB + String _getCurrentPlatform() { + if (kIsWeb) return 'web'; + if (Platform.isAndroid) return 'android'; + if (Platform.isIOS) return 'ios'; + if (Platform.isWindows) return 'windows'; + if (Platform.isMacOS) return 'macos'; + if (Platform.isLinux) return 'linux'; + return 'unknown'; + } + + // Il motore matematico (resta invariato) + bool _isVersionLower({required String current, required String minimum}) { + if (current == minimum) return false; + List currentParts = current .split('.') .map((e) => int.tryParse(e) ?? 0) .toList(); - List minParts = min + List minParts = minimum .split('.') .map((e) => int.tryParse(e) ?? 0) .toList(); - for (int i = 0; i < 3; i++) { - int currentPart = currentParts.length > i ? currentParts[i] : 0; - int minPart = minParts.length > i ? minParts[i] : 0; - - if (currentPart < minPart) return true; - if (currentPart > minPart) return false; + while (currentParts.length < 3) { + currentParts.add(0); } - return false; + while (minParts.length < 3) { + minParts.add(0); + } + + if (currentParts[0] != minParts[0]) { + return currentParts[0] < minParts[0]; + } + if (currentParts[1] != minParts[1]) { + return currentParts[1] < minParts[1]; + } + return currentParts[2] < minParts[2]; } } diff --git a/lib/main.dart b/lib/main.dart index 2e6d37e..709ed1e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -352,9 +352,7 @@ class _GlobalUpdateCheckerState extends State // Riapriamo il rubinetto di Supabase _versionSubscription = GetIt.I() - .from( - 'app_versions', - ) // <-- Sostituisci col nome reale della tua tabella + .from('app_config') // <-- Sostituisci col nome reale della tua tabella .stream(primaryKey: ['id']) .listen((_) { _checkVersion(); diff --git a/pubspec.yaml b/pubspec.yaml index 315b615..3bda720 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.18+36 +version: 1.1.20+38 environment: sdk: ^3.11.3