78 lines
2.1 KiB
Dart
78 lines
2.1 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter/widgets.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:making_school_asignment_app/common/job/app_version.dart';
|
||
import 'package:making_school_asignment_app/common/mixins/request_tool_mixin.dart';
|
||
|
||
import 'package:yx_app_upgrade_flutter/yx_app_upgrade_flutter.dart';
|
||
|
||
class UpgradeLogic extends GetxService with RequestToolMixin {
|
||
Timer? _timer;
|
||
|
||
@override
|
||
void onReady() {
|
||
/// 首次检查更新
|
||
Future.delayed(const Duration(seconds: 10)).then(
|
||
(value) => checkUpdate(Get.context!),
|
||
);
|
||
|
||
/// 定时检查更新
|
||
_timer?.cancel();
|
||
_timer = Timer.periodic(
|
||
const Duration(seconds: 40),
|
||
(_) => checkUpdate(Get.context!),
|
||
);
|
||
super.onReady();
|
||
}
|
||
|
||
@override
|
||
void onClose() {
|
||
_timer?.cancel();
|
||
super.onClose();
|
||
}
|
||
|
||
void checkUpdate(BuildContext context) {
|
||
UpgradeAuxiliaryUtils.instance.initiateVersionCheck(
|
||
context,
|
||
future: (int upType) async {
|
||
// upType: 1 为 Android, 2 为 iOS
|
||
try {
|
||
AppVersion? result = await getClient().getLastAppVersion(
|
||
'making_school_asignment_app',
|
||
upType,
|
||
);
|
||
if (result != null) return _convertToAppUpgradeVersion(result);
|
||
} catch (e, stack) {
|
||
debugPrint('fetchLatestUpgradeVersion error: $e\n$stack');
|
||
}
|
||
return null;
|
||
},
|
||
);
|
||
}
|
||
|
||
/// 将 UpdateappResult 转换为 AppUpgradeVersion
|
||
AppUpgradeVersion? _convertToAppUpgradeVersion(AppVersion? model) {
|
||
if (model == null) return null;
|
||
|
||
// 将文件大小从 KB 转换为字节
|
||
final filePath =
|
||
(model.appFileUrl?.isNotEmpty ?? false) ? model.appFileUrl : null;
|
||
|
||
return AppUpgradeVersion(
|
||
versionName: model.version,
|
||
// versionBuildNumber: model.version,
|
||
isForce: false,
|
||
updateContent: model.description ?? '修复BUG,优化体验',
|
||
downloadUrl: filePath,
|
||
appStoreUrl: filePath,
|
||
// apkSize: apkSizeBytes,
|
||
supportedMethods: [
|
||
AppUpgradeMethod.market,
|
||
AppUpgradeMethod.inApp,
|
||
AppUpgradeMethod.browser,
|
||
],
|
||
);
|
||
}
|
||
}
|