mod: add check update

Closes #112

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2025-01-08 11:28:28 +08:00
parent 57722eb579
commit 28b6b769b2
6 changed files with 143 additions and 118 deletions

View File

@@ -2,7 +2,9 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'package:PiliPlus/build_config.dart';
import 'package:PiliPlus/grpc/app/main/community/reply/v1/reply.pb.dart';
import 'package:PiliPlus/http/api.dart';
import 'package:PiliPlus/http/constants.dart';
import 'package:PiliPlus/http/init.dart';
import 'package:PiliPlus/http/member.dart';
@@ -18,6 +20,7 @@ import 'package:PiliPlus/utils/feed_back.dart';
import 'package:PiliPlus/utils/login.dart';
import 'package:PiliPlus/utils/storage.dart';
import 'package:crypto/crypto.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
@@ -789,120 +792,119 @@ class Utils {
};
}
// 版本对比
// static bool needUpdate(localVersion, remoteVersion) {
// return localVersion != remoteVersion;
// }
// 检查更新
// static Future<bool> checkUpdate() async {
// SmartDialog.dismiss();
// var currentInfo = await PackageInfo.fromPlatform();
// var result = await Request().get(Api.latestApp, extra: {'ua': 'mob'});
// if (result.data.isEmpty) {
// SmartDialog.showToast('检查更新失败,github接口未返回数据请检查网络');
// return false;
// }
// LatestDataModel data = LatestDataModel.fromJson(result.data[0]);
// String buildNumber = currentInfo.buildNumber;
// String remoteVersion = data.tagName!;
// if (Platform.isAndroid) {
// buildNumber = buildNumber.substring(0, buildNumber.length - 1);
// } else if (Platform.isIOS) {
// remoteVersion = remoteVersion.replaceAll('-beta', '');
// }
// bool isUpdate =
// Utils.needUpdate("${currentInfo.version}+$buildNumber", remoteVersion);
// if (isUpdate) {
// SmartDialog.show(
// animationType: SmartAnimationType.centerFade_otherSlide,
// builder: (context) {
// return AlertDialog(
// title: const Text('🎉 发现新版本 '),
// content: SizedBox(
// height: 280,
// child: SingleChildScrollView(
// child: Column(
// mainAxisSize: MainAxisSize.min,
// mainAxisAlignment: MainAxisAlignment.start,
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// Text(
// data.tagName!,
// style: const TextStyle(fontSize: 20),
// ),
// const SizedBox(height: 8),
// Text(data.body!),
// TextButton(
// onPressed: () {
// launchUrl(
// Uri.parse(
// "https://github.com/bggRGjQaUbCoE/PiliPlus/commits/main/"),
// mode: LaunchMode.externalApplication,
// );
// },
// child: Text(
// "点此查看完整更新即commit内容",
// style: TextStyle(
// color: Theme.of(context).colorScheme.primary),
// )),
// ],
// ),
// ),
// ),
// actions: [
// TextButton(
// onPressed: () {
// GStorage.setting.put(SettingBoxKey.autoUpdate, false);
// SmartDialog.dismiss();
// },
// child: Text(
// '不再提醒',
// style:
// TextStyle(color: Theme.of(context).colorScheme.outline),
// ),
// ),
// TextButton(
// onPressed: () => SmartDialog.dismiss(),
// child: Text(
// '取消',
// style:
// TextStyle(color: Theme.of(context).colorScheme.outline),
// ),
// ),
// TextButton(
// onPressed: () => matchVersion(data),
// child: const Text('Github'),
// ),
// ],
// );
// },
// );
// }
// return true;
// }
static Future checkUpdate() async {
SmartDialog.dismiss();
try {
dynamic res = await Request().get(Api.latestApp, extra: {'ua': 'mob'});
if (res.data.isEmpty) {
SmartDialog.showToast('检查更新失败,GitHub接口未返回数据请检查网络');
return;
}
DateTime latest = DateTime.parse(res.data[0]['created_at']);
latest = latest.copyWith(hour: latest.hour + 8);
DateTime current = DateTime.parse(BuildConfig.buildTime);
if (current.compareTo(latest) < 0) {
SmartDialog.show(
animationType: SmartAnimationType.centerFade_otherSlide,
builder: (context) {
return AlertDialog(
title: const Text('🎉 发现新版本 '),
content: SizedBox(
height: 280,
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${res.data[0]['tag_name']}',
style: const TextStyle(fontSize: 20),
),
const SizedBox(height: 8),
Text('${res.data[0]['body']}'),
TextButton(
onPressed: () {
launchURL(
'https://github.com/bggRGjQaUbCoE/PiliPlus/commits/main');
},
child: Text(
"点此查看完整更新(即commit)内容",
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
),
),
),
],
),
),
),
actions: [
TextButton(
onPressed: () {
SmartDialog.dismiss();
GStorage.setting.put(SettingBoxKey.autoUpdate, false);
},
child: Text(
'不再提醒',
style: TextStyle(
color: Theme.of(context).colorScheme.outline,
),
),
),
TextButton(
onPressed: SmartDialog.dismiss,
child: Text(
'取消',
style: TextStyle(
color: Theme.of(context).colorScheme.outline,
),
),
),
TextButton(
onPressed: () {
onDownload(res.data[0]);
},
child: const Text('Github'),
),
],
);
},
);
}
} catch (e) {
debugPrint('failed to check update: $e');
}
}
// 下载适用于当前系统的安装包
// static Future matchVersion(data) async {
// await SmartDialog.dismiss();
// // 获取设备信息
// DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
// if (Platform.isAndroid) {
// AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
// // [arm64-v8a]
// String abi = androidInfo.supportedAbis.first;
// late String downloadUrl;
// if (data.assets.isNotEmpty) {
// for (var i in data.assets) {
// if (i.downloadUrl.contains(abi)) {
// downloadUrl = i.downloadUrl;
// }
// }
// // 应用外下载
// launchURL(downloadUrl);
// }
// }
// }
static Future onDownload(data) async {
await SmartDialog.dismiss();
try {
void download(plat) {
if (data['assets'].isNotEmpty) {
for (dynamic i in data['assets']) {
if (i['name'].contains(plat)) {
launchURL(i['browser_download_url']);
break;
}
}
}
}
if (Platform.isAndroid) {
// 获取设备信息
AndroidDeviceInfo androidInfo = await DeviceInfoPlugin().androidInfo;
// [arm64-v8a]
download(androidInfo.supportedAbis.first);
} else {
download('ios');
}
} catch (_) {
launchURL('https://github.com/bggRGjQaUbCoE/PiliPlus/releases/latest');
}
}
// 时间戳转时间
static tampToSeektime(number) {