diff --git a/src/app/xtream-tauri/portal-channels-list/portal-channels-list.component.ts b/src/app/xtream-tauri/portal-channels-list/portal-channels-list.component.ts index d1d057d8a..4a55d00ad 100644 --- a/src/app/xtream-tauri/portal-channels-list/portal-channels-list.component.ts +++ b/src/app/xtream-tauri/portal-channels-list/portal-channels-list.component.ts @@ -25,7 +25,6 @@ import { ActivatedRoute } from '@angular/router'; import { TranslateModule } from '@ngx-translate/core'; import { XtreamCategory } from '../../../../shared/xtream-category.interface'; import { XtreamItem } from '../../../../shared/xtream-item.interface'; -import { DatabaseService } from '../../services/database.service'; import { FilterPipe } from '../../shared/pipes/filter.pipe'; import { FavoritesService } from '../services/favorites.service'; import { XtreamStore } from '../xtream.store'; @@ -37,7 +36,6 @@ interface EpgProgram { end: string; start_timestamp: string; stop_timestamp: string; - // ...other properties } @Component({ @@ -65,7 +63,6 @@ export class PortalChannelsListComponent implements AfterViewInit { readonly xtreamStore = inject(XtreamStore); private readonly favoritesService = inject(FavoritesService); - private readonly dbService = inject(DatabaseService); private readonly route = inject(ActivatedRoute); readonly channels = this.xtreamStore.selectItemsFromSelectedCategory; @@ -180,46 +177,20 @@ export class PortalChannelsListComponent implements AfterViewInit { return selectedCategory !== null && selectedCategory === itemId; } - async toggleFavorite(event: Event, item: any): Promise { + toggleFavorite(event: Event, item: any) { event.stopPropagation(); - const playlist = this.xtreamStore.currentPlaylist(); - - // Update UI state immediately - const currentFavoriteState = - this.favorites.get(item.xtream_id) || false; - this.favorites.set(item.xtream_id, !currentFavoriteState); - - try { - const db = await this.dbService.getConnection(); - const content: any = await db.select( - 'SELECT id FROM content WHERE xtream_id = ?', - [item.xtream_id] - ); - - if (!content || content.length === 0) { - console.error('Content not found in database'); - // Revert UI state on error - this.favorites.set(item.xtream_id, currentFavoriteState); - return; - } - - const contentId = content[0].id; - - if (!currentFavoriteState) { - await this.favoritesService.addToFavorites({ - content_id: contentId, - playlist_id: playlist.id, - }); - } else { - await this.favoritesService.removeFromFavorites( - contentId, - playlist.id - ); - } - } catch (error) { - console.error('Error toggling favorite:', error); - // Revert UI state on error - this.favorites.set(item.xtream_id, currentFavoriteState); - } + this.xtreamStore + .toggleFavorite( + item.xtream_id, + this.xtreamStore.currentPlaylist().id + ) + .then((result: boolean) => { + if (result) { + this.favorites.set(item.xtream_id, true); + } else { + this.favorites.delete(item.xtream_id); + } + this.cdr.detectChanges(); + }); } } diff --git a/src/app/xtream-tauri/with-favorites.feature.ts b/src/app/xtream-tauri/with-favorites.feature.ts index bec41359f..61f8871ef 100644 --- a/src/app/xtream-tauri/with-favorites.feature.ts +++ b/src/app/xtream-tauri/with-favorites.feature.ts @@ -20,6 +20,7 @@ export const withFavorites = function () { favoritesService = inject(FavoritesService) ) => ({ async toggleFavorite(xtreamId: number, playlistId: string) { + let result = false; if (!xtreamId || !playlistId) return; const db = await dbService.getConnection(); @@ -47,14 +48,17 @@ export const withFavorites = function () { contentId, playlistId ); + result = false; } else { await favoritesService.addToFavorites({ content_id: contentId, playlist_id: playlistId, }); + result = true; } patchState(store, { isFavorite: !isFavorite }); + return result; }, async checkFavoriteStatus(