v check fix
This commit is contained in:
@@ -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<String?> 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<int> currentParts = current
|
||||
.split('.')
|
||||
.map((e) => int.tryParse(e) ?? 0)
|
||||
.toList();
|
||||
List<int> minParts = min
|
||||
List<int> 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];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user