From 4e1f5fb539a733e6a70f6b7cf5c813e7c698d8bc Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sun, 29 Sep 2024 19:30:50 +0800 Subject: [PATCH] refactor: various miscellaneous optimizations --- src/app/App.svelte | 15 ++++----------- src/app/global.d.ts | 20 +++++++++++--------- xcode/App-Shared/ViewController.swift | 2 +- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/app/App.svelte b/src/app/App.svelte index 2f3a9433..1c6e2973 100644 --- a/src/app/App.svelte +++ b/src/app/App.svelte @@ -7,15 +7,8 @@ let directory = "init"; - window.APP = { - show: () => {}, - printVersion: (v, b) => { - version = v; - build = b; - }, - printDirectory: (d) => { - directory = d; - }, + window.webapp = { + updateDirectory: (newDir) => (directory = newDir), }; async function initialize() { @@ -25,11 +18,11 @@ } function changeDirectory() { - window.webkit?.messageHandlers.controller.postMessage("CHANGE_DIRECTORY"); + webkit.postMessage("CHANGE_DIRECTORY"); } function openDirectory() { - window.webkit?.messageHandlers.controller.postMessage("OPEN_DIRECTORY"); + webkit.postMessage("OPEN_DIRECTORY"); } diff --git a/src/app/global.d.ts b/src/app/global.d.ts index 0b01b0d8..6a83a4d5 100644 --- a/src/app/global.d.ts +++ b/src/app/global.d.ts @@ -5,23 +5,25 @@ declare global { interface Window { - APP: { - show: ( - platform: "ios" | "mac", - enabled: boolean, - useSettingsInsteadOfPreferences: boolean, - ) => void; - printVersion: (v: string, b: string) => void; - printDirectory: (d: string) => void; + webapp: { + updateDirectory: (directory: string) => void; }; webkit: { messageHandlers: { controller: { - postMessage: function; + postMessage: ( + message: T, + ) => Promise>; }; }; }; } } +type MessageBody = "INIT" | "CHANGE_DIRECTORY" | "OPEN_DIRECTORY"; + +type MessageReply = T extends "INIT" + ? { build: string; version: string; directory: string } + : void; + export {}; diff --git a/xcode/App-Shared/ViewController.swift b/xcode/App-Shared/ViewController.swift index 5fc72a59..ac0d457b 100644 --- a/xcode/App-Shared/ViewController.swift +++ b/xcode/App-Shared/ViewController.swift @@ -120,6 +120,6 @@ class ViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHan func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) { Preferences.scriptsDirectoryUrl = url - webView.evaluateJavaScript("APP.printDirectory('\(getCurrentScriptsDirectoryString())')") + webView.evaluateJavaScript("webapp.updateDirectory('\(getCurrentScriptsDirectoryString())')") } }