diff --git a/main.js b/main.js index ab771de..3d24d27 100644 --- a/main.js +++ b/main.js @@ -2,19 +2,48 @@ storage = storages.create("mower-ng helper"); -wx = storage.get("wx", 10); -wy = storage.get("wy", 10); -http_proto = storage.get("http_proto", "http"); -host = storage.get("host", "127.0.0.1"); -port = storage.get("port", "58000"); +const config_items = { + wx: 10, + wy: 10, + http_proto: "http", + host: "127.0.0.1", + port: "58000", + auto_close: false, +}; + +config = {}; + +Object.entries(config_items).forEach(([key, value]) => { + config[key + "_"] = storage.get(key, value); + Object.defineProperty(config, key, { + get() { + return this[key + "_"]; + }, + set(new_value) { + this[key + "_"] = new_value; + storage.put(key, new_value); + }, + }); +}); +Object.defineProperties(config, { + scheduler_url: { + get() { + return `${this.http_proto}://${this.host}:${this.port}/scheduler`; + }, + }, + ws_url: { + get() { + ws_proto = this.http_proto == "https" ? "wss" : "ws"; + return `${ws_proto}://${this.host}:${this.port}/ws`; + }, + }, +}); function save_config() { - http_proto = ui.proto.getText(); - host = ui.host.getText(); - port = ui.port.getText(); - storage.put("http_proto", http_proto); - storage.put("host", host); - storage.put("port", port); + config.http_proto = ui.proto.getText(); + config.host = ui.host.getText(); + config.port = ui.port.getText(); + config.auto_close = ui.auto_close.isChecked(); } w = null; @@ -25,7 +54,7 @@ ui.layout( <img id="logo" w="64" h="64" src="file://logo.png" /> <vertical marginLeft="8"> <text text="mower-ng helper" textColor="black" textSize="22sp" /> - <text text="v1.1.0" /> + <text text="v1.2.0" /> </vertical> </horizontal> <horizontal gravity="center"> @@ -40,16 +69,20 @@ ui.layout( <text text="port" w="40" /> <input id="port" text="58000" w="200" /> </horizontal> - <horizontal gravity="center" margin="0 6 0 0"> - <button w="130" id="start" text="开启悬浮窗" /> - <button w="130" id="stop" text="关闭悬浮窗" /> + <horizontal gravity="left" w="255" margin="0 6 0 6"> + <checkbox id="auto_close" text="任务开始后,自动关闭悬浮窗" /> + </horizontal> + <horizontal gravity="center"> + <button w="125" id="start" text="开启悬浮窗" /> + <button w="125" id="stop" text="关闭悬浮窗" /> </horizontal> </vertical> ); -ui.proto.setText(http_proto); -ui.host.setText(host); -ui.port.setText(port); +ui.proto.setText(config.http_proto); +ui.host.setText(config.host); +ui.port.setText(config.port); +ui.auto_close.setChecked(config.auto_close); function toggle_ui(running) { group_waiting = [ui.proto, ui.host, ui.port, ui.start]; @@ -72,7 +105,6 @@ function toggle_ui(running) { toggle_ui(false); ws = null; -ws_proto = http_proto == "https" ? "wss" : "ws"; reg = /^[0-9].*/; log = []; @@ -109,9 +141,7 @@ function time_diff(diff) { function update_tasks() { if (w) { - request = new Request.Builder() - .url(`${http_proto}://${host}:${port}/scheduler`) - .build(); + request = new Request.Builder().url(config.scheduler_url).build(); response = client.newCall(request).execute(); scheduler = JSON.parse(response.body().string()); setTimeout(update_tasks, 5000); @@ -130,6 +160,7 @@ function stop_floaty() { w.close(); w = null; } + scheduler = { idle: false, next_time: null }; stop_ws(); clearInterval(w_timer); toggle_ui(false); @@ -190,7 +221,7 @@ ui.start.click(() => { ); w.setSize(-2, -2); - w.setPosition(wx, wy); + w.setPosition(config.wx, config.wy); w.container.setOnTouchListener((view, event) => { switch (event.getAction()) { @@ -201,13 +232,11 @@ ui.start.click(() => { case event.ACTION_MOVE: dx = event.getRawX() - X; dy = event.getRawY() - Y; - w.setPosition(wx + dx, wy + dy); + w.setPosition(config.wx + dx, config.wy + dy); return true; case event.ACTION_UP: - wx += dx; - wy += dy; - storage.put("wx", wx); - storage.put("wy", wy); + config.wx += dx; + config.wy += dy; return true; } }); @@ -220,9 +249,7 @@ ui.start.click(() => { stop_ws(); return; } - request = new Request.Builder() - .url(`${ws_proto}://${host}:${port}/ws`) - .build(); + request = new Request.Builder().url(config.ws_url).build(); ws = client.newWebSocket( request, new okhttp3.WebSocketListener({ @@ -260,6 +287,10 @@ ui.start.click(() => { next_time = no_time; remain_time = no_time; } + if (config.auto_close && remain_time == no_time) { + stop_floaty(); + return; + } ui.run(() => { w.next.setText(`下次任务:${next_time}`); w.remain.setText(`剩余时间:${remain_time}`); @@ -267,7 +298,14 @@ ui.start.click(() => { }, 1000); threads.start(() => { - update_tasks(); + try { + update_tasks(); + } catch { + ui.run(() => { + stop_floaty(); + alert("网络连接错误", "请检查设置填写是否正确!"); + }); + } }); }); diff --git a/project.json b/project.json index b841139..10448de 100644 --- a/project.json +++ b/project.json @@ -16,5 +16,5 @@ "packageName": "vip.zhaozuohong.mowernghelper", "scripts": {}, "versionCode": 2, - "versionName": "1.1.0" + "versionName": "1.2.0" }