Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RNMobile] VideoPress Android WebView #30521

Merged
merged 15 commits into from
May 11, 2023
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Use native embed WebView for the VideoPress editor preview on Android
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { store as coreStore } from '@wordpress/core-data';
import { useDispatch } from '@wordpress/data';
import { useState, useEffect, useRef, useCallback } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { requestEmbedFullscreenPreview } from '@wordpress/react-native-bridge';
/**
* External dependencies
*/
import { View, Text, Platform } from 'react-native';
import { View, Text, Platform, Pressable } from 'react-native';
/**
* Internal dependencies
*/
Expand All @@ -22,7 +23,7 @@ import style from './style.scss';

const VIDEO_PREVIEW_ATTEMPTS_LIMIT = 10;
const DEFAULT_PLAYER_ASPECT_RATIO = 16 / 9; // This is the observed default aspect ratio from VideoPress embeds.

const IS_ANDROID = Platform.OS === 'android';
fluiddot marked this conversation as resolved.
Show resolved Hide resolved
/**
* VideoPlayer react component
*
Expand All @@ -43,6 +44,7 @@ export default function Player( { isSelected, attributes } ) {
seekbarColor,
seekbarLoadingColor,
seekbarPlayedColor,
title,
} = attributes;

const iconStyle = style[ 'videopress-player__loading-icon' ];
Expand Down Expand Up @@ -130,7 +132,7 @@ export default function Player( { isSelected, attributes } ) {
// Android does not receive a 'videopress_ready' event,
// so we need to set the player as ready when the WebView loads.
const onLoadEnd = useCallback( () => {
Platform.OS === 'android' && setIsPlayerReady( true );
IS_ANDROID && setIsPlayerReady( true );
}, [] );

const loadingOverlay = (
Expand All @@ -144,14 +146,30 @@ export default function Player( { isSelected, attributes } ) {
</View>
);

let overlay = ! isSelected && <View style={ style[ 'videopress-player__overlay' ] } />;

// On Android render an overlay to send the embed markup to a native WebView.
if ( isSelected && IS_ANDROID ) {
overlay = (
<View style={ style[ 'videopress-player__overlay' ] }>
<Pressable
style={ style[ 'videopress-player__open-embed-button' ] }
onPress={ () => {
requestEmbedFullscreenPreview( html, title );
} }
/>
Comment on lines +155 to +160
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following some of the issues we spotted when testing (wordpress-mobile/gutenberg-mobile#5758), I'm wondering if we should limit opening the WebView until the video reaches a ready state. Maybe we could wait until the player is ready and loaded to enable this button, WDYT?

I was also thinking if we could get the HTML from the WebView we display within the editor to pass it to the new WebView, but seems this data is not exposed by the react-native-webview library.

In any case, since the companion PRs to this one have been merged, I'll go ahead and merge it as-is. Hence, potential improvements to this approach would need a separate PR.

</View>
);
}

// Show the loading overlay when:
// 1. Player is not ready
// 2. Player is loaded but preview is not ready
const showLoadingOverlay = ! isPlayerReady || ( isPlayerLoaded && ! isPreviewReady );

return (
<View style={ [ style[ 'videopress-player' ], { aspectRatio } ] }>
{ ! isSelected && <View style={ style[ 'videopress-player__overlay' ] } /> }
{ overlay }
{ showLoadingOverlay && loadingOverlay }
{ html && (
<SandBox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
z-index: 1;
}

.videopress-player__open-embed-button {
width: 100%;
height: 100%;
fluiddot marked this conversation as resolved.
Show resolved Hide resolved
}

.videopress-player__loading {
display: flex;
justify-content: space-evenly;
Expand Down