Skip to content

Commit

Permalink
feat: generate global playlist with all favorites
Browse files Browse the repository at this point in the history
this commit closes #97
  • Loading branch information
4gray committed Jan 29, 2022
1 parent c7d5310 commit 764201a
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 113 deletions.
35 changes: 30 additions & 5 deletions api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import axios from 'axios';
import { app, BrowserWindow, ipcMain, session } from 'electron';
import { parse } from 'iptv-playlist-parser';
import Nedb, { Cursor } from 'nedb-promises-ts';
import { GLOBAL_FAVORITES_PLAYLIST_ID } from './shared/constants';
import {
CHANNEL_SET_USER_AGENT,
EPG_ERROR,
Expand All @@ -27,6 +28,10 @@ import {
PLAYLIST_UPDATE_RESPONSE,
} from './shared/ipc-commands';
import { Playlist, PlaylistUpdateState } from './shared/playlist.interface';
import {
aggregateFavoriteChannels,
createFavoritesPlaylist,
} from './shared/playlist.utils';
import { ParsedPlaylist } from './src/typings.d';

const fs = require('fs');
Expand Down Expand Up @@ -97,12 +102,16 @@ export class Api {
ipcMain.on(PLAYLIST_GET_ALL, (event) => this.sendAllPlaylists(event));

ipcMain.on(PLAYLIST_GET_BY_ID, (event, args) => {
db.findOne({ _id: args.id }).then((playlist) => {
this.setUserAgent(playlist.userAgent);
event.sender.send(PLAYLIST_PARSE_RESPONSE, {
payload: playlist,
if (args.id === GLOBAL_FAVORITES_PLAYLIST_ID) {
this.sendPlaylistWithGlobalFavorites(event);
} else {
db.findOne({ _id: args.id }).then((playlist) => {
this.setUserAgent(playlist?.userAgent);
event.sender.send(PLAYLIST_PARSE_RESPONSE, {
payload: playlist,
});
});
});
}
});

ipcMain.on(PLAYLIST_REMOVE_BY_ID, (event, args) => {
Expand Down Expand Up @@ -234,6 +243,22 @@ export class Api {
this.refreshPlaylists();
}

/**
* Sends a message with playlist that contains favorite channels from all available playlists
* @param event ipc main event
*/
sendPlaylistWithGlobalFavorites(event: Electron.IpcMainEvent) {
db.find({ type: { $exists: false } }).then((playlists: Playlist[]) => {
const favoriteChannels = aggregateFavoriteChannels(playlists);
const favPlaylist = createFavoritesPlaylist(favoriteChannels);

event.sender.send(PLAYLIST_PARSE_RESPONSE, {
type: PLAYLIST_PARSE_RESPONSE,
payload: favPlaylist,
});
});
}

/**
* Set default listeners for custom-titlebar
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
import { MatSnackBar } from '@angular/material/snack-bar';
import * as _ from 'lodash';
import { Observable } from 'rxjs';
import { Channel, ChannelQuery, ChannelStore } from '../../../state';
import { Channel } from '../../../../../shared/channel.interface';
import { ChannelQuery, ChannelStore } from '../../../state';

@Component({
selector: 'app-channel-list-container',
Expand Down
19 changes: 10 additions & 9 deletions src/app/player/components/epg-list/epg-list.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { MockComponent, MockProvider, MockModule, MockPipe } from 'ng-mocks';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
/* eslint-disable @typescript-eslint/unbound-method */
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
import { TranslatePipe } from '@ngx-translate/core';
import { MatTooltipModule } from '@angular/material/tooltip';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { EpgListComponent, EpgData } from './epg-list.component';
import { MatIconModule } from '@angular/material/icon';
import { MatListModule } from '@angular/material/list';
import { DataService } from '../../../services/data.service';
import { ElectronServiceStub } from '../../../services/electron.service.stub';
import { MatTooltipModule } from '@angular/material/tooltip';
import { TranslatePipe } from '@ngx-translate/core';
import * as moment from 'moment';
import { MockComponent, MockModule, MockPipe, MockProvider } from 'ng-mocks';
import { Channel } from '../../../../../shared/channel.interface';
import { EPG_GET_PROGRAM_DONE } from '../../../../../shared/ipc-commands';
import { Channel, ChannelStore } from '../../../state';
import { DataService } from '../../../services/data.service';
import { ElectronServiceStub } from '../../../services/electron.service.stub';
import { MomentDatePipe } from '../../../shared/pipes/moment-date.pipe';
import { MatIconModule } from '@angular/material/icon';
import { ChannelStore } from '../../../state';
import { EpgListItemComponent } from './epg-list-item/epg-list-item.component';
import { EpgData, EpgListComponent } from './epg-list.component';

describe('EpgListComponent', () => {
let component: EpgListComponent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
SimpleChanges,
ViewChild,
} from '@angular/core';
import { Channel } from '../../../state';
import Hls from 'hls.js';
import { Channel } from '../../../../../shared/channel.interface';

/**
* This component contains the implementation of HTML5 based video player
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Channel } from './../../../state/channel.model';
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { EpgProgram } from '../../models/epg-program.model';
import * as moment from 'moment';
import { Channel } from '../../../../../shared/channel.interface';
import { EpgProgram } from '../../models/epg-program.model';

@Component({
selector: 'app-info-overlay',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<mat-icon>menu</mat-icon>
</button>
<button
*ngIf="playlistId !== 'GLOBAL_FAVORITES'"
mat-icon-button
(click)="addToFavorites(activeChannel)"
[matTooltip]="'TOP_MENU.TOGGLE_FAVORITE_FLAG' | translate"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { InfoOverlayComponent } from './../info-overlay/info-overlay.component';
import { MatTooltipModule } from '@angular/material/tooltip';
import { TranslatePipe } from '@ngx-translate/core';
/* eslint-disable @typescript-eslint/unbound-method */
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { VideoPlayerComponent } from './video-player.component';
import { MockComponent, MockModule, MockPipe } from 'ng-mocks';
import { ChannelListContainerComponent } from '../channel-list-container/channel-list-container.component';
import { VjsPlayerComponent } from '../vjs-player/vjs-player.component';
import { MatSidenavModule } from '@angular/material/sidenav';
import { MatIconModule } from '@angular/material/icon';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatSidenavModule } from '@angular/material/sidenav';
import { MatSnackBar } from '@angular/material/snack-bar';
import { ChannelStore } from '../../../state/channel.store';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatTooltipModule } from '@angular/material/tooltip';
import { TranslatePipe } from '@ngx-translate/core';
import { MockComponent, MockModule, MockPipe } from 'ng-mocks';
import * as MOCKED_PLAYLIST from '../../../../mocks/playlist.json';
import { createChannel } from '../../../state';
import { HtmlVideoPlayerComponent } from '../html-video-player/html-video-player.component';
import { EpgListComponent } from '../epg-list/epg-list.component';
import { VideoPlayer } from '../../../settings/settings.interface';
import { DataService } from '../../../services/data.service';
import { ElectronServiceStub } from '../../../services/electron.service.stub';
import { VideoPlayer } from '../../../settings/settings.interface';
import { createChannel } from '../../../state';
import { ChannelStore } from '../../../state/channel.store';
import { ChannelListContainerComponent } from '../channel-list-container/channel-list-container.component';
import { EpgListComponent } from '../epg-list/epg-list.component';
import { HtmlVideoPlayerComponent } from '../html-video-player/html-video-player.component';
import { VjsPlayerComponent } from '../vjs-player/vjs-player.component';
import { InfoOverlayComponent } from './../info-overlay/info-overlay.component';
import { VideoPlayerComponent } from './video-player.component';

class MatSnackBarStub {
open(): void {}
Expand Down
14 changes: 9 additions & 5 deletions src/app/player/components/video-player/video-player.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { EpgProgram } from './../../models/epg-program.model';
import { Component, OnInit, ViewChild } from '@angular/core';
import { ChannelQuery, Channel, ChannelStore } from '../../../state';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { MatSidenav } from '@angular/material/sidenav';
import { MatSnackBar } from '@angular/material/snack-bar';
import { StorageMap } from '@ngx-pwa/local-storage';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { Channel } from '../../../../../shared/channel.interface';
import { Settings, VideoPlayer } from '../../../settings/settings.interface';
import { MatSnackBar } from '@angular/material/snack-bar';
import { STORE_KEY } from '../../../shared/enums/store-keys.enum';
import { ChannelQuery, ChannelStore } from '../../../state';
import { EpgProgram } from './../../models/epg-program.model';

@Component({
selector: 'app-video-player',
Expand Down Expand Up @@ -50,6 +51,9 @@ export class VideoPlayerComponent implements OnInit {
/** Sidebar object */
@ViewChild('sidenav') sideNav: MatSidenav;

/** ID of the current playlist */
playlistId = this.channelQuery.getValue().playlistId;

/**
* Creates an instance of VideoPlayerComponent
* @param channelQuery akita's channel query
Expand Down
50 changes: 7 additions & 43 deletions src/app/services/pwa.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { guid } from '@datorama/akita';
import { Channel } from 'diagnostics_channel';
import { parse } from 'iptv-playlist-parser';
import { NgxIndexedDBService } from 'ngx-indexed-db';
import { catchError, combineLatest, map, switchMap, throwError } from 'rxjs';
import { GLOBAL_FAVORITES_PLAYLIST_ID } from '../../../shared/constants';
import {
ERROR,
PLAYLIST_GET_ALL,
Expand All @@ -24,16 +24,15 @@ import {
Playlist,
PlaylistUpdateState,
} from '../../../shared/playlist.interface';
import {
aggregateFavoriteChannels,
createFavoritesPlaylist,
} from '../../../shared/playlist.utils';
import { AppConfig } from '../../environments/environment';
import { ParsedPlaylist } from '../../typings';
import { DbStores } from '../indexed-db.config';
import { DataService } from './data.service';

/**
* Id of the channel with favorite channels aggregated from all added playlists
*/
export const GLOBAL_FAVORITES_PLAYLIST_ID = 'GLOBAL_FAVORITES';

@Injectable({
providedIn: 'root',
})
Expand Down Expand Up @@ -63,41 +62,6 @@ export class PwaService extends DataService {
return AppConfig.version;
}

/**
* Aggregates favorite channels as objects from all available playlists
* @param playlists all available playlists
* @returns favorite channels
*/
aggregateFavoriteChannels(playlists: Playlist[]): Channel[] {
const favorites = [];
playlists.forEach((playlist) => {
if (playlist.favorites?.length > 0) {
playlist.playlist.items.forEach((channel) => {
if (playlist.favorites.includes(channel.id)) {
favorites.push(channel);
}
});
}
});
return favorites;
}

/**
* Creates a simplified playlist object which is used for global favorites
* @param channels channels list
* @returns simplified playlist object
*/
createFavoritesPlaylist(channels: Channel[]): Partial<Playlist> {
return {
id: GLOBAL_FAVORITES_PLAYLIST_ID,
_id: GLOBAL_FAVORITES_PLAYLIST_ID,
count: channels.length,
playlist: {
items: channels,
},
};
}

/**
* Returns the count of favorite channels from all playlists
*/
Expand All @@ -124,9 +88,9 @@ export class PwaService extends DataService {
.pipe(
map((playlists: Playlist[]) => {
const favoriteChannels =
this.aggregateFavoriteChannels(playlists);
aggregateFavoriteChannels(playlists);
const favPlaylist =
this.createFavoritesPlaylist(favoriteChannels);
createFavoritesPlaylist(favoriteChannels);
return favPlaylist;
})
)
Expand Down
33 changes: 1 addition & 32 deletions src/app/state/channel.model.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,4 @@
/**
* Represents channel object
* TODO: define channel interface in iptv-parser library
*/
export interface Channel {
id: string;
url: string;
name: string;
group: {
title: string;
};
tvg: {
id: string;
name: string;
language: string;
country: string;
url: string;
logo: string;
rec: string;
};
epgParams?: string;
timeshift?: string;
catchup?: {
type?: string;
source?: string;
days?: string;
};
http: {
referrer: string;
'user-agent': string;
};
}
import { Channel } from '../../../shared/channel.interface';

/**
* Creates new channel object based on the given fields
Expand Down
2 changes: 1 addition & 1 deletion src/app/state/channel.store.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Injectable } from '@angular/core';
import { EntityState, EntityStore, StoreConfig } from '@datorama/akita';
import * as moment from 'moment';
import { Channel } from '../../../shared/channel.interface';
import {
CHANNEL_SET_USER_AGENT,
EPG_GET_PROGRAM,
PLAYLIST_UPDATE_FAVORITES,
} from '../../../shared/ipc-commands';
import { EpgProgram } from '../player/models/epg-program.model';
import { DataService } from '../services/data.service';
import { Channel } from './channel.model';

export interface ChannelState extends EntityState<Channel> {
active: Channel;
Expand Down

0 comments on commit 764201a

Please sign in to comment.