@@ -13,13 +13,19 @@ import 'package:flutter_cache_manager/flutter_cache_manager.dart';
1313import 'package:html/parser.dart' ;
1414import 'package:http/http.dart' as http;
1515import 'package:image_gallery_saver/image_gallery_saver.dart' ;
16+ import 'package:install_plugin/install_plugin.dart' ;
1617import 'package:intl/intl.dart' ;
1718import 'package:loftify/Models/enums.dart' ;
1819import 'package:loftify/Utils/hive_util.dart' ;
20+ import 'package:loftify/Utils/iprint.dart' ;
21+ import 'package:loftify/Utils/notification_util.dart' ;
22+ import 'package:loftify/Utils/uri_util.dart' ;
1923import 'package:loftify/Widgets/BottomSheet/slide_captcha_bottom_sheet.dart' ;
24+ import 'package:loftify/Widgets/Dialog/custom_dialog.dart' ;
2025import 'package:loftify/Widgets/Item/item_builder.dart' ;
2126import 'package:palette_generator/palette_generator.dart' ;
2227import 'package:path_provider/path_provider.dart' ;
28+ import 'package:permission_handler/permission_handler.dart' ;
2329import 'package:restart_app/restart_app.dart' ;
2430import 'package:share_plus/share_plus.dart' ;
2531
@@ -105,7 +111,6 @@ class Utils {
105111 Utils .removeImageParam (element) == Utils .removeImageParam (image));
106112 }
107113
108- //从imageUrl中提取出文件名
109114 static String extractFileNameFromUrl (String imageUrl) {
110115 return Uri .parse (imageUrl).pathSegments.last;
111116 }
@@ -594,4 +599,82 @@ class Utils {
594599 },
595600 );
596601 }
602+
603+ static Future <void > downloadAndUpdate (
604+ BuildContext context,
605+ String apkUrl,
606+ String htmlUrl, {
607+ String ? version,
608+ bool isUpdate = true ,
609+ Function (double )? onReceiveProgress,
610+ }) async {
611+ await Permission .storage.onDeniedCallback (() {
612+ IToast .showTop (context, text: "请授予文件存储权限" );
613+ }).onGrantedCallback (() async {
614+ if (Utils .isNotEmpty (apkUrl)) {
615+ double progressValue = 0.0 ;
616+ var appDocDir = await getTemporaryDirectory ();
617+ String savePath =
618+ "${appDocDir .path }/${Utils .extractFileNameFromUrl (apkUrl )}" ;
619+ try {
620+ await Dio ().download (
621+ apkUrl,
622+ savePath,
623+ onReceiveProgress: (count, total) {
624+ final value = count / total;
625+ if (progressValue != value) {
626+ if (progressValue < 1.0 ) {
627+ progressValue = count / total;
628+ } else {
629+ progressValue = 0.0 ;
630+ }
631+ NotificationUtil .sendProgressNotification (
632+ 0 ,
633+ (progressValue * 100 ).toInt (),
634+ title: isUpdate
635+ ? '正在下载新版本安装包...'
636+ : '正在下载版本${version ?? "" }的安装包...' ,
637+ payload: version ?? "" ,
638+ );
639+ onReceiveProgress? .call (progressValue);
640+ }
641+ },
642+ ).then ((response) async {
643+ if (response.statusCode == 200 ) {
644+ NotificationUtil .closeNotification (0 );
645+ NotificationUtil .sendInfoNotification (
646+ 1 ,
647+ "下载完成" ,
648+ isUpdate
649+ ? "新版本安装包已经下载完成,点击立即安装"
650+ : "版本${version ?? "" }的安装包已经下载完成,点击立即安装" ,
651+ payload: savePath,
652+ );
653+ } else {
654+ UriUtil .openExternal (htmlUrl);
655+ }
656+ });
657+ } catch (e) {
658+ IPrint .debug (e);
659+ NotificationUtil .closeNotification (0 );
660+ NotificationUtil .sendInfoNotification (
661+ 2 ,
662+ "下载失败,请重试" ,
663+ "新版本安装包下载失败,请重试" ,
664+ );
665+ }
666+ } else {
667+ UriUtil .openExternal (htmlUrl);
668+ }
669+ }).onPermanentlyDeniedCallback (() {
670+ IToast .showTop (context, text: "已拒绝文件存储权限,将跳转到浏览器下载" );
671+ UriUtil .openExternal (apkUrl);
672+ }).onRestrictedCallback (() {
673+ IToast .showTop (context, text: "请授予文件存储权限" );
674+ }).onLimitedCallback (() {
675+ IToast .showTop (context, text: "请授予文件存储权限" );
676+ }).onProvisionalCallback (() {
677+ IToast .showTop (context, text: "请授予文件存储权限" );
678+ }).request ();
679+ }
597680}
0 commit comments