NuvioStreaming/src/services/catalogOrderService.ts
Amarjit Singh 2262889479 Add catalog/collection reorder, enable/disable toggle, and fix catalog name display
- Add CatalogOrderScreen for reordering catalogs and collections on home screen
- Add catalogOrderService for persisting display order in MMKV
- Add enable/disable toggle for collections in CollectionManagementScreen
- Fix catalog name display in FolderDetailScreen tabs and CollectionEditorScreen
- Filter disabled collections from reorder screen and home screen
- Integrate display order into HomeScreen listData memo

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-22 23:17:18 -07:00

72 lines
2 KiB
TypeScript

import { mmkvStorage } from './mmkvStorage';
import EventEmitter from 'eventemitter3';
const ORDER_STORAGE_KEY = 'catalog_display_order';
export const catalogOrderEmitter = new EventEmitter();
export const CATALOG_ORDER_EVENTS = {
CHANGED: 'catalog_order_changed',
} as const;
export class CatalogOrderService {
private static instance: CatalogOrderService;
private constructor() {}
static getInstance(): CatalogOrderService {
if (!CatalogOrderService.instance) {
CatalogOrderService.instance = new CatalogOrderService();
}
return CatalogOrderService.instance;
}
private async getStorageKey(): Promise<string> {
const scope = await mmkvStorage.getItem('@user:current') || 'local';
return `@user:${scope}:${ORDER_STORAGE_KEY}`;
}
async getOrder(): Promise<string[]> {
try {
const key = await this.getStorageKey();
const json = await mmkvStorage.getItem(key);
if (!json) return [];
const parsed = JSON.parse(json);
if (!Array.isArray(parsed)) return [];
return parsed;
} catch {
return [];
}
}
async saveOrder(order: string[]): Promise<void> {
try {
const key = await this.getStorageKey();
await mmkvStorage.setItem(key, JSON.stringify(order));
catalogOrderEmitter.emit(CATALOG_ORDER_EVENTS.CHANGED);
} catch {
// silent fail
}
}
async resetOrder(): Promise<void> {
try {
const key = await this.getStorageKey();
await mmkvStorage.setItem(key, JSON.stringify([]));
catalogOrderEmitter.emit(CATALOG_ORDER_EVENTS.CHANGED);
} catch {
// silent fail
}
}
/** Build a catalog key from addon/type/id */
static catalogKey(addonId: string, type: string, catalogId: string): string {
return `${addonId}:${type}:${catalogId}`;
}
/** Build a collection key from collection id */
static collectionKey(collectionId: string): string {
return `collection_${collectionId}`;
}
}
export const catalogOrderService = CatalogOrderService.getInstance();