Skip to content

Commit

Permalink
Improved the edge to edge mode in landscape mode.
Browse files Browse the repository at this point in the history
* It was showing the content behind the display cutout in landscape mode.
* Removed the unnecessary code from BaseFragment.
* Enhanced the applying edge to edge mode for lower devices(Before Android 10).
  • Loading branch information
MohitMaliDeveloper committed Jan 2, 2025
1 parent ba80604 commit 207ff40
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ class KiwixMainActivity : CoreMainActivity() {
closeNavigationDrawer()
onNavigationItemSelected(item)
}
applyEdgeToEdgeInsets()
}
activityKiwixMainBinding.bottomNavView.setupWithNavController(navController)
lifecycleScope.launch {
Expand All @@ -138,7 +137,7 @@ class KiwixMainActivity : CoreMainActivity() {
handleZimFileIntent(intent)
handleNotificationIntent(intent)
handleGetContentIntent(intent)
readerTableOfContentsDrawer.applyEdgeToEdgeInsets()
activityKiwixMainBinding.root.applyEdgeToEdgeInsets()
}

private suspend fun migrateInternalToPublicAppDirectory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.extensions.applyEdgeToEdgeInsets
import org.kiwix.kiwixmobile.core.extensions.enableEdgeToEdgeMode
import org.kiwix.kiwixmobile.core.extensions.getToolbarNavigationIcon
import org.kiwix.kiwixmobile.core.extensions.setFragmentBackgroundColorForAndroid15AndAbove
import org.kiwix.kiwixmobile.core.extensions.setToolTipWithContentDescription
import org.kiwix.kiwixmobile.core.main.CoreReaderFragment

/**
* All fragments should inherit from this fragment.
Expand All @@ -56,8 +54,6 @@ abstract class BaseFragment : Fragment() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
setFragmentBackgroundColorForAndroid15AndAbove()
}
// Ignore adding the bottom padding to coreReaderFragment.
this.view.applyEdgeToEdgeInsets(this !is CoreReaderFragment)
}

// Setup toolbar to handle common back pressed event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ open class ErrorActivity : BaseActivity() {
}
setupReportButton()
activityKiwixErrorBinding?.restartButton?.setOnClickListener { restartApp() }
activityKiwixErrorBinding?.root.applyEdgeToEdgeInsets(true)
activityKiwixErrorBinding?.root.applyEdgeToEdgeInsets()
}

override fun onDestroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@
package org.kiwix.kiwixmobile.core.extensions

import android.annotation.SuppressLint
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import androidx.annotation.ColorInt
import androidx.appcompat.widget.TooltipCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.core.view.updatePadding
import androidx.core.view.updateLayoutParams
import com.google.android.material.snackbar.BaseTransientBottomBar
import com.google.android.material.snackbar.Snackbar

Expand Down Expand Up @@ -91,48 +93,58 @@ fun View.setToolTipWithContentDescription(description: String) {

fun View.showFullScreenMode(window: Window) {
WindowCompat.setDecorFitsSystemWindows(window, false)
WindowInsetsControllerCompat(window, window.decorView).apply {
hide(WindowInsetsCompat.Type.systemBars())
hide(WindowInsetsCompat.Type.displayCutout())
systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowInsetsControllerCompat(window, window.decorView).apply {
hide(WindowInsetsCompat.Type.systemBars())
hide(WindowInsetsCompat.Type.displayCutout())
systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}
@Suppress("Deprecation")
window.apply {
addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)
}
}

fun View.closeFullScreenMode(window: Window) {
WindowCompat.setDecorFitsSystemWindows(window, false)
WindowInsetsControllerCompat(window, window.decorView).apply {
show(WindowInsetsCompat.Type.systemBars())
show(WindowInsetsCompat.Type.displayCutout())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowInsetsControllerCompat(window, window.decorView).apply {
show(WindowInsetsCompat.Type.systemBars())
show(WindowInsetsCompat.Type.displayCutout())
}
}
@Suppress("DEPRECATION")
window.apply {
addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)
clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
}

/**
* Adjusts the view's top margin and optionally its bottom padding to handle edge-to-edge insets
* (e.g., system bars, display cutouts, and system gestures).
* Applies edge-to-edge insets to the current view by adjusting its margins
* to account for system bars and display cutouts (e.g., status bar, navigation bar, and notches).
*
* By default, this method adds bottom padding to avoid content being obscured by the system bar.
* However, bottom padding can be skipped for specific cases like the reader fragment, where adding
* it might cause the BottomAppBar to overlap the content.
* This method ensures that the view avoids overlapping with system UI components by dynamically
* setting margins based on the insets provided by the system.
*
* @param shouldAddBottomPadding Whether to add padding to the bottom of the view to handle system insets.
* Defaults to `true`.
* Usage: Call this method on any view to apply edge-to-edge handling.
*/
fun View?.applyEdgeToEdgeInsets(shouldAddBottomPadding: Boolean = true) {
fun View?.applyEdgeToEdgeInsets() {
this?.let {
ViewCompat.setOnApplyWindowInsetsListener(it) { view, windowInsets ->
val insets = windowInsets.getInsets(
WindowInsetsCompat.Type.systemBars()
or WindowInsetsCompat.Type.displayCutout()
)
val layoutParams = view.layoutParams as? ViewGroup.MarginLayoutParams
layoutParams?.topMargin = insets.top
view.layoutParams = layoutParams
// Optionally add padding to the bottom to prevent content from being obscured by system bars.
if (shouldAddBottomPadding) {
val systemBarsInsets =
windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
view.updatePadding(bottom = systemBarsInsets.bottom)
val systemBarsInsets =
windowInsets.getInsets(
WindowInsetsCompat.Type.displayCutout() or
WindowInsetsCompat.Type.systemBars()
)
view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
topMargin = systemBarsInsets.top
leftMargin = systemBarsInsets.left
bottomMargin = systemBarsInsets.bottom
rightMargin = systemBarsInsets.right
}
WindowInsetsCompat.CONSUMED
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
<item name="colorAccent">?colorSecondary</item>
<item name="android:navigationBarColor">@color/black</item>

<!-- To prevent showing content behind the cutouts -->
<item name="android:windowLayoutInDisplayCutoutMode" tools:ignore="NewApi">never</item>
</style>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ class CustomMainActivity : CoreMainActivity() {
super.onCreate(savedInstanceState)
activityCustomMainBinding = ActivityCustomMainBinding.inflate(layoutInflater)
setContentView(activityCustomMainBinding.root)
drawerNavView.applyEdgeToEdgeInsets()
readerTableOfContentsDrawer.applyEdgeToEdgeInsets()
activityCustomMainBinding.root.applyEdgeToEdgeInsets()
if (savedInstanceState != null) {
return
}
Expand Down

0 comments on commit 207ff40

Please sign in to comment.