From e4f7e126e541cdc5cef4cb8e130783158fa574c4 Mon Sep 17 00:00:00 2001 From: 21pages Date: Tue, 24 Sep 2024 11:37:30 +0800 Subject: [PATCH] fix check update (#9444) check_software_update runs in a new thread, won't return directly Signed-off-by: 21pages --- flutter/lib/consts.dart | 2 ++ flutter/lib/desktop/pages/desktop_home_page.dart | 16 ++++++++++++++-- flutter/lib/mobile/pages/connection_page.dart | 16 ++++++++++++++-- src/common.rs | 10 ++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/flutter/lib/consts.dart b/flutter/lib/consts.dart index edc7f4278..17d5cec27 100644 --- a/flutter/lib/consts.dart +++ b/flutter/lib/consts.dart @@ -570,3 +570,5 @@ enum WindowsTarget { extension WindowsTargetExt on int { WindowsTarget get windowsVersion => getWindowsTarget(this); } + +const kCheckSoftwareUpdateFinish = 'check_software_update_finish'; \ No newline at end of file diff --git a/flutter/lib/desktop/pages/desktop_home_page.dart b/flutter/lib/desktop/pages/desktop_home_page.dart index dee990af4..90fa67ded 100644 --- a/flutter/lib/desktop/pages/desktop_home_page.dart +++ b/flutter/lib/desktop/pages/desktop_home_page.dart @@ -664,9 +664,17 @@ class _DesktopHomePageState extends State void initState() { super.initState(); if (!bind.isCustomClient()) { + platformFFI.registerEventHandler( + kCheckSoftwareUpdateFinish, kCheckSoftwareUpdateFinish, + (Map evt) async { + if (evt['url'] is String) { + setState(() { + updateUrl = evt['url']; + }); + } + }); Timer(const Duration(seconds: 1), () async { - updateUrl = await bind.mainGetSoftwareUpdateUrl(); - if (updateUrl.isNotEmpty) setState(() {}); + bind.mainGetSoftwareUpdateUrl(); }); } _updateTimer = periodic_immediate(const Duration(seconds: 1), () async { @@ -824,6 +832,10 @@ class _DesktopHomePageState extends State _uniLinksSubscription?.cancel(); Get.delete(tag: 'stop-service'); _updateTimer?.cancel(); + if (!bind.isCustomClient()) { + platformFFI.unregisterEventHandler( + kCheckSoftwareUpdateFinish, kCheckSoftwareUpdateFinish); + } super.dispose(); } diff --git a/flutter/lib/mobile/pages/connection_page.dart b/flutter/lib/mobile/pages/connection_page.dart index c6e812389..181f36e58 100644 --- a/flutter/lib/mobile/pages/connection_page.dart +++ b/flutter/lib/mobile/pages/connection_page.dart @@ -70,9 +70,17 @@ class _ConnectionPageState extends State { } if (isAndroid) { if (!bind.isCustomClient()) { + platformFFI.registerEventHandler( + kCheckSoftwareUpdateFinish, kCheckSoftwareUpdateFinish, + (Map evt) async { + if (evt['url'] is String) { + setState(() { + _updateUrl = evt['url']; + }); + } + }); Timer(const Duration(seconds: 1), () async { - _updateUrl = await bind.mainGetSoftwareUpdateUrl(); - if (_updateUrl.isNotEmpty) setState(() {}); + bind.mainGetSoftwareUpdateUrl(); }); } } @@ -353,6 +361,10 @@ class _ConnectionPageState extends State { if (Get.isRegistered()) { Get.delete(); } + if (!bind.isCustomClient()) { + platformFFI.unregisterEventHandler( + kCheckSoftwareUpdateFinish, kCheckSoftwareUpdateFinish); + } super.dispose(); } } diff --git a/src/common.rs b/src/common.rs index 995085070..c02aea45e 100644 --- a/src/common.rs +++ b/src/common.rs @@ -830,6 +830,16 @@ async fn check_software_update_() -> hbb_common::ResultType<()> { if get_version_number(&latest_release_version) > get_version_number(crate::VERSION) { *SOFTWARE_UPDATE_URL.lock().unwrap() = response_url; } + #[cfg(feature = "flutter")] + { + let mut m = HashMap::new(); + m.insert("name", "check_software_update_finish"); + let url = SOFTWARE_UPDATE_URL.lock().unwrap().clone(); + m.insert("url", url.as_str()); + if let Ok(data) = serde_json::to_string(&m) { + let _ = crate::flutter::push_global_event(crate::flutter::APP_TYPE_MAIN, data); + } + } Ok(()) }