重整多余的UI组件和未定功能的逻辑链路的修正
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/core/app/app_user_state.dart';
|
||||
import 'package:maibu_satabot_v2/core/domain/entities/user_entity.dart';
|
||||
import 'package:open_file/open_file.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
@@ -11,19 +15,52 @@ import 'update_state.dart';
|
||||
/// 更新 Cubit
|
||||
class UpdateCubit extends Cubit<UpdateState> {
|
||||
final VersionCheckService _versionService;
|
||||
final Logger _logger;
|
||||
final AppUserCubit _users;
|
||||
final Logger _logger = Logger();
|
||||
late final StreamSubscription<AppUserState> _adminChanges;
|
||||
UserEntity? _updateOwner;
|
||||
|
||||
UpdateCubit(this._versionService)
|
||||
: _logger = Logger(),
|
||||
super(UpdateInitial());
|
||||
UpdateCubit(this._versionService, this._users) : super(const UpdateInitial()) {
|
||||
_adminChanges = _users.stream.listen((_) {
|
||||
final owner = _updateOwner;
|
||||
if (owner != null && !_canContinue(owner)) {
|
||||
_updateOwner = null;
|
||||
emit(const UpdateInitial());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
UserEntity? get _adminUser {
|
||||
final user = _users.state.user;
|
||||
return user?.isAdmin == true ? user : null;
|
||||
}
|
||||
|
||||
UserEntity? _beginUpdate() {
|
||||
if (isClosed) return null;
|
||||
final admin = _adminUser;
|
||||
if (admin != null) _updateOwner = admin;
|
||||
return admin;
|
||||
}
|
||||
|
||||
bool _canContinue(UserEntity admin) =>
|
||||
!isClosed && identical(_updateOwner, admin) && identical(_adminUser, admin);
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
await _adminChanges.cancel();
|
||||
await super.close();
|
||||
}
|
||||
|
||||
/// 检查版本更新
|
||||
Future<void> checkUpdate() async {
|
||||
emit(UpdateChecking());
|
||||
final admin = _beginUpdate();
|
||||
if (admin == null) return;
|
||||
emit(const UpdateChecking());
|
||||
|
||||
try {
|
||||
// 从 pubspec.yaml 读取版本信息
|
||||
final String versionString = await rootBundle.loadString('pubspec.yaml');
|
||||
if (!_canContinue(admin)) return;
|
||||
final versionMatch = RegExp(r'version:\s*(\d+\.\d+\.\d+)\+(\d+)').firstMatch(versionString);
|
||||
|
||||
final currentVersion = versionMatch?.group(1) ?? '1.0.0';
|
||||
@@ -37,6 +74,7 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
currentVersionCode: currentVersionCode,
|
||||
);
|
||||
|
||||
if (!_canContinue(admin)) return;
|
||||
if (versionInfo == null) {
|
||||
_logger.i('✅ 已是最新版本');
|
||||
emit(UpdateUpToDate());
|
||||
@@ -49,6 +87,7 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
|
||||
emit(UpdateAvailable(versionInfo));
|
||||
} catch (e) {
|
||||
if (!_canContinue(admin)) return;
|
||||
_logger.e('❌ 检查更新失败: $e');
|
||||
emit(UpdateFailure('检查更新失败: $e'));
|
||||
}
|
||||
@@ -56,6 +95,8 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
|
||||
/// 应用差量补丁
|
||||
Future<void> applyPatch(String patchUrl, String version, int targetVersionCode, {String? md5}) async {
|
||||
final admin = _beginUpdate();
|
||||
if (admin == null) return;
|
||||
emit(const UpdateDownloading(0.0, isPatch: true));
|
||||
|
||||
try {
|
||||
@@ -67,11 +108,13 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
targetVersionCode: targetVersionCode,
|
||||
md5: md5,
|
||||
onProgress: (progress) {
|
||||
// 🔥 实时更新下载进度
|
||||
emit(UpdateDownloading(progress, isPatch: true));
|
||||
if (_canContinue(admin)) {
|
||||
emit(UpdateDownloading(progress, isPatch: true));
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
if (!_canContinue(admin)) return;
|
||||
if (success) {
|
||||
emit(const UpdateInstalling(isPatch: true));
|
||||
emit(UpdateSuccess());
|
||||
@@ -79,6 +122,7 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
emit(const UpdateFailure('补丁应用失败'));
|
||||
}
|
||||
} catch (e) {
|
||||
if (!_canContinue(admin)) return;
|
||||
_logger.e('❌ 应用补丁异常: $e');
|
||||
emit(UpdateFailure('应用补丁失败: $e'));
|
||||
}
|
||||
@@ -86,15 +130,20 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
|
||||
/// 自动下载并安装 APK(App 内下载)
|
||||
Future<void> downloadAndInstallApk(String apkUrl) async {
|
||||
final admin = _beginUpdate();
|
||||
if (admin == null) return;
|
||||
emit(const UpdateDownloading(0.0, isPatch: false));
|
||||
|
||||
try {
|
||||
_logger.i('⬇️ 开始下载完整 APK');
|
||||
|
||||
final apkPath = await _versionService.downloadApk(apkUrl, (progress) {
|
||||
emit(UpdateDownloading(progress, isPatch: false));
|
||||
if (_canContinue(admin)) {
|
||||
emit(UpdateDownloading(progress, isPatch: false));
|
||||
}
|
||||
});
|
||||
|
||||
if (!_canContinue(admin)) return;
|
||||
if (apkPath != null) {
|
||||
_logger.i('✅ APK 下载完成: $apkPath');
|
||||
// 🔥 关键:先清理版本号记录,并等待用户看到提示
|
||||
@@ -105,6 +154,7 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
|
||||
// 🔥 短暂延迟,让用户看到清理完成的提示
|
||||
await Future.delayed(const Duration(milliseconds: 800));
|
||||
if (!_canContinue(admin)) return;
|
||||
|
||||
// 🔥 直接调起系统安装页面,不再等待用户点击
|
||||
_logger.i('📦 自动调起系统安装页面');
|
||||
@@ -113,6 +163,7 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
emit(const UpdateFailure('APK 下载失败'));
|
||||
}
|
||||
} catch (e) {
|
||||
if (!_canContinue(admin)) return;
|
||||
_logger.e('❌ 下载安装 APK 异常: $e');
|
||||
emit(UpdateFailure('下载安装 APK 失败: $e'));
|
||||
}
|
||||
@@ -120,6 +171,8 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
|
||||
/// 触发系统安装页面
|
||||
Future<void> triggerInstall(String apkPath) async {
|
||||
final admin = _beginUpdate();
|
||||
if (admin == null) return;
|
||||
emit(const UpdateInstalling(isPatch: false));
|
||||
|
||||
try {
|
||||
@@ -129,6 +182,7 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
// 🔥 验证文件是否存在
|
||||
final file = File(apkPath);
|
||||
if (!await file.exists()) {
|
||||
if (!_canContinue(admin)) return;
|
||||
_logger.e('❌ APK 文件不存在: $apkPath');
|
||||
emit(UpdateFailure('APK 文件不存在,请重新下载'));
|
||||
return;
|
||||
@@ -139,6 +193,7 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
|
||||
// 🔥 关键修复:将APK复制到外部存储,避免某些Android版本的安装限制
|
||||
final externalDir = await getExternalStorageDirectory();
|
||||
if (!_canContinue(admin)) return;
|
||||
String finalApkPath = apkPath;
|
||||
|
||||
if (externalDir != null) {
|
||||
@@ -157,7 +212,9 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
// 打开 APK 文件,触发系统安装界面
|
||||
_logger.i('🚀 调用 OpenFile.open...');
|
||||
_logger.i('🚀 最终APK路径: $finalApkPath');
|
||||
if (!_canContinue(admin)) return;
|
||||
final result = await OpenFile.open(finalApkPath);
|
||||
if (!_canContinue(admin)) return;
|
||||
_logger.i('📦 安装结果: ${result.message} (type: ${result.type})');
|
||||
|
||||
// 如果用户取消安装,显示提示信息并保留文件路径
|
||||
@@ -180,6 +237,7 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
emit(UpdateSuccess());
|
||||
}
|
||||
} catch (e) {
|
||||
if (!_canContinue(admin)) return;
|
||||
_logger.e('❌ 调起安装页面异常: $e');
|
||||
emit(UpdateFailure('调起安装页面失败: $e'));
|
||||
}
|
||||
@@ -187,24 +245,29 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
|
||||
/// 整包更新:浏览器下载 + 引导弹窗
|
||||
Future<void> fullApkUpdateWithBrowser(String apkUrl) async {
|
||||
final admin = _beginUpdate();
|
||||
if (admin == null) return;
|
||||
try {
|
||||
_logger.i('🌐 整包更新:打开浏览器下载');
|
||||
|
||||
final uri = Uri.parse(apkUrl);
|
||||
if (await canLaunchUrl(uri)) {
|
||||
final canLaunch = await canLaunchUrl(uri);
|
||||
if (!_canContinue(admin)) return;
|
||||
if (canLaunch) {
|
||||
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
||||
_logger.i('✅ 已打开浏览器下载');
|
||||
|
||||
// 🔥 整包更新后,立即清除补丁版本记录(避免死循环)
|
||||
await _versionService.clearPatchVersionInfo();
|
||||
_logger.i('🗑️ 已清除补丁版本记录');
|
||||
|
||||
emit(UpdateSuccess());
|
||||
|
||||
if (_canContinue(admin)) emit(const UpdateSuccess());
|
||||
} else {
|
||||
_logger.e('❌ 无法打开浏览器');
|
||||
emit(const UpdateFailure('无法打开浏览器'));
|
||||
}
|
||||
} catch (e) {
|
||||
if (!_canContinue(admin)) return;
|
||||
_logger.e('❌ 打开浏览器异常: $e');
|
||||
emit(UpdateFailure('打开浏览器失败: $e'));
|
||||
}
|
||||
@@ -212,6 +275,7 @@ class UpdateCubit extends Cubit<UpdateState> {
|
||||
|
||||
/// 取消更新
|
||||
void cancelUpdate() {
|
||||
_updateOwner = null;
|
||||
_logger.i('❌ 用户取消更新');
|
||||
emit(UpdateInitial());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user