forked from Sky24n/FlutterRepos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_util.dart
88 lines (75 loc) · 2.35 KB
/
version_util.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import 'dart:io';
import 'package:base_library/src/util/index.dart';
import 'package:dio/dio.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
class VersionUtil {
static final VersionUtil _singleton = VersionUtil._init();
static Dio _dio = Dio();
List<OnDownloadProgress> listeners = List();
bool isDownload = false;
factory VersionUtil() {
return _singleton;
}
VersionUtil._init();
void downloadApk(String urlPath, ValueChanged changed) async {
if (isDownload == true || ObjectUtil.isEmpty(urlPath)) return;
try {
await DirectoryUtil.getInstance();
String apkDirPath = DirectoryUtil.getStoragePath(category: 'Download');
LogUtil.e("apkDirPath: $apkDirPath");
Directory apkDir = DirectoryUtil.createDirSync(apkDirPath);
String apkName = Util.getFileName(urlPath);
String apkPath = '$apkDirPath/$apkName';
String apkTempPath = '$apkDirPath/temp_$apkName';
LogUtil.e("apkPath: $apkPath");
LogUtil.e("apkTempPath: $apkTempPath");
File file = File(apkPath);
if (file.existsSync()) {
changed(apkPath);
isDownload = false;
listeners.forEach((listener) {
listener(1, 1);
});
return;
}
isDownload = true;
Response response = await _dio.download(
urlPath,
apkTempPath,
onProgress: (int count, int total) {
LogUtil.e(
"onReceiveProgress total: $total, count: $count, prect: ${count / total}");
listeners.forEach((listener) {
listener(count, total);
});
if (count == total) {
isDownload = false;
File file = File(apkTempPath);
File fileNew = file.copySync(apkPath);
file.deleteSync();
changed(apkPath);
}
},
);
} catch (e) {
LogUtil.e("download apk error: ${e?.toString()}");
isDownload = false;
listeners.forEach((listener) {
listener(-1, 1);
});
}
}
void addListener(OnDownloadProgress listener) {
if (listener == null) return;
if (!listeners.contains(listener)) {
listeners.add(listener);
}
}
void removeListener(OnDownloadProgress listener) {
if (listener == null) return;
if (listeners.contains(listener)) {
listeners.remove(listener);
}
}
}