127 lines
3.5 KiB
Dart
127 lines
3.5 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:install_plugin/install_plugin.dart';
|
|
import 'package:news_getx/data/model/app.dart';
|
|
import 'package:news_getx/data/repository/app_repository.dart';
|
|
import 'package:news_getx/data/services/config.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
// todo 如何用完一次就消失
|
|
class DownloadController extends GetxController {
|
|
// 进度条
|
|
var _progressValue = 0.0.obs;
|
|
|
|
double get progressValue => _progressValue.value;
|
|
|
|
set progressValue(double value) {
|
|
_progressValue.value = value;
|
|
}
|
|
|
|
/// 下载文件 & 安装
|
|
Future<void> _downloadAPKAndSetup(fileUrl) async {
|
|
_progressValue.value = 0.0;
|
|
|
|
// 下载
|
|
Directory? externalDir = await getExternalStorageDirectory();
|
|
|
|
if (externalDir == null) {
|
|
return;
|
|
}
|
|
// 存储路径
|
|
String fullPath = "${externalDir.path}/release.apk";
|
|
|
|
// Dio dio = Dio(BaseOptions(
|
|
// responseType: ResponseType.bytes,
|
|
// followRedirects: false,
|
|
// validateStatus: (status) {
|
|
// return status! < 500;
|
|
// }
|
|
// ));
|
|
//
|
|
// // 获取APP文件
|
|
// Response response = await dio.get(fileUrl);
|
|
//
|
|
// // 文件写入
|
|
// File file = File(fullPath);
|
|
// var raf = file.openSync(mode: FileMode.write);
|
|
// raf.writeByteSync(response.data);
|
|
// await raf.close();
|
|
|
|
await Dio().download(fileUrl, fullPath, onReceiveProgress: (count, total) {
|
|
final value = count / total;
|
|
if (_progressValue.value != value) {
|
|
if (_progressValue.value < 1.0) {
|
|
_progressValue.value = count / total;
|
|
} else {
|
|
_progressValue.value = 0.0;
|
|
}
|
|
print("${(_progressValue * 100).toStringAsFixed(0)}%");
|
|
}
|
|
});
|
|
|
|
// 安装
|
|
await InstallPlugin.installApk(fullPath);
|
|
}
|
|
}
|
|
|
|
/// App 更新
|
|
class AppUpdateUtil {
|
|
static AppUpdateUtil _instance = AppUpdateUtil._internal();
|
|
|
|
factory AppUpdateUtil() => _instance;
|
|
|
|
AppUpdateUtil._internal();
|
|
|
|
AppUpdateResponse? _appUpdateInfo;
|
|
|
|
/// 获取更新信息
|
|
Future run() async {
|
|
// 提交 设备类型、发行渠道、架构、机型
|
|
AppUpdateRequest appUpdateRequest = AppUpdateRequest(
|
|
device: Platform.isIOS ? "ios" : "android",
|
|
channel: ConfigService.channel,
|
|
architecture: ConfigService.isIOS
|
|
? ConfigService.iosDeviceInfo!.utsname.machine
|
|
: ConfigService.androidDeviceInfo!.device,
|
|
model: ConfigService.isIOS
|
|
? ConfigService.iosDeviceInfo!.name
|
|
: ConfigService.androidDeviceInfo!.brand,
|
|
);
|
|
|
|
_appUpdateInfo = await AppRepository().update(appUpdateRequest);
|
|
|
|
_runAppUpdate();
|
|
}
|
|
|
|
/// 检查是否有新版
|
|
Future _runAppUpdate() async {
|
|
// 比较版本
|
|
final isNewVersion =
|
|
_appUpdateInfo!.latestVersion!.compareTo(ConfigService.to.version) > 0;
|
|
|
|
// 发现新版本
|
|
if (isNewVersion) {
|
|
// todo 确认弹窗 确认完成下载并显示进度条
|
|
Get.defaultDialog(
|
|
title: "发现新版本 ${_appUpdateInfo!.latestVersion}",
|
|
content: GetBuilder<DownloadController>(
|
|
init: DownloadController(),
|
|
builder: (controller) {
|
|
return LinearProgressIndicator(value: controller.progressValue);
|
|
},
|
|
),
|
|
);
|
|
|
|
// _appUpdateConformDialog(
|
|
//
|
|
// );
|
|
}
|
|
}
|
|
|
|
/// 升级确认对话框
|
|
void _appUpdateConformDialog(VoidCallback onPressed) {}
|
|
}
|