forked from Blendman974/faceclaw
Navigate app links to mapbox.com to get a token
This commit is contained in:
1 parent
5c17498a46
commit
fb89517a5a
2 files changed
+110
-10
No files matched your search
@@ -28,12 +28,14 @@ import { WindowMenu, WindowMenuLayer } from "../../ui/window-menu";
|
|||||||
import type { LayerContext } from "../../ui/layers";
|
import type { LayerContext } from "../../ui/layers";
|
||||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||||
import { onSettingsStoreChanged } from "../../native/settings-store";
|
import { onSettingsStoreChanged } from "../../native/settings-store";
|
||||||
|
import { openUrlOnPhone } from "../../native/open-url";
|
||||||
import {
|
import {
|
||||||
navigateDestinationAddressDraftSetting,
|
navigateDestinationAddressDraftSetting,
|
||||||
navigateDestinationNameDraftSetting,
|
navigateDestinationNameDraftSetting,
|
||||||
navigateRememberRecentSetting,
|
navigateRememberRecentSetting,
|
||||||
navigateDisplayModeSetting,
|
navigateDisplayModeSetting,
|
||||||
navigateVerticalPositionSetting,
|
navigateVerticalPositionSetting,
|
||||||
|
mapboxApiKeySetting,
|
||||||
enumSettingMenuItem,
|
enumSettingMenuItem,
|
||||||
type ConfigSettingString,
|
type ConfigSettingString,
|
||||||
} from "../../ui/dashboard-settings";
|
} from "../../ui/dashboard-settings";
|
||||||
@@ -68,6 +70,7 @@ import {
|
|||||||
fetchStaticMapGray,
|
fetchStaticMapGray,
|
||||||
geocodeForward,
|
geocodeForward,
|
||||||
isMapboxConfigured,
|
isMapboxConfigured,
|
||||||
|
MAPBOX_TOKEN_SETTING_KEY,
|
||||||
type GeocodeCandidate,
|
type GeocodeCandidate,
|
||||||
type Route,
|
type Route,
|
||||||
type RouteProfile,
|
type RouteProfile,
|
||||||
@@ -190,10 +193,14 @@ type NavTarget =
|
|||||||
| { kind: "query"; query: string; label?: string }
|
| { kind: "query"; query: string; label?: string }
|
||||||
| { kind: "place"; name: string; place: string; longitude: number; latitude: number };
|
| { kind: "place"; name: string; place: string; longitude: number; latitude: number };
|
||||||
|
|
||||||
/** An entry on the idle page's destination list. */
|
/**
|
||||||
|
* An entry on the idle page's list: a destination, or (while no Mapbox
|
||||||
|
* token is set) one of the setup actions offered in its place.
|
||||||
|
*/
|
||||||
type IdleEntry =
|
type IdleEntry =
|
||||||
| { kind: "saved"; destination: SavedDestination }
|
| { kind: "saved"; destination: SavedDestination }
|
||||||
| { kind: "recent"; destination: RecentDestination };
|
| { kind: "recent"; destination: RecentDestination }
|
||||||
|
| { kind: "action"; label: string; detail: string; run: () => void };
|
||||||
|
|
||||||
/** Idle-page list selection (index into idleEntries()). */
|
/** Idle-page list selection (index into idleEntries()). */
|
||||||
let idleSelection = 0;
|
let idleSelection = 0;
|
||||||
@@ -208,6 +215,8 @@ type DestinationEdit = {
|
|||||||
setting: ConfigSettingString;
|
setting: ConfigSettingString;
|
||||||
title: string;
|
title: string;
|
||||||
onDone: (value: string) => void;
|
onDone: (value: string) => void;
|
||||||
|
/** Runs on double-click (cancel); edits of a live setting restore it here. */
|
||||||
|
onCancel?: () => void;
|
||||||
};
|
};
|
||||||
let editing: DestinationEdit | null = null;
|
let editing: DestinationEdit | null = null;
|
||||||
|
|
||||||
@@ -251,7 +260,9 @@ function post(message: WorkerAppReply): void {
|
|||||||
// Destinations edited on the phone (the text editor, or the Settings app's
|
// Destinations edited on the phone (the text editor, or the Settings app's
|
||||||
// Home/Work rows) repaint the idle list / the edit screen live.
|
// Home/Work rows) repaint the idle list / the edit screen live.
|
||||||
onSettingsStoreChanged((key) => {
|
onSettingsStoreChanged((key) => {
|
||||||
if (key.startsWith("navigate.")) render();
|
// The token key repaints too: the phone editor writes it live during
|
||||||
|
// Edit token, and the idle page changes shape once one is set.
|
||||||
|
if (key.startsWith("navigate.") || key === MAPBOX_TOKEN_SETTING_KEY) render();
|
||||||
});
|
});
|
||||||
|
|
||||||
// The host queues messages until this arrives: posts to a worker whose bundle
|
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||||
@@ -374,7 +385,7 @@ function startNavigation(query: string, requestedProfile: RouteProfile): Promise
|
|||||||
async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfile): Promise<string> {
|
async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfile): Promise<string> {
|
||||||
const query = target.kind === "query" ? target.query : target.name;
|
const query = target.kind === "query" ? target.query : target.name;
|
||||||
if (!isMapboxConfigured()) {
|
if (!isMapboxConfigured()) {
|
||||||
statusMessage = "Set a Mapbox token in Settings > API Keys.";
|
statusMessage = "Navigation needs a Mapbox token. This is free (up to a usage limit); open Mapbox in a browser on your phone or pick Edit token below.";
|
||||||
phase = "idle";
|
phase = "idle";
|
||||||
render();
|
render();
|
||||||
throw new Error(statusMessage);
|
throw new Error(statusMessage);
|
||||||
@@ -1036,8 +1047,13 @@ function closeMenusAnd(ctx: LayerContext, action: () => void): void {
|
|||||||
* Open the phone text editor on a staging setting. The editor writes the
|
* Open the phone text editor on a staging setting. The editor writes the
|
||||||
* draft live (repainted via the settings listener); click here confirms.
|
* draft live (repainted via the settings listener); click here confirms.
|
||||||
*/
|
*/
|
||||||
function beginEdit(setting: ConfigSettingString, title: string, onDone: (value: string) => void): void {
|
function beginEdit(
|
||||||
editing = { setting, title, onDone };
|
setting: ConfigSettingString,
|
||||||
|
title: string,
|
||||||
|
onDone: (value: string) => void,
|
||||||
|
onCancel?: () => void,
|
||||||
|
): void {
|
||||||
|
editing = { setting, title, onDone, onCancel };
|
||||||
post({ type: "start-text-setting-edit", settingId: setting.id });
|
post({ type: "start-text-setting-edit", settingId: setting.id });
|
||||||
render();
|
render();
|
||||||
}
|
}
|
||||||
@@ -1047,6 +1063,7 @@ function finishEdit(confirmed: boolean): void {
|
|||||||
editing = null;
|
editing = null;
|
||||||
post({ type: "end-text-setting-edit" });
|
post({ type: "end-text-setting-edit" });
|
||||||
if (current && confirmed) current.onDone(current.setting.get());
|
if (current && confirmed) current.onDone(current.setting.get());
|
||||||
|
else if (current) current.onCancel?.();
|
||||||
// onDone may have started the next step; only fully leave when it didn't.
|
// onDone may have started the next step; only fully leave when it didn't.
|
||||||
if (!editing) {
|
if (!editing) {
|
||||||
navigateDestinationNameDraftSetting.set("");
|
navigateDestinationNameDraftSetting.set("");
|
||||||
@@ -1075,10 +1092,52 @@ function beginAddDestination(): void {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Edit the Mapbox token in place: the phone editor writes the real setting
|
||||||
|
* live, so cancelling puts back whatever was there before.
|
||||||
|
*/
|
||||||
|
function beginTokenEdit(): void {
|
||||||
|
const previous = mapboxApiKeySetting.get();
|
||||||
|
beginEdit(
|
||||||
|
mapboxApiKeySetting,
|
||||||
|
mapboxApiKeySetting.glassesEditTitle,
|
||||||
|
() => {
|
||||||
|
statusMessage = "";
|
||||||
|
},
|
||||||
|
() => mapboxApiKeySetting.set(previous),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Idle page destination list
|
// Idle page destination list
|
||||||
|
|
||||||
|
/** Where a user gets a token: Mapbox's access-tokens page (sign-up/login first when needed). */
|
||||||
|
const MAPBOX_TOKENS_URL = "https://account.mapbox.com/access-tokens/";
|
||||||
|
|
||||||
|
/** Offered in place of the destination list until a Mapbox token is set. */
|
||||||
|
function tokenSetupEntries(): IdleEntry[] {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
kind: "action",
|
||||||
|
label: "Open mapbox.com",
|
||||||
|
detail: "Get a free token in the phone browser",
|
||||||
|
run: () => {
|
||||||
|
statusMessage = openUrlOnPhone(MAPBOX_TOKENS_URL)
|
||||||
|
? "Opened mapbox.com on your phone. Copy your public token (pk...) and pick Edit token."
|
||||||
|
: "Could not open a browser on the phone. Visit account.mapbox.com/access-tokens to get a token.";
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
kind: "action",
|
||||||
|
label: "Edit token",
|
||||||
|
detail: "Type or paste it in the phone app",
|
||||||
|
run: () => beginTokenEdit(),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
function idleEntries(): IdleEntry[] {
|
function idleEntries(): IdleEntry[] {
|
||||||
|
if (!isMapboxConfigured()) return tokenSetupEntries();
|
||||||
const entries: IdleEntry[] = loadSavedDestinations()
|
const entries: IdleEntry[] = loadSavedDestinations()
|
||||||
.filter((destination) => destination.address)
|
.filter((destination) => destination.address)
|
||||||
.map((destination): IdleEntry => ({ kind: "saved", destination }));
|
.map((destination): IdleEntry => ({ kind: "saved", destination }));
|
||||||
@@ -1094,6 +1153,10 @@ function clampIdleSelection(): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function navigateToIdleEntry(entry: IdleEntry): void {
|
function navigateToIdleEntry(entry: IdleEntry): void {
|
||||||
|
if (entry.kind === "action") {
|
||||||
|
entry.run();
|
||||||
|
return;
|
||||||
|
}
|
||||||
const target: NavTarget =
|
const target: NavTarget =
|
||||||
entry.kind === "saved"
|
entry.kind === "saved"
|
||||||
? { kind: "query", query: entry.destination.address, label: entry.destination.name }
|
? { kind: "query", query: entry.destination.address, label: entry.destination.name }
|
||||||
@@ -1233,8 +1296,9 @@ function paintIdle(image: GrayImage, win: NavWindow): void {
|
|||||||
image.drawText(mediumFont, 24, 16, "Navigate", 245);
|
image.drawText(mediumFont, 24, 16, "Navigate", 245);
|
||||||
const busy = phase !== "idle";
|
const busy = phase !== "idle";
|
||||||
const entries = busy ? [] : idleEntries();
|
const entries = busy ? [] : idleEntries();
|
||||||
const hint = !isMapboxConfigured()
|
const configured = isMapboxConfigured();
|
||||||
? "Set a Mapbox token in Settings > API Keys to enable navigation."
|
const hint = !configured
|
||||||
|
? "Navigation needs a Mapbox public token (free at mapbox.com). Get one, then enter it here or in Settings > API Keys."
|
||||||
: entries.length
|
: entries.length
|
||||||
? "Pick a destination below, or say one via Voice input (system menu, long-press)."
|
? "Pick a destination below, or say one via Voice input (system menu, long-press)."
|
||||||
: "Ask the voice assistant to navigate somewhere, or pick Voice input from the system menu (long-press) to say a destination. Save Home, Work and other places from the app menu.";
|
: "Ask the voice assistant to navigate somewhere, or pick Voice input from the system menu (long-press) to say a destination. Save Home, Work and other places from the app menu.";
|
||||||
@@ -1255,7 +1319,7 @@ function paintIdle(image: GrayImage, win: NavWindow): void {
|
|||||||
paintIdleList(image, win, entries, y);
|
paintIdleList(image, win, entries, y);
|
||||||
drawFooter(
|
drawFooter(
|
||||||
image,
|
image,
|
||||||
`${GESTURE_SCROLL} select ${GESTURE_CLICK} go ${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`,
|
`${GESTURE_SCROLL} select ${GESTURE_CLICK} ${configured ? "go" : "choose"} ${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
drawFooter(image, `${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`);
|
drawFooter(image, `${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`);
|
||||||
@@ -1281,7 +1345,18 @@ function paintIdleList(image: GrayImage, win: NavWindow, entries: IdleEntry[], t
|
|||||||
if (selected) drawSelectionHighlight(image, rowX, y, rowWidth, rowHeight - 2, win.focused);
|
if (selected) drawSelectionHighlight(image, rowX, y, rowWidth, rowHeight - 2, win.focused);
|
||||||
const textX = rowX + 6;
|
const textX = rowX + 6;
|
||||||
const textY = y + LIST_ROW_TEXT_INSET;
|
const textY = y + LIST_ROW_TEXT_INSET;
|
||||||
if (entry.kind === "saved") {
|
if (entry.kind === "action") {
|
||||||
|
const label = truncateText(smallFont, entry.label, labelWidth);
|
||||||
|
image.drawText(smallFont, textX, textY, label, selected ? 245 : 210);
|
||||||
|
const detailX = textX + labelWidth + 8;
|
||||||
|
image.drawText(
|
||||||
|
smallFont,
|
||||||
|
detailX,
|
||||||
|
textY,
|
||||||
|
truncateText(smallFont, entry.detail, rowX + rowWidth - 6 - detailX),
|
||||||
|
selected ? 170 : 130,
|
||||||
|
);
|
||||||
|
} else if (entry.kind === "saved") {
|
||||||
const label = truncateText(smallFont, entry.destination.name, labelWidth);
|
const label = truncateText(smallFont, entry.destination.name, labelWidth);
|
||||||
image.drawText(smallFont, textX, textY, label, selected ? 245 : 210);
|
image.drawText(smallFont, textX, textY, label, selected ? 245 : 210);
|
||||||
const detailX = textX + labelWidth + 8;
|
const detailX = textX + labelWidth + 8;
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* Open a URL in the phone's browser. Uses the application context with
|
||||||
|
* NEW_TASK so it works from worker threads (no Activity in hand). Returns
|
||||||
|
* false when no browser is available or on a non-Android platform.
|
||||||
|
*/
|
||||||
|
import { Utils } from "@nativescript/core";
|
||||||
|
|
||||||
|
declare const android: any;
|
||||||
|
declare const global: any;
|
||||||
|
|
||||||
|
export function openUrlOnPhone(url: string): boolean {
|
||||||
|
if (!global.isAndroid) return false;
|
||||||
|
try {
|
||||||
|
const context = Utils.android.getApplicationContext();
|
||||||
|
if (!context) return false;
|
||||||
|
const intent = new android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(url));
|
||||||
|
intent.addCategory(android.content.Intent.CATEGORY_BROWSABLE);
|
||||||
|
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
context.startActivity(intent);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(`open-url: could not open ${url}: ${error}`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user