Skip to content

Commit

Permalink
feat(pwa): load a m3u playlist as a URL parameter #176
Browse files Browse the repository at this point in the history
  • Loading branch information
4gray committed Oct 19, 2022
1 parent b96df78 commit 344bd75
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
1 change: 1 addition & 0 deletions shared/playlist.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export interface Playlist {
updateDate?: number;
updateState?: PlaylistUpdateState;
position?: number;
isTemporary?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MatSidenavModule } from '@angular/material/sidenav';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatTooltipModule } from '@angular/material/tooltip';
import { ActivatedRoute } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslatePipe } from '@ngx-translate/core';
import { MockComponent, MockModule, MockPipe } from 'ng-mocks';
Expand Down Expand Up @@ -47,6 +48,16 @@ describe('VideoPlayerComponent', () => {
providers: [
{ provide: MatSnackBar, useClass: MatSnackBarStub },
{ provide: DataService, useClass: ElectronServiceStub },
{
provide: ActivatedRoute,
useValue: {
snapshot: {
queryParams: {
url: 'https://iptvnator/list.m3u',
},
},
},
},
],
imports: [
MockModule(MatSidenavModule),
Expand Down
22 changes: 20 additions & 2 deletions src/app/player/components/video-player/video-player.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import {
} from '@angular/core';
import { MatSidenav } from '@angular/material/sidenav';
import { MatSnackBar } from '@angular/material/snack-bar';
import { Router } from '@angular/router';
import { ActivatedRoute, Router } from '@angular/router';
import { StorageMap } from '@ngx-pwa/local-storage';
import { filter, map, Observable, skipWhile } from 'rxjs';
import { Channel } from '../../../../../shared/channel.interface';
import {
PLAYLIST_GET_ALL,
PLAYLIST_PARSE_BY_URL,
PLAYLIST_PARSE_RESPONSE,
} from '../../../../../shared/ipc-commands';
import { Playlist } from '../../../../../shared/playlist.interface';
Expand Down Expand Up @@ -114,9 +115,10 @@ export class VideoPlayerComponent implements OnInit, OnDestroy {
* Creates an instance of VideoPlayerComponent
*/
constructor(
private activatedRoute: ActivatedRoute,
private channelQuery: ChannelQuery,
private channelStore: ChannelStore,
public dataService: DataService,
private dataService: DataService,
private ngZone: NgZone,
private overlay: Overlay,
private router: Router,
Expand All @@ -132,6 +134,22 @@ export class VideoPlayerComponent implements OnInit, OnDestroy {
ngOnInit(): void {
this.applySettings();
this.setRendererListeners();
this.getPlaylistUrlAsParam();
}

/**
* Opens a playlist provided as a url param
*/
getPlaylistUrlAsParam() {
const URL_REGEX = /^(http|https|file):\/\/[^ "]+$/;
const playlistUrl = this.activatedRoute.snapshot.queryParams.url;

if (playlistUrl && playlistUrl.match(URL_REGEX)) {
this.dataService.sendIpcEvent(PLAYLIST_PARSE_BY_URL, {
url: playlistUrl,
isTemporary: true,
});
}
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/app/services/pwa.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
first,
interval,
map,
of,
switchMap,
throwError,
} from 'rxjs';
Expand Down Expand Up @@ -86,12 +87,12 @@ export class PwaService extends DataService {
everySixHours$
);

everySixHoursOnceAppIsStable$.subscribe(() =>
this.swUpdate.checkForUpdate()
);
everySixHoursOnceAppIsStable$.subscribe(() => {
this.swUpdate.checkForUpdate();
});

this.swUpdate.versionUpdates.subscribe(() => {
let snackBarRef = this.snackBar.open(
const snackBarRef = this.snackBar.open(
'Update available',
'Refresh'
);
Expand Down Expand Up @@ -341,6 +342,11 @@ export class PwaService extends DataService {
this.http
.get(`${this.corsProxyUrl}${payload.url}`)
.pipe(
switchMap((response) =>
payload.isTemporary
? of(response)
: this.dbService.add(DbStores.Playlists, response)
),
catchError((error) => {
window.postMessage({
type: ERROR,
Expand All @@ -351,12 +357,6 @@ export class PwaService extends DataService {
})
)
.subscribe((response: any) => {
this.dbService
.add(DbStores.Playlists, response)
.subscribe(() => {
console.log('playlist was added...');
});

window.postMessage({
type: PLAYLIST_PARSE_RESPONSE,
payload: response,
Expand Down

0 comments on commit 344bd75

Please sign in to comment.