94 lines
3.0 KiB
Dart
94 lines
3.0 KiB
Dart
import 'dart:io' show Platform;
|
|
|
|
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';
|
|
|
|
enum UpgradeCheckSource { automatic, manual }
|
|
|
|
class UpdateService {
|
|
static bool get isSupported =>
|
|
!kIsWeb && (Platform.isAndroid || Platform.isIOS);
|
|
|
|
static Future<void> showIfNeeded(
|
|
BuildContext context,
|
|
WidgetRef ref,
|
|
UpgradeInfo info, {
|
|
required UpgradeCheckSource source,
|
|
VoidCallback? onLatest,
|
|
}) async {
|
|
final shouldShow = source == UpgradeCheckSource.automatic
|
|
? info.show == 1 && info.hasPublishedUpdate
|
|
: info.hasPublishedUpdate;
|
|
if (!shouldShow) {
|
|
onLatest?.call();
|
|
return;
|
|
}
|
|
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 ? '前往更新' : '下载更新'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
static Future<void> _startUpdate(Uri uri) async {
|
|
if (Platform.isIOS) {
|
|
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
|
return;
|
|
}
|
|
await AzhonAppUpdate.update(
|
|
UpdateModel(
|
|
uri.toString(),
|
|
'jinzhi-update.apk',
|
|
'ic_launcher',
|
|
'https://apps.apple.com/search?term=%E9%87%91%E5%80%BC',
|
|
showNotification: true,
|
|
showBgdToast: false,
|
|
),
|
|
);
|
|
}
|
|
}
|