53 lines
1.6 KiB
Dart
53 lines
1.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../../core/di/injection.dart';
|
|
import '../../features/app_update/app_update_coordinator.dart';
|
|
import '../../l10n/app_strings.dart';
|
|
import '../../shared/widgets/app_snackbar.dart';
|
|
|
|
bool isNewerAppVersion(String latest, String current) {
|
|
List<int> parts(String v) =>
|
|
v.split('.').map((e) => int.tryParse(e.replaceAll(RegExp(r'[^0-9]'), '')) ?? 0).toList();
|
|
final a = parts(latest);
|
|
final b = parts(current);
|
|
final len = a.length > b.length ? a.length : b.length;
|
|
for (var i = 0; i < len; i++) {
|
|
final x = i < a.length ? a[i] : 0;
|
|
final y = i < b.length ? b[i] : 0;
|
|
if (x > y) return true;
|
|
if (x < y) return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// True when the last server check reported an available update.
|
|
Future<bool> checkAppUpdateAvailable() async {
|
|
if (!sl.isRegistered<AppUpdateCoordinator>()) return false;
|
|
final last = sl<AppUpdateCoordinator>().lastResult;
|
|
if (last != null) return last.updateAvailable;
|
|
try {
|
|
final result = await sl<AppUpdateCoordinator>().checkAndPrompt(
|
|
force: false,
|
|
blockNavigationIfMandatory: false,
|
|
);
|
|
return result?.updateAvailable == true;
|
|
} catch (_) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<void> handleAppUpdateTap(
|
|
BuildContext context, {
|
|
required bool updateAvailable,
|
|
}) async {
|
|
if (sl.isRegistered<AppUpdateCoordinator>()) {
|
|
await sl<AppUpdateCoordinator>().handleManualTap(context);
|
|
return;
|
|
}
|
|
if (!context.mounted) return;
|
|
if (updateAvailable) {
|
|
showSuccessSnackBar(context, context.strings.updateAvailableHint);
|
|
} else {
|
|
showSuccessSnackBar(context, context.strings.appUpToDate);
|
|
}
|
|
}
|