From a03ad1283792e566b11a95b880c0af57a2fe8956 Mon Sep 17 00:00:00 2001 From: wgh19 Date: Thu, 13 Jun 2024 23:01:01 +0800 Subject: [PATCH] check update --- assets/tr.json | 14 ++++++-- lib/appdata.dart | 1 + lib/main.dart | 2 ++ lib/pages/settings_page.dart | 11 +++++++ lib/utils/update.dart | 63 ++++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 lib/utils/update.dart diff --git a/assets/tr.json b/assets/tr.json index 9dc809b..4e5bdd9 100644 --- a/assets/tr.json +++ b/assets/tr.json @@ -174,7 +174,12 @@ "External browser": "外部浏览器", "Show comments": "显示评论", "Show original image": "显示原图", - "Illustrations": "插画" + "Illustrations": "插画", + "New version available": "新版本可用", + "A new version of Pixes is available. Do you want to update now?" : "Pixes有新版本可用. 您要立即更新吗?", + "Update": "更新", + "Check for updates": "检查更新", + "Check for updates on startup": "启动时检查更新" }, "zh_TW": { "Search": "搜索", @@ -351,6 +356,11 @@ "External browser": "外部瀏覽器", "Show comments": "顯示評論", "Show original image": "顯示原圖", - "Illustrations": "插畫" + "Illustrations": "插畫", + "New version available": "新版本可用", + "A new version of Pixes is available. Do you want to update now?" : "Pixes有新版本可用. 您要立即更新嗎?", + "Update": "更新", + "Check for updates": "檢查更新", + "Check for updates on startup": "啟動時檢查更新" } } \ No newline at end of file diff --git a/lib/appdata.dart b/lib/appdata.dart index 3981ad6..b7cc2fd 100644 --- a/lib/appdata.dart +++ b/lib/appdata.dart @@ -36,6 +36,7 @@ class _Appdata { LogicalKeyboardKey.keyG.keyId, ], "showOriginalImage": false, + "checkUpdate": true, }; bool lock = false; diff --git a/lib/main.dart b/lib/main.dart index 2698843..7d8d4bb 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -19,11 +19,13 @@ import "package:pixes/pages/main_page.dart"; import "package:pixes/utils/app_links.dart"; import "package:pixes/utils/loop.dart"; import "package:pixes/utils/translation.dart"; +import "package:pixes/utils/update.dart"; import "package:pixes/utils/window.dart"; import "package:window_manager/window_manager.dart"; void main() { runZonedGuarded(() async { + Future.delayed(const Duration(seconds: 3), checkUpdate); WidgetsFlutterBinding.ensureInitialized(); FlutterError.onError = (details) { Log.error("Unhandled", "${details.exception}\n${details.stack}"); diff --git a/lib/pages/settings_page.dart b/lib/pages/settings_page.dart index cc65e49..8fab479 100644 --- a/lib/pages/settings_page.dart +++ b/lib/pages/settings_page.dart @@ -12,6 +12,7 @@ import 'package:pixes/foundation/app.dart'; import 'package:pixes/pages/main_page.dart'; import 'package:pixes/utils/io.dart'; import 'package:pixes/utils/translation.dart'; +import 'package:pixes/utils/update.dart'; import 'package:url_launcher/url_launcher_string.dart'; import 'logs.dart'; @@ -182,6 +183,16 @@ class _SettingsPageState extends State { child: Column( children: [ buildItem(title: "Version", subtitle: App.version), + buildItem( + title: "Check for updates on startup".tl, + action: ToggleSwitch( + checked: appdata.settings["checkUpdate"], + onChanged: (value) { + setState(() { + appdata.settings["checkUpdate"] = value; + }); + appdata.writeData(); + })), buildItem( title: "Github", action: IconButton( diff --git a/lib/utils/update.dart b/lib/utils/update.dart new file mode 100644 index 0000000..0a936d3 --- /dev/null +++ b/lib/utils/update.dart @@ -0,0 +1,63 @@ +import 'package:fluent_ui/fluent_ui.dart'; +import 'package:pixes/appdata.dart'; +import 'package:pixes/foundation/app.dart'; +import 'package:pixes/network/app_dio.dart'; +import 'package:pixes/utils/translation.dart'; +import 'package:url_launcher/url_launcher_string.dart'; + +Future getLatestVersion() async { + var dio = AppDio(); + var res = await dio + .get("https://api.github.com/repos/wgh136/pixes/releases/latest"); + return (res.data["tag_name"] as String).replaceFirst("v", ""); +} + +/// Compare two versions. +/// Return `true` if `a` is greater than `b`. +bool compareVersion(String a, String b) { + var aList = a.split(".").map(int.parse).toList(); + var bList = b.split(".").map(int.parse).toList(); + for (var i = 0; i < aList.length; i++) { + if (aList[i] > bList[i]) { + return true; + } else if (aList[i] < bList[i]) { + return false; + } + } + return false; +} + +Future checkUpdate() async { + if (appdata.account == null) return; + try { + var latestVersion = await getLatestVersion(); + if (compareVersion(latestVersion, App.version)) { + showDialog( + context: App.rootNavigatorKey.currentContext!, + builder: (context) => ContentDialog( + title: Text("New version available".tl), + content: Text( + "A new version of Pixes is available. Do you want to update now?" + .tl, + ), + actions: [ + Button( + child: Text("Cancel".tl), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + FilledButton( + child: Text("Update".tl), + onPressed: () { + Navigator.of(context).pop(); + launchUrlString( + "https://github.com/wgh136/pixes/releases/latest"); + }) + ], + )); + } + } catch (e) { + // ignore + } +}