From 28b6b769b2c4771abd4aecaf7fd88c257d5fe85d Mon Sep 17 00:00:00 2001 From: bggRGjQaUbCoE Date: Wed, 8 Jan 2025 11:28:28 +0800 Subject: [PATCH] mod: add check update Closes #112 Signed-off-by: bggRGjQaUbCoE --- lib/pages/about/index.dart | 11 +- lib/pages/main/controller.dart | 4 + lib/pages/setting/widgets/model.dart | 14 ++ lib/pages/setting/widgets/switch_item.dart | 3 - lib/utils/storage.dart | 5 +- lib/utils/utils.dart | 224 +++++++++++---------- 6 files changed, 143 insertions(+), 118 deletions(-) diff --git a/lib/pages/about/index.dart b/lib/pages/about/index.dart index ace53c2a..804906cc 100644 --- a/lib/pages/about/index.dart +++ b/lib/pages/about/index.dart @@ -83,11 +83,15 @@ class _AboutPageState extends State { ), Obx( () => ListTile( - // onTap: () => _aboutController.tapOnVersion(), + onTap: Utils.checkUpdate, + onLongPress: () => + Utils.copyText(_aboutController.currentVersion.value), title: const Text('当前版本'), leading: const Icon(Icons.commit_outlined), - trailing: Text(_aboutController.currentVersion.value, - style: subTitleStyle), + trailing: Text( + _aboutController.currentVersion.value, + style: subTitleStyle, + ), ), ), ListTile( @@ -100,6 +104,7 @@ Commit Hash: ${BuildConfig.commitHash}''', leading: const Icon(Icons.info_outline), onTap: () => Utils.launchURL( 'https://github.com/bggRGjQaUbCoE/PiliPlus/commit/${BuildConfig.commitHash}'), + onLongPress: () => Utils.copyText(BuildConfig.commitHash), ), // Obx( // () => ListTile( diff --git a/lib/pages/main/controller.dart b/lib/pages/main/controller.dart index 6f76eb7b..b0ab3549 100644 --- a/lib/pages/main/controller.dart +++ b/lib/pages/main/controller.dart @@ -9,6 +9,7 @@ import 'package:PiliPlus/pages/home/view.dart'; import 'package:PiliPlus/pages/media/view.dart'; import 'package:PiliPlus/utils/extension.dart'; import 'package:PiliPlus/utils/global_data.dart'; +import 'package:PiliPlus/utils/utils.dart'; import 'package:get/get.dart'; import 'package:flutter/material.dart'; import 'package:PiliPlus/utils/storage.dart'; @@ -41,6 +42,9 @@ class MainController extends GetxController { @override void onInit() { super.onInit(); + if (GStorage.autoUpdate) { + Utils.checkUpdate(); + } hideTabBar = GStorage.setting.get(SettingBoxKey.hideTabBar, defaultValue: true); isLogin.value = GStorage.isLogin; diff --git a/lib/pages/setting/widgets/model.dart b/lib/pages/setting/widgets/model.dart index 07ff751b..c96f3ca0 100644 --- a/lib/pages/setting/widgets/model.dart +++ b/lib/pages/setting/widgets/model.dart @@ -29,6 +29,7 @@ import 'package:PiliPlus/utils/feed_back.dart'; import 'package:PiliPlus/utils/global_data.dart'; import 'package:PiliPlus/utils/recommend_filter.dart'; import 'package:PiliPlus/utils/storage.dart'; +import 'package:PiliPlus/utils/utils.dart'; import 'package:auto_orientation/auto_orientation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -1886,4 +1887,17 @@ List get extraSettings => [ setKey: SettingBoxKey.autoClearCache, defaultVal: false, ), + SettingsModel( + settingsType: SettingsType.sw1tch, + title: '检查更新', + subtitle: '每次启动时检查是否需要更新', + leading: const Icon(Icons.system_update_alt_outlined), + setKey: SettingBoxKey.autoUpdate, + defaultVal: true, + onChanged: (val) { + if (val) { + Utils.checkUpdate(); + } + }, + ), ]; diff --git a/lib/pages/setting/widgets/switch_item.dart b/lib/pages/setting/widgets/switch_item.dart index 14916026..ca37b632 100644 --- a/lib/pages/setting/widgets/switch_item.dart +++ b/lib/pages/setting/widgets/switch_item.dart @@ -84,9 +84,6 @@ class _SetSwitchItemState extends State { val = value ?? !val; await GStorage.setting.put(widget.setKey, val); - // if (widget.setKey == SettingBoxKey.autoUpdate && value == true) { - // Utils.checkUpdate(); - // } widget.onChanged?.call(val); if (widget.needReboot == true) { SmartDialog.showToast('重启生效'); diff --git a/lib/utils/storage.dart b/lib/utils/storage.dart index 6644b70c..e1876396 100644 --- a/lib/utils/storage.dart +++ b/lib/utils/storage.dart @@ -301,6 +301,9 @@ class GStorage { static bool get cdnSpeedTest => setting.get(SettingBoxKey.cdnSpeedTest, defaultValue: true); + static bool get autoUpdate => + GStorage.setting.get(SettingBoxKey.autoUpdate, defaultValue: true); + static List get dynamicDetailRatio => List.from(setting .get(SettingBoxKey.dynamicDetailRatio, defaultValue: [60.0, 40.0])); @@ -475,7 +478,7 @@ class SettingBoxKey { applyFilterToRelatedVideos = 'applyFilterToRelatedVideos', /// 其他 - // autoUpdate = 'autoUpdate', + autoUpdate = 'autoUpdate', autoClearCache = 'autoClearCache', defaultShowComment = 'defaultShowComment', replySortType = 'replySortType', diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index 541bf8d3..523ccc69 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -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 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) {