96 lines
2.7 KiB
Dart
96 lines
2.7 KiB
Dart
import 'dart:async';
|
|
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;
|
|
}
|
|
}
|
|
|
|
Timer? _updateLaterTimer;
|
|
|
|
static final UpgradeAuxiliaryUtils _instance = UpgradeAuxiliaryUtils._();
|
|
|
|
/// 获取 UpgradeService 单例
|
|
static UpgradeAuxiliaryUtils get instance => _instance;
|
|
|
|
/// 启动版本检查
|
|
/// reUpdateLater 是否恢复重新更新
|
|
/// resetTimeMinute 重置时间 默认30分钟
|
|
Future<void> initiateVersionCheck(
|
|
BuildContext context, {
|
|
required Future<AppUpgradeVersion?> Function(int upType) future,
|
|
UpgradeConfig? config,
|
|
bool reUpdateLater = true,
|
|
int resetTimeMinute = 30,
|
|
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;
|
|
|
|
/// 30分钟 后重置,可以重新弹出
|
|
if (reUpdateLater) {
|
|
_updateLaterTimer?.cancel();
|
|
_updateLaterTimer = Timer(Duration(seconds: 60 * resetTimeMinute), () {
|
|
_updateLater = false;
|
|
});
|
|
}
|
|
onUpdateLater?.call();
|
|
},
|
|
config: config ?? (kDebugMode ? UpgradeConfig.development : UpgradeConfig.production),
|
|
);
|
|
}
|
|
}
|