fix: horizontal margin of fullscreen player not updated

This commit is contained in:
Bnyro 2023-11-15 16:51:15 +01:00
parent 5245e62f97
commit c8749888e9
5 changed files with 29 additions and 32 deletions

View File

@ -8,6 +8,7 @@ import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.helpers.LocaleHelper
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.helpers.ThemeHelper
import com.github.libretube.helpers.WindowHelper
/**
* Activity that applies the LibreTube theme and the in-app language
@ -26,6 +27,11 @@ open class BaseActivity : AppCompatActivity() {
}
}
/**
* Whether the phone of the user has a cutout like a notch or not
*/
var hasCutout = false
override fun onCreate(savedInstanceState: Bundle?) {
// set the app theme (e.g. Material You)
ThemeHelper.updateTheme(this)
@ -35,6 +41,8 @@ open class BaseActivity : AppCompatActivity() {
requestOrientationChange()
hasCutout = WindowHelper.hasCutout(window.decorView)
super.onCreate(savedInstanceState)
}

View File

@ -690,6 +690,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
updateResolutionOnFullscreenChange(true)
openOrCloseFullscreenDialog(true)
binding.player.updateMarginsByFullscreenMode()
}
@SuppressLint("SourceLockedOrientationActivity")
@ -716,6 +718,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
openOrCloseFullscreenDialog(false)
checkForNecessaryOrientationRestart()
binding.player.updateMarginsByFullscreenMode()
}
private fun openOrCloseFullscreenDialog(open: Boolean) {

View File

@ -3,6 +3,7 @@ package com.github.libretube.ui.views
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.graphics.Color
import android.os.Handler
@ -98,10 +99,6 @@ open class CustomExoPlayerView(
private val supportFragmentManager
get() = activity.supportFragmentManager
private val hasCutout by lazy {
WindowHelper.hasCutout(this)
}
private fun toggleController() {
if (isControllerFullyVisible) hideController() else showController()
}
@ -553,9 +550,16 @@ open class CustomExoPlayerView(
open fun isFullscreen() =
resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
override fun onConfigurationChanged(newConfig: Configuration) {
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
updateMarginsByFullscreenMode()
}
/**
* Updates the margins according to the current orientation and fullscreen mode
*/
fun updateMarginsByFullscreenMode() {
// add a larger bottom margin to the time bar in landscape mode
binding.progressBar.updateLayoutParams<MarginLayoutParams> {
bottomMargin = (if (isFullscreen()) 20f else 10f).dpToPx()
@ -563,19 +567,17 @@ open class CustomExoPlayerView(
updateTopBarMargin()
// don't add extra padding if there's no cutout
if (!hasCutout && binding.topBar.marginStart == 0) return
// don't add extra padding if there's no cutout and no margin set that would need to be undone
if (!(context as BaseActivity).hasCutout && binding.topBar.marginStart == LANDSCAPE_MARGIN_HORIZONTAL_NONE) return
// add a margin to the top and the bottom bar in landscape mode for notches
val newMargin = when (newConfig.orientation) {
Configuration.ORIENTATION_LANDSCAPE -> LANDSCAPE_MARGIN_HORIZONTAL
else -> 0
}
val isForcedPortrait = activity.requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
val horizontalMargin = if (isFullscreen() && !isForcedPortrait) LANDSCAPE_MARGIN_HORIZONTAL else LANDSCAPE_MARGIN_HORIZONTAL_NONE
listOf(binding.topBar, binding.bottomBar).forEach {
it.updateLayoutParams<MarginLayoutParams> {
marginStart = newMargin
marginEnd = newMargin
marginStart = horizontalMargin
marginEnd = horizontalMargin
}
}
}
@ -599,7 +601,7 @@ open class CustomExoPlayerView(
*/
fun updateTopBarMargin() {
binding.topBar.updateLayoutParams<MarginLayoutParams> {
topMargin = getTopBarMarginDp().toFloat().dpToPx()
topMargin = (if (isFullscreen()) 18f else 0f).dpToPx()
}
}
@ -616,10 +618,6 @@ open class CustomExoPlayerView(
runnableHandler.postDelayed(100, UPDATE_POSITION_TOKEN, this::updateCurrentPosition)
}
open fun getTopBarMarginDp(): Int {
return if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) 10 else 0
}
override fun onSingleTap() {
toggleController()
}
@ -728,5 +726,6 @@ open class CustomExoPlayerView(
private const val ANIMATION_DURATION = 100L
private const val AUTO_HIDE_CONTROLLER_DELAY = 2000L
private val LANDSCAPE_MARGIN_HORIZONTAL = 20f.dpToPx()
private val LANDSCAPE_MARGIN_HORIZONTAL_NONE = 0f.dpToPx()
}
}

View File

@ -20,11 +20,6 @@ class OfflinePlayerView(
toggleSystemBars(true)
}
override fun getTopBarMarginDp(): Int {
// the offline player requires a bigger top bar margin
return if (isFullscreen()) 18 else super.getTopBarMarginDp()
}
override fun minimizeOrExitPlayer() {
(context as AppCompatActivity).onBackPressedDispatcher.onBackPressed()
}

View File

@ -1,7 +1,6 @@
package com.github.libretube.ui.views
import android.content.Context
import android.content.res.Configuration
import android.util.AttributeSet
import android.view.Window
import androidx.core.os.bundleOf
@ -194,14 +193,6 @@ class OnlinePlayerView(
}
}
override fun getTopBarMarginDp(): Int {
return when {
resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE -> 15
playerViewModel?.isFullscreen?.value == true -> 20
else -> super.getTopBarMarginDp()
}
}
override fun isFullscreen(): Boolean {
return playerViewModel?.isFullscreen?.value ?: super.isFullscreen()
}