refactor show and hide system bars function

This commit is contained in:
anilbeesetti 2023-04-01 18:51:55 +05:30
parent 6e452ad156
commit fb8145752c
3 changed files with 20 additions and 26 deletions

View File

@ -5,8 +5,7 @@ import android.os.Build
import android.view.WindowManager
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import com.github.libretube.ui.extensions.hideSystemBars
import com.github.libretube.ui.extensions.showSystemBars
import com.github.libretube.ui.extensions.toggleSystemBars
object WindowHelper {
fun toggleFullscreen(activity: Activity, isFullscreen: Boolean) {
@ -21,10 +20,12 @@ object WindowHelper {
WindowCompat.setDecorFitsSystemWindows(window, !isFullscreen)
if (isFullscreen) {
activity.hideSystemBars(WindowInsetsCompat.Type.systemBars())
} else {
activity.showSystemBars(WindowInsetsCompat.Type.systemBars())
}
// Show the system bars when it is not fullscreen and hide them when it is fullscreen
// System bars means status bar and the navigation bar
// See: https://developer.android.com/training/system-ui/immersive#kotlin
activity.toggleSystemBars(
types = WindowInsetsCompat.Type.systemBars(),
showBars = !isFullscreen
)
}
}

View File

@ -5,16 +5,13 @@ import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat.Type.InsetsType
import androidx.core.view.WindowInsetsControllerCompat
fun Activity.hideSystemBars(@InsetsType types: Int) {
fun Activity.toggleSystemBars(@InsetsType types: Int, showBars: Boolean) {
WindowCompat.getInsetsController(window, window.decorView).apply {
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
hide(types)
}
}
fun Activity.showSystemBars(@InsetsType types: Int) {
WindowCompat.getInsetsController(window, window.decorView).apply {
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
show(types)
if (showBars) {
show(types)
} else {
hide(types)
}
}
}

View File

@ -35,8 +35,7 @@ import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.helpers.WindowHelper
import com.github.libretube.obj.BottomSheetItem
import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.extensions.hideSystemBars
import com.github.libretube.ui.extensions.showSystemBars
import com.github.libretube.ui.extensions.toggleSystemBars
import com.github.libretube.ui.interfaces.OnlinePlayerOptions
import com.github.libretube.ui.interfaces.PlayerGestureOptions
import com.github.libretube.ui.interfaces.PlayerOptions
@ -209,14 +208,11 @@ internal class CustomExoPlayerView(
ControllerVisibilityListener { visibility ->
playerViewModel?.isFullscreen?.value?.let { isFullscreen ->
if (!isFullscreen) return@let
when (visibility) {
View.VISIBLE -> {
activity.showSystemBars(WindowInsetsCompat.Type.statusBars())
}
View.GONE -> {
activity.hideSystemBars(WindowInsetsCompat.Type.statusBars())
}
}
// Show status bar only not navigation bar if the player controls are visible and hide it otherwise
activity.toggleSystemBars(
types = WindowInsetsCompat.Type.statusBars(),
showBars = visibility == View.VISIBLE
)
}
}
)