forked from Blendman974/faceclaw
164 lines
6.1 KiB
TypeScript
164 lines
6.1 KiB
TypeScript
/** Launcher assignments keep their legacy format; explicitly created folders may be empty. */
|
|
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
|
|
|
import { renameLauncherFolderOrder, removeLauncherFolderOrder } from "./launcher-layout";
|
|
|
|
const STORAGE_KEY = "launcher.folders";
|
|
const NAMES_KEY = "launcher.folderNames";
|
|
|
|
/** Only user-created/renamed folders persist after their last member leaves. */
|
|
export function getExplicitFolderNames(): string[] {
|
|
try {
|
|
const value = JSON.parse(getStringSetting(NAMES_KEY, "[]"));
|
|
return Array.isArray(value) ? [...new Set(value.filter((name): name is string =>
|
|
typeof name === "string" && !!name.trim()).map(name => name.trim()))] : [];
|
|
} catch { return []; }
|
|
}
|
|
|
|
export function folderNameError(input: string, previous?: string): string | null {
|
|
const name = input.trim();
|
|
if (!name) return "Enter a folder name.";
|
|
if (name.length > FOLDER_NAME_MAX_LENGTH) return `Use at most ${FOLDER_NAME_MAX_LENGTH} characters.`;
|
|
if (/[\u0000-\u001f\u007f]/.test(name)) return "Use a single-line folder name.";
|
|
if (previous !== undefined && !getFolders().has(previous)) return "This folder no longer exists.";
|
|
for (const existing of getFolders().keys()) {
|
|
if (existing !== previous && existing.toLowerCase() === name.toLowerCase()) return "A folder with this name already exists.";
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function createFolder(input: string): boolean {
|
|
if (folderNameError(input)) return false;
|
|
setStringSetting(NAMES_KEY, JSON.stringify([...getExplicitFolderNames(), input.trim()]));
|
|
return true;
|
|
}
|
|
|
|
export function renameFolder(previous: string, input: string): boolean {
|
|
if (folderNameError(input, previous)) return false;
|
|
const name = input.trim();
|
|
if (name === previous) return true;
|
|
const assignments = getFolderAssignments();
|
|
for (const appId of Object.keys(assignments)) if (assignments[appId] === previous) assignments[appId] = name;
|
|
// Preserve position and per-folder order when the visible name changes.
|
|
renameLauncherFolderOrder(previous, name);
|
|
setStringSetting(NAMES_KEY, JSON.stringify([...getExplicitFolderNames().filter(n => n !== previous), name]));
|
|
setStringSetting(STORAGE_KEY, JSON.stringify(assignments));
|
|
return true;
|
|
}
|
|
|
|
/** The out-of-the-box grouping, applied only while the setting is unset. */
|
|
const DEFAULT_ASSIGNMENTS: Record<string, string> = {
|
|
blocks: "Games",
|
|
freecell: "Games",
|
|
minesweeper: "Games",
|
|
pinball: "Games",
|
|
flappy: "Games",
|
|
calculator: "Prototypes",
|
|
microphones: "Prototypes",
|
|
nightscout: "Prototypes",
|
|
roam: "Prototypes",
|
|
};
|
|
|
|
export const FOLDER_NAME_MAX_LENGTH = 24;
|
|
|
|
/** appId -> folder name. Malformed stored JSON reads as "no folders". */
|
|
export function getFolderAssignments(): Record<string, string> {
|
|
const raw = getStringSetting(STORAGE_KEY, "");
|
|
if (!raw) return { ...DEFAULT_ASSIGNMENTS };
|
|
try {
|
|
const parsed = JSON.parse(raw);
|
|
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return {};
|
|
const assignments: Record<string, string> = Object.create(null);
|
|
for (const appId of Object.keys(parsed)) {
|
|
const folder = parsed[appId];
|
|
if (typeof folder === "string" && folder.trim()) {
|
|
assignments[appId] = folder.trim();
|
|
}
|
|
}
|
|
return assignments;
|
|
} catch {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
/** folder name -> appIds (assignment order), folder names sorted. */
|
|
export function getFolders(): Map<string, string[]> {
|
|
const assignments = getFolderAssignments();
|
|
const unsorted = new Map<string, string[]>();
|
|
for (const appId of Object.keys(assignments)) {
|
|
const folder = assignments[appId]!;
|
|
const members = unsorted.get(folder);
|
|
if (members) {
|
|
members.push(appId);
|
|
} else {
|
|
unsorted.set(folder, [appId]);
|
|
}
|
|
}
|
|
for (const name of getExplicitFolderNames()) if (!unsorted.has(name)) unsorted.set(name, []);
|
|
const folders = new Map<string, string[]>();
|
|
for (const name of Array.from(unsorted.keys()).sort((a, b) => a.localeCompare(b))) {
|
|
folders.set(name, unsorted.get(name)!);
|
|
}
|
|
return folders;
|
|
}
|
|
|
|
/** Assign an app to a folder, or (with null) return it to the top level. */
|
|
export function setAppFolder(appId: string, folder: string | null): void {
|
|
const assignments = getFolderAssignments();
|
|
if (folder) {
|
|
assignments[appId] = folder;
|
|
} else {
|
|
delete assignments[appId];
|
|
}
|
|
setStringSetting(STORAGE_KEY, JSON.stringify(assignments));
|
|
}
|
|
|
|
/** Move every app out of a folder. Returns how many apps were moved. */
|
|
export function disbandFolder(folder: string): number {
|
|
const assignments = getFolderAssignments();
|
|
let moved = 0;
|
|
for (const appId of Object.keys(assignments)) {
|
|
if (assignments[appId] === folder) {
|
|
delete assignments[appId];
|
|
moved++;
|
|
}
|
|
}
|
|
setStringSetting(NAMES_KEY, JSON.stringify(getExplicitFolderNames().filter(name => name !== folder)));
|
|
removeLauncherFolderOrder(folder);
|
|
if (moved > 0) {
|
|
setStringSetting(STORAGE_KEY, JSON.stringify(assignments));
|
|
}
|
|
return moved;
|
|
}
|
|
|
|
/**
|
|
* An unused name for a ring-created folder ("New folder", "New folder 2", …).
|
|
* Ring input has no text entry, so new folders start with a generated name;
|
|
* Rename folder uses the existing phone text editor for a custom name.
|
|
*/
|
|
export function unusedNewFolderName(): string {
|
|
const existing = new Set(Array.from(getFolders().keys()).map((name) => name.toLowerCase()));
|
|
for (let i = 1; ; i++) {
|
|
const candidate = i === 1 ? "New folder" : `New folder ${i}`;
|
|
if (!existing.has(candidate.toLowerCase())) return candidate;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Match an existing folder name case-insensitively (so "games" reuses
|
|
* "Games" instead of creating a near-duplicate), else the trimmed input.
|
|
*/
|
|
export function resolveFolderName(input: string): string {
|
|
const trimmed = input.trim();
|
|
for (const name of getFolders().keys()) {
|
|
if (name.toLowerCase() === trimmed.toLowerCase()) return name;
|
|
}
|
|
return trimmed;
|
|
}
|
|
|
|
/** Raw stored value, for cheap change detection by the launcher window. */
|
|
export function getFolderStateFingerprint(): string {
|
|
return JSON.stringify([getStringSetting(STORAGE_KEY, ""), getStringSetting(NAMES_KEY, ""),
|
|
getStringSetting("launcher.folderNameDraft", "")]);
|
|
}
|