feat: initial project scaffold with Watch companion app
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import WatchConnectivity from "../../modules/watch-connectivity";
|
||||
|
||||
type WatchConnectionStatus = "connected" | "disconnected" | "pending";
|
||||
|
||||
export function useWatchConnection() {
|
||||
const [status, setStatus] = useState<WatchConnectionStatus>("pending");
|
||||
const [isPaired, setIsPaired] = useState(false);
|
||||
const [isInstalled, setIsInstalled] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
checkStatus();
|
||||
const interval = setInterval(checkStatus, 5000);
|
||||
return () => clearInterval(interval);
|
||||
}, []);
|
||||
|
||||
const checkStatus = async () => {
|
||||
try {
|
||||
const supported = await WatchConnectivity.isSupported();
|
||||
if (!supported) {
|
||||
setStatus("disconnected");
|
||||
return;
|
||||
}
|
||||
|
||||
const paired = await WatchConnectivity.isPaired();
|
||||
const installed = await WatchConnectivity.isWatchAppInstalled();
|
||||
const reachable = await WatchConnectivity.isReachable();
|
||||
|
||||
setIsPaired(paired);
|
||||
setIsInstalled(installed);
|
||||
setStatus(reachable ? "connected" : "disconnected");
|
||||
} catch {
|
||||
setStatus("disconnected");
|
||||
}
|
||||
};
|
||||
|
||||
return { status, isPaired, isInstalled };
|
||||
}
|
||||
Reference in New Issue
Block a user