From 98ff7f4fa4576e00d3de0afd05395f673622ddd8 Mon Sep 17 00:00:00 2001 From: 4gray Date: Mon, 24 Jan 2022 00:31:42 +0100 Subject: [PATCH] feat(pwa): auto-generate playlist with global favorites --- .../recent-playlists.component.html | 11 +- .../recent-playlists.component.scss | 4 + .../channel-list-container.component.html | 2 +- .../channel-list-container.component.ts | 12 +- src/app/services/pwa.service.ts | 174 +++++++++++++++--- 5 files changed, 168 insertions(+), 35 deletions(-) diff --git a/src/app/home/recent-playlists/recent-playlists.component.html b/src/app/home/recent-playlists/recent-playlists.component.html index 62511b4d0..40cae1c8a 100644 --- a/src/app/home/recent-playlists/recent-playlists.component.html +++ b/src/app/home/recent-playlists/recent-playlists.component.html @@ -1,11 +1,20 @@ - cloud + search
{{ 'HOME.PLAYLISTS.NO_PLAYLISTS' | translate }}
{{ 'HOME.PLAYLISTS.ADD_FIRST' | translate }}
+ + star +
Global favorites
+
+ Auto-generated playlist with aggregated favorites from all playlists +
+ +
+ - + { - window.postMessage({ - type: PLAYLIST_PARSE_RESPONSE, - payload: playlist, + if (payload.id === GLOBAL_FAVORITES_PLAYLIST_ID) { + this.sendPlaylistWithGlobalFavorites(); + } else { + this.dbService + .getByIndex(DbStores.Playlists, '_id', payload.id) + .subscribe((playlist) => { + window.postMessage({ + type: PLAYLIST_PARSE_RESPONSE, + payload: playlist, + }); }); - }); + } } else if (type === PLAYLIST_REMOVE_BY_ID) { this.dbService .delete(DbStores.Playlists, payload.id) @@ -98,12 +187,16 @@ export class PwaService extends DataService { }); }); } else if (type === PLAYLIST_GET_ALL) { - this.dbService.getAll(DbStores.Playlists).subscribe((playlists) => { - window.postMessage({ - type: PLAYLIST_GET_ALL_RESPONSE, - payload: playlists, + this.dbService + .getAll(DbStores.Playlists) + .subscribe((playlists: Playlist[]) => { + window.postMessage({ + type: PLAYLIST_GET_ALL_RESPONSE, + payload: playlists.sort( + (a, b) => a.position - b.position + ), + }); }); - }); } else if (type === PLAYLIST_SAVE_DETAILS) { this.dbService .getByID(DbStores.Playlists, payload._id) @@ -142,6 +235,14 @@ export class PwaService extends DataService { .get(`${this.corsProxyUrl}${payload.url}`, { responseType: 'text', }) + .pipe( + catchError((error) => { + window.postMessage({ + type: ERROR, + }); + return throwError(() => error); + }) + ) .subscribe((response: any) => { const refreshedPlaylist = this.convertFileStringToPlaylist(response); @@ -171,6 +272,23 @@ export class PwaService extends DataService { }); }); }); + } else if (type === PLAYLIST_UPDATE_POSITIONS) { + const requests = payload.map((playlist, index) => { + return this.dbService + .getByID(DbStores.Playlists, playlist._id) + .pipe( + switchMap((playlist: Playlist) => { + return this.dbService.update(DbStores.Playlists, { + ...playlist, + position: index, + }); + }) + ); + }); + + combineLatest(requests).subscribe(() => { + console.log('playlist positions were updated...'); + }); } } @@ -180,29 +298,27 @@ export class PwaService extends DataService { */ fetchFromUrl(payload: Partial): void { this.http - .get(`${this.corsProxyUrl}${payload.url}`, { - responseType: 'text', - }) + .get(`${this.corsProxyUrl}${payload.url}`) + .pipe( + catchError((error) => { + window.postMessage({ + type: ERROR, + message: 'something went wrong', + status: error.status, + }); + return throwError(() => error); + }) + ) .subscribe((response: any) => { - const parsedPlaylist = - this.convertFileStringToPlaylist(response); - const playlistObject = this.createPlaylistObject( - payload.title, - parsedPlaylist, - payload.url, - 'URL' - ); - - // save to db this.dbService - .add(DbStores.Playlists, playlistObject) + .add(DbStores.Playlists, response) .subscribe(() => { console.log('playlist was added...'); }); window.postMessage({ type: PLAYLIST_PARSE_RESPONSE, - payload: playlistObject, + payload: response, }); }); }