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 android.view.WindowManager
import androidx.core.view.WindowCompat import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import com.github.libretube.ui.extensions.hideSystemBars import com.github.libretube.ui.extensions.toggleSystemBars
import com.github.libretube.ui.extensions.showSystemBars
object WindowHelper { object WindowHelper {
fun toggleFullscreen(activity: Activity, isFullscreen: Boolean) { fun toggleFullscreen(activity: Activity, isFullscreen: Boolean) {
@ -21,10 +20,12 @@ object WindowHelper {
WindowCompat.setDecorFitsSystemWindows(window, !isFullscreen) WindowCompat.setDecorFitsSystemWindows(window, !isFullscreen)
if (isFullscreen) { // Show the system bars when it is not fullscreen and hide them when it is fullscreen
activity.hideSystemBars(WindowInsetsCompat.Type.systemBars()) // System bars means status bar and the navigation bar
} else { // See: https://developer.android.com/training/system-ui/immersive#kotlin
activity.showSystemBars(WindowInsetsCompat.Type.systemBars()) 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.WindowInsetsCompat.Type.InsetsType
import androidx.core.view.WindowInsetsControllerCompat 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 { WindowCompat.getInsetsController(window, window.decorView).apply {
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
if (showBars) {
show(types)
} else {
hide(types) hide(types)
} }
}
fun Activity.showSystemBars(@InsetsType types: Int) {
WindowCompat.getInsetsController(window, window.decorView).apply {
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
show(types)
} }
} }

View File

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