diff --git a/lib/modules/application/application_controller.dart b/lib/modules/application/application_controller.dart index d548b0c..c21c66f 100644 --- a/lib/modules/application/application_controller.dart +++ b/lib/modules/application/application_controller.dart @@ -166,6 +166,7 @@ class ApplicationController extends GetxController { await Future.delayed(Duration(seconds: 3), () async { if (ConfigService.isIOS == false && await Permission.storage.isGranted == false) { + // 请求权限 await [Permission.storage].request(); } if (await Permission.storage.isGranted) { diff --git a/lib/utils/update.dart b/lib/utils/update.dart index 1fa3c03..8b241a1 100644 --- a/lib/utils/update.dart +++ b/lib/utils/update.dart @@ -1,7 +1,8 @@ import 'dart:io'; +import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; -import 'package:get/get.dart'; +import 'package:get/get.dart' hide Response; import 'package:install_plugin/install_plugin.dart'; import 'package:news_getx/data/model/app.dart'; import 'package:news_getx/data/repository/app_repository.dart'; @@ -10,6 +11,38 @@ import 'package:news_getx/modules/widgets/toast.dart'; import 'package:news_getx/utils/http.dart'; import 'package:path_provider/path_provider.dart'; +class DownloadController extends GetxController { + // 进度条 + var _progressValue = 0.0.obs; + + double get progressValue => _progressValue.value; + + set progressValue(double value) { + _progressValue.value = value; + } + + Future download(String fileUrl, String fullPath) async { + progressValue = 0; + return await HttpUtil().dio.download( + fileUrl, + fullPath, + options: Options( + receiveTimeout: Duration(minutes: 5), + ), + onReceiveProgress: (count, total) { + final value = count / total; + if (progressValue != value) { + if (progressValue < 1.0) { + progressValue = count / total; + } else { + progressValue = 0.0; + } + } + }, + ); + } +} + /// App 更新 class AppUpdateUtil { static AppUpdateUtil _instance = AppUpdateUtil._internal(); @@ -84,9 +117,33 @@ class AppUpdateUtil { break; } } - if (!isFileFound) { - await HttpUtil().dio.download(fileUrl, fullPath); + final DownloadController downloadController = Get.put( + DownloadController()); + Get.dialog( + AlertDialog( + title: Text('文件下载中'), + content: Obx(() { + final percentage = downloadController.progressValue * 100; + + if (percentage == 100) { + Get.back(); + } + + return Row( + children: [ + Text('正在下载'), + Expanded(child: LinearProgressIndicator( + value: downloadController.progressValue)), + Text( + "${percentage.toStringAsFixed(0)}%"), + ], + ); + }), + ), + barrierDismissible: false, + ); + await downloadController.download(fileUrl, fullPath); } // 安装 await InstallPlugin.installApk(fullPath);