import { Dialogs, Observable } from '@nativescript/core'; import { brightnessSetting, displayLayoutSetting, type BrightnessSetting } from '../ui/dashboard-settings'; type ControlsTab = 'settings' | 'watch' | 'ring'; // Remember the tab across navigation on both platforms, without persisting it. let lastControlsTab: ControlsTab = 'watch'; /** Shared state and actions for remote-controls.xml; transport stays in the host. */ export class RemoteControlsViewModel extends Observable { private _controlsTab: ControlsTab = lastControlsTab; private _batteryLine = ""; get settingsTabVisibility(): "visible" | "collapse" { return this._controlsTab === "settings" ? "visible" : "collapse"; } get watchTabVisibility(): "visible" | "collapse" { return this._controlsTab === "watch" ? "visible" : "collapse"; } get ringTabVisibility(): "visible" | "collapse" { return this._controlsTab === "ring" ? "visible" : "collapse"; } get settingsTabClass(): string { return this._controlsTab === "settings" ? "tab-button tab-button-selected" : "tab-button"; } get watchTabClass(): string { return this._controlsTab === "watch" ? "tab-button tab-button-selected" : "tab-button"; } get ringTabClass(): string { return this._controlsTab === "ring" ? "tab-button tab-button-selected" : "tab-button"; } /** * The watch face's status line reads "Display off": dim it and show the wake * gesture under it. The host overrides this and notifies padFocusClass and * padWakeHintVisibility when it changes. */ protected get padDisplayAsleep(): boolean { return false; } get padFocusClass(): string { return this.padDisplayAsleep ? "pad-focus pad-focus-asleep" : "pad-focus"; } get padWakeHintVisibility(): "visible" | "collapse" { return this.padDisplayAsleep ? "visible" : "collapse"; } /** * A smaller line under the watch face's status line, or "" to hide it: the * charging case's level while the glasses charge in it. The host overrides * this and notifies padDetailLine and padDetailVisibility when it changes. */ get padDetailLine(): string { return ""; } get padDetailVisibility(): "visible" | "collapse" { return this.padDetailLine ? "visible" : "collapse"; } onSettingsTabTap(): void { this.setControlsTab("settings"); } onWatchTabTap(): void { this.setControlsTab("watch"); } onRingTabTap(): void { this.setControlsTab("ring"); } protected setControlsTab(tab: ControlsTab): void { if (this._controlsTab === tab) return; this._controlsTab = tab; // Remembered across navigations (module-level) so the page comes back on // the tab it left on; deliberately not persisted to disk. lastControlsTab = tab; this.notifyPropertyChange("settingsTabVisibility", this.settingsTabVisibility); this.notifyPropertyChange("watchTabVisibility", this.watchTabVisibility); this.notifyPropertyChange("ringTabVisibility", this.ringTabVisibility); this.notifyPropertyChange("settingsTabClass", this.settingsTabClass); this.notifyPropertyChange("watchTabClass", this.watchTabClass); this.notifyPropertyChange("ringTabClass", this.ringTabClass); } // ---- display mode, brightness and battery levels, on the Settings tab ---- /** The Settings tab's battery row (formatBatteryLine), or "" to hide it; set by the host. */ get batteryLine(): string { return this._batteryLine; } set batteryLine(value: string) { if (this._batteryLine === value) return; this._batteryLine = value; this.notifyPropertyChange("batteryLine", value); this.notifyPropertyChange("batteryLineVisibility", this.batteryLineVisibility); } get batteryLineVisibility(): "visible" | "collapse" { return this._batteryLine ? "visible" : "collapse"; } get displayModeLabel(): string { return displayLayoutSetting.displayValue() + " ▾"; } async onDisplayModeTap(): Promise { const current = displayLayoutSetting.get(); // Full Screen is offered only with a bottom or popup app switcher. const values = displayLayoutSetting.values.filter((value) => !displayLayoutSetting.isDisabled(value)); const options = values.map((value) => displayLayoutSetting.displayValue(value) + (value === current ? " ✓" : "")); const picked = await Dialogs.action({ title: "Display mode", cancelButtonText: "Cancel", actions: options }); const index = options.indexOf(picked); if (index < 0) return; const value = values[index]!; if (value !== current) displayLayoutSetting.set(value); this.notifyPropertyChange("displayModeLabel", this.displayModeLabel); } get brightnessAuto(): boolean { return brightnessSetting.get() === "auto"; } get brightnessSliderEnabled(): boolean { return !this.brightnessAuto; } /** The slider's position; while Auto, the last manual level (or 50). */ get brightnessPercent(): number { const value = brightnessSetting.get(); if (value === "auto") return this.lastManualBrightness; const numeric = parseInt(value, 10); return Number.isFinite(numeric) ? numeric : 50; } private lastManualBrightness = 50; onBrightnessChange(args: { value?: number; object?: { value?: number } }): void { if (this.brightnessAuto) return; const raw = typeof args.value === "number" ? args.value : Number(args.object?.value ?? NaN); if (!Number.isFinite(raw)) return; // The setting only has every tenth level; snap to the nearest. const level = Math.min(100, Math.max(2, Math.round(raw / 10) * 10)); this.lastManualBrightness = level; const value = String(level) as BrightnessSetting; if (brightnessSetting.get() !== value) brightnessSetting.set(value); } onBrightnessAutoChange(args: { value?: boolean; object?: { checked?: boolean } }): void { const on = typeof args.value === "boolean" ? args.value : Boolean(args.object?.checked); if (on) { if (brightnessSetting.get() !== "auto") { this.lastManualBrightness = this.brightnessPercent; brightnessSetting.set("auto"); } } else if (brightnessSetting.get() === "auto") { brightnessSetting.set(String(this.lastManualBrightness) as BrightnessSetting); } this.refreshDisplayControls(); } public refreshDisplayControls(): void { this.notifyPropertyChange("brightnessAuto", this.brightnessAuto); this.notifyPropertyChange("brightnessSliderEnabled", this.brightnessSliderEnabled); this.notifyPropertyChange("brightnessPercent", this.brightnessPercent); this.notifyPropertyChange("displayModeLabel", this.displayModeLabel); } }