UNPKG

@applicaster/zapp-react-native-utils

Version:

Applicaster Zapp React Native utilities package

151 lines (114 loc) 3.88 kB
import * as R from "ramda"; import { playerManager } from "@applicaster/zapp-react-native-utils/appUtils/playerManager"; import { isString } from "@applicaster/zapp-react-native-utils/typeGuards"; import { isFilledArray } from "@applicaster/zapp-react-native-utils/arrayUtils"; import { isTV } from "@applicaster/zapp-react-native-utils/reactUtils"; import { getBoolFromConfigValue } from "../configurationUtils"; import { Dimensions } from "react-native"; export { getPlayerActionButtons } from "./getPlayerActionButtons"; /** * Gets duration value from player manager, and from extensions * then checks whether the value from either is a not a valid number * If the number is invalid or less than zero we can assume it is live * @param {object} content * @returns {boolean} */ export function isLiveLegacy(content) { const isNaNCurry = R.curry(isNaN); const isNotaValidNumber = R.anyPass([ R.isEmpty, R.isNil, isNaNCurry, R.equals(Infinity), ]); const durationFromExt = R.path(["extensions", "duration"], content); const durationFromMgr = playerManager.getDuration(); const duration = Math.floor(durationFromExt || durationFromMgr); const isLive = R.anyPass([isNotaValidNumber, R.lte(R.__, 0)])(duration); return isLive; } export function isLiveByDuration(duration): boolean { const durationNumber = duration && parseFloat(duration); if (R.isNil(durationNumber) || R.isEmpty(durationNumber)) { return false; } return durationNumber <= 0; } export function hasLiveExtension(entry): boolean { return !!( entry?.type?.value === "channel" || getBoolFromConfigValue(entry?.extensions?.live) || getBoolFromConfigValue(entry?.extensions?.isLive) ); } export function isEntryLive(entry): boolean { if (R.isNil(entry)) { return false; } const duration = R.path(["extensions", "duration"], entry); return hasLiveExtension(entry) || isLiveByDuration(duration); } function isLiveByManager(): boolean { const playerStateIsLive = playerManager.getActivePlayer()?.playerState?.isLive; if (playerStateIsLive) { return true; } const durationFromPlayerManager = playerManager.getDuration(); return isLiveByDuration(durationFromPlayerManager); } export function isLive(entry: ZappEntry): boolean { return isEntryLive(entry) || isLiveByManager(); } export const isAudioItem = (item: Option<ZappEntry>) => { if ( isString(item?.content?.type) && item?.content?.type?.includes?.("audio") ) { return true; } if (isString(item?.type?.value) && item?.type?.value?.includes?.("audio")) { return true; } return false; }; export const isInlineTV = (screenData) => { return isTV() && isFilledArray(screenData?.ui_components); }; const isPercentage = (value: string | number): boolean => { if (typeof value === "string") { return value.includes("%"); } return false; }; const getPercentageOf = (percent: string, value: number) => { const percentageValue = parseFloat(percent.replace("%", "")); if (isNaN(percentageValue)) { return value; } return (value * percentageValue) / 100; }; type DimensionsT = { width: number | string; height: number | string | undefined; aspectRatio?: number; }; export const getTabletWidth = ( tablet_landscape_sidebar_width, dimensions: DimensionsT ) => { const { width: SCREEN_WIDTH } = Dimensions.get("screen"); const { width } = dimensions; let widthValue = Number(width); if (isPercentage(width)) { widthValue = getPercentageOf(width.toString(), SCREEN_WIDTH); } const sidebarWidth = Number(tablet_landscape_sidebar_width?.replace("%", "")); if (tablet_landscape_sidebar_width?.includes("%")) { return widthValue * (1 - sidebarWidth / 100); } if (Number.isNaN(sidebarWidth)) { return widthValue * 0.65; } return widthValue - sidebarWidth; };