Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

feat: better update dialog (#1024)

leetao66a1f2b18b14
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index f3c07471..16674d9d 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -40,6 +40,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
+ <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility?hl=en and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
diff --git a/lib/pages/about/about_page.dart b/lib/pages/about/about_page.dart
index 566f1354..c420758c 100644
--- a/lib/pages/about/about_page.dart
+++ b/lib/pages/about/about_page.dart
@@ -258,7 +258,14 @@ class _AboutPageState extends State<AboutPage> {
),
SettingsTile.navigation(
onPressed: (_) {
- myController.checkUpdata();
+ Modular.to.pushNamed('/settings/update');
+ },
+ title: const Text('更新设置'),
+ description: const Text('检查更新、下载安装新版本'),
+ ),
+ SettingsTile.navigation(
+ onPressed: (_) {
+ myController.checkUpdate();
},
title: const Text('检查更新'),
value: const Text('当前版本 ${Api.version}'),
diff --git a/lib/pages/init_page.dart b/lib/pages/init_page.dart
index c7ba57e4..d0dfe9e4 100644
--- a/lib/pages/init_page.dart
+++ b/lib/pages/init_page.dart
@@ -204,7 +204,7 @@ class _InitPageState extends State<InitPage> {
bool autoUpdate =
setting.get(SettingBoxKey.autoUpdate, defaultValue: true);
if (autoUpdate) {
- Modular.get<MyController>().checkUpdata(type: 'auto');
+ Modular.get<MyController>().checkUpdate(type: 'auto');
}
}
}
diff --git a/lib/pages/my/my_controller.dart b/lib/pages/my/my_controller.dart
index d9606d68..955bdcca 100644
--- a/lib/pages/my/my_controller.dart
+++ b/lib/pages/my/my_controller.dart
@@ -1,13 +1,10 @@
import 'package:kazumi/bean/dialog/dialog_helper.dart';
-import 'package:kazumi/utils/utils.dart';
-import 'package:flutter/material.dart';
-import 'package:kazumi/request/api.dart';
-import 'package:url_launcher/url_launcher.dart';
import 'package:logger/logger.dart';
import 'package:kazumi/utils/logger.dart';
import 'package:mobx/mobx.dart';
import 'package:kazumi/utils/storage.dart';
import 'package:hive/hive.dart';
+import 'package:kazumi/utils/auto_updater.dart';
part 'my_controller.g.dart';
@@ -68,42 +65,23 @@ abstract class _MyController with Store {
GStorage.shieldList.flush();
}
- Future<bool> checkUpdata({String type = 'manual'}) async {
- Utils.latest().then((value) {
- if (Api.version == value) {
- if (type == 'manual') {
- KazumiDialog.showToast(message: '当前已经是最新版本!');
- }
+ Future<bool> checkUpdate({String type = 'manual'}) async {
+ try {
+ final autoUpdater = AutoUpdater();
+
+ if (type == 'manual') {
+ await autoUpdater.manualCheckForUpdates();
} else {
- KazumiDialog.show(
- builder: (context) {
- return AlertDialog(
- title: Text('发现新版本 $value'),
- actions: [
- TextButton(
- onPressed: () => KazumiDialog.dismiss(),
- child: Text(
- '稍后',
- style:
- TextStyle(color: Theme.of(context).colorScheme.outline),
- ),
- ),
- TextButton(
- onPressed: () =>
- launchUrl(Uri.parse("${Api.sourceUrl}/releases/latest")),
- child: const Text('Github'),
- ),
- ],
- );
- },
- );
+ await autoUpdater.autoCheckForUpdates();
}
- }).catchError((err) {
+
+ return true;
+ } catch (err) {
KazumiLogger().log(Level.error, '检查更新失败 ${err.toString()}');
if (type == 'manual') {
- KazumiDialog.showToast(message: '当前是最新版本!');
+ KazumiDialog.showToast(message: '检查更新失败,请稍后重试');
}
- });
- return true;
+ return false;
+ }
}
}
diff --git a/lib/pages/settings/settings_module.dart b/lib/pages/settings/settings_module.dart
index 80f60fec..0d192f46 100644
--- a/lib/pages/settings/settings_module.dart
+++ b/lib/pages/settings/settings_module.dart
@@ -8,6 +8,7 @@ import 'package:kazumi/pages/settings/player_settings.dart';
import 'package:kazumi/pages/settings/displaymode_settings.dart';
import 'package:kazumi/pages/settings/decoder_settings.dart';
import 'package:kazumi/pages/settings/super_resolution_settings.dart';
+import 'package:kazumi/pages/settings/update_page.dart';
import 'package:kazumi/pages/webdav_editor/webdav_module.dart';
class SettingsModule extends Module {
@@ -22,6 +23,7 @@ class SettingsModule extends Module {
r.child("/player/decoder", child: (_) => const DecoderSettings());
// r.child("/other", child: (_) => const OtherSettingsPage());
r.child("/player/super", child: (_) => const SuperResolutionSettings());
+ r.child("/update", child: (_) => const UpdatePage());
r.module("/webdav", module: WebDavModule());
r.module("/about", module: AboutModule());
r.module("/plugin", module: PluginModule());
diff --git a/lib/pages/settings/update_page.dart b/lib/pages/settings/update_page.dart
new file mode 100644
index 00000000..8ae4eea9
--- /dev/null
+++ b/lib/pages/settings/update_page.dart
@@ -0,0 +1,147 @@
+import 'package:flutter/material.dart';
+import 'package:kazumi/bean/appbar/sys_app_bar.dart';
+import 'package:kazumi/bean/dialog/dialog_helper.dart';
+import 'package:kazumi/request/api.dart';
+import 'package:kazumi/utils/auto_updater.dart';
+import 'package:kazumi/utils/storage.dart';
+import 'package:hive/hive.dart';
+import 'package:card_settings_ui/card_settings_ui.dart';
+
+class UpdatePage extends StatefulWidget {
+ const UpdatePage({super.key});
+
+ @override
+ State<UpdatePage> createState() => _UpdatePageState();
+}
+
+class _UpdatePageState extends State<UpdatePage> {
+ Box setting = GStorage.setting;
+ late bool autoUpdate;
+ bool isChecking = false;
+ UpdateInfo? latestUpdate;
+
+ @override
+ void initState() {
+ super.initState();
+ autoUpdate = setting.get(SettingBoxKey.autoUpdate, defaultValue: true);
+ _checkForUpdates();
+ }
+
+ Future<void> _checkForUpdates() async {
+ if (isChecking) return;
+
+ setState(() {
+ isChecking = true;
+ });
+
+ try {
+ final autoUpdater = AutoUpdater();
+ final updateInfo = await autoUpdater.checkForUpdates();
+
+ setState(() {
+ latestUpdate = updateInfo;
+ isChecking = false;
+ });
+ } catch (e) {
+ setState(() {
+ isChecking = false;
+ });
+ KazumiDialog.showToast(message: '检查更新失败');
+ }
+ }
+
+ void _manualCheckUpdate() async {
+ final autoUpdater = AutoUpdater();
+ await autoUpdater.manualCheckForUpdates();
+ // 检查完成后刷新状态
+ _checkForUpdates();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return Scaffold(
+ appBar: const SysAppBar(title: Text('应用更新')),
+ body: Center(
+ child: SizedBox(
+ width: (MediaQuery.of(context).size.width > 1000) ? 1000 : null,
+ child: SettingsList(
+ sections: [
+ SettingsSection(
+ title: const Text('当前版本'),
+ tiles: [
+ SettingsTile.navigation(
+ title: const Text('版本号'),
+ value: Text(Api.version),
+ leading: const Icon(Icons.info_outline),
+ onPressed: null,
+ ),
+ SettingsTile.navigation(
+ title: const Text('更新状态'),
+ value: _buildUpdateStatusWidget(),
+ leading: const Icon(Icons.update),
+ onPressed: null,
+ ),
+ ],
+ ),
+ SettingsSection(
+ title: const Text('更新设置'),
+ tiles: [
+ SettingsTile.navigation(
+ onPressed: (_) => _manualCheckUpdate(),
+ title: const Text('检查更新'),
+ description: const Text('立即检查是否有新版本'),
+ leading: const Icon(Icons.refresh),
+ trailing: isChecking
+ ? const SizedBox(
+ width: 16,
+ height: 16,
+ child: CircularProgressIndicator(strokeWidth: 2),
+ )
+ : SizedBox.shrink(),
+ ),
+ ],
+ ),
+ if (latestUpdate != null) ...[
+ SettingsSection(
+ title: const Text('可用更新'),
+ tiles: [
+ SettingsTile.navigation(
+ title: Text('新版本 ${latestUpdate!.version}'),
+ leading: const Icon(Icons.system_update),
+ onPressed: (_) {
+ _manualCheckUpdate();
+ },
+ ),
+ ],
+ ),
+ ],
+ ],
+ ),
+ ),
+ ),
+ );
+ }
+
+ Widget _buildUpdateStatusWidget() {
+ if (isChecking) {
+ return const Row(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ SizedBox(
+ width: 12,
+ height: 12,
+ child: CircularProgressIndicator(strokeWidth: 2),
+ ),
+ SizedBox(width: 8),
+ Text('检查中...'),
+ ],
+ );
+ }
+
+ if (latestUpdate != null) {
+ return const Text('有新版本可用');
+ }
+
+ return const Text('已是最新版本');
+ }
+}
diff --git a/lib/utils/auto_updater.dart b/lib/utils/auto_updater.dart
new file mode 100644
index 00000000..87e3e20d
--- /dev/null
+++ b/lib/utils/auto_updater.dart
@@ -0,0 +1,756 @@
+import 'dart:io';
+import 'package:dio/dio.dart';
+import 'package:flutter/material.dart';
+import 'package:hive/hive.dart';
+import 'package:kazumi/bean/dialog/dialog_helper.dart';
+import 'package:kazumi/request/api.dart';
+import 'package:kazumi/utils/logger.dart';
+import 'package:kazumi/utils/storage.dart';
+import 'package:kazumi/utils/utils.dart';
+import 'package:logger/logger.dart';
+import 'package:open_filex/open_filex.dart';
+import 'package:path_provider/path_provider.dart';
+import 'package:url_launcher/url_launcher.dart';
+
+/// 安装类型枚举
+enum InstallationType {
+ windowsMsix, // Kazumi_windows_1.7.5.msix
+ windowsPortable, // Kazumi_windows_1.7.5.zip
+ linuxDeb, // Kazumi_linux_1.7.5_amd64.deb
+ linuxTar, // Kazumi_linux_1.7.5_amd64.tar.gz
+ macosDmg, // Kazumi_macos_1.7.5.dmg
+ androidApk, // Kazumi_android_1.7.5.apk
+ ios, // iOS App
+ unknown,
+}
+
+/// 更新信息类
+class UpdateInfo {
+ final String version;
+ final String description;
+ final String downloadUrl;
+ final String releaseNotes;
+ final String publishedAt;
+ final InstallationType? installationType;
+ final List<InstallationType> availableInstallationTypes;
+ final List<dynamic> assets;
+
+ UpdateInfo({
+ required this.version,
+ required this.description,
+ required this.downloadUrl,
+ required this.releaseNotes,
+ required this.publishedAt,
+ this.installationType,
+ this.availableInstallationTypes = const [],
+ this.assets = const [],
+ });
+
+ /// 获取默认的安装类型(第一个可用类型)
+ InstallationType get recommendedInstallationType {
+ if (availableInstallationTypes.isNotEmpty) {
+ return availableInstallationTypes.first;
+ }
+ return installationType ?? InstallationType.unknown;
+ }
+}
+
+class AutoUpdater {
+ static final AutoUpdater _instance = AutoUpdater._internal();
+
+ factory AutoUpdater() => _instance;
+
+ AutoUpdater._internal();
+
+ final Dio _dio = Dio();
+
+ Box get setting => GStorage.setting;
+
+ /// 检测所有可能的安装类型
+ Future<List<InstallationType>> _detectAvailableInstallationTypes() async {
+ List<InstallationType> availableTypes = [];
+
+ try {
+ if (Platform.isWindows) {
+ // Windows 平台支持 MSIX 和 ZIP 便携版
+ availableTypes.add(InstallationType.windowsMsix);
+ availableTypes.add(InstallationType.windowsPortable);
+ } else if (Platform.isLinux) {
+ // Linux 平台支持 DEB 和 TAR.GZ
+ availableTypes.add(InstallationType.linuxDeb);
+ availableTypes.add(InstallationType.linuxTar);
+ } else if (Platform.isMacOS) {
+ // macOS 平台支持 DMG
+ availableTypes.add(InstallationType.macosDmg);
+ } else if (Platform.isIOS) {
+ // iOS 平台通过 Github
+ availableTypes.add(InstallationType.ios);
+ } else if (Platform.isAndroid) {
+ // Android 平台支持 APK
+ availableTypes.add(InstallationType.androidApk);
+ }
+ } catch (e) {
+ KazumiLogger().log(Level.warning, '检测安装类型失败: ${e.toString()}');
+ }
+
+ if (availableTypes.isEmpty) {
+ availableTypes.add(InstallationType.unknown);
+ }
+
+ return availableTypes;
+ }
+
+ /// 检查是否有新版本可用
+ Future<UpdateInfo?> checkForUpdates() async {
+ try {
+ final response = await _dio.get(Api.latestApp);
+ final data = response.data;
+
+ if (data == null || !data.containsKey('tag_name')) {
+ throw Exception('无效的响应数据');
+ }
+
+ final remoteVersion = data['tag_name'] as String;
+ final currentVersion = Api.version;
+
+ if (Utils.needUpdate(currentVersion, remoteVersion)) {
+ final availableTypes = await _detectAvailableInstallationTypes();
+
+ return UpdateInfo(
+ version: remoteVersion,
+ description: data['body'] ?? '发现新版本',
+ downloadUrl: '',
+ // 将在用户选择安装类型后填充
+ releaseNotes: data['html_url'] ?? '',
+ publishedAt: data['published_at'] ?? '',
+ installationType: availableTypes.first,
+ // 保持兼容性
+ availableInstallationTypes: availableTypes,
+ assets: data['assets'] ?? [],
+ );
+ }
+
+ return null;
+ } catch (e) {
+ KazumiLogger().log(Level.error, '检查更新失败: ${e.toString()}');
+ rethrow;
+ }
+ }
+
+ /// 自动检查更新(仅在启用自动更新时)
+ Future<void> autoCheckForUpdates() async {
+ final autoUpdate =
+ setting.get(SettingBoxKey.autoUpdate, defaultValue: true);
+ if (!autoUpdate) return;
+
+ try {
+ final updateInfo = await checkForUpdates();
+ if (updateInfo != null) {
+ _showUpdateDialog(updateInfo, isAutoCheck: true);
+ }
+ } catch (e) {
+ // 自动检查失败时不显示错误
+ KazumiLogger().log(Level.warning, '自动检查更新失败: ${e.toString()}');
+ }
+ }
+
+ /// 手动检查更新
+ Future<void> manualCheckForUpdates() async {
+ try {
+ final updateInfo = await checkForUpdates();
+ if (updateInfo != null) {
+ _showUpdateDialog(updateInfo, isAutoCheck: false);
+ } else {
+ KazumiDialog.showToast(message: '当前已经是最新版本!');
+ }
+ } catch (e) {
+ KazumiDialog.showToast(message: '检查更新失败');
+ }
+ }
+
+ /// 显示更新对话框
+ void _showUpdateDialog(UpdateInfo updateInfo, {bool isAutoCheck = false}) {
+ KazumiDialog.show(
+ builder: (context) {
+ return AlertDialog(
+ title: Text('发现新版本 ${updateInfo.version}'),
+ content: SingleChildScrollView(
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Text(updateInfo.description),
+ if (updateInfo.publishedAt.isNotEmpty) ...[
+ const SizedBox(height: 8),
+ Text(
+ '发布时间: ${Utils.formatDate(updateInfo.publishedAt)}',
+ style: Theme.of(context).textTheme.bodySmall,
+ ),
+ ],
+ const SizedBox(height: 8),
+ if (!Platform.isLinux && !Platform.isIOS) ...[
+ Container(
+ padding: const EdgeInsets.all(8),
+ decoration: BoxDecoration(
+ color:
+ Theme.of(context).colorScheme.surfaceContainerHighest,
+ borderRadius: BorderRadius.circular(4),
+ ),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Text(
+ '选择安装类型:',
+ style: Theme.of(context).textTheme.labelSmall,
+ ),
+ const SizedBox(height: 8),
+ ...updateInfo.availableInstallationTypes.map((type) {
+ return Container(
+ margin: const EdgeInsets.symmetric(vertical: 2),
+ child: Material(
+ color: Colors.transparent,
+ child: InkWell(
+ borderRadius: BorderRadius.circular(4),
+ onTap: () {
+ KazumiDialog.dismiss();
+ _downloadUpdateWithType(updateInfo, type);
+ },
+ child: Container(
+ padding: const EdgeInsets.symmetric(
+ horizontal: 12, vertical: 8),
+ decoration: BoxDecoration(
+ border: Border.all(
+ color: Theme.of(context)
+ .colorScheme
+ .outline
+ .withValues(alpha: 0.3),
+ ),
+ borderRadius: BorderRadius.circular(4),
+ ),
+ child: Row(
+ children: [
+ Icon(
+ Icons.download,
+ size: 16,
+ color: Theme.of(context)
+ .colorScheme
+ .primary,
+ ),
+ const SizedBox(width: 8),
+ Expanded(
+ child: Text(
+ _getInstallationTypeDescription(type),
+ style: Theme.of(context)
+ .textTheme
+ .bodySmall,
+ ),
+ ),
+ Icon(
+ Icons.arrow_forward_ios,
+ size: 12,
+ color: Theme.of(context)
+ .colorScheme
+ .outline,
+ ),
+ ],
+ ),
+ ),
+ ),
+ ),
+ );
+ }),
+ ],
+ ),
+ ),
+ ],
+ ],
+ ),
+ ),
+ actions: [
+ if (isAutoCheck)
+ TextButton(
+ onPressed: () {
+ setting.put(SettingBoxKey.autoUpdate, false);
+ KazumiDialog.dismiss();
+ KazumiDialog.showToast(message: '已关闭自动更新');
+ },
+ child: Text(
+ '关闭自动更新',
+ style:
+ TextStyle(color: Theme.of(context).colorScheme.outline),
+ ),
+ ),
+ TextButton(
+ onPressed: () => KazumiDialog.dismiss(),
+ child: Text(
+ '稍后提醒',
+ style: TextStyle(color: Theme.of(context).colorScheme.outline),
+ ),
+ ),
+ if (updateInfo.releaseNotes.isNotEmpty)
+ TextButton(
+ onPressed: () {
+ launchUrl(Uri.parse(updateInfo.releaseNotes),
+ mode: LaunchMode.externalApplication);
+ },
+ child: const Text('查看详情'),
+ ),
+ TextButton(
+ onPressed: () {
+ KazumiDialog.dismiss();
+ // 直接使用第一个可用的安装类型
+ if (updateInfo.availableInstallationTypes.isNotEmpty) {
+ _downloadUpdateWithType(
+ updateInfo, updateInfo.availableInstallationTypes.first);
+ }
+ },
+ child: const Text('立即更新'),
+ ),
+ ],
+ );
+ },
+ );
+ }
+
+ /// 获取安装类型的描述
+ String _getInstallationTypeDescription(InstallationType type) {
+ switch (type) {
+ case InstallationType.windowsMsix:
+ return 'Windows MSIX 包';
+ case InstallationType.windowsPortable:
+ return 'Windows 便携版 (ZIP)';
+ case InstallationType.linuxDeb:
+ return 'Linux DEB 包';
+ case InstallationType.linuxTar:
+ return 'Linux TAR 包';
+ case InstallationType.macosDmg:
+ return 'macOS DMG 镜像';
+ case InstallationType.androidApk:
+ return 'Android APK';
+ case InstallationType.ios:
+ return 'iOS ipa';
+ case InstallationType.unknown:
+ return '未知安装类型';
+ }
+ }
+
+ /// 根据选择的类型下载更新
+ Future<void> _downloadUpdateWithType(
+ UpdateInfo updateInfo, InstallationType selectedType) async {
+ try {
+ // iOS 和 Linux 直接跳转到 Release 页面
+ if (selectedType == InstallationType.ios ||
+ selectedType == InstallationType.linuxDeb ||
+ selectedType == InstallationType.linuxTar) {
+ String releaseUrl = updateInfo.releaseNotes;
+ if (releaseUrl.isEmpty) {
+ releaseUrl = Api.latestApp;
+ }
+ launchUrl(Uri.parse(releaseUrl), mode: LaunchMode.externalApplication);
+ return;
+ }
+
+ final downloadUrl =
+ await _getDownloadUrlForType(updateInfo.assets, selectedType);
+ if (downloadUrl.isEmpty) {
+ KazumiDialog.showToast(
+ message:
+ '没有找到 ${_getInstallationTypeDescription(selectedType)} 的下载链接');
+ return;
+ }
+
+ // 创建一个临时的 UpdateInfo 对象用于下载
+ final downloadInfo = UpdateInfo(
+ version: updateInfo.version,
+ description: updateInfo.description,
+ downloadUrl: downloadUrl,
+ releaseNotes: updateInfo.releaseNotes,
+ publishedAt: updateInfo.publishedAt,
+ installationType: selectedType,
+ availableInstallationTypes: [selectedType],
+ assets: updateInfo.assets,
+ );
+
+ _downloadUpdate(downloadInfo);
+ } catch (e) {
+ KazumiDialog.showToast(message: '下载失败: ${e.toString()}');
+ KazumiLogger().log(Level.error, '下载更新失败: ${e.toString()}');
+ }
+ }
+
+ /// 下载更新
+ Future<void> _downloadUpdate(UpdateInfo updateInfo) async {
+ if (updateInfo.downloadUrl.isEmpty) {
+ KazumiDialog.showToast(message: '没有找到合适的下载链接');
+ return;
+ }
+
+ // 显示下载进度对话框
+ KazumiDialog.show(
+ clickMaskDismiss: false,
+ builder: (context) {
+ return AlertDialog(
+ title: const Text('正在下载更新'),
+ content: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ ValueListenableBuilder<double>(
+ valueListenable: _downloadProgress,
+ builder: (context, value, child) {
+ return Column(
+ children: [
+ LinearProgressIndicator(value: value),
+ const SizedBox(height: 8),
+ Text('${(value * 100).toStringAsFixed(1)}%'),
+ ],
+ );
+ },
+ ),
+ ],
+ ),
+ actions: [
+ TextButton(
+ onPressed: () {
+ _cancelDownload();
+ KazumiDialog.dismiss();
+ },
+ child: const Text('取消'),
+ ),
+ ],
+ );
+ },
+ );
+
+ try {
+ final downloadPath =
+ await _downloadFile(updateInfo.downloadUrl, updateInfo.version);
+
+ // 不自动关闭对话框,而是显示下载完成状态
+ _showDownloadCompleteDialog(downloadPath, updateInfo);
+ } catch (e) {
+ KazumiDialog.dismiss();
+
+ // 显示详细的错误信息
+ String errorMessage = '下载失败';
+ if (e.toString().contains('Permission denied') ||
+ e.toString().contains('Operation not permitted')) {
+ errorMessage = '权限不足,文件已保存到应用临时目录';
+ } else if (e.toString().contains('No space left')) {
+ errorMessage = '磁盘空间不足';
+ } else if (e.toString().contains('Network')) {
+ errorMessage = '网络连接错误';
+ }
+
+ KazumiDialog.show(
+ builder: (context) {
+ return AlertDialog(
+ title: const Text('下载失败'),
+ content: Column(
+ mainAxisSize: MainAxisSize.min,
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Text(errorMessage),
+ const SizedBox(height: 8),
+ Text(
+ '错误详情: ${e.toString()}',
+ style: Theme.of(context).textTheme.bodySmall,
+ ),
+ ],
+ ),
+ actions: [
+ TextButton(
+ onPressed: () => KazumiDialog.dismiss(),
+ child: const Text('确定'),
+ ),
+ TextButton(
+ onPressed: () {
+ KazumiDialog.dismiss();
+ // 重新尝试下载
+ _downloadUpdate(updateInfo);
+ },
+ child: const Text('重试'),
+ ),
+ ],
+ );
+ },
+ );
+
+ KazumiLogger().log(Level.error, '下载更新失败: ${e.toString()}');
+ }
+ }
+
+ final ValueNotifier<double> _downloadProgress = ValueNotifier(0.0);
+ CancelToken? _cancelToken;
+
+ void _cancelDownload() {
+ _cancelToken?.cancel();
+ }
+
+ /// 显示下载完成对话框
+ void _showDownloadCompleteDialog(String filePath, UpdateInfo updateInfo) {
+ // 替换当前的下载进度对话框内容
+ KazumiDialog.dismiss();
+
+ KazumiDialog.show(
+ builder: (context) {
+ return AlertDialog(
+ title: const Text('下载完成'),
+ content: Column(
+ mainAxisSize: MainAxisSize.min,
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Row(
+ children: [
+ Icon(
+ Icons.check_circle,
+ color: Theme.of(context).colorScheme.primary,
+ size: 20,
+ ),
+ const SizedBox(width: 8),
+ Expanded(
+ child: Text('新版本 ${updateInfo.version} 已下载完成'),
+ ),
+ ],
+ ),
+ const SizedBox(height: 12),
+ Text(
+ '⚠️ 安装过程中应用将会退出',
+ style: TextStyle(
+ color: Theme.of(context).colorScheme.error,
+ fontSize: 12,
+ ),
+ ),
+ const SizedBox(height: 12),
+ Container(
+ padding: const EdgeInsets.all(8),
+ decoration: BoxDecoration(
+ color: Theme.of(context).colorScheme.surfaceContainerHighest,
+ borderRadius: BorderRadius.circular(4),
+ ),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Text(
+ '文件位置:',
+ style: Theme.of(context).textTheme.labelSmall,
+ ),
+ const SizedBox(height: 4),
+ SelectableText(
+ filePath,
+ style: Theme.of(context).textTheme.bodySmall?.copyWith(
+ fontFamily: 'monospace',
+ ),
+ ),
+ ],
+ ),
+ ),
+ ],
+ ),
+ actions: [
+ TextButton(
+ onPressed: () => KazumiDialog.dismiss(),
+ child: Text(
+ '稍后安装',
+ style: TextStyle(color: Theme.of(context).colorScheme.outline),
+ ),
+ ),
+ TextButton(
+ onPressed: () {
+ // 在文件管理器中显示文件
+ _revealInFileManager(filePath);
+ },
+ child: const Text('打开文件夹'),
+ ),
+ TextButton(
+ onPressed: () {
+ KazumiDialog.dismiss();
+ _installUpdate(
+ filePath, updateInfo.recommendedInstallationType);
+ },
+ child: const Text('立即安装'),
+ ),
+ ],
+ );
+ },
+ );
+ }
+
+ /// 下载文件
+ Future<String> _downloadFile(String url, String version) async {
+ final fileName = _getFileNameFromUrl(url, version);
+
+ // 统一使用临时目录
+ final tempDir = await getTemporaryDirectory();
+ final filePath = '${tempDir.path}/$fileName';
+ final file = File(filePath);
+
+ // 检查文件是否已存在
+ if (await file.exists()) {
+ try {
+ // 获取远程文件大小来验证本地文件是否完整
+ final headResponse = await _dio.head(url);
+ final remoteSize =
+ int.tryParse(headResponse.headers.value('content-length') ?? '0') ??
+ 0;
+ final localSize = await file.length();
+
+ if (remoteSize > 0 && localSize == remoteSize) {
+ // 文件已存在且大小匹配,直接返回
+ KazumiLogger().log(Level.info, '文件已存在且完整,跳过下载: $filePath');
+ _downloadProgress.value = 1.0;
+ return filePath;
+ } else if (localSize > 0) {
+ // 文件存在但不完整,删除后重新下载
+ KazumiLogger().log(
+ Level.info, '检测到不完整文件 (本地: $localSize, 远程: $remoteSize),删除后重新下载');
+ await file.delete();
+ }
+ } catch (e) {
+ // 如果获取远程文件信息失败,删除本地文件重新下载
+ KazumiLogger().log(Level.warning, '无法验证文件完整性,删除后重新下载: ${e.toString()}');
+ if (await file.exists()) {
+ await file.delete();
+ }
+ }
+ }
+
+ _cancelToken = CancelToken();
+
+ await _dio.download(
+ url,
+ filePath,
+ cancelToken: _cancelToken,
+ onReceiveProgress: (received, total) {
+ if (total > 0) {
+ _downloadProgress.value = received / total;
+ }
+ },
+ );
+
+ return filePath;
+ }
+
+ /// 安装更新
+ void _installUpdate(
+ String filePath, InstallationType installationType) async {
+ try {
+ // 显示准备退出的提示
+ KazumiDialog.showToast(message: '准备安装更新,应用即将退出...');
+
+ await Future.delayed(const Duration(seconds: 2));
+
+ if (Platform.isWindows) {
+ if (installationType == InstallationType.windowsMsix) {
+ final Uri fileUri = Uri.file(filePath);
+ if (await canLaunchUrl(fileUri)) {
+ await launchUrl(fileUri);
+ } else {
+ throw 'Could not launch $fileUri';
+ }
+ } else {
+ await Process.start('explorer.exe', [filePath], runInShell: true);
+ }
+ } else if (Platform.isMacOS) {
+ if (filePath.endsWith('.dmg')) {
+ await Process.start('open', [filePath]);
+ exit(0);
+ }
+ } else if (Platform.isAndroid) {
+ final result = await OpenFilex.open(filePath);
+ if (result.type != ResultType.done) {
+ KazumiDialog.showToast(message: '无法打开安装文件: ${result.message}');
+ return;
+ }
+ }
+ await Future.delayed(const Duration(seconds: 1));
+ exit(0);
+ } catch (e) {
+ KazumiDialog.showToast(message: '启动安装程序失败: ${e.toString()}');
+ KazumiLogger().log(Level.error, '启动安装程序失败: ${e.toString()}');
+ }
+ }
+
+ /// 在文件管理器中显示文件
+ void _revealInFileManager(String filePath) async {
+ try {
+ if (Platform.isWindows) {
+ await Process.start('explorer.exe', ['/select,', filePath],
+ runInShell: true);
+ } else if (Platform.isMacOS) {
+ await Process.start('open', ['-R', filePath]);
+ } else if (Platform.isLinux) {
+ // 尝试打开包含文件的文件夹
+ final directory = Directory(filePath).parent.path;
+ await Process.start('xdg-open', [directory]);
+ }
+ KazumiDialog.dismiss();
+ } catch (e) {
+ KazumiDialog.showToast(message: '无法打开文件管理器');
+ KazumiLogger().log(Level.warning, '打开文件管理器失败: ${e.toString()}');
+ }
+ }
+
+ /// 根据安装类型获取下载链接
+ Future<String> _getDownloadUrlForType(
+ List<dynamic> assets, InstallationType type) async {
+ final patterns = _getFilePatterns(type);
+
+ for (final asset in assets) {
+ final name = asset['name'] as String? ?? '';
+ final downloadUrl = asset['browser_download_url'] as String? ?? '';
+
+ for (final pattern in patterns) {
+ if (name.toLowerCase().contains(pattern.toLowerCase()) &&
+ downloadUrl.isNotEmpty) {
+ return downloadUrl;
+ }
+ }
+ }
+
+ return '';
+ }
+
+ /// 获取合适的下载链接
+ /// 根据安装类型获取文件名模式
+ List<String> _getFilePatterns(InstallationType installationType) {
+ switch (installationType) {
+ case InstallationType.windowsMsix:
+ return ['windows', '.msix'];
+ case InstallationType.windowsPortable:
+ return ['windows', '.zip'];
+ case InstallationType.macosDmg:
+ return ['macos', '.dmg'];
+ case InstallationType.androidApk:
+ return ['android', '.apk'];
+ // 以下类型直接跳转到 GitHub Release 页面,不需要下载文件
+ case InstallationType.linuxDeb:
+ case InstallationType.linuxTar:
+ case InstallationType.ios:
+ case InstallationType.unknown:
+ return [];
+ }
+ }
+
+ /// 从URL获取文件名
+ String _getFileNameFromUrl(String url, String version) {
+ final uri = Uri.parse(url);
+ final fileName = uri.pathSegments.last;
+
+ if (fileName.isNotEmpty) {
+ return fileName;
+ }
+
+ // 回退方案
+ String extension = '';
+ if (Platform.isWindows) {
+ extension = '.msix';
+ } else if (Platform.isMacOS) {
+ extension = '.dmg';
+ } else if (Platform.isLinux) {
+ extension = '.deb';
+ } else if (Platform.isAndroid) {
+ extension = '.apk';
+ }
+ return 'Kazumi-$version$extension';
+ }
+}
diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart
index ec773c18..7a6d6a0d 100644
--- a/lib/utils/utils.dart
+++ b/lib/utils/utils.dart
@@ -522,4 +522,16 @@ class Utils {
var digest = sha256.convert(bytes);
return base64Encode(digest.bytes);
}
+
+
+ /// 格式化日期
+ /// eg: 2025-07-27T09:14:12Z -> 2025-07-27
+ static String formatDate(String dateString) {
+ try {
+ final date = DateTime.parse(dateString);
+ return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
+ } catch (e) {
+ return dateString;
+ }
+ }
}
diff --git a/pubspec.yaml b/pubspec.yaml
index 9f316b32..26ab00e0 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -111,6 +111,8 @@ dependencies:
flutter_inappwebview_ios: ^1.1.2
flutter_inappwebview_macos: ^1.1.2
flutter_inappwebview_android: ^1.1.3
+ upgrader: ^11.4.0
+ open_filex: ^4.7.0
dependency_overrides:
media_kit_libs_linux: