-
Notifications
You must be signed in to change notification settings - Fork 13
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
Simplify getters by always throwing #347
base: PRN-92/refactor--remove-rejectpromiseonexceptionblock
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,11 +11,10 @@ import com.bitmovin.player.api.ui.ScalingMode | |||||
import com.bitmovin.player.api.ui.UiConfig | ||||||
import com.bitmovin.player.reactnative.converter.toRNPlayerViewConfigWrapper | ||||||
import com.bitmovin.player.reactnative.converter.toRNStyleConfigWrapperFromPlayerConfig | ||||||
import com.bitmovin.player.reactnative.extensions.customMessageHandlerModule | ||||||
import com.bitmovin.player.reactnative.extensions.fullscreenHandlerModule | ||||||
import com.bitmovin.player.reactnative.extensions.getBooleanOrNull | ||||||
import com.bitmovin.player.reactnative.extensions.getModule | ||||||
import com.bitmovin.player.reactnative.extensions.playerModule | ||||||
import com.bitmovin.player.reactnative.ui.CustomMessageHandlerModule | ||||||
import com.bitmovin.player.reactnative.ui.FullscreenHandlerModule | ||||||
import com.bitmovin.player.reactnative.ui.RNPictureInPictureHandler | ||||||
import com.facebook.react.bridge.* | ||||||
import com.facebook.react.module.annotations.ReactModule | ||||||
|
@@ -46,6 +45,7 @@ class RNPlayerViewManager(private val context: ReactApplicationContext) : Simple | |||||
override fun getName() = MODULE_NAME | ||||||
|
||||||
private var customMessageHandlerBridgeId: NativeId? = null | ||||||
private val handler = Handler(Looper.getMainLooper()) | ||||||
|
||||||
/** | ||||||
* The component's native view factory. RN may call this method multiple times | ||||||
|
@@ -181,53 +181,51 @@ class RNPlayerViewManager(private val context: ReactApplicationContext) : Simple | |||||
} | ||||||
|
||||||
private fun attachFullscreenBridge(view: RNPlayerView, fullscreenBridgeId: NativeId) { | ||||||
Handler(Looper.getMainLooper()).post { | ||||||
view.playerView?.setFullscreenHandler( | ||||||
context.getModule<FullscreenHandlerModule>()?.getInstance(fullscreenBridgeId), | ||||||
handler.postAndLogException { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is already in the |
||||||
view.getPlayerViewOrThrow().setFullscreenHandler( | ||||||
context.fullscreenHandlerModule.getInstance(fullscreenBridgeId), | ||||||
) | ||||||
} | ||||||
} | ||||||
|
||||||
private fun setFullscreen(view: RNPlayerView, isFullscreenRequested: Boolean) { | ||||||
Handler(Looper.getMainLooper()).post { | ||||||
val playerView = view.playerView ?: return@post | ||||||
if (playerView.isFullscreen == isFullscreenRequested) return@post | ||||||
if (isFullscreenRequested) { | ||||||
playerView.enterFullscreen() | ||||||
} else { | ||||||
playerView.exitFullscreen() | ||||||
handler.postAndLogException { | ||||||
with(view.getPlayerViewOrThrow()) { | ||||||
when { | ||||||
isFullscreen == isFullscreenRequested -> Unit // No changes | ||||||
isFullscreenRequested -> enterFullscreen() | ||||||
!isFullscreenRequested -> exitFullscreen() | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
private fun setPictureInPicture(view: RNPlayerView, isPictureInPictureRequested: Boolean) { | ||||||
runInMainLooperAndLogException { | ||||||
val playerView = view.playerView ?: throw IllegalStateException("The player view is not yet created") | ||||||
if (playerView.isPictureInPicture != isPictureInPictureRequested) return@runInMainLooperAndLogException | ||||||
if (isPictureInPictureRequested) { | ||||||
playerView.enterPictureInPicture() | ||||||
} else { | ||||||
playerView.exitPictureInPicture() | ||||||
handler.postAndLogException { | ||||||
with(view.getPlayerViewOrThrow()) { | ||||||
when { | ||||||
isPictureInPicture == isPictureInPictureRequested -> Unit // No changes | ||||||
isPictureInPictureRequested -> enterPictureInPicture() | ||||||
!isPictureInPictureRequested -> exitPictureInPicture() | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
private fun setScalingMode(view: RNPlayerView, scalingMode: String) { | ||||||
Handler(Looper.getMainLooper()).post { | ||||||
view.playerView?.scalingMode = ScalingMode.valueOf(scalingMode) | ||||||
handler.postAndLogException { | ||||||
view.getPlayerViewOrThrow().scalingMode = ScalingMode.valueOf(scalingMode) | ||||||
} | ||||||
} | ||||||
|
||||||
private fun setCustomMessageHandlerBridgeId(view: RNPlayerView, customMessageHandlerBridgeId: NativeId) { | ||||||
this.customMessageHandlerBridgeId = customMessageHandlerBridgeId | ||||||
attachCustomMessageHandlerBridge(view) | ||||||
attachCustomMessageHandlerBridge(view, customMessageHandlerBridgeId) | ||||||
} | ||||||
|
||||||
private fun attachCustomMessageHandlerBridge(view: RNPlayerView) { | ||||||
view.playerView?.setCustomMessageHandler( | ||||||
context.getModule<CustomMessageHandlerModule>() | ||||||
?.getInstance(customMessageHandlerBridgeId) | ||||||
?.customMessageHandler, | ||||||
private fun attachCustomMessageHandlerBridge(view: RNPlayerView, customMessageHandlerBridgeId: NativeId) { | ||||||
view.getPlayerViewOrThrow().setCustomMessageHandler( | ||||||
context.customMessageHandlerModule.getInstance(customMessageHandlerBridgeId).customMessageHandler, | ||||||
) | ||||||
} | ||||||
|
||||||
|
@@ -237,9 +235,8 @@ class RNPlayerViewManager(private val context: ReactApplicationContext) : Simple | |||||
* @param playerId `Player` instance id inside `PlayerModule`'s registry. | ||||||
*/ | ||||||
private fun attachPlayer(view: RNPlayerView, playerId: NativeId, playerConfig: ReadableMap?) { | ||||||
runInMainLooperAndLogException { | ||||||
val player = playerId.let { context.playerModule?.getPlayerOrNull(it) } | ||||||
?: throw InvalidParameterException("Cannot create a PlayerView, invalid playerId was passed.") | ||||||
handler.postAndLogException { | ||||||
val player = context.playerModule.getPlayer(playerId) | ||||||
val playbackConfig = playerConfig?.getMap("playbackConfig") | ||||||
val isPictureInPictureEnabled = view.config?.pictureInPictureConfig?.isEnabled == true || | ||||||
playbackConfig?.getBooleanOrNull("isPictureInPictureEnabled") == true | ||||||
|
@@ -270,7 +267,7 @@ class RNPlayerViewManager(private val context: ReactApplicationContext) : Simple | |||||
LayoutParams.MATCH_PARENT, | ||||||
) | ||||||
view.setPlayerView(playerView) | ||||||
attachCustomMessageHandlerBridge(view) | ||||||
customMessageHandlerBridgeId?.let { attachCustomMessageHandlerBridge(view, it) } | ||||||
} | ||||||
|
||||||
if (rnStyleConfigWrapper?.styleConfig?.isUiEnabled != false && | ||||||
|
@@ -285,13 +282,15 @@ class RNPlayerViewManager(private val context: ReactApplicationContext) : Simple | |||||
} | ||||||
} | ||||||
|
||||||
private inline fun runInMainLooperAndLogException(crossinline block: () -> Unit) { | ||||||
Handler(Looper.getMainLooper()).post { | ||||||
try { | ||||||
block() | ||||||
} catch (e: Exception) { | ||||||
Log.e(MODULE_NAME, "Error while command", e) | ||||||
} | ||||||
/** Post and log any exceptions instead of crashing the app. */ | ||||||
private inline fun Handler.postAndLogException(crossinline block: () -> Unit) = post { | ||||||
try { | ||||||
block() | ||||||
} catch (e: Exception) { | ||||||
Log.e(MODULE_NAME, "Error while executing command", e) | ||||||
} | ||||||
} | ||||||
|
||||||
private fun RNPlayerView.getPlayerViewOrThrow() = playerView | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I quite like the require naming convention that androidx libraries use: https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#requireActivity()
Suggested change
|
||||||
?: throw IllegalStateException("The player view is not yet created") | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.