fix:app检测更新补充逻辑
This commit is contained in:
1 parent
05e89b80bb
commit
b4343ea528
5 files changed
+260
-56
No files matched your search
@@ -4,10 +4,11 @@ import 'package:flutter/foundation.dart' show kIsWeb;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_app_update/flutter_app_update.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import '../data/update_api_client.dart';
|
||||
import '../presentation/update_dialog.dart';
|
||||
import 'update_progress.dart';
|
||||
|
||||
enum UpgradeCheckSource { automatic, manual }
|
||||
|
||||
@@ -22,6 +23,8 @@ class UpdateService {
|
||||
required UpgradeCheckSource source,
|
||||
VoidCallback? onLatest,
|
||||
}) async {
|
||||
if (ref.read(updateDialogVisibleProvider)) return;
|
||||
|
||||
final shouldShow = source == UpgradeCheckSource.automatic
|
||||
? info.show == 1 && info.hasPublishedUpdate
|
||||
: info.hasPublishedUpdate;
|
||||
@@ -32,62 +35,57 @@ class UpdateService {
|
||||
final uri = Uri.tryParse(info.url);
|
||||
if (uri == null || uri.scheme != 'https' || !uri.hasAuthority) return;
|
||||
final force = info.force == 1;
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: !force,
|
||||
builder: (dialogContext) => PopScope(
|
||||
canPop: !force,
|
||||
child: AlertDialog(
|
||||
title: Text(
|
||||
'发现新版本${info.latestVersion.isEmpty ? '' : ' ${info.latestVersion}'}',
|
||||
),
|
||||
content: Text(info.message.isEmpty ? '发现新版本,建议立即更新。' : info.message),
|
||||
actions: [
|
||||
if (!force)
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(dialogContext),
|
||||
child: const Text('稍后再说'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () async {
|
||||
Navigator.pop(dialogContext);
|
||||
final package = await PackageInfo.fromPlatform();
|
||||
try {
|
||||
await ref
|
||||
.read(updateApiClientProvider)
|
||||
.reportClick(
|
||||
buildId: info.versionCode.isEmpty
|
||||
? package.buildNumber
|
||||
: info.versionCode,
|
||||
source: source == UpgradeCheckSource.manual
|
||||
? 'manual'
|
||||
: 'auto',
|
||||
);
|
||||
} catch (_) {}
|
||||
await _startUpdate(uri);
|
||||
},
|
||||
child: Text(Platform.isIOS ? '前往更新' : '下载更新'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
ref.read(updateDialogVisibleProvider.notifier).state = true;
|
||||
try {
|
||||
await showModalBottomSheet<void>(
|
||||
context: context,
|
||||
useRootNavigator: true,
|
||||
isDismissible: !force,
|
||||
enableDrag: !force,
|
||||
isScrollControlled: true,
|
||||
builder: (_) => UpdateDialog(upgrade: info, source: source),
|
||||
);
|
||||
} finally {
|
||||
ref.read(updateProgressProvider.notifier).state = null;
|
||||
ref.read(updateUrlProvider.notifier).state = null;
|
||||
ref.read(updateDialogVisibleProvider.notifier).state = false;
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> _startUpdate(Uri uri) async {
|
||||
static Future<void> startUpdate(
|
||||
String downloadUrl, {
|
||||
bool showNotification = true,
|
||||
}) async {
|
||||
if (kIsWeb) return;
|
||||
if (Platform.isIOS) {
|
||||
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
||||
await _openIosUpdateUrl(downloadUrl);
|
||||
return;
|
||||
}
|
||||
await AzhonAppUpdate.update(
|
||||
UpdateModel(
|
||||
uri.toString(),
|
||||
downloadUrl,
|
||||
'jinzhi-update.apk',
|
||||
'ic_launcher',
|
||||
'https://apps.apple.com/search?term=%E9%87%91%E5%80%BC',
|
||||
showNotification: true,
|
||||
showNotification: showNotification,
|
||||
showBgdToast: false,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static Future<void> install(String url) async {
|
||||
if (kIsWeb) return;
|
||||
if (Platform.isIOS) {
|
||||
await _openIosUpdateUrl(url);
|
||||
return;
|
||||
}
|
||||
await AzhonAppUpdate.install(url);
|
||||
}
|
||||
|
||||
static Future<void> _openIosUpdateUrl(String downloadUrl) async {
|
||||
final uri = Uri.tryParse(downloadUrl);
|
||||
if (uri != null && uri.scheme == 'https' && uri.hasAuthority) {
|
||||
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user