57 lines
1.8 KiB
Dart
57 lines
1.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
import '../../core/config/app_config.dart';
|
|
import '../../core/di/injection.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;
|
|
}
|
|
|
|
Future<bool> checkAppUpdateAvailable() async {
|
|
final config = sl.isRegistered<AppConfig>() ? sl<AppConfig>() : null;
|
|
final latest = config?.latestAppVersion?.trim();
|
|
if (latest == null || latest.isEmpty) return false;
|
|
try {
|
|
final info = await PackageInfo.fromPlatform();
|
|
return isNewerAppVersion(latest, info.version);
|
|
} catch (_) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<void> handleAppUpdateTap(
|
|
BuildContext context, {
|
|
required bool updateAvailable,
|
|
}) async {
|
|
final s = context.strings;
|
|
final config = sl.isRegistered<AppConfig>() ? sl<AppConfig>() : null;
|
|
final storeUrl = config?.appStoreUrl?.trim();
|
|
if (updateAvailable && storeUrl != null && storeUrl.isNotEmpty) {
|
|
final uri = Uri.tryParse(storeUrl);
|
|
if (uri != null) {
|
|
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
|
return;
|
|
}
|
|
}
|
|
if (!context.mounted) return;
|
|
if (updateAvailable) {
|
|
showSuccessSnackBar(context, s.updateAvailableHint);
|
|
} else {
|
|
showSuccessSnackBar(context, s.appUpToDate);
|
|
}
|
|
}
|