Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

Commit

Permalink
Account for display cut outs
Browse files Browse the repository at this point in the history
  • Loading branch information
tunjid committed Feb 23, 2021
1 parent 072ffd4 commit 9aaa69e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/adetunjidahunsi.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import androidx.fragment.app.Fragment
import com.tunjid.androidx.R
import com.tunjid.androidx.core.delegates.fragmentArgs
import com.tunjid.androidx.databinding.FragmentImageDetailBinding
import com.tunjid.androidx.uidrivers.uiState
import com.tunjid.androidx.uidrivers.updatePartial

class DoggoFragment : Fragment(R.layout.fragment_image_detail) {

Expand All @@ -17,6 +19,7 @@ class DoggoFragment : Fragment(R.layout.fragment_image_detail) {
super.onViewCreated(view, savedInstanceState)

view.tag = doggo
view.setOnClickListener { ::uiState.updatePartial { copy(isImmersive = !isImmersive) } }

val binding = FragmentImageDetailBinding.bind(view)
object : DoggoBinder {
Expand Down
44 changes: 35 additions & 9 deletions app/src/main/java/com/tunjid/androidx/uidrivers/GlobalUiDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.os.Build
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import android.widget.EditText
import androidx.annotation.ColorInt
Expand Down Expand Up @@ -122,10 +123,7 @@ class GlobalUiDriver(
}

init {
host.window.decorView.systemUiVisibility = FULL_CONTROL_SYSTEM_UI_FLAGS
host.window.navigationBarColor = host.colorAt(R.color.transparent)
host.window.statusBarColor = host.colorAt(R.color.transparent)

host.window.assumeControl()
binding.root.setOnApplyWindowInsetsListener(rootInsetsListener)

binding.toolbar.setNavigationOnClickListener { navigator.pop() }
Expand Down Expand Up @@ -157,6 +155,7 @@ class GlobalUiDriver(
UiState::lightStatusBar.distinct onChanged this::setLightStatusBar
UiState::fragmentContainerState.distinct onChanged this::updateFragmentContainer
UiState::backgroundColor.distinct onChanged binding.contentRoot::animateBackground
UiState::isImmersive.distinct onChanged this::updateImmersivity

UiState::bottomNavPositionalState.distinct onChanged this::updateBottomNav
UiState::snackbarPositionalState.distinct onChanged this::updateSnackbar
Expand Down Expand Up @@ -236,6 +235,21 @@ class GlobalUiDriver(
}
}

private fun updateImmersivity(isImmersive: Boolean) = uiFlagTweak { systemUiFlags ->
when (isImmersive) {
true -> (systemUiFlags
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
)
false -> (systemUiFlags
and View.SYSTEM_UI_FLAG_FULLSCREEN.inv()
and View.SYSTEM_UI_FLAG_HIDE_NAVIGATION.inv()
and View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY.inv()
)
}
}

private fun setLightStatusBar(lightStatusBar: Boolean) = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> uiFlagTweak { flags ->
if (lightStatusBar) flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
Expand Down Expand Up @@ -267,11 +281,6 @@ class GlobalUiDriver(

companion object {
const val ANIMATION_DURATION = 300
private const val FULL_CONTROL_SYSTEM_UI_FLAGS =
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
}

/**
Expand Down Expand Up @@ -334,3 +343,20 @@ private class UISizes(host: FragmentActivity) {
}

private infix fun Int.countIf(condition: Boolean) = if (condition) this else 0

private fun Window.assumeControl() {
val context = decorView.context
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val windowAttributes = attributes
windowAttributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
attributes = windowAttributes
}
decorView.systemUiVisibility = FULL_CONTROL_SYSTEM_UI_FLAGS
navigationBarColor = context.colorAt(R.color.transparent)
statusBarColor = context.colorAt(R.color.transparent)
}

private const val FULL_CONTROL_SYSTEM_UI_FLAGS =
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1 change: 1 addition & 0 deletions app/src/main/java/com/tunjid/androidx/uidrivers/UiState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ data class UiState(
@field:ColorInt
@get:ColorInt
val navBarColor: Int = Color.BLACK,
val isImmersive: Boolean = false,
val lightStatusBar: Boolean = false,
val showsBottomNav: Boolean? = null,
val insetFlags: InsetDescriptor = InsetFlags.ALL,
Expand Down

0 comments on commit 9aaa69e

Please sign in to comment.