NuvioStreaming/src/services/storageService.ts
2025-04-15 22:12:37 +05:30

108 lines
No EOL
3.1 KiB
TypeScript

import AsyncStorage from '@react-native-async-storage/async-storage';
import { logger } from '../utils/logger';
interface WatchProgress {
currentTime: number;
duration: number;
lastUpdated: number;
}
class StorageService {
private static instance: StorageService;
private readonly WATCH_PROGRESS_KEY = '@watch_progress:';
private watchProgressSubscribers: (() => void)[] = [];
private constructor() {}
public static getInstance(): StorageService {
if (!StorageService.instance) {
StorageService.instance = new StorageService();
}
return StorageService.instance;
}
private getWatchProgressKey(id: string, type: string, episodeId?: string): string {
return this.WATCH_PROGRESS_KEY + `${type}:${id}${episodeId ? `:${episodeId}` : ''}`;
}
public async setWatchProgress(
id: string,
type: string,
progress: WatchProgress,
episodeId?: string
): Promise<void> {
try {
const key = this.getWatchProgressKey(id, type, episodeId);
await AsyncStorage.setItem(key, JSON.stringify(progress));
// Notify subscribers
this.notifyWatchProgressSubscribers();
} catch (error) {
logger.error('Error saving watch progress:', error);
}
}
private notifyWatchProgressSubscribers(): void {
this.watchProgressSubscribers.forEach(callback => callback());
}
public subscribeToWatchProgressUpdates(callback: () => void): () => void {
this.watchProgressSubscribers.push(callback);
// Return unsubscribe function
return () => {
const index = this.watchProgressSubscribers.indexOf(callback);
if (index > -1) {
this.watchProgressSubscribers.splice(index, 1);
}
};
}
public async getWatchProgress(
id: string,
type: string,
episodeId?: string
): Promise<WatchProgress | null> {
try {
const key = this.getWatchProgressKey(id, type, episodeId);
const data = await AsyncStorage.getItem(key);
return data ? JSON.parse(data) : null;
} catch (error) {
logger.error('Error getting watch progress:', error);
return null;
}
}
public async removeWatchProgress(
id: string,
type: string,
episodeId?: string
): Promise<void> {
try {
const key = this.getWatchProgressKey(id, type, episodeId);
await AsyncStorage.removeItem(key);
// Notify subscribers
this.notifyWatchProgressSubscribers();
} catch (error) {
logger.error('Error removing watch progress:', error);
}
}
public async getAllWatchProgress(): Promise<Record<string, WatchProgress>> {
try {
const keys = await AsyncStorage.getAllKeys();
const watchProgressKeys = keys.filter(key => key.startsWith(this.WATCH_PROGRESS_KEY));
const pairs = await AsyncStorage.multiGet(watchProgressKeys);
return pairs.reduce((acc, [key, value]) => {
if (value) {
acc[key.replace(this.WATCH_PROGRESS_KEY, '')] = JSON.parse(value);
}
return acc;
}, {} as Record<string, WatchProgress>);
} catch (error) {
logger.error('Error getting all watch progress:', error);
return {};
}
}
}
export const storageService = StorageService.getInstance();