68 lines
2.1 KiB
Dart
68 lines
2.1 KiB
Dart
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';
|
|
}
|
|
|
|
// 2. Recuperiamo la configurazione minima da Supabase
|
|
final data = await _supabase
|
|
.from('app_config')
|
|
.select()
|
|
.eq('platform', platformKey)
|
|
.maybeSingle();
|
|
|
|
if (data == null) return null;
|
|
|
|
final String minVersion = data['min_version'];
|
|
final String downloadUrl = data['download_url'];
|
|
|
|
// 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!
|
|
}
|
|
|
|
return null;
|
|
} catch (e) {
|
|
debugPrint('Errore controllo versione: $e');
|
|
return null; // In caso di errore non blocchiamo l'utente
|
|
}
|
|
}
|
|
|
|
bool _isVersionLower(String current, String min) {
|
|
List<int> currentParts = current
|
|
.split('.')
|
|
.map((e) => int.tryParse(e) ?? 0)
|
|
.toList();
|
|
List<int> minParts = min
|
|
.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;
|
|
}
|
|
return false;
|
|
}
|
|
}
|