95 lines
2.9 KiB
Dart
95 lines
2.9 KiB
Dart
import 'dart:io';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
|
|
class VersionCheckService {
|
|
Future<String?> checkForceUpdate() async {
|
|
try {
|
|
// 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
|
|
}
|
|
|
|
String minVersionFromDb = dbResponse['min_version'] as String;
|
|
String downloadUrl = dbResponse['download_url'] as String;
|
|
|
|
// 3. Recuperiamo la versione locale di Flutter
|
|
PackageInfo packageInfo = await PackageInfo.fromPlatform();
|
|
String localVersionRaw = packageInfo.version;
|
|
|
|
// 🥷 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();
|
|
|
|
// 🥷 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 durante il check versione: $e");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// 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 = minimum
|
|
.split('.')
|
|
.map((e) => int.tryParse(e) ?? 0)
|
|
.toList();
|
|
|
|
while (currentParts.length < 3) {
|
|
currentParts.add(0);
|
|
}
|
|
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];
|
|
}
|
|
}
|