@@ -31,7 +31,6 @@ import android.view.View
3131import android.view.View.OnLongClickListener
3232import android.view.View.OnTouchListener
3333import android.view.ViewGroup
34- import android.view.ViewParent
3534import android.view.ViewTreeObserver
3635import android.view.WindowManager
3736import android.view.animation.DecelerateInterpolator
@@ -126,10 +125,8 @@ import org.schabi.newpipe.util.image.CoilHelper.loadDetailsThumbnail
126125import java.util.LinkedList
127126import java.util.List
128127import java.util.Objects
129- import java.util.Optional
130128import java.util.concurrent.TimeUnit
131129import java.util.function.Consumer
132- import java.util.function.Function
133130import kotlin.math.abs
134131import kotlin.math.max
135132import kotlin.math.min
@@ -229,8 +226,8 @@ class VideoDetailFragment :
229226 // It will do nothing if the player is not in fullscreen mode
230227 hideSystemUiIfNeeded()
231228
232- val playerUi: Optional < MainPlayerUi > =
233- player!! .UIs ().getOpt< MainPlayerUi > (MainPlayerUi ::class .java)
229+ val playerUi: MainPlayerUi ? =
230+ player!! .UIs ().get (MainPlayerUi ::class .java)
234231 if (! player!! .videoPlayerSelected() && ! playAfterConnect) {
235232 return
236233 }
@@ -239,19 +236,22 @@ class VideoDetailFragment :
239236 // If the video is playing but orientation changed
240237 // let's make the video in fullscreen again
241238 checkLandscape()
242- } else if (playerUi.map<Boolean ?>(Function { ui: MainPlayerUi ? -> ui!! .isFullscreen() && ! ui.isVerticalVideo() })
243- .orElse(false ) && // Tablet UI has orientation-independent fullscreen
239+ } else if (playerUi != null &&
240+ playerUi.isFullscreen() &&
241+ ! playerUi.isVerticalVideo() &&
242+ // Tablet UI has orientation-independent fullscreen
244243 ! DeviceUtils .isTablet(activity)
245244 ) {
246245 // Device is in portrait orientation after rotation but UI is in fullscreen.
247246 // Return back to non-fullscreen state
248- playerUi.ifPresent( Consumer { obj : MainPlayerUi ? -> obj !! . toggleFullscreen() } )
247+ playerUi.toggleFullscreen()
249248 }
250249
251250 if (playAfterConnect ||
252251 (
253- currentInfo != null && this .isAutoplayEnabled &&
254- playerUi.isEmpty()
252+ currentInfo != null &&
253+ this .isAutoplayEnabled &&
254+ playerUi == null
255255 )
256256 ) {
257257 autoPlayEnabled = true // forcefully start playing
@@ -572,8 +572,7 @@ class VideoDetailFragment :
572572 View .OnClickListener { v: View ? ->
573573 if (playerIsNotStopped()) {
574574 player!! .playPause()
575- player!! .UIs ().getOpt<VideoPlayerUi >(VideoPlayerUi ::class .java)
576- .ifPresent(Consumer { ui: VideoPlayerUi ? -> ui!! .hideControls(0 , 0 ) })
575+ player!! .UIs ().get(VideoPlayerUi ::class .java)?.hideControls(0 , 0 )
577576 showSystemUi()
578577 } else {
579578 autoPlayEnabled = true // forcefully start playing
@@ -776,9 +775,7 @@ class VideoDetailFragment :
776775
777776 override fun onKeyDown (keyCode : Int ): Boolean {
778777 return this .isPlayerAvailable &&
779- player!! .UIs ().getOpt<VideoPlayerUi >(VideoPlayerUi ::class .java)
780- .map<Boolean ?>(Function { playerUi: VideoPlayerUi ? -> playerUi!! .onKeyDown(keyCode) })
781- .orElse(false )
778+ player!! .UIs ().get(VideoPlayerUi ::class .java)?.onKeyDown(keyCode) == true
782779 }
783780
784781 override fun onBackPressed (): Boolean {
@@ -1137,14 +1134,11 @@ class VideoDetailFragment :
11371134 // If a user watched video inside fullscreen mode and than chose another player
11381135 // return to non-fullscreen mode
11391136 if (this .isPlayerAvailable) {
1140- player!! .UIs ().getOpt<MainPlayerUi >(MainPlayerUi ::class .java)
1141- .ifPresent(
1142- Consumer { playerUi: MainPlayerUi ? ->
1143- if (playerUi!! .isFullscreen()) {
1144- playerUi.toggleFullscreen()
1145- }
1146- }
1147- )
1137+ player!! .UIs ().get(MainPlayerUi ::class .java)?.let {
1138+ if (it.isFullscreen) {
1139+ it.toggleFullscreen()
1140+ }
1141+ }
11481142 }
11491143 }
11501144
@@ -1288,14 +1282,14 @@ class VideoDetailFragment :
12881282 */
12891283 private fun hideMainPlayerOnLoadingNewStream () {
12901284 val root = this .root
1291- if (noPlayerServiceAvailable() || root.isEmpty() || ! player!! .videoPlayerSelected()) {
1285+ if (noPlayerServiceAvailable() || root == null || ! player!! .videoPlayerSelected()) {
12921286 return
12931287 }
12941288
12951289 removeVideoPlayerView()
12961290 if (this .isAutoplayEnabled) {
12971291 playerService!! .stopForImmediateReusing()
1298- root.ifPresent( Consumer { view : View -> view. setVisibility(View .GONE ) } )
1292+ root.setVisibility(View .GONE )
12991293 } else {
13001294 PlayerHolder .stopService()
13011295 }
@@ -1376,18 +1370,16 @@ class VideoDetailFragment :
13761370 }
13771371 // setup the surface view height, so that it fits the video correctly
13781372 setHeightThumbnail()
1379- player!! .UIs ().getOpt<MainPlayerUi >(MainPlayerUi ::class .java)
1380- .ifPresent(
1381- Consumer { playerUi: MainPlayerUi ? ->
1382- // sometimes binding would be null here, even though getView() != null above u.u
1383- if (binding != null ) {
1384- // prevent from re-adding a view multiple times
1385- playerUi!! .removeViewFromParent()
1386- binding!! .playerPlaceholder.addView(playerUi.getBinding().getRoot())
1387- playerUi.setupVideoSurfaceIfNeeded()
1388- }
1389- }
1390- )
1373+ player!! .UIs ().get(MainPlayerUi ::class .java)?.let { playerUi ->
1374+ val b = binding
1375+ // sometimes binding would be null here, even though getView() != null above u.u
1376+ if (b != null ) {
1377+ // prevent from re-adding a view multiple times
1378+ playerUi.removeViewFromParent()
1379+ b.playerPlaceholder.addView(playerUi.getBinding().getRoot())
1380+ playerUi.setupVideoSurfaceIfNeeded()
1381+ }
1382+ }
13911383 }
13921384 )
13931385 }
@@ -1396,8 +1388,7 @@ class VideoDetailFragment :
13961388 makeDefaultHeightForVideoPlaceholder()
13971389
13981390 if (player != null ) {
1399- player!! .UIs ().getOpt<VideoPlayerUi >(VideoPlayerUi ::class .java)
1400- .ifPresent(Consumer { obj: VideoPlayerUi ? -> obj!! .removeViewFromParent() })
1391+ player!! .UIs ().get(VideoPlayerUi ::class .java)?.removeViewFromParent()
14011392 }
14021393 }
14031394
@@ -1474,15 +1465,12 @@ class VideoDetailFragment :
14741465 binding!! .detailThumbnailImageView.setMinimumHeight(newHeight)
14751466 if (this .isPlayerAvailable) {
14761467 val maxHeight = (metrics.heightPixels * MAX_PLAYER_HEIGHT ).toInt()
1477- player!! .UIs ().getOpt<VideoPlayerUi >(VideoPlayerUi ::class .java)
1478- .ifPresent(
1479- Consumer { ui: VideoPlayerUi ? ->
1480- ui!! .getBinding().surfaceView.setHeights(
1481- newHeight,
1482- if (ui.isFullscreen()) newHeight else maxHeight
1483- )
1484- }
1468+ player!! .UIs ().get(VideoPlayerUi ::class .java)?.let {
1469+ it.binding.surfaceView.setHeights(
1470+ newHeight,
1471+ if (it.isFullscreen) newHeight else maxHeight
14851472 )
1473+ }
14861474 }
14871475 }
14881476
@@ -2065,9 +2053,9 @@ class VideoDetailFragment :
20652053
20662054 override fun onFullscreenStateChanged (fullscreen : Boolean ) {
20672055 setupBrightness()
2068- if (! this .isPlayerAndPlayerServiceAvailable || player !! . UIs ()
2069- .getOpt< MainPlayerUi >( MainPlayerUi ::class .java).isEmpty() ||
2070- this .root.map< ViewParent ?>( Function { obj : View ? -> obj !! .getParent() }).isEmpty()
2056+ if (! this .isPlayerAndPlayerServiceAvailable ||
2057+ player?. UIs ()?.get( MainPlayerUi ::class .java) == null ||
2058+ this .root?.parent == null
20712059 ) {
20722060 return
20732061 }
@@ -2096,8 +2084,7 @@ class VideoDetailFragment :
20962084 if (DeviceUtils .isTablet(activity) &&
20972085 (! PlayerHelper .globalScreenOrientationLocked(activity) || isLandscape)
20982086 ) {
2099- player!! .UIs ().getOpt<MainPlayerUi >(MainPlayerUi ::class .java)
2100- .ifPresent(Consumer { obj: MainPlayerUi ? -> obj!! .toggleFullscreen() })
2087+ player!! .UIs ().get(MainPlayerUi ::class .java)?.toggleFullscreen()
21012088 return
21022089 }
21032090
@@ -2203,10 +2190,8 @@ class VideoDetailFragment :
22032190 }
22042191
22052192 private val isFullscreen: Boolean
2206- get() = this .isPlayerAvailable && player!! .UIs ()
2207- .getOpt<VideoPlayerUi >(VideoPlayerUi ::class .java)
2208- .map<Boolean ?>(Function { obj: VideoPlayerUi ? -> obj!! .isFullscreen() })
2209- .orElse(false )
2193+ get() = this .isPlayerAvailable && player?.UIs ()
2194+ ?.get(VideoPlayerUi ::class .java)?.isFullscreen() == true
22102195
22112196 private fun playerIsNotStopped (): Boolean {
22122197 return this .isPlayerAvailable && ! player!! .isStopped()
@@ -2290,8 +2275,7 @@ class VideoDetailFragment :
22902275 setAutoPlay(true )
22912276 }
22922277
2293- player!! .UIs ().getOpt<MainPlayerUi >(MainPlayerUi ::class .java)
2294- .ifPresent(Consumer { obj: MainPlayerUi ? -> obj!! .checkLandscape() })
2278+ player!! .UIs ().get(MainPlayerUi ::class .java)?.checkLandscape()
22952279 // Let's give a user time to look at video information page if video is not playing
22962280 if (PlayerHelper .globalScreenOrientationLocked(activity) && ! player!! .isPlaying()) {
22972281 player!! .play()
@@ -2590,8 +2574,7 @@ class VideoDetailFragment :
25902574 player!! .isPlaying() &&
25912575 ! this @VideoDetailFragment.isFullscreen && ! DeviceUtils .isTablet(activity)
25922576 ) {
2593- player!! .UIs ().getOpt<MainPlayerUi >(MainPlayerUi ::class .java)
2594- .ifPresent(Consumer { obj: MainPlayerUi ? -> obj!! .toggleFullscreen() })
2577+ player!! .UIs ().get(MainPlayerUi ::class .java)?.toggleFullscreen()
25952578 }
25962579 setOverlayLook(binding!! .appBarLayout, behavior, 1f )
25972580 }
@@ -2605,8 +2588,7 @@ class VideoDetailFragment :
26052588 // Re-enable clicks
26062589 setOverlayElementsClickable(true )
26072590 if (this @VideoDetailFragment.isPlayerAvailable) {
2608- player!! .UIs ().getOpt<MainPlayerUi >(MainPlayerUi ::class .java)
2609- .ifPresent(Consumer { obj: MainPlayerUi ? -> obj!! .closeItemsList() })
2591+ player!! .UIs ().get(MainPlayerUi ::class .java)?.closeItemsList()
26102592 }
26112593 setOverlayLook(binding!! .appBarLayout, behavior, 0f )
26122594 }
@@ -2616,13 +2598,11 @@ class VideoDetailFragment :
26162598 showSystemUi()
26172599 }
26182600 if (this @VideoDetailFragment.isPlayerAvailable) {
2619- player!! .UIs ().getOpt<MainPlayerUi >(MainPlayerUi ::class .java).ifPresent(
2620- Consumer { ui: MainPlayerUi ? ->
2621- if (ui!! .isControlsVisible()) {
2622- ui.hideControls(0 , 0 )
2623- }
2601+ player!! .UIs ().get(MainPlayerUi ::class .java)?.let {
2602+ if (it.isControlsVisible) {
2603+ it.hideControls(0 , 0 )
26242604 }
2625- )
2605+ }
26262606 }
26272607 }
26282608
@@ -2724,18 +2704,8 @@ class VideoDetailFragment :
27242704 val isPlayerAndPlayerServiceAvailable: Boolean
27252705 get() = player != null && playerService != null
27262706
2727- val root: Optional <View ?>
2728- get() = Optional .ofNullable<Player ?>(player)
2729- .flatMap<VideoPlayerUi ?>(
2730- Function { player1: Player ? ->
2731- player1!! .UIs ().getOpt<VideoPlayerUi >(VideoPlayerUi ::class .java)
2732- }
2733- )
2734- .map<View ?>(
2735- Function { playerUi: VideoPlayerUi ? ->
2736- playerUi!! .getBinding().getRoot()
2737- }
2738- )
2707+ val root: View ?
2708+ get() = player?.UIs ()?.get(VideoPlayerUi ::class .java)?.binding?.root
27392709
27402710 private fun updateBottomSheetState (newState : Int ) {
27412711 bottomSheetState = newState
0 commit comments