|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
|
|
import 'package:install_plugin/install_plugin.dart';
|
|
|
|
import 'package:one_key/constant/constant.dart';
|
|
|
|
import 'package:one_key/models/version.dart';
|
|
|
|
import 'package:one_key/net/base_resp.dart';
|
|
|
|
import 'package:one_key/net/dio_util.dart';
|
|
|
|
import 'package:one_key/net/http_url.dart';
|
|
|
|
import 'package:one_key/provider/user_model.dart';
|
|
|
|
import 'package:one_key/res/colors.dart';
|
|
|
|
import 'package:one_key/utils/log_util.dart';
|
|
|
|
import 'package:one_key/utils/utils.dart';
|
|
|
|
import 'package:one_key/widgets/MyDivider.dart';
|
|
|
|
import 'package:one_key/widgets/v_empty_view.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
|
|
class UpdateModel {
|
|
|
|
int _progress; //下载进度
|
|
|
|
Function _setState;
|
|
|
|
//检查更新
|
|
|
|
checkUpdate(BuildContext context) {
|
|
|
|
Future.delayed(new Duration(milliseconds: 1000), () {
|
|
|
|
if (Random().nextInt(10) > 0) {
|
|
|
|
checkUpdate(BuildContext context) async {
|
|
|
|
String deviceId = await Utils.getUUID();
|
|
|
|
UserModel userModel = Provider.of<UserModel>(context, listen: false);
|
|
|
|
int versionCode = await Utils.getCurrBuildNum();
|
|
|
|
Map<String, String> params = {
|
|
|
|
"deviceId": deviceId,
|
|
|
|
"sid": userModel.user.sellerid,
|
|
|
|
"versionCode": versionCode.toString()
|
|
|
|
};
|
|
|
|
BaseResp<List> baseResp = await DioUtil()
|
|
|
|
.request<List>(context, Method.get, HttpUrl.check_update, data: params)
|
|
|
|
.catchError((error) {
|
|
|
|
print(error.toString());
|
|
|
|
});
|
|
|
|
if (baseResp.code == Constant.status_success) {
|
|
|
|
LogUtil.v(baseResp.data);
|
|
|
|
Map<String, String> dataMap = baseResp.data[0];
|
|
|
|
if (int.parse(dataMap["version"]) > versionCode) {
|
|
|
|
Version version = Version.fromJson({
|
|
|
|
"version": "v1.0.3",
|
|
|
|
"file_url":
|
|
|
|
"http://xt03.colorcun.com/uploads/20191105/67e38355c41528b24a97e9ac71a221ea.apk",
|
|
|
|
"content": "1.bug修复;\n2.功能更新;"
|
|
|
|
"version": dataMap["versionName"],
|
|
|
|
"file_url":dataMap["file_url"],
|
|
|
|
"content": dataMap["content"]
|
|
|
|
});
|
|
|
|
showUpdateDialog(context, version);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// if(Platform.isAndroid){
|
|
|
|
//
|
|
|
|
// }else{
|
|
|
|
//// Response response=await DioUtil().getDio().get("http://itunes.apple.com/lookup?id=1510697490");
|
|
|
|
//// Map<String,dynamic> dataMap=jsonDecode(response.data);
|
|
|
|
//// String version=((dataMap["results"] as List)[0] as Map)["version"] as String;
|
|
|
|
//// String currVersion=await Utils.getCurrVersion();
|
|
|
|
//// if(version.compareTo(currVersion)>0){
|
|
|
|
//// Version version = Version.fromJson({
|
|
|
|
//// "version": "version",
|
|
|
|
//// "file_url":dataMap["file_url"],
|
|
|
|
//// "content": dataMap["content"]
|
|
|
|
//// });
|
|
|
|
//// showUpdateDialog(context, version);
|
|
|
|
//// }
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
//版本更新弹框
|
|
...
|
...
|
@@ -90,7 +129,8 @@ class UpdateModel { |
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
MyDivider(ScreenUtil().setWidth(1),ScreenUtil().setWidth(94)),
|
|
|
|
MyDivider(
|
|
|
|
ScreenUtil().setWidth(1), ScreenUtil().setWidth(94)),
|
|
|
|
Expanded(
|
|
|
|
child: GestureDetector(
|
|
|
|
onTap: () {
|
|
...
|
...
|
@@ -117,16 +157,18 @@ class UpdateModel { |
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return Center(child: UnconstrainedBox(
|
|
|
|
constrainedAxis: Axis.vertical,
|
|
|
|
child: ConstrainedBox(
|
|
|
|
constraints: BoxConstraints(maxWidth: ScreenUtil().setWidth(560)),
|
|
|
|
child: DefaultTextStyle(
|
|
|
|
style: TextStyle(decoration: TextDecoration.none),
|
|
|
|
child: widget,
|
|
|
|
)
|
|
|
|
return Center(
|
|
|
|
child: UnconstrainedBox(
|
|
|
|
constrainedAxis: Axis.vertical,
|
|
|
|
child: ConstrainedBox(
|
|
|
|
constraints:
|
|
|
|
BoxConstraints(maxWidth: ScreenUtil().setWidth(560)),
|
|
|
|
child: DefaultTextStyle(
|
|
|
|
style: TextStyle(decoration: TextDecoration.none),
|
|
|
|
child: widget,
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
),);
|
|
|
|
);
|
|
|
|
return AlertDialog(
|
|
|
|
content: widget,
|
|
|
|
contentPadding: EdgeInsets.zero,
|
|
...
|
...
|
@@ -141,16 +183,17 @@ class UpdateModel { |
|
|
|
if (permissions[PermissionGroup.storage] == PermissionStatus.granted) {
|
|
|
|
final dir = await getExternalStorageDirectory();
|
|
|
|
String savePath = dir.path + "/one_key";
|
|
|
|
Directory directory=new Directory(savePath);
|
|
|
|
if(directory.existsSync()){//删除历史apk
|
|
|
|
final List<FileSystemEntity> files=directory.listSync();
|
|
|
|
for(FileSystemEntity file in files){
|
|
|
|
Directory directory = new Directory(savePath);
|
|
|
|
if (directory.existsSync()) {
|
|
|
|
//删除历史apk
|
|
|
|
final List<FileSystemEntity> files = directory.listSync();
|
|
|
|
for (FileSystemEntity file in files) {
|
|
|
|
file.deleteSync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
showDownloadProgress(context);
|
|
|
|
DioUtil().download(file_url, savePath, onProgress: (received, total) {
|
|
|
|
if(_progress!=100){
|
|
|
|
if (_progress != 100) {
|
|
|
|
int progress = ((received / total) * 100).round();
|
|
|
|
_progress = progress;
|
|
|
|
_setState(() {});
|
|
...
|
...
|
@@ -171,7 +214,7 @@ class UpdateModel { |
|
|
|
builder: (ct) {
|
|
|
|
return AlertDialog(
|
|
|
|
content: StatefulBuilder(builder: (context, setState) {
|
|
|
|
if (_progress==100) {
|
|
|
|
if (_progress == 100) {
|
|
|
|
Navigator.of(ct).pop();
|
|
|
|
}
|
|
|
|
_setState = setState;
|
...
|
...
|
|