2023-04-24 14:09:50 +03:00
import React , { RefObject } from 'react' ;
2021-08-02 15:55:52 +03:00
import Video from 'react-native-youtube-iframe' ;
2023-03-21 09:47:52 +02:00
import { connect } from 'react-redux' ;
2021-08-02 15:55:52 +03:00
import { PLAYBACK_STATUSES } from '../../constants' ;
import AbstractVideoManager , {
2023-04-24 14:09:50 +03:00
IProps ,
2022-09-27 10:10:28 +03:00
_mapStateToProps
2021-08-02 15:55:52 +03:00
} from './AbstractVideoManager' ;
/ * *
* Passed to the webviewProps in order to avoid the usage of the ios player on which we cannot hide the controls .
*
* @private
* /
const webviewUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36' ; // eslint-disable-line max-len
2023-04-24 14:09:50 +03:00
interface IState {
paused : boolean ;
}
2021-08-02 15:55:52 +03:00
/ * *
* Manager of youtube shared video .
* /
2023-04-24 14:09:50 +03:00
class YoutubeVideoManager extends AbstractVideoManager < IState > {
playerRef : RefObject < typeof Video > ;
2021-08-02 15:55:52 +03:00
/ * *
* Initializes a new VideoManager instance .
*
* @param { Object } props - This component ' s props .
*
* @returns { void }
* /
2023-04-24 14:09:50 +03:00
constructor ( props : IProps ) {
2021-08-02 15:55:52 +03:00
super ( props ) ;
this . state = {
paused : false
} ;
this . playerRef = React . createRef ( ) ;
this . _onReady = this . _onReady . bind ( this ) ;
this . _onChangeState = this . _onChangeState . bind ( this ) ;
}
/ * *
* Retrieves the current player ref .
* /
get player() {
return this . playerRef . current ;
}
/ * *
* Indicates the playback state of the video .
*
* @returns { string }
* /
getPlaybackStatus() {
let status ;
if ( this . state . paused ) {
status = PLAYBACK_STATUSES . PAUSED ;
} else {
status = PLAYBACK_STATUSES . PLAYING ;
}
return status ;
}
/ * *
* Retrieves current time .
*
* @returns { number }
* /
getTime() {
2023-04-24 14:09:50 +03:00
// @ts-ignore
2021-08-02 15:55:52 +03:00
return this . player ? . getCurrentTime ( ) ;
}
/ * *
* Seeks video to provided time .
*
* @param { number } time - The time to seek to .
*
* @returns { void }
* /
2023-04-24 14:09:50 +03:00
seek ( time : number ) {
2021-08-02 15:55:52 +03:00
if ( this . player ) {
2023-04-24 14:09:50 +03:00
// @ts-ignore
2021-08-02 15:55:52 +03:00
this . player . seekTo ( time ) ;
}
}
/ * *
* Plays video .
*
* @returns { void }
* /
play() {
this . setState ( {
paused : false
} ) ;
}
/ * *
* Pauses video .
*
* @returns { void }
* /
pause() {
this . setState ( {
paused : true
} ) ;
}
/ * *
* Handles state change event .
*
* @param { string } event - State event .
* @returns { void }
* /
2023-04-24 14:09:50 +03:00
_onChangeState ( event : string ) {
2021-08-02 15:55:52 +03:00
if ( event === 'paused' ) {
this . setState ( {
paused : true
} , ( ) = > {
this . onPause ( ) ;
} ) ;
}
2024-08-27 10:45:39 -05:00
if ( event === PLAYBACK_STATUSES . PLAYING ) {
2021-08-02 15:55:52 +03:00
this . setState ( {
paused : false
} , ( ) = > {
this . onPlay ( ) ;
} ) ;
}
}
/ * *
* Handles onReady event .
*
* @returns { void }
* /
_onReady() {
this . setState ( {
paused : false
} ) ;
}
/ * *
* Retrieves video tag params .
*
* @returns { void }
* /
getPlayerOptions() {
const { _isOwner , videoId , width , height } = this . props ;
2023-04-24 14:09:50 +03:00
const options : any = {
2021-08-02 15:55:52 +03:00
height ,
initialPlayerParams : {
controls : _isOwner ,
modestbranding : true ,
preventFullScreen : true
} ,
play : ! this . state . paused ,
ref : this.playerRef ,
videoId ,
volume : 50 ,
webViewProps : {
bounces : false ,
mediaPlaybackRequiresUserAction : false ,
scrollEnabled : false ,
userAgent : webviewUserAgent
} ,
width
} ;
if ( _isOwner ) {
options . onChangeState = this . _onChangeState ;
options . onReady = this . _onReady ;
}
return options ;
}
/ * *
* Implements React Component ' s render .
*
* @inheritdoc
* /
2025-03-12 10:19:11 -05:00
override render() {
2023-05-12 13:24:50 +03:00
return (
< Video
ref = { this . playerRef }
{ . . . this . getPlayerOptions ( ) } / >
) ;
2021-08-02 15:55:52 +03:00
}
}
export default connect ( _mapStateToProps ) ( YoutubeVideoManager ) ;