更新功能完成啦!

This commit is contained in:
oldchen
2019-07-27 09:47:01 +08:00
parent 9a111d3478
commit 27d92c8cad
13 changed files with 225 additions and 19 deletions

View File

@ -1,5 +1,4 @@
import 'package:todo_list/json/all_beans.dart';
import 'package:todo_list/json/weather_bean.dart';
import 'api_strategy.dart';
export 'package:dio/dio.dart';
@ -88,7 +87,8 @@ class ApiService {
"https://free-api.heweather.com/s6/weather/now",
(data) {
WeatherBean weatherBean = WeatherBean.fromMap(data);
if (weatherBean.HeWeather6[weatherBean.HeWeather6.length - 1].status == "ok") {
if (weatherBean.HeWeather6[weatherBean.HeWeather6.length - 1].status ==
"ok") {
success(weatherBean);
} else {
failed(weatherBean);
@ -101,4 +101,25 @@ class ApiService {
token: token,
);
}
//检查更新
void checkUpdate(
Function success,
Function error,
Map<String, String> params,
CancelToken token,
) {
ApiStrategy.getInstance().post(
"app/checkUpdate",
(data) {
UpdateInfoBean updateInfoBean = UpdateInfoBean.fromMap(data);
success(updateInfoBean);
},
params: params,
errorCallBack: (errorMessage) {
error(errorMessage);
},
token: token,
);
}
}

View File

@ -173,6 +173,7 @@ class DemoLocalizations {
}
String get update => Intl.message('update', name: 'update', desc: '升级',);
String get newVersionIsComing => Intl.message('New version is comming!', name: 'newVersionIsComing', desc: '新版本来啦!',);
String get noUpdate => Intl.message('It is the latest version', name: 'noUpdate', desc: '已是最新版本',);
String get welcomeWord{

View File

@ -93,6 +93,7 @@ class MessageLookup extends MessageLookupByLibrary {
"navigatorSetting" : MessageLookupByLibrary.simpleMessage("Navigator Setting"),
"netPicture" : MessageLookupByLibrary.simpleMessage("Network Picture"),
"newVersionIsComing" : MessageLookupByLibrary.simpleMessage("New version is comming!"),
"noUpdate" : MessageLookupByLibrary.simpleMessage("It is the latest version"),
"ok" : MessageLookupByLibrary.simpleMessage("ok"),
"openSystemSetting" : MessageLookupByLibrary.simpleMessage("Open System Setting"),
"pickAColor" : MessageLookupByLibrary.simpleMessage("Pick a color!"),

View File

@ -93,6 +93,7 @@ class MessageLookup extends MessageLookupByLibrary {
"navigatorSetting" : MessageLookupByLibrary.simpleMessage("导航栏设置"),
"netPicture" : MessageLookupByLibrary.simpleMessage("网络图片"),
"newVersionIsComing" : MessageLookupByLibrary.simpleMessage("新版本来啦!"),
"noUpdate" : MessageLookupByLibrary.simpleMessage("已是最新版本"),
"ok" : MessageLookupByLibrary.simpleMessage("确定"),
"openSystemSetting" : MessageLookupByLibrary.simpleMessage("打开系统设置"),
"pickAColor" : MessageLookupByLibrary.simpleMessage("选择一个颜色吧!"),

View File

@ -1,2 +1,4 @@
export 'common_bean.dart';
export 'photo_bean.dart';
export 'photo_bean.dart';
export 'update_info_bean.dart';
export 'weather_bean.dart';

View File

@ -0,0 +1,55 @@
class UpdateInfoBean {
/**
* appVersion : "1.0.0"
* appName : "无"
* updateInfo : "无"
* downloadUrl : "无"
* appId : "001"
*/
String appVersion;
String appName;
String updateInfo;
String downloadUrl;
String appId;
static UpdateInfoBean fromMap(Map<String, dynamic> map) {
UpdateInfoBean update_info_bean = new UpdateInfoBean();
update_info_bean.appVersion = map['appVersion'];
update_info_bean.appName = map['appName'];
update_info_bean.updateInfo = map['updateInfo'];
update_info_bean.downloadUrl = map['downloadUrl'];
update_info_bean.appId = map['appId'];
return update_info_bean;
}
static List<UpdateInfoBean> fromMapList(dynamic mapList) {
List<UpdateInfoBean> list = new List(mapList.length);
for (int i = 0; i < mapList.length; i++) {
list[i] = fromMap(mapList[i]);
}
return list;
}
static bool needUpdate(String oldVersion, String newVersion){
final oldList = oldVersion.split(".");
final newList = newVersion.split(".");
bool needUpdate = false;
for (var i = 0; i < oldList.length; i++) {
String oldNumString = oldList[i];
String newNumString = newList[i];
int oldNum = int.parse(oldNumString);
int newNum = int.parse(newNumString);
if(newNum > oldNum){
needUpdate = true;
return needUpdate;
}
}
return needUpdate;
}
}

View File

@ -1,13 +1,18 @@
import 'dart:io';
import 'dart:math';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:todo_list/config/api_service.dart';
import 'package:todo_list/i10n/localization_intl.dart';
import 'package:package_info/package_info.dart';
import 'package:todo_list/json/update_info_bean.dart';
import 'package:todo_list/model/global_model.dart';
import 'package:todo_list/pages/webview_page.dart';
import 'package:todo_list/widgets/loading_widget.dart';
import 'package:todo_list/widgets/net_loading_widget.dart';
import 'package:todo_list/widgets/update_dialog.dart';
class AboutPage extends StatefulWidget {
@ -65,7 +70,10 @@ class _AboutPageState extends State<AboutPage> {
width: 70,
height: 70,
margin: EdgeInsets.all(10),
child: Image.asset("images/icon_1.png",fit: BoxFit.contain,))),
child: Image.asset(
"images/icon_1.png",
fit: BoxFit.contain,
))),
),
Container(
margin: EdgeInsets.only(left: 50, top: 2),
@ -112,15 +120,16 @@ class _AboutPageState extends State<AboutPage> {
],
),
),
Platform.isAndroid ? Container(
child: IconButton(icon: Icon(Icons.cloud_upload,), onPressed: (){
showDialog(context: context, builder: (ctx){
return UpdateDialog(
);
});
}),
) : SizedBox(),
Platform.isAndroid
? Container(
child: IconButton(
icon: Icon(
Icons.cloud_upload,
),
onPressed: () => checkUpdate(globalModel),
),
)
: SizedBox(),
],
),
Expanded(
@ -210,4 +219,51 @@ class _AboutPageState extends State<AboutPage> {
}
return Text(data);
}
void checkUpdate(GlobalModel globalModel) {
final loadingController = globalModel.loadingController;
showDialog(
context: context,
builder: (ctx) {
CancelToken cancelToken = CancelToken();
return NetLoadingWidget(
loadingController: loadingController,
successText: DemoLocalizations.of(context).noUpdate,
onSuccess: (){
Navigator.pop(context);
},
onRequest: () {
ApiService.instance.checkUpdate(
(UpdateInfoBean updateInfo) async{
final packageInfo = await PackageInfo.fromPlatform();
bool needUpdate = UpdateInfoBean.needUpdate(packageInfo.version, updateInfo.appVersion);
if(!needUpdate){
Navigator.of(context).pop();
showDialog(context: context, builder: (ctx2){
return UpdateDialog(
version: updateInfo.appVersion,
updateUrl: updateInfo.downloadUrl,
updateInfo: updateInfo.updateInfo,
updateInfoColor: globalModel.logic.getWhiteInDark(),
backgroundColor: globalModel.logic.getPrimaryGreyInDark(context),
);
});
}
loadingController.setFlag(LoadingFlag.success);
},
(msg) {
loadingController.setFlag(LoadingFlag.error);
},
{
"language": globalModel.currentLocale.languageCode,
"appId": "001"
},
cancelToken,
);
},
cancelToken: cancelToken,
);
});
}
}

View File

@ -61,7 +61,7 @@ class _NetLoadingWidgetState extends State<NetLoadingWidget> {
Column(
children: <Widget>[
Text(
widget.successText,
widget.successText ?? "",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 30),
),

View File

@ -15,12 +15,13 @@ class UpdateDialog extends StatefulWidget {
final String updateUrl;
final bool isForce;
final Color backgroundColor;
final Color updateInfoColor;
UpdateDialog({
this.version = "1.0.0",
this.updateInfo = "",
this.updateUrl = "",
this.isForce = false, this.backgroundColor,
this.isForce = false, this.backgroundColor, this.updateInfoColor,
});
@override
@ -33,7 +34,7 @@ class UpdateDialogState extends State<UpdateDialog> {
@override
Widget build(BuildContext context) {
final bgColor = Theme
final bgColor = widget.backgroundColor ?? Theme
.of(context)
.primaryColor;
final size = MediaQuery
@ -65,7 +66,7 @@ class UpdateDialogState extends State<UpdateDialog> {
DemoLocalizations
.of(context)
.newVersionIsComing,
style: TextStyle(color: Colors.white, fontSize: 20),
style: TextStyle(color:widget.updateInfoColor ?? Colors.white, fontSize: 20),
),
color: Colors.transparent,
)),
@ -81,7 +82,7 @@ class UpdateDialogState extends State<UpdateDialog> {
scrollDirection: Axis.vertical,
child: Text(
widget.updateInfo ?? "",
style: TextStyle(color: Colors.white),
style: TextStyle(color: widget.updateInfoColor ?? Colors.white),
),
),
))),

View File

@ -440,6 +440,12 @@
"type": "text",
"placeholders": {}
},
"noUpdate": "It is the latest version",
"@noUpdate": {
"description": "已是最新版本",
"type": "text",
"placeholders": {}
},
"writeAtLeastOneTaskItem": "Please write at least one task.",
"@writeAtLeastOneTaskItem": {
"description": "请至少写下一项任务哦",

View File

@ -1,5 +1,5 @@
{
"@@last_modified": "2019-07-26T18:12:05.019730",
"@@last_modified": "2019-07-27T09:39:19.620685",
"appName": "One Day List",
"@appName": {
"description": "app的名字",
@ -446,6 +446,12 @@
"type": "text",
"placeholders": {}
},
"noUpdate": "It is the latest version",
"@noUpdate": {
"description": "已是最新版本",
"type": "text",
"placeholders": {}
},
"welcomeWord": "Hello! ",
"@welcomeWord": {
"description": "主页的欢迎词",

View File

@ -422,6 +422,12 @@
"type": "text",
"placeholders": {}
},
"noUpdate": "已是最新版本",
"@noUpdate": {
"description": "已是最新版本",
"type": "text",
"placeholders": {}
},
"bgChangeWithCard": "背景跟随任务图标颜色",
"@bgChangeWithCard": {
"description": "背景跟随任务图标颜色",

View File

@ -0,0 +1,50 @@
import 'package:test/test.dart';
void main(){
bool needUpdate(String oldVersion, String newVersion){
final oldList = oldVersion.split(".");
final newList = newVersion.split(".");
bool needUpdate = false;
for (var i = 0; i < oldList.length; i++) {
String oldNumString = oldList[i];
String newNumString = newList[i];
int oldNum = int.parse(oldNumString);
int newNum = int.parse(newNumString);
if(newNum > oldNum){
needUpdate = true;
return needUpdate;
}
}
return needUpdate;
}
test("测试版本号对比", (){
bool update1 = needUpdate("1.0.0", "1.0.0");
bool update2 = needUpdate("1.0.0", "1.0.1");
bool update3 = needUpdate("1.0.2", "1.0.1");
bool update4 = needUpdate("1.0.0", "1.1.0");
bool update5 = needUpdate("1.0.0", "2.0.0");
bool update6 = needUpdate("1.0.0", "1.0.11");
bool update7 = needUpdate("1.0.0", "1.11.0");
expect(update1, false);
expect(update2, true);
expect(update3, false);
expect(update4, true);
expect(update5, true);
expect(update6, true);
expect(update7, true);
});
}