From 01d1cc51fd1ffbc30069d8d791ef5ed999ffff6b Mon Sep 17 00:00:00 2001 From: 4gray <4gray@users.noreply.github.com> Date: Thu, 17 Aug 2023 23:49:05 +0200 Subject: [PATCH] refactor: add store logic for navigation to adjacent channels --- src/app/state/actions.ts | 7 ++++++ src/app/state/effects.ts | 46 +++++++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/app/state/actions.ts b/src/app/state/actions.ts index cfb0ca544..e9d146084 100644 --- a/src/app/state/actions.ts +++ b/src/app/state/actions.ts @@ -119,3 +119,10 @@ export const updatePlaylistPositions = createAction( export const removeAllPlaylists = createAction( `${STORE_KEY} Remove all playlists` ); + +export const setAdjacentChannelAsActive = createAction( + `${STORE_KEY} Set adjacent channel as active`, + props<{ + direction: 'next' | 'previous'; + }>() +); diff --git a/src/app/state/effects.ts b/src/app/state/effects.ts index eea33d041..a8b1eb33e 100644 --- a/src/app/state/effects.ts +++ b/src/app/state/effects.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/no-unused-vars */ import { Injectable } from '@angular/core'; import { MatSnackBar } from '@angular/material/snack-bar'; import { Router } from '@angular/router'; @@ -6,8 +5,14 @@ import { Actions, createEffect, ofType } from '@ngrx/effects'; import { Store } from '@ngrx/store'; import { StorageMap } from '@ngx-pwa/local-storage'; import { TranslateService } from '@ngx-translate/core'; -import { firstValueFrom } from 'rxjs'; -import { combineLatestWith, map, switchMap, tap } from 'rxjs/operators'; +import { + combineLatestWith, + firstValueFrom, + map, + switchMap, + tap, + withLatestFrom, +} from 'rxjs'; import { CHANNEL_SET_USER_AGENT, EPG_GET_PROGRAM, @@ -19,7 +24,9 @@ import { Settings, VideoPlayer } from '../settings/settings.interface'; import { STORE_KEY } from '../shared/enums/store-keys.enum'; import * as PlaylistActions from './actions'; import { + selectActive, selectActivePlaylistId, + selectChannels, selectFavorites, selectIsEpgAvailable, } from './selectors'; @@ -34,7 +41,7 @@ export class PlaylistEffects { this.store.select(selectFavorites), this.store.select(selectActivePlaylistId) ), - switchMap(([action, favorites, playlistId]) => + switchMap(([, favorites, playlistId]) => this.playlistsService.updateFavorites(playlistId, favorites) ) ); @@ -83,7 +90,8 @@ export class PlaylistEffects { if ( settings && Object.keys(settings).length > 0 && - settings.player === VideoPlayer.MPV + settings.player === VideoPlayer.MPV && + channel.radio !== 'true' ) this.dataService.sendIpcEvent(OPEN_MPV_PLAYER, { url: channel.url, @@ -249,6 +257,34 @@ export class PlaylistEffects { { dispatch: false } ); + setAdjacentChannelAsActive$ = createEffect(() => { + return this.actions$.pipe( + ofType(PlaylistActions.setAdjacentChannelAsActive), + withLatestFrom( + this.store.select(selectChannels), + this.store.select(selectActive) + ), + map(([action, channels, activeChannel]) => { + let adjacentChannel; + const index = channels.findIndex( + (channel) => channel.id === activeChannel.id + ); + if (action.direction === 'next') { + if (index === channels.length - 1) + adjacentChannel = activeChannel; + adjacentChannel = channels[index + 1]; + } else if (action.direction === 'previous') { + if (index === -1 || index === 0) + adjacentChannel = activeChannel; + adjacentChannel = channels[index - 1]; + } + return PlaylistActions.setActiveChannelSuccess({ + channel: adjacentChannel, + }); + }) + ); + }); + constructor( private actions$: Actions, private playlistsService: PlaylistsService,