From 6033a3cde99ca7857ebdf101a3e883aa32211612 Mon Sep 17 00:00:00 2001 From: nyne Date: Mon, 20 Jan 2025 15:18:16 +0800 Subject: [PATCH] Add app api --- assets/init.js | 34 ++++++++++++++++++++++++++++++++++ lib/foundation/js_engine.dart | 4 ++++ 2 files changed, 38 insertions(+) diff --git a/assets/init.js b/assets/init.js index 372231a..e5cd807 100644 --- a/assets/init.js +++ b/assets/init.js @@ -1309,4 +1309,38 @@ let UI = { initialIndex: initialIndex }) } +} + +/** + * App related apis + * @since 1.2.1 + */ +let APP = { + /** + * Get the app version + * @returns {string} - The app version + */ + get version() { + return appVersion // defined in the engine + }, + + /** + * Get current app locale + * @returns {string} - The app locale, in the format of [languageCode]_[countryCode] + */ + get locale() { + return sendMessage({ + method: 'getLocale' + }) + }, + + /** + * Get current running platform + * @returns {string} - The platform name, "android", "ios", "windows", "macos", "linux" + */ + get platform() { + return sendMessage({ + method: 'getPlatform' + }) + } } \ No newline at end of file diff --git a/lib/foundation/js_engine.dart b/lib/foundation/js_engine.dart index eee6a6c..9a5e723 100644 --- a/lib/foundation/js_engine.dart +++ b/lib/foundation/js_engine.dart @@ -155,6 +155,10 @@ class JsEngine with _JSEngineApi, JsUiApi { return Future.delayed(Duration(milliseconds: message["time"])); case "UI": return handleUIMessage(Map.from(message)); + case "getLocale": + return "${App.locale.languageCode}-${App.locale.countryCode}"; + case "getPlatform": + return Platform.operatingSystem; } } return null;