yx_app_upgrade_flutter/lib/models/upgrade_info.dart

162 lines
5.2 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'app_market.dart';
import 'app_upgrade_method.dart';
/// App升级信息模型
class UpgradeInfo {
/// 是否有新版本
final bool hasUpdate;
/// 是否强制更新
final bool isForceUpdate;
/// 版本号
final int versionBuildNumber;
/// 版本名称
final String versionName;
/// 当前版本号
final int currentBuildNumber;
/// 当前版本名称
final String currentVersionName;
/// 更新说明
final String updateContent;
/// 下载地址Android APK地址
final String? downloadUrl;
/// App Store地址iOS
final String? appStoreUrl;
/// APK文件大小字节
final int? apkSize;
/// APK MD5值用于校验
final String? apkMd5;
/// 应用商店白名单用于Android多渠道更新
/// 配置后,只允许跳转到白名单中且设备已安装的应用市场
/// 如果设备上没有白名单中的任何应用市场,将提示用户选择其他更新方式
final List<AppMarket>? appMarkets;
/// 支持的更新方式
final List<AppUpgradeMethod> supportedMethods;
UpgradeInfo({
this.hasUpdate = false,
required this.isForceUpdate,
required this.versionBuildNumber,
required this.versionName,
required this.updateContent,
required this.currentBuildNumber,
required this.currentVersionName,
this.downloadUrl,
this.appStoreUrl,
this.apkSize,
this.apkMd5,
this.appMarkets,
this.supportedMethods = const [AppUpgradeMethod.market, AppUpgradeMethod.browser, AppUpgradeMethod.inApp],
});
/// 从JSON创建
/// [currentBuildNumber] 当前版本号
/// [currentVersion] 当前版本名称
factory UpgradeInfo.fromJson(
Map<String, dynamic> json, {
required int currentBuildNumber,
required String currentVersionName,
}) {
final versionBuildNumber = json['versionBuildNumber'];
final versionName = json['versionName'];
// 解析 supportedMethods
List<AppUpgradeMethod> supportedMethods;
if (json['supportedMethods'] != null) {
supportedMethods = (json['supportedMethods'] as List).map((e) {
// 这里假设JSON中传的是索引或字符串简单起见如果是内部使用通常不会有这个字段
// 除非是从 AppUpgradeVersion 传过来。
// 如果是原生传过来的,我们需要约定格式。
// 暂时默认为全支持,或者如果提供了就解析。
// 简单处理:如果是字符串列表
if (e is String) {
switch (e) {
case 'market':
return AppUpgradeMethod.market;
case 'browser':
return AppUpgradeMethod.browser;
case 'inApp':
return AppUpgradeMethod.inApp;
default:
return AppUpgradeMethod.inApp;
}
}
// 如果是索引
if (e is int && e >= 0 && e < AppUpgradeMethod.values.length) {
return AppUpgradeMethod.values[e];
}
return AppUpgradeMethod.inApp;
}).toList();
} else {
supportedMethods = [AppUpgradeMethod.market, AppUpgradeMethod.browser, AppUpgradeMethod.inApp];
}
return UpgradeInfo(
hasUpdate: versionBuildNumber != currentBuildNumber || versionName != currentVersionName,
isForceUpdate: json['isForceUpdate'] ?? false,
versionBuildNumber: versionBuildNumber,
versionName: versionName,
currentBuildNumber: currentBuildNumber,
currentVersionName: currentVersionName,
updateContent: json['updateContent'] ?? '',
downloadUrl: json['downloadUrl'] as String?,
appStoreUrl: json['appStoreUrl'] as String?,
apkSize: json['apkSize'] as int?,
apkMd5: json['apkMd5'] as String?,
appMarkets: (json['appMarkets'] as List<dynamic>?)?.map((e) => AppMarket.fromString(e as String)).toList(),
supportedMethods: supportedMethods,
);
}
Map<String, dynamic> toJson() {
return {
'hasUpdate': hasUpdate,
'isForceUpdate': isForceUpdate,
'versionBuildNumber': versionBuildNumber,
'versionName': versionName,
'updateContent': updateContent,
'currentBuildNumber': currentBuildNumber,
'currentVersionName': currentVersionName,
'downloadUrl': downloadUrl,
'appStoreUrl': appStoreUrl,
'apkSize': apkSize,
'apkMd5': apkMd5,
'appMarkets': appMarkets?.map((e) => e.name).toList(),
'supportedMethods': supportedMethods.map((e) => e.name).toList(),
};
}
@override
String toString() {
return 'UpgradeInfo(hasUpdate: $hasUpdate, isForceUpdate: $isForceUpdate, versionBuildNumber: $versionBuildNumber, versionName: $versionName, currentBuildNumber: $currentBuildNumber, currentVersionName: $currentVersionName, updateContent: $updateContent, appStoreUrl: $appStoreUrl, apkSize: $apkSize, apkMd5: $apkMd5, appMarkets: $appMarkets, supportedMethods: $supportedMethods)';
}
}
/// 下载进度信息
class DownloadProgress {
/// 已下载字节数
final int received;
/// 总字节数
final int total;
/// 下载进度0.0 - 1.0
double get progress => total > 0 ? received / total : 0.0;
/// 进度百分比0 - 100
int get percentage => (progress * 100).toInt();
DownloadProgress({required this.received, required this.total});
}