92 lines
2.6 KiB
Dart
92 lines
2.6 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:url_launcher/url_launcher.dart';
|
|
|
|
import '../data/update_api_client.dart';
|
|
import '../presentation/update_dialog.dart';
|
|
import 'update_progress.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 {
|
|
if (ref.read(updateDialogVisibleProvider)) return;
|
|
|
|
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;
|
|
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(
|
|
String downloadUrl, {
|
|
bool showNotification = true,
|
|
}) async {
|
|
if (kIsWeb) return;
|
|
if (Platform.isIOS) {
|
|
await _openIosUpdateUrl(downloadUrl);
|
|
return;
|
|
}
|
|
await AzhonAppUpdate.update(
|
|
UpdateModel(
|
|
downloadUrl,
|
|
'jinzhi-update.apk',
|
|
'ic_launcher',
|
|
'https://apps.apple.com/search?term=%E9%87%91%E5%80%BC',
|
|
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);
|
|
}
|
|
}
|
|
}
|