Skip to content

Commit bff95bd

Browse files
committed
WLOC-Helper:improve:UI底部卡片调整,读取坐标逻辑调整(不再使用本地缓存)
1 parent d527e7d commit bff95bd

3 files changed

Lines changed: 45 additions & 61 deletions

File tree

‎WLOC-Helper/index.tsx‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,6 @@ function App() {
10001000
<HStack
10011001
spacing={6}
10021002
padding={{ vertical: 8, leading: 12, trailing: 12 }}
1003-
glassEffect={{ type: "rect", cornerRadius: 14 }}
10041003
>
10051004
<Text font="caption" foregroundStyle="secondaryLabel">致谢:</Text>
10061005
<Text font="caption" foregroundStyle="systemBlue">Yu9191/wloc</Text>
@@ -1020,7 +1019,7 @@ function App() {
10201019
{renderProxyModuleControl()}
10211020
<Spacer />
10221021
<Button action={handleOpenLocationSettings}>
1023-
<Image systemName="apple.logo" foregroundStyle="systemBlue" font="title3" />
1022+
<Image systemName="gearshape" foregroundStyle="systemBlue" font="title3" />
10241023
</Button>
10251024
</HStack>
10261025

@@ -1039,16 +1038,16 @@ function App() {
10391038
</Text>
10401039
</HStack>
10411040

1042-
<HStack spacing={12} frame={{ maxWidth: "infinity" }}>
1041+
<HStack spacing={8} frame={{ maxWidth: "infinity" }}>
10431042
<Button
10441043
action={handleSave}
10451044
frame={{ maxWidth: "infinity" }}
10461045
background={{ style: "systemBlue", shape: "capsule" }}
10471046
tint="white"
10481047
>
1049-
<HStack spacing={6} padding={{ vertical: 10, leading: 16, trailing: 20 }}>
1050-
<Image systemName="square.and.arrow.down" font="subheadline" />
1051-
<Text font="subheadline" fontWeight="medium">
1048+
<HStack spacing={4} padding={{ vertical: 9, leading: 10, trailing: 10 }}>
1049+
<Image systemName="square.and.arrow.down" font="caption" />
1050+
<Text font="caption" fontWeight="medium" lineLimit={1} allowsTightening>
10521051
储存到设备
10531052
</Text>
10541053
</HStack>
@@ -1063,13 +1062,28 @@ function App() {
10631062
background={{ style: isCurrentCoordFavorited() ? "systemGray" : "systemOrange", shape: "capsule" }}
10641063
tint="white"
10651064
>
1066-
<HStack spacing={6} padding={{ vertical: 10, leading: 16, trailing: 20 }}>
1067-
<Image systemName={isCurrentCoordFavorited() ? "star.slash" : "star"} font="subheadline" />
1068-
<Text font="subheadline" fontWeight="medium">
1065+
<HStack spacing={4} padding={{ vertical: 9, leading: 10, trailing: 10 }}>
1066+
<Image systemName={isCurrentCoordFavorited() ? "star.slash" : "star"} font="caption" />
1067+
<Text font="caption" fontWeight="medium" lineLimit={1} allowsTightening>
10691068
{isCurrentCoordFavorited() ? "取消收藏" : "收藏"}
10701069
</Text>
10711070
</HStack>
10721071
</Button>
1072+
<Button
1073+
action={handleClear}
1074+
disabled={!loc}
1075+
frame={{ maxWidth: "infinity" }}
1076+
background={{ style: "systemRed", shape: "capsule" }}
1077+
tint="white"
1078+
opacity={loc ? 1 : 0.45}
1079+
>
1080+
<HStack spacing={4} padding={{ vertical: 9, leading: 10, trailing: 10 }}>
1081+
<Image systemName="trash" font="caption" />
1082+
<Text font="caption" fontWeight="medium" lineLimit={1} allowsTightening>
1083+
移除定位
1084+
</Text>
1085+
</HStack>
1086+
</Button>
10731087
</HStack>
10741088

10751089
<HStack spacing={8} frame={{ maxWidth: "infinity" }}>
@@ -1079,10 +1093,6 @@ function App() {
10791093
<Text font="subheadline" foregroundStyle="systemGreen">
10801094
经度 {loc.longitude.toFixed(6)} 纬度 {loc.latitude.toFixed(6)} {loc.accuracy ? `精度 ${loc.accuracy}m` : ""}
10811095
</Text>
1082-
<Spacer />
1083-
<Button action={handleClear}>
1084-
<Image systemName="trash" foregroundStyle="systemRed" font="subheadline" />
1085-
</Button>
10861096
</>
10871097
) : (
10881098
<>

‎WLOC-Helper/pages/MapPage.tsx‎

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from "scripting";
2121
import type { AppSettings, Coordinate, ActiveLocation, MapLayerId } from "../types";
2222
import { DEFAULT_SPAN } from "../constants";
23-
import { loadActiveCache, saveActiveCache } from "../utils/storage";
23+
import { clearActiveCache } from "../utils/storage";
2424
import { queryDevice } from "../api/deviceApi";
2525

2626
// 从 MapCameraPosition 中提取中心坐标
@@ -53,17 +53,7 @@ export function MapPage({
5353
onCoordChange,
5454
onActiveLocChange,
5555
}: MapPageProps) {
56-
const initialActive = loadActiveCache();
57-
58-
// 相机位置:有缓存坐标则用缓存,否则 automatic
59-
const cameraPosition = useObservable<MapCameraPosition>(
60-
initialActive
61-
? MapCameraPosition.region({
62-
center: { latitude: initialActive.latitude, longitude: initialActive.longitude },
63-
span: { latitudeDelta: DEFAULT_SPAN.latitudeDelta, longitudeDelta: DEFAULT_SPAN.longitudeDelta },
64-
})
65-
: MapCameraPosition.automatic(),
66-
);
56+
const cameraPosition = useObservable<MapCameraPosition>(MapCameraPosition.automatic());
6757

6858

6959

@@ -82,9 +72,8 @@ export function MapPage({
8272

8373
// 轮询:检测手势平移带来的坐标变化
8474
useEffect(() => {
85-
const cached = initialActive;
86-
let lastLat = cached?.latitude ?? 0;
87-
let lastLng = cached?.longitude ?? 0;
75+
let lastLat = 0;
76+
let lastLng = 0;
8877
let stopped = false;
8978

9079
function poll() {
@@ -127,39 +116,30 @@ export function MapPage({
127116
return () => mapSelection.unsubscribe(cb);
128117
}, []);
129118

130-
// 启动时:缓存坐标 → 查询设备 → GPS 定位
119+
// 启动时:查询设备持久化坐标 → GPS 定位。当前生效坐标只以 WLOC 模块查询结果为准。
131120
useEffect(() => {
132121
(async () => {
133-
// 1. 使用缓存坐标
134-
const cached = initialActive;
135-
if (cached) {
136-
onActiveLocChange(cached);
137-
moveCameraTo(cached.latitude, cached.longitude);
138-
onCoordChange(cached.latitude, cached.longitude);
139-
}
122+
clearActiveCache();
140123

141-
// 2. 查询设备生效坐标
142124
try {
143125
const loc = await queryDevice(settings.saveApi);
144126
onActiveLocChange(loc);
145-
saveActiveCache(loc);
146127
if (loc) {
147128
moveCameraTo(loc.latitude, loc.longitude);
148129
onCoordChange(loc.latitude, loc.longitude);
149130
return;
150131
}
151-
} catch {}
152-
153-
// 3. 无保存坐标时尝试 GPS 定位
154-
if (!cached) {
155-
try {
156-
const gps = await Location.requestCurrent({ forceRequest: true });
157-
if (gps) {
158-
moveCameraTo(gps.latitude, gps.longitude);
159-
onCoordChange(gps.latitude, gps.longitude);
160-
}
161-
} catch {}
132+
} catch {
133+
onActiveLocChange(null);
162134
}
135+
136+
try {
137+
const gps = await Location.requestCurrent({ forceRequest: true });
138+
if (gps) {
139+
moveCameraTo(gps.latitude, gps.longitude);
140+
onCoordChange(gps.latitude, gps.longitude);
141+
}
142+
} catch {}
163143
})();
164144
}, []);
165145

‎WLOC-Helper/utils/storage.ts‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// 持久化存储层:基于 Scripting 的 Storage API(全局命名空间),
2-
// 封装收藏位置、应用设置、当前生效坐标缓存三类数据。
2+
// 封装收藏位置和应用设置,并清理旧版生效坐标缓存。
33

4-
import type { AppSettings, FavoriteLocation, ActiveLocation } from "../types";
4+
import type { AppSettings, FavoriteLocation } from "../types";
55
import { DEFAULT_SETTINGS, STORAGE_KEYS } from "../constants";
66

77
// ── 收藏位置 ───────────────────────────────────────────────────────
@@ -55,15 +55,9 @@ export function saveSettings(settings: AppSettings): boolean {
5555
return Storage.set(STORAGE_KEYS.settings, settings);
5656
}
5757

58-
// ── 当前生效坐标缓存 ─────────────────────────────────────────────────
58+
// ── 旧版当前生效坐标缓存清理 ───────────────────────────────────────────
5959

60-
// 读取本地缓存的设备生效坐标(上次成功 save/query 的结果)
61-
export function loadActiveCache(): ActiveLocation | null {
62-
return Storage.get<ActiveLocation>(STORAGE_KEYS.activeCache);
63-
}
64-
65-
// 写入生效坐标缓存
66-
export function saveActiveCache(loc: ActiveLocation | null): void {
67-
if (loc == null) Storage.remove(STORAGE_KEYS.activeCache);
68-
else Storage.set(STORAGE_KEYS.activeCache, loc);
60+
// 当前生效坐标必须来自 WLOC 模块查询结果,不能使用脚本本地缓存兜底。
61+
export function clearActiveCache(): void {
62+
Storage.remove(STORAGE_KEYS.activeCache);
6963
}

0 commit comments

Comments
 (0)