From 7fd7b3ff3222aa655958440a5d9565094e3954cc Mon Sep 17 00:00:00 2001 From: Krzysztof Moch Date: Wed, 11 Oct 2023 21:56:54 +0200 Subject: [PATCH] feat: refactor resize prop handler (#3286) --- .../exoplayer/ReactExoplayerViewManager.java | 37 +++++++++---------- ios/Video/RCTVideo.swift | 32 +++++++++++++--- ios/Video/RCTVideoManager.swift | 9 ----- src/Video.tsx | 18 +-------- src/VideoNativeComponent.ts | 18 +-------- src/utils.ts | 10 +---- 6 files changed, 47 insertions(+), 77 deletions(-) diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java index 30744258..6c9e9888 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java @@ -119,16 +119,6 @@ public class ReactExoplayerViewManager extends ViewGroupManager getExportedViewConstants() { - return MapBuilder.of( - "ScaleNone", Integer.toString(ResizeMode.RESIZE_MODE_FIT), - "ScaleAspectFit", Integer.toString(ResizeMode.RESIZE_MODE_FIT), - "ScaleToFill", Integer.toString(ResizeMode.RESIZE_MODE_FILL), - "ScaleAspectFill", Integer.toString(ResizeMode.RESIZE_MODE_CENTER_CROP) - ); - } - @ReactProp(name = PROP_DRM) public void setDRM(final ReactExoplayerView videoView, @Nullable ReadableMap drm) { if (drm != null && drm.hasKey(PROP_DRM_TYPE)) { @@ -212,8 +202,23 @@ public class ReactExoplayerViewManager extends ViewGroupManager [AnyHashable : Any]? { - return [ - "ScaleNone": AVLayerVideoGravity.resizeAspect, - "ScaleToFill": AVLayerVideoGravity.resize, - "ScaleAspectFit": AVLayerVideoGravity.resizeAspect, - "ScaleAspectFill": AVLayerVideoGravity.resizeAspectFill - ] - } - override class func requiresMainQueueSetup() -> Bool { return true } diff --git a/src/Video.tsx b/src/Video.tsx index 7f83d7ad..2e4faf4d 100644 --- a/src/Video.tsx +++ b/src/Video.tsx @@ -8,9 +8,8 @@ import React, { type ComponentRef, } from 'react'; import {View, StyleSheet, Image, Platform} from 'react-native'; -import NativeVideoComponent, {RCTVideoConstants} from './VideoNativeComponent'; +import NativeVideoComponent from './VideoNativeComponent'; import type { - NativeVideoResizeMode, OnAudioFocusChangedData, OnAudioTracksData, OnPlaybackStateChangedData, @@ -150,19 +149,6 @@ const Video = forwardRef( }; }, [source]); - const _resizeMode: NativeVideoResizeMode = useMemo(() => { - switch (resizeMode) { - case 'contain': - return RCTVideoConstants.ScaleAspectFit; - case 'cover': - return RCTVideoConstants.ScaleAspectFill; - case 'stretch': - return RCTVideoConstants.ScaleToFill; - default: - return RCTVideoConstants.ScaleNone; - } - }, [resizeMode]); - const _drm = useMemo(() => { if (!drm) { return; @@ -476,7 +462,7 @@ const Video = forwardRef( src={src} drm={_drm} style={StyleSheet.absoluteFill} - resizeMode={_resizeMode} + resizeMode={resizeMode} fullscreen={isFullscreen} restoreUserInterfaceForPIPStopCompletionHandler={ _restoreUserInterfaceForPIPStopCompletionHandler diff --git a/src/VideoNativeComponent.ts b/src/VideoNativeComponent.ts index a995166b..3fd5afff 100644 --- a/src/VideoNativeComponent.ts +++ b/src/VideoNativeComponent.ts @@ -4,7 +4,6 @@ import type { ViewProps, } from 'react-native'; import {NativeModules, requireNativeComponent} from 'react-native'; -import {getViewManagerConfig} from './utils'; // -------- There are types for native component (future codegen) -------- // if you are looking for types for react component, see src/types/video.ts @@ -233,15 +232,13 @@ export type OnVideoErrorData = Readonly<{ export type OnAudioFocusChangedData = Readonly<{ hasFocus: boolean; }>; - -export type NativeVideoResizeMode = unknown; export interface VideoNativeProps extends ViewProps { src?: VideoSrc; drm?: Drm; adTagUrl?: string; allowsExternalPlayback?: boolean; // ios, true maxBitRate?: number; - resizeMode?: NativeVideoResizeMode; + resizeMode?: 'none' | 'cover' | 'contain' | 'stretch'; repeat?: boolean; automaticallyWaitsToMinimizeStalling?: boolean; textTracks?: TextTracks; @@ -364,22 +361,9 @@ export interface VideoDecoderPropertiesType { isHEVCSupported: () => Promise<'unsupported' | 'hardware' | 'software'>; } -export type VideoViewManagerConfig = { - Constants: { - ScaleNone: unknown; - ScaleToFill: unknown; - ScaleAspectFit: unknown; - ScaleAspectFill: unknown; - }; - Commands: {[key: string]: number}; -}; - export const VideoManager = NativeModules.VideoManager as VideoManagerType; export const VideoDecoderProperties = NativeModules.VideoDecoderProperties as VideoDecoderPropertiesType; -export const RCTVideoConstants = ( - getViewManagerConfig('RCTVideo') as VideoViewManagerConfig -).Constants; export default requireNativeComponent( 'RCTVideo', diff --git a/src/utils.ts b/src/utils.ts index d24bc025..84beb7d3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,5 @@ import type {Component, RefObject, ComponentClass} from 'react'; -import {Image, UIManager, findNodeHandle} from 'react-native'; +import {Image, findNodeHandle} from 'react-native'; import type {ImageSourcePropType} from 'react-native'; import type {ReactVideoSource} from './types/video'; @@ -35,11 +35,3 @@ export function getReactTag( return reactTag; } - -export function getViewManagerConfig(name: string) { - if ('getViewManagerConfig' in UIManager) { - return UIManager.getViewManagerConfig(name); - } - - return UIManager[name]; -}