Build and Release FLUX (Multi-Platform) / build-web (push) Successful in 1m53s
Build and Release FLUX (Multi-Platform) / build-linux (push) Successful in 1m20s
Build and Release FLUX (Multi-Platform) / build-windows (push) Failing after 6m18s
Build and Release FLUX (Multi-Platform) / build-macos (push) Failing after 14m10s
140 lines
4.0 KiB
Dart
140 lines
4.0 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';
|
|
|
|
enum UpdateType {
|
|
none,
|
|
optional,
|
|
mandatory,
|
|
}
|
|
|
|
class UpdateInfo {
|
|
final UpdateType type;
|
|
final String latestVersion;
|
|
final String minVersion;
|
|
final String downloadUrl;
|
|
|
|
const UpdateInfo({
|
|
required this.type,
|
|
required this.latestVersion,
|
|
required this.minVersion,
|
|
required this.downloadUrl,
|
|
});
|
|
|
|
bool get isMandatory => type == UpdateType.mandatory;
|
|
bool get isOptional => type == UpdateType.optional;
|
|
bool get hasUpdate => type != UpdateType.none;
|
|
}
|
|
|
|
class VersionCheckService {
|
|
Future<UpdateInfo?> checkUpdate() async {
|
|
try {
|
|
final platformAliases = _getPlatformAliases();
|
|
|
|
final dbResponse = await Supabase.instance.client
|
|
.from('app_config')
|
|
.select('platform, min_version, latest_version, download_url');
|
|
|
|
if (dbResponse.isEmpty) {
|
|
return null;
|
|
}
|
|
|
|
final rows = List<Map<String, dynamic>>.from(dbResponse);
|
|
final config = rows.firstWhere(
|
|
(r) {
|
|
final p = (r['platform'] as String?)?.toLowerCase().trim() ?? '';
|
|
return platformAliases.any((alias) => alias.toLowerCase() == p);
|
|
},
|
|
orElse: () => {},
|
|
);
|
|
|
|
if (config.isEmpty) {
|
|
return null;
|
|
}
|
|
|
|
final minVersionRaw = (config['min_version'] as String?) ?? '0.0.0';
|
|
final latestVersionRaw =
|
|
(config['latest_version'] as String?) ?? minVersionRaw;
|
|
final downloadUrl = (config['download_url'] as String?) ?? '';
|
|
|
|
final packageInfo = await PackageInfo.fromPlatform();
|
|
final localVersionRaw = packageInfo.version;
|
|
|
|
final cleanLocal = _cleanVersion(localVersionRaw);
|
|
final cleanMin = _cleanVersion(minVersionRaw);
|
|
final cleanLatest = _cleanVersion(latestVersionRaw);
|
|
|
|
// Se la versione locale è inferiore alla minima ammessa -> obbligatorio
|
|
if (_isVersionLower(current: cleanLocal, target: cleanMin)) {
|
|
return UpdateInfo(
|
|
type: UpdateType.mandatory,
|
|
latestVersion: latestVersionRaw,
|
|
minVersion: minVersionRaw,
|
|
downloadUrl: downloadUrl,
|
|
);
|
|
}
|
|
|
|
// Se è >= della minima ma inferiore all'ultima disponibile -> facoltativo
|
|
if (_isVersionLower(current: cleanLocal, target: cleanLatest)) {
|
|
return UpdateInfo(
|
|
type: UpdateType.optional,
|
|
latestVersion: latestVersionRaw,
|
|
minVersion: minVersionRaw,
|
|
downloadUrl: downloadUrl,
|
|
);
|
|
}
|
|
|
|
return null;
|
|
} catch (e) {
|
|
debugPrint("Errore durante il check versione: $e");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<String?> checkForceUpdate() async {
|
|
final info = await checkUpdate();
|
|
return info?.isMandatory == true ? info?.downloadUrl : null;
|
|
}
|
|
|
|
String _cleanVersion(String raw) {
|
|
return raw.split('+').first.replaceAll('v', '').trim();
|
|
}
|
|
|
|
List<String> _getPlatformAliases() {
|
|
if (kIsWeb) return const ['web'];
|
|
if (Platform.isAndroid) return const ['android'];
|
|
if (Platform.isIOS) return const ['ios'];
|
|
if (Platform.isWindows) return const ['windows'];
|
|
if (Platform.isMacOS) return const ['macos'];
|
|
if (Platform.isLinux) return const ['AppImage', 'linux'];
|
|
return const ['unknown'];
|
|
}
|
|
|
|
bool _isVersionLower({required String current, required String target}) {
|
|
if (current == target) return false;
|
|
|
|
final currentParts = current
|
|
.split('.')
|
|
.map((e) => int.tryParse(e) ?? 0)
|
|
.toList();
|
|
final targetParts = target
|
|
.split('.')
|
|
.map((e) => int.tryParse(e) ?? 0)
|
|
.toList();
|
|
|
|
final maxLength = currentParts.length > targetParts.length
|
|
? currentParts.length
|
|
: targetParts.length;
|
|
|
|
for (int i = 0; i < maxLength; i++) {
|
|
final c = i < currentParts.length ? currentParts[i] : 0;
|
|
final t = i < targetParts.length ? targetParts[i] : 0;
|
|
if (c != t) {
|
|
return c < t;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|