处理辅助函数
This commit is contained in:
parent
e30aef8b44
commit
af52743e94
|
|
@ -0,0 +1,80 @@
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:flutter/widgets.dart';
|
||||||
|
import 'package:yx_app_upgrade_flutter/app_upgrade_simple.dart';
|
||||||
|
import 'package:yx_app_upgrade_flutter/models/app_upgrade_version.dart';
|
||||||
|
|
||||||
|
/// App 升级服务
|
||||||
|
class UpgradeAuxiliaryUtils {
|
||||||
|
/// 设备类型 1:安卓, 2:IOS
|
||||||
|
late final int? deviceType;
|
||||||
|
|
||||||
|
/// 是否已经点击稍后更新按钮
|
||||||
|
bool _updateLater = false;
|
||||||
|
|
||||||
|
/// 版本检查是否已完成
|
||||||
|
bool completed = false;
|
||||||
|
|
||||||
|
/// 上次检查的版本信息
|
||||||
|
AppUpgradeVersion? _lastVersion;
|
||||||
|
|
||||||
|
UpgradeAuxiliaryUtils._() {
|
||||||
|
if (Platform.isAndroid) {
|
||||||
|
deviceType = 1;
|
||||||
|
} else if (Platform.isIOS) {
|
||||||
|
deviceType = 2;
|
||||||
|
} else {
|
||||||
|
deviceType = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static final UpgradeAuxiliaryUtils _instance = UpgradeAuxiliaryUtils._();
|
||||||
|
|
||||||
|
/// 获取 UpgradeService 单例
|
||||||
|
static UpgradeAuxiliaryUtils get instance => _instance;
|
||||||
|
|
||||||
|
/// 启动版本检查
|
||||||
|
Future<void> initiateVersionCheck(
|
||||||
|
BuildContext context, {
|
||||||
|
required Future<AppUpgradeVersion?> Function(int upType) future,
|
||||||
|
UpgradeConfig? config,
|
||||||
|
bool? showNoUpdateToast,
|
||||||
|
bool? autoInstall,
|
||||||
|
VoidCallback? onComplete,
|
||||||
|
VoidCallback? onUpdateLater,
|
||||||
|
}) async {
|
||||||
|
if (deviceType == null || completed) return;
|
||||||
|
completed = true;
|
||||||
|
await AppUpgradeSimple.instance.checkUpdate(
|
||||||
|
context: context,
|
||||||
|
future: () async {
|
||||||
|
final result = await future(deviceType!);
|
||||||
|
|
||||||
|
// 如果用户选择了"稍后更新",且版本未发生变化,则跳过本次检查
|
||||||
|
if (_updateLater &&
|
||||||
|
_lastVersion != null &&
|
||||||
|
result != null &&
|
||||||
|
result.versionName == _lastVersion!.versionName &&
|
||||||
|
result.versionBuildNumber == _lastVersion!.versionBuildNumber) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _lastVersion = result;
|
||||||
|
},
|
||||||
|
showNoUpdateToast: showNoUpdateToast,
|
||||||
|
autoInstall: autoInstall,
|
||||||
|
onComplete: () {
|
||||||
|
// 版本检查完成
|
||||||
|
debugPrint("更新插件执行完成....:");
|
||||||
|
completed = false;
|
||||||
|
onComplete?.call();
|
||||||
|
},
|
||||||
|
onUpdateLater: () {
|
||||||
|
_updateLater = true;
|
||||||
|
onUpdateLater?.call();
|
||||||
|
},
|
||||||
|
config: config ?? (kDebugMode ? UpgradeConfig.development : UpgradeConfig.production),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,3 +2,4 @@ library;
|
||||||
|
|
||||||
export 'package:yx_app_upgrade_flutter/app_upgrade_simple.dart';
|
export 'package:yx_app_upgrade_flutter/app_upgrade_simple.dart';
|
||||||
export 'package:yx_app_upgrade_flutter/models/index.dart';
|
export 'package:yx_app_upgrade_flutter/models/index.dart';
|
||||||
|
export 'package:yx_app_upgrade_flutter/upgrade_auxiliary_utils.dart';
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue