forked from Blendman974/faceclaw
EvenHub app uses logo; move login from the Settings app into the EvenHub app
This commit is contained in:
1 parent
83e0fa919f
commit
deac81f98f
15 files changed
+443
-90
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { type GrayImage } from "../graphics/image";
|
||||
import { type Plane } from "../graphics/plane";
|
||||
import { type IconName } from "../graphics/icons";
|
||||
import { type LayerActions } from "../ui/layers";
|
||||
@@ -18,6 +19,8 @@ export type AppDefinition = {
|
||||
title: string;
|
||||
/** Launcher-grid icon. */
|
||||
icon: IconName;
|
||||
/** App-supplied launcher icon renderer; icon remains the fallback. */
|
||||
renderIcon?: (size: number) => GrayImage | null;
|
||||
/** Open the app's window (or focus it if already open). */
|
||||
launch: (ctx: AppContext, params?: AppLaunchParams) => Promise<void>;
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { ConfigSettingString } from "../../ui/dashboard-settings";
|
||||
|
||||
/**
|
||||
* EvenHub owns its account configuration. The storage keys intentionally
|
||||
* match the former Settings-app entries so existing users stay signed in.
|
||||
*/
|
||||
export const evenHubEmailSetting = new ConfigSettingString({
|
||||
id: "evenhub-email",
|
||||
label: "Email",
|
||||
storageKey: "integrations.evenhub.email",
|
||||
defaultValue: "",
|
||||
editorTitle: "Email",
|
||||
glassesEditTitle: "Edit Even email",
|
||||
inputKind: "email",
|
||||
normalize: (value) => (value ?? "").replace(/[\x00-\x1f]+/g, "").trim(),
|
||||
});
|
||||
|
||||
export const evenHubPasswordSetting = new ConfigSettingString({
|
||||
id: "evenhub-password",
|
||||
label: "Password",
|
||||
storageKey: "integrations.evenhub.password",
|
||||
defaultValue: "",
|
||||
editorTitle: "Password",
|
||||
glassesEditTitle: "Edit Even password",
|
||||
inputKind: "password",
|
||||
normalize: (value) => (value ?? "").replace(/[\x00-\x1f]+/g, ""),
|
||||
formatValue: (value) => (value ? "(set)" : "(not set)"),
|
||||
});
|
||||
|
||||
export function hasEvenHubCredentials(): boolean {
|
||||
return Boolean(evenHubEmailSetting.get().trim() && evenHubPasswordSetting.get());
|
||||
}
|
||||
|
||||
export function clearEvenHubPassword(): void {
|
||||
evenHubPasswordSetting.set("");
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
* the same signed-header scheme used by the firmware API.
|
||||
*/
|
||||
import { Application } from "@nativescript/core";
|
||||
import { evenHubEmailSetting, evenHubPasswordSetting } from "../../ui/dashboard-settings";
|
||||
import { evenHubEmailSetting, evenHubPasswordSetting, hasEvenHubCredentials } from "./credentials";
|
||||
|
||||
declare const android: any;
|
||||
|
||||
@@ -60,7 +60,14 @@ export type EvenHubAppDownload = {
|
||||
};
|
||||
|
||||
export function isEvenHubStoreConfigured(): boolean {
|
||||
return Boolean(evenHubEmailSetting.get().trim() && evenHubPasswordSetting.get());
|
||||
return hasEvenHubCredentials();
|
||||
}
|
||||
|
||||
export class EvenHubAuthenticationError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = "EvenHubAuthenticationError";
|
||||
}
|
||||
}
|
||||
|
||||
export class EvenHubApiClient {
|
||||
@@ -162,7 +169,7 @@ export class EvenHubApiClient {
|
||||
private ensureLogin(): Promise<string> {
|
||||
if (this.token) return Promise.resolve(this.token);
|
||||
if (!isEvenHubStoreConfigured()) {
|
||||
return Promise.reject(new Error("Set the EvenHub email and password in Settings > EvenHub."));
|
||||
return Promise.reject(new EvenHubAuthenticationError("Enter your Even account email and password."));
|
||||
}
|
||||
if (!this.loginPromise) {
|
||||
this.loginPromise = this.login().finally(() => {
|
||||
@@ -173,15 +180,20 @@ export class EvenHubApiClient {
|
||||
}
|
||||
|
||||
private async login(): Promise<string> {
|
||||
const response = await this.request("POST", "/v2/g/login", {
|
||||
commonVersion: 1,
|
||||
body: { email: evenHubEmailSetting.get().trim(), passwd: evenHubPasswordSetting.get() },
|
||||
});
|
||||
const data = asRecord(unwrap(response.envelope, response.httpStatus, "/v2/g/login"));
|
||||
const token = typeof data.token === "string" ? data.token : "";
|
||||
if (!token) throw new Error("Even login succeeded without returning a session token.");
|
||||
this.token = token;
|
||||
return token;
|
||||
try {
|
||||
const response = await this.request("POST", "/v2/g/login", {
|
||||
commonVersion: 1,
|
||||
body: { email: evenHubEmailSetting.get().trim(), passwd: evenHubPasswordSetting.get() },
|
||||
});
|
||||
const data = asRecord(unwrap(response.envelope, response.httpStatus, "/v2/g/login"));
|
||||
const token = typeof data.token === "string" ? data.token : "";
|
||||
if (!token) throw new Error("Even login succeeded without returning a session token.");
|
||||
this.token = token;
|
||||
return token;
|
||||
} catch (error) {
|
||||
if (error instanceof EvenHubAuthenticationError) throw error;
|
||||
throw new EvenHubAuthenticationError(String((error as Error)?.message ?? error));
|
||||
}
|
||||
}
|
||||
|
||||
private async request(
|
||||
@@ -321,7 +333,7 @@ async function fetchWithTimeout(url: string, init: RequestInit, timeoutMs = REQU
|
||||
|
||||
function unwrap(envelope: ApiEnvelope, httpStatus: number, path: string): unknown {
|
||||
if (httpStatus === 401) {
|
||||
throw new Error("Even rejected the login session. Check Settings > EvenHub.");
|
||||
throw new EvenHubAuthenticationError("Even rejected the login session.");
|
||||
}
|
||||
if (httpStatus < 200 || httpStatus >= 300) {
|
||||
throw new Error(`EvenHub request failed (HTTP ${httpStatus}, ${path}).`);
|
||||
|
||||
@@ -12,6 +12,8 @@ import { shell } from "../../ui/shell/shell";
|
||||
import { EvenHubStoreLayer } from "./store-layer";
|
||||
import { launchPackage } from "./manager";
|
||||
import { createInProcessWindow, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { renderEvenRealitiesLogo } from "../../graphics/even-realities-logo";
|
||||
import { makeImageWindowIcon, windowIcon } from "../../ui/shell/chrome-layer";
|
||||
|
||||
const EVENHUB_STORE_WINDOW_ID = "evenhub:store";
|
||||
const EVENHUB_STORE_SURFACE_ID = "window:evenhub:store";
|
||||
@@ -28,6 +30,7 @@ const evenhubApp: AppDefinition = {
|
||||
appId: "evenhub",
|
||||
title: "EvenHub",
|
||||
icon: "package",
|
||||
renderIcon: renderEvenRealitiesLogo,
|
||||
launch: async (ctx) => {
|
||||
const existing = shell.getWindows().find((window) => window.windowId === EVENHUB_STORE_WINDOW_ID);
|
||||
if (existing) {
|
||||
@@ -38,7 +41,6 @@ const evenhubApp: AppDefinition = {
|
||||
let created: InProcessWindow | null = null;
|
||||
const store = new EvenHubStoreLayer({
|
||||
launchApp: (appId) => ctx.launchApp(appId),
|
||||
openSettings: () => ctx.launchApp("settings", { section: "EvenHub" }),
|
||||
appendLog: ctx.appendLog,
|
||||
});
|
||||
await ctx.launchInProcessApp(EVENHUB_STORE_WINDOW_ID, EVENHUB_STORE_SURFACE_ID, (options) => {
|
||||
@@ -48,6 +50,7 @@ const evenhubApp: AppDefinition = {
|
||||
title: "EvenHub",
|
||||
iconLetter: "EH",
|
||||
icon: "package",
|
||||
drawIcon: makeImageWindowIcon(renderEvenRealitiesLogo, windowIcon("package", "EH")),
|
||||
closeable: true,
|
||||
menuItems: () => (created?.stack.isAtBase() ? store.buildMenuItems() : []),
|
||||
actions: options.actions,
|
||||
@@ -55,7 +58,10 @@ const evenhubApp: AppDefinition = {
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: options.onClosed,
|
||||
onClosed: () => {
|
||||
store.closeCredentialEditor(options.actions);
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
return created;
|
||||
});
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/bdffont";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures";
|
||||
import { type DashboardInputEvent, type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { type DashboardInputEvent, type Layer, type LayerActions, type LayerContext } from "../../ui/layers";
|
||||
import {
|
||||
drawListScrollbar,
|
||||
drawSelectionHighlight,
|
||||
@@ -11,7 +11,17 @@ import {
|
||||
type MenuItem,
|
||||
} from "../../ui/menu";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { evenHubApi, isEvenHubStoreConfigured, type EvenHubStoreApp } from "./even-api";
|
||||
import {
|
||||
evenHubApi,
|
||||
EvenHubAuthenticationError,
|
||||
isEvenHubStoreConfigured,
|
||||
type EvenHubStoreApp,
|
||||
} from "./even-api";
|
||||
import {
|
||||
clearEvenHubPassword,
|
||||
evenHubEmailSetting,
|
||||
evenHubPasswordSetting,
|
||||
} from "./credentials";
|
||||
import { EvenHubStoreDetailLayer } from "./store-detail-layer";
|
||||
|
||||
const HEADER_HEIGHT = 34;
|
||||
@@ -21,7 +31,6 @@ const LIST_X = 18;
|
||||
|
||||
export type EvenHubStoreLayerOptions = {
|
||||
launchApp: (appId: string) => Promise<void> | void;
|
||||
openSettings: () => Promise<void> | void;
|
||||
appendLog: (message: string) => void;
|
||||
};
|
||||
|
||||
@@ -34,14 +43,20 @@ export class EvenHubStoreLayer implements Layer {
|
||||
private nextPage = 1;
|
||||
private started = false;
|
||||
private loading = false;
|
||||
private status = "";
|
||||
private showingLogin = !isEvenHubStoreConfigured();
|
||||
private status = this.showingLogin ? "Sign in to browse public apps." : "";
|
||||
private credentialEditorOpen = false;
|
||||
|
||||
constructor(private readonly options: EvenHubStoreLayerOptions) {}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
if (!this.started) {
|
||||
this.started = true;
|
||||
void this.reload(ctx);
|
||||
if (this.showingLogin) {
|
||||
this.openCredentialEditor(ctx);
|
||||
} else {
|
||||
void this.reload(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
const font = getDefaultSmallFont();
|
||||
@@ -49,11 +64,26 @@ export class EvenHubStoreLayer implements Layer {
|
||||
const image = new GrayImage(width, height, 0);
|
||||
image.drawText(font, LIST_X, 8, "EvenHub", 220);
|
||||
|
||||
const subtitle = !isEvenHubStoreConfigured()
|
||||
? "Set account details in Settings > EvenHub"
|
||||
: this.status || (this.total ? `${this.apps.length} of ${this.total} public apps` : "Public apps");
|
||||
const subtitle = this.status || (this.total ? `${this.apps.length} of ${this.total} public apps` : "Public apps");
|
||||
image.drawText(font, LIST_X, 22, truncateText(font, subtitle, width - LIST_X * 2), 125);
|
||||
|
||||
if (this.showingLogin) {
|
||||
const message = this.loading
|
||||
? "Signing in..."
|
||||
: "Enter your Even account email and password in the phone app.";
|
||||
for (const [index, line] of wrapText(font, message, width - LIST_X * 2).entries()) {
|
||||
image.drawText(font, LIST_X, HEADER_HEIGHT + 22 + index * 15, line, 190);
|
||||
}
|
||||
image.drawText(
|
||||
font,
|
||||
LIST_X,
|
||||
height - 16,
|
||||
truncateText(font, `${GESTURE_CLICK} edit credentials ${GESTURE_DOUBLE_CLICK} back`, width - LIST_X * 2),
|
||||
105,
|
||||
);
|
||||
return image;
|
||||
}
|
||||
|
||||
if (this.apps.length === 0) {
|
||||
const message = this.loading ? "Loading apps..." : this.status || "No apps returned.";
|
||||
image.drawText(font, LIST_X, HEADER_HEIGHT + 28, truncateText(font, message, width - LIST_X * 2), 190);
|
||||
@@ -83,14 +113,20 @@ export class EvenHubStoreLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
const hint = !isEvenHubStoreConfigured()
|
||||
? `${GESTURE_CLICK} settings ${GESTURE_DOUBLE_CLICK} back`
|
||||
: `${GESTURE_SCROLL} select ${GESTURE_CLICK} details ${GESTURE_DOUBLE_CLICK} back`;
|
||||
const hint = `${GESTURE_SCROLL} select ${GESTURE_CLICK} details ${GESTURE_DOUBLE_CLICK} back`;
|
||||
image.drawText(font, LIST_X, height - 16, truncateText(font, hint, width - LIST_X * 2), 105);
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (this.showingLogin) {
|
||||
if (event.type === "click" && !this.loading) {
|
||||
this.openCredentialEditor(ctx);
|
||||
} else if (event.type === "double-click") {
|
||||
shell.yieldFocusToSidebar();
|
||||
}
|
||||
return;
|
||||
}
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
||||
@@ -102,10 +138,6 @@ export class EvenHubStoreLayer implements Layer {
|
||||
}
|
||||
return;
|
||||
case "click":
|
||||
if (!isEvenHubStoreConfigured()) {
|
||||
await this.options.openSettings();
|
||||
return;
|
||||
}
|
||||
if (!this.loading) {
|
||||
const app = this.apps[this.selectedIndex];
|
||||
if (app) {
|
||||
@@ -127,6 +159,7 @@ export class EvenHubStoreLayer implements Layer {
|
||||
}
|
||||
|
||||
buildMenuItems(): MenuItem[] {
|
||||
if (this.showingLogin) return [];
|
||||
return [
|
||||
{
|
||||
label: "Refresh",
|
||||
@@ -136,15 +169,23 @@ export class EvenHubStoreLayer implements Layer {
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "EvenHub settings",
|
||||
label: "Log Out",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
void this.options.openSettings();
|
||||
clearEvenHubPassword();
|
||||
evenHubApi.clearSession();
|
||||
this.enterLogin(ctx, "Signed out.");
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
closeCredentialEditor(actions: Pick<LayerActions, "endTextSettingEdit">): void {
|
||||
if (!this.credentialEditorOpen) return;
|
||||
this.credentialEditorOpen = false;
|
||||
void actions.endTextSettingEdit();
|
||||
}
|
||||
|
||||
private async reload(ctx: LayerContext): Promise<void> {
|
||||
if (this.loading) return;
|
||||
evenHubApi.clearSession();
|
||||
@@ -155,7 +196,7 @@ export class EvenHubStoreLayer implements Layer {
|
||||
this.scrollRow = 0;
|
||||
this.status = "";
|
||||
if (!isEvenHubStoreConfigured()) {
|
||||
ctx.actions.requestRender();
|
||||
this.enterLogin(ctx);
|
||||
return;
|
||||
}
|
||||
await this.loadNextPage(ctx);
|
||||
@@ -174,7 +215,13 @@ export class EvenHubStoreLayer implements Layer {
|
||||
this.nextPage = page.page + 1;
|
||||
this.status = "";
|
||||
} catch (error) {
|
||||
this.status = cleanError(error);
|
||||
const message = cleanError(error);
|
||||
if (error instanceof EvenHubAuthenticationError) {
|
||||
this.options.appendLog(`evenhub store login failed: ${message}`);
|
||||
this.enterLogin(ctx, `Login failed: ${message}`);
|
||||
return;
|
||||
}
|
||||
this.status = message;
|
||||
this.options.appendLog(`evenhub store: ${this.status}`);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
@@ -182,6 +229,38 @@ export class EvenHubStoreLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
private enterLogin(ctx: LayerContext, status = "Sign in to browse public apps."): void {
|
||||
this.showingLogin = true;
|
||||
this.loading = false;
|
||||
this.apps = [];
|
||||
this.total = 0;
|
||||
this.status = status;
|
||||
this.openCredentialEditor(ctx);
|
||||
ctx.actions.requestRender();
|
||||
}
|
||||
|
||||
private openCredentialEditor(ctx: LayerContext): void {
|
||||
if (this.credentialEditorOpen) return;
|
||||
this.credentialEditorOpen = true;
|
||||
void ctx.actions.startTextSettingsEdit(
|
||||
[evenHubEmailSetting, evenHubPasswordSetting],
|
||||
"Sign in to EvenHub",
|
||||
() => {
|
||||
this.credentialEditorOpen = false;
|
||||
void this.submitCredentials(ctx);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
private async submitCredentials(ctx: LayerContext): Promise<void> {
|
||||
if (!isEvenHubStoreConfigured()) {
|
||||
this.enterLogin(ctx, "Email and password are required.");
|
||||
return;
|
||||
}
|
||||
this.showingLogin = false;
|
||||
await this.reload(ctx);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function cleanError(error: unknown): string {
|
||||
|
||||
@@ -25,7 +25,12 @@ const launcherApp: AppDefinition = {
|
||||
apps: () => [
|
||||
...ctx.apps
|
||||
.filter((app) => app.showInLauncher !== false)
|
||||
.map((app) => ({ appId: app.appId, label: app.title, icon: app.icon })),
|
||||
.map((app) => ({
|
||||
appId: app.appId,
|
||||
label: app.title,
|
||||
icon: app.icon,
|
||||
renderIcon: app.renderIcon,
|
||||
})),
|
||||
...getInstalledEvenHubApps().map((app) => ({
|
||||
appId: installedEvenHubAppId(app.packageId),
|
||||
label: app.name,
|
||||
|
||||
Reference in new issue
Block a user