feat(favorites): add button and local store choice
This commit is contained in:
@@ -3,4 +3,5 @@ export const STORAGE_KEYS = {
|
||||
CREDENTIALS: "komgaCredentials",
|
||||
USER: "stripUser",
|
||||
TTL_CONFIG: "ttlConfig",
|
||||
FAVORITES: "stripstream_favorites",
|
||||
} as const;
|
||||
|
||||
31
src/lib/services/favorite.service.ts
Normal file
31
src/lib/services/favorite.service.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { storageService } from "./storage.service";
|
||||
|
||||
export class FavoriteService {
|
||||
/**
|
||||
* Vérifie si une série est dans les favoris
|
||||
*/
|
||||
static isFavorite(seriesId: string): boolean {
|
||||
return storageService.isFavorite(seriesId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajoute une série aux favoris
|
||||
*/
|
||||
static addToFavorites(seriesId: string): void {
|
||||
storageService.addFavorite(seriesId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retire une série des favoris
|
||||
*/
|
||||
static removeFromFavorites(seriesId: string): void {
|
||||
storageService.removeFavorite(seriesId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère tous les IDs des séries favorites
|
||||
*/
|
||||
static getAllFavoriteIds(): string[] {
|
||||
return storageService.getFavorites();
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ const {
|
||||
CREDENTIALS: KOMGACREDENTIALS_KEY,
|
||||
USER: USER_KEY,
|
||||
TTL_CONFIG: TTL_CONFIG_KEY,
|
||||
FAVORITES: FAVORITES_KEY,
|
||||
} = STORAGE_KEYS;
|
||||
|
||||
interface TTLConfig {
|
||||
@@ -16,7 +17,7 @@ interface TTLConfig {
|
||||
imagesTTL: number;
|
||||
}
|
||||
|
||||
class StorageService {
|
||||
export class StorageService {
|
||||
private static instance: StorageService;
|
||||
|
||||
private constructor() {}
|
||||
@@ -170,6 +171,51 @@ class StorageService {
|
||||
ttlConfig: TTL_CONFIG_KEY,
|
||||
};
|
||||
}
|
||||
|
||||
getFavorites(): string[] {
|
||||
try {
|
||||
const favorites = localStorage.getItem(FAVORITES_KEY);
|
||||
return favorites ? JSON.parse(favorites) : [];
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération des favoris:", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
addFavorite(seriesId: string): void {
|
||||
try {
|
||||
const favorites = this.getFavorites();
|
||||
if (!favorites.includes(seriesId)) {
|
||||
favorites.push(seriesId);
|
||||
localStorage.setItem(FAVORITES_KEY, JSON.stringify(favorites));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de l'ajout aux favoris:", error);
|
||||
}
|
||||
}
|
||||
|
||||
removeFavorite(seriesId: string): void {
|
||||
try {
|
||||
const favorites = this.getFavorites();
|
||||
const index = favorites.indexOf(seriesId);
|
||||
if (index > -1) {
|
||||
favorites.splice(index, 1);
|
||||
localStorage.setItem(FAVORITES_KEY, JSON.stringify(favorites));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la suppression des favoris:", error);
|
||||
}
|
||||
}
|
||||
|
||||
isFavorite(seriesId: string): boolean {
|
||||
try {
|
||||
const favorites = this.getFavorites();
|
||||
return favorites.includes(seriesId);
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la vérification des favoris:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const storageService = StorageService.getInstance();
|
||||
|
||||
Reference in New Issue
Block a user