Skip to content

Commit

Permalink
Refactor methods in helpers to use PlaybackState instead of $scope
Browse files Browse the repository at this point in the history
  • Loading branch information
YouKnowBlom committed Aug 1, 2021
1 parent fbf19a7 commit 82c77f0
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ import { MediaSourceInfo } from './api/generated/models/media-source-info';
import { BaseItemDto } from './api/generated/models/base-item-dto';
import { BaseItemPerson } from './api/generated/models/base-item-person';
import { UserDto } from './api/generated/models/user-dto';
import { GlobalScope, BusMessage, ItemIndex, ItemQuery } from './types/global';
import { BusMessage, ItemIndex, ItemQuery } from './types/global';
import { PlaybackState } from './components/playbackManager';

/**
* Get current playback position in ticks, adjusted for server seeking
*
* @param $scope - global context variable
* @param state - playback state.
* @returns position in ticks
*/
export function getCurrentPositionTicks($scope: GlobalScope): number {
export function getCurrentPositionTicks(state: PlaybackState): number {
let positionTicks = window.playerManager.getCurrentTimeSec() * 10000000;
const mediaInformation = window.playerManager.getMediaInformation();

if (mediaInformation && !mediaInformation.customData.canClientSeek) {
positionTicks += $scope.startPositionTicks || 0;
positionTicks += state.startPositionTicks || 0;
}

return positionTicks;
Expand All @@ -27,30 +28,30 @@ export function getCurrentPositionTicks($scope: GlobalScope): number {
/**
* Get parameters used for playback reporting
*
* @param $scope - global context variable
* @param state - playback state.
* @returns progress information for use with the reporting APIs
*/
export function getReportingParams($scope: GlobalScope): PlaybackProgressInfo {
export function getReportingParams(state: PlaybackState): PlaybackProgressInfo {
/* Math.round() calls:
* on 10.7, any floating point will give an API error,
* so it's actually really important to make sure that
* those fields are always rounded.
*/
return {
AudioStreamIndex: $scope.audioStreamIndex,
CanSeek: $scope.canSeek,
AudioStreamIndex: state.audioStreamIndex,
CanSeek: state.canSeek,
IsMuted: window.volume.muted,
IsPaused:
window.playerManager.getPlayerState() ===
cast.framework.messages.PlayerState.PAUSED,
ItemId: $scope.itemId,
LiveStreamId: $scope.liveStreamId,
MediaSourceId: $scope.mediaSourceId,
PlayMethod: $scope.playMethod,
PlaySessionId: $scope.playSessionId,
PositionTicks: Math.round(getCurrentPositionTicks($scope)),
ItemId: state.itemId,
LiveStreamId: state.liveStreamId,
MediaSourceId: state.mediaSourceId,
PlayMethod: state.playMethod,
PlaySessionId: state.playSessionId,
PositionTicks: Math.round(getCurrentPositionTicks(state)),
RepeatMode: window.repeatMode,
SubtitleStreamIndex: $scope.subtitleStreamIndex,
SubtitleStreamIndex: state.subtitleStreamIndex,
VolumeLevel: Math.round(window.volume.level * 100)
};
}
Expand Down Expand Up @@ -107,12 +108,12 @@ export function getNextPlaybackItemInfo(): ItemIndex | null {
* about the item that is currently playing. This is sent over the cast protocol over to
* the connected client (or clients?).
*
* @param $scope - global context
* @param playbackState - playback state.
* @param reportingData - object full of random information
* @returns lots of data for the connected client
*/
export function getSenderReportingData(
$scope: GlobalScope,
playbackState: PlaybackState,
reportingData: PlaybackProgressInfo
): any {
const state: any = {
Expand All @@ -123,10 +124,10 @@ export function getSenderReportingData(

state.NowPlayingItem = {
Id: reportingData.ItemId,
RunTimeTicks: $scope.runtimeTicks
RunTimeTicks: playbackState.runtimeTicks
};

const item = $scope.item;
const item = playbackState.item;

if (item) {
const nowPlayingItem = state.NowPlayingItem;
Expand All @@ -135,7 +136,7 @@ export function getSenderReportingData(
nowPlayingItem.Chapters = item.Chapters || [];

// TODO: Fill these
const mediaSource = item.MediaSources.filter((m: any) => {
const mediaSource = item.MediaSources?.filter((m: any) => {
return m.Id == reportingData.MediaSourceId;
})[0];

Expand Down Expand Up @@ -193,7 +194,7 @@ export function getSenderReportingData(
nowPlayingItem.LogoImageTag = item.ParentLogoImageTag;
}

if ($scope.playNextItem) {
if (playbackState.playNextItemBool) {
const nextItemInfo = getNextPlaybackItemInfo();

if (nextItemInfo) {
Expand Down

0 comments on commit 82c77f0

Please sign in to comment.