Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions apps/mobile/src/apps/Media.tsx

Large diffs are not rendered by default.

100 changes: 100 additions & 0 deletions apps/mobile/src/apps/PhotoViewer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { useMemo, useRef, useState } from "react";
import { Modal, PanResponder, StatusBar, Text, View, type GestureResponderEvent, type ImageSourcePropType, type StyleProp, type ViewStyle } from "react-native";
import { Image } from "expo-image";
import { FIXED, num, type } from "../ui";

type Source = { uri: string; headers?: Record<string, string> };

/** How far a finger goes sideways before a swipe steps to the next photo. */
const SWIPE = 60;
const MAX_ZOOM = 6;
/** Zoomed in this far, the original replaces the preview. */
const ORIGINAL_AT = 1.2;

/**
* A photo that swipes to its neighbours (#557) and, with `zoom`, is pinched or double-tapped larger; a single tap is
* `onTap`. Plain responder events (no gesture library): the app has none, and these few gestures do not need one.
* Zoomed past ORIGINAL_AT, `original` is loaded over the preview.
*/
export function SwipePhoto({ preview, original, zoom, onPrev, onNext, onTap, style }: { preview: Source; original?: Source | null; zoom?: boolean; onPrev?: () => void; onNext?: () => void; onTap?: () => void; style?: StyleProp<ViewStyle> }) {
const [view, setView] = useState({ s: 1, x: 0, y: 0 });
const [full, setFull] = useState(false);
const cur = useRef(view);
cur.current = view;
const props = useRef({ zoom, onPrev, onNext, onTap });
props.current = { zoom, onPrev, onNext, onTap };
const g = useRef({ start: view, d0: 0, mid0: [0, 0], at: 0, moved: false, two: false, lastTap: 0, tapTimer: null as ReturnType<typeof setTimeout> | null });

const responder = useMemo(() => {
const dist = (e: GestureResponderEvent) => { const t = e.nativeEvent.touches; return t.length < 2 ? 0 : Math.hypot(t[0].pageX - t[1].pageX, t[0].pageY - t[1].pageY); };
const set = (v: { s: number; x: number; y: number }) => { cur.current = v; setView(v); };
return PanResponder.create({
onStartShouldSetPanResponder: () => true,
onMoveShouldSetPanResponder: () => true,
// Inline (no zoom) a page that scrolls may take a vertical drag over; full screen keeps every touch.
onPanResponderTerminationRequest: () => !props.current.zoom,
onPanResponderGrant: (e) => { Object.assign(g.current, { start: cur.current, d0: dist(e), at: Date.now(), moved: false, two: e.nativeEvent.touches.length > 1 }); },
onPanResponderMove: (e, s) => {
const st = g.current, p = props.current;
if (Math.abs(s.dx) + Math.abs(s.dy) > 8) st.moved = true;
if (e.nativeEvent.touches.length > 1 && p.zoom) {
// A second finger came down: pinch from here.
if (!st.two || !st.d0) { st.two = true; st.d0 = dist(e); st.start = cur.current; return; }
const k = Math.min(MAX_ZOOM, Math.max(1, (st.start.s * dist(e)) / st.d0));
set({ s: k, x: st.start.x, y: st.start.y });
if (k > ORIGINAL_AT) setFull(true);
return;
}
if (st.two) return;
// Zoomed: one finger moves the picture. Not zoomed: it follows a swipe a little.
if (st.start.s > 1) set({ s: st.start.s, x: st.start.x + s.dx, y: st.start.y + s.dy });
else set({ s: 1, x: s.dx * 0.4, y: 0 });
},
onPanResponderRelease: (_e, s) => {
const st = g.current, p = props.current;
if (!st.moved && !st.two && Date.now() - st.at < 300) {
// A tap: two quickly zoom in or out (when zooming is on), one is onTap.
const now = Date.now();
if (p.zoom && now - st.lastTap < 300) {
if (st.tapTimer) clearTimeout(st.tapTimer);
st.lastTap = 0;
const zoomed = cur.current.s > 1;
set(zoomed ? { s: 1, x: 0, y: 0 } : { s: 2.5, x: 0, y: 0 });
if (!zoomed) setFull(true);
return;
}
st.lastTap = now;
st.tapTimer = setTimeout(() => p.onTap?.(), p.zoom ? 300 : 0);
return;
}
if (cur.current.s <= 1.02) {
set({ s: 1, x: 0, y: 0 });
if (!st.two && Math.abs(s.dx) > SWIPE && Math.abs(s.dx) > Math.abs(s.dy)) (s.dx < 0 ? p.onNext : p.onPrev)?.();
}
},
onPanResponderTerminate: () => { if (cur.current.s <= 1.02) set({ s: 1, x: 0, y: 0 }); },
});
}, []);

const src = full && original ? original : preview;
return (
<View style={[{ overflow: "hidden" }, style]} {...responder.panHandlers}>
<Image source={src as ImageSourcePropType} placeholder={src === preview ? undefined : (preview as ImageSourcePropType)} placeholderContentFit="contain" contentFit="contain" cachePolicy="disk"
style={{ position: "absolute", top: 0, left: 0, right: 0, bottom: 0, transform: [{ translateX: view.x }, { translateY: view.y }, { scale: view.s }] }} />
</View>
);
}

/** The photo alone on a black screen (#557): swipe to step, pinch or double-tap to zoom, tap or back to leave. */
export function FullPhoto({ visible, preview, original, label, onClose, onPrev, onNext }: { visible: boolean; preview: Source; original?: Source | null; label?: string; onClose: () => void; onPrev?: () => void; onNext?: () => void }) {
return (
<Modal visible={visible} animationType="fade" onRequestClose={onClose} statusBarTranslucent>
<StatusBar hidden={visible} />
<View style={{ flex: 1, backgroundColor: FIXED.black }}>
{/* A new photo starts unzoomed: the key gives each its own view. */}
<SwipePhoto key={preview.uri} preview={preview} original={original} zoom onPrev={onPrev} onNext={onNext} onTap={onClose} style={{ flex: 1 }} />
{label && <Text pointerEvents="none" style={{ position: "absolute", top: 16, right: 16, ...type("caption", "bold"), ...num, color: FIXED.white }}>{label}</Text>}
</View>
</Modal>
);
}
29 changes: 0 additions & 29 deletions apps/mobile/src/ext/hardware/bt/keySeal.test.ts

This file was deleted.

54 changes: 0 additions & 54 deletions apps/mobile/src/ext/hardware/bt/keySeal.ts

This file was deleted.

4 changes: 1 addition & 3 deletions apps/mobile/src/ext/hardware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { registerApp } from "../../apps/registry";
import { registerCameraControlView } from "../../camera/controls";
import { log } from "../../log";
import { onSync } from "../../sync";
import { backupAll } from "./keyBackup";
import { syncDevices } from "./library";
import { syncMeshNetworks } from "./meshNetworks";
import "./proxy";
Expand All @@ -18,11 +17,10 @@ registerCameraControlView("hardware", { Sheet: HardwareSheet });
registerApp({ id: "hardware", label: "Hardware", icon: "light-fixture", needsProject: false, extension: "hardware", Screen: HardwareApp });

// Beside the uploads, after them (08-extensions.md): networks first (a light names its network), then the
// devices, then the sealed keys of anything paired offline. It never delays or fails a photo upload.
// devices. It never delays or fails a photo upload.
onSync(async () => {
try {
await syncMeshNetworks();
await syncDevices();
await backupAll();
} catch (e) { log("info", "hardware sync skipped", { error: e }); }
});
117 changes: 0 additions & 117 deletions apps/mobile/src/ext/hardware/keyBackup.ts

This file was deleted.

16 changes: 8 additions & 8 deletions apps/mobile/src/ext/hardware/keyFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { adoptMeshNetwork, deviceId, forgetGroups, getMeshNetworks, heldHere, im

/**
* The mesh keys as a file (#516): everything another installation needs to control the same Aputure lights without
* pairing them again, on any server (production, staging, a local one). Unlike the key backup (keyBackup.ts: sealed,
* on this account's server) it is plain text the owner carries: the networks' keys with every light's device key,
* pairing them again, on any server (production, staging, a local one). It is plain text the owner carries: the
* networks' keys with every light's device key,
* their counters, and the lights as devices (the importing server gets them under the same ids).
*
* One rule comes with it. A light drops every message whose sequence number is not above the last it saw from the
Expand Down Expand Up @@ -39,21 +39,21 @@ export function exportKeyFile(now = new Date()): string | null {
export async function importKeyFile(file: KeyFile): Promise<{ networks: number; devices: number }> {
for (const n of file.networks) {
importSecrets(n.id, JSON.stringify(n.secrets));
// Another installation set the lights' groups up its own way: start them afresh (as after a restore).
// Another installation set the lights' groups up its own way: start them afresh (as a new holder must).
forgetGroups(n.id);
const seq = seqFor(n.id);
const start = Math.max(seq.reserved, n.seq) + IMPORT_MARGIN;
seq.skipTo(start);
const local = getMeshNetworks().find((x) => x.id === n.id);
const record: MeshNetwork = {
id: n.id, name: local?.name ?? n.name, createdAt: local?.createdAt ?? n.createdAt, backupAt: local?.backupAt ?? null, synced: false,
id: n.id, name: local?.name ?? n.name, createdAt: local?.createdAt ?? n.createdAt, synced: false,
ivIndex: Math.max(n.ivIndex, local?.ivIndex ?? 0), nextUnicast: Math.max(n.nextUnicast, local?.nextUnicast ?? 0), holder: deviceId(), seqFloor: start,
};
// On this server the network may be new, this installation's already, or in another phone's hands: then it is taken over.
let server = (await meshApi.list()).find((x) => x.id === n.id);
if (server && server.holder && server.holder !== deviceId()) server = await meshApi.claim(n.id, deviceId());
adoptMeshNetwork({ ...record, backupAt: server?.backupAt ?? record.backupAt });
await saveMeshNetwork({ ...record, backupAt: server?.backupAt ?? record.backupAt });
const server = (await meshApi.list()).find((x) => x.id === n.id);
if (server && server.holder && server.holder !== deviceId()) await meshApi.claim(n.id, deviceId());
adoptMeshNetwork(record);
await saveMeshNetwork(record);
}
await syncDevices();
let devices = 0;
Expand Down
Loading
Loading