Improved the edge to edge mode in landscape mode.

* 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).
This commit is contained in:
MohitMaliFtechiz 2024-12-30 18:58:44 +05:30 committed by Kelson
parent e555e628bd
commit 77f67d4c8f
6 changed files with 46 additions and 38 deletions

View File

@ -129,7 +129,6 @@ class KiwixMainActivity : CoreMainActivity() {
closeNavigationDrawer() closeNavigationDrawer()
onNavigationItemSelected(item) onNavigationItemSelected(item)
} }
applyEdgeToEdgeInsets()
} }
activityKiwixMainBinding.bottomNavView.setupWithNavController(navController) activityKiwixMainBinding.bottomNavView.setupWithNavController(navController)
lifecycleScope.launch { lifecycleScope.launch {
@ -138,7 +137,7 @@ class KiwixMainActivity : CoreMainActivity() {
handleZimFileIntent(intent) handleZimFileIntent(intent)
handleNotificationIntent(intent) handleNotificationIntent(intent)
handleGetContentIntent(intent) handleGetContentIntent(intent)
readerTableOfContentsDrawer.applyEdgeToEdgeInsets() activityKiwixMainBinding.root.applyEdgeToEdgeInsets()
} }
private suspend fun migrateInternalToPublicAppDirectory() { private suspend fun migrateInternalToPublicAppDirectory() {

View File

@ -26,12 +26,10 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import org.kiwix.kiwixmobile.core.R 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.enableEdgeToEdgeMode
import org.kiwix.kiwixmobile.core.extensions.getToolbarNavigationIcon import org.kiwix.kiwixmobile.core.extensions.getToolbarNavigationIcon
import org.kiwix.kiwixmobile.core.extensions.setFragmentBackgroundColorForAndroid15AndAbove import org.kiwix.kiwixmobile.core.extensions.setFragmentBackgroundColorForAndroid15AndAbove
import org.kiwix.kiwixmobile.core.extensions.setToolTipWithContentDescription import org.kiwix.kiwixmobile.core.extensions.setToolTipWithContentDescription
import org.kiwix.kiwixmobile.core.main.CoreReaderFragment
/** /**
* All fragments should inherit from this fragment. * All fragments should inherit from this fragment.
@ -56,8 +54,6 @@ abstract class BaseFragment : Fragment() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
setFragmentBackgroundColorForAndroid15AndAbove() setFragmentBackgroundColorForAndroid15AndAbove()
} }
// Ignore adding the bottom padding to coreReaderFragment.
this.view.applyEdgeToEdgeInsets(this !is CoreReaderFragment)
} }
// Setup toolbar to handle common back pressed event // Setup toolbar to handle common back pressed event

View File

@ -86,7 +86,7 @@ open class ErrorActivity : BaseActivity() {
} }
setupReportButton() setupReportButton()
activityKiwixErrorBinding?.restartButton?.setOnClickListener { restartApp() } activityKiwixErrorBinding?.restartButton?.setOnClickListener { restartApp() }
activityKiwixErrorBinding?.root.applyEdgeToEdgeInsets(true) activityKiwixErrorBinding?.root.applyEdgeToEdgeInsets()
} }
override fun onDestroy() { override fun onDestroy() {

View File

@ -19,16 +19,18 @@
package org.kiwix.kiwixmobile.core.extensions package org.kiwix.kiwixmobile.core.extensions
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.os.Build
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.Window import android.view.Window
import android.view.WindowManager
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import androidx.appcompat.widget.TooltipCompat import androidx.appcompat.widget.TooltipCompat
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat 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.BaseTransientBottomBar
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
@ -91,48 +93,58 @@ fun View.setToolTipWithContentDescription(description: String) {
fun View.showFullScreenMode(window: Window) { fun View.showFullScreenMode(window: Window) {
WindowCompat.setDecorFitsSystemWindows(window, false) WindowCompat.setDecorFitsSystemWindows(window, false)
WindowInsetsControllerCompat(window, window.decorView).apply { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
hide(WindowInsetsCompat.Type.systemBars()) WindowInsetsControllerCompat(window, window.decorView).apply {
hide(WindowInsetsCompat.Type.displayCutout()) hide(WindowInsetsCompat.Type.systemBars())
systemBarsBehavior = hide(WindowInsetsCompat.Type.displayCutout())
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE 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) { fun View.closeFullScreenMode(window: Window) {
WindowCompat.setDecorFitsSystemWindows(window, false) WindowCompat.setDecorFitsSystemWindows(window, false)
WindowInsetsControllerCompat(window, window.decorView).apply { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
show(WindowInsetsCompat.Type.systemBars()) WindowInsetsControllerCompat(window, window.decorView).apply {
show(WindowInsetsCompat.Type.displayCutout()) 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 * Applies edge-to-edge insets to the current view by adjusting its margins
* (e.g., system bars, display cutouts, and system gestures). * 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. * This method ensures that the view avoids overlapping with system UI components by dynamically
* However, bottom padding can be skipped for specific cases like the reader fragment, where adding * setting margins based on the insets provided by the system.
* it might cause the BottomAppBar to overlap the content.
* *
* @param shouldAddBottomPadding Whether to add padding to the bottom of the view to handle system insets. * Usage: Call this method on any view to apply edge-to-edge handling.
* Defaults to `true`.
*/ */
fun View?.applyEdgeToEdgeInsets(shouldAddBottomPadding: Boolean = true) { fun View?.applyEdgeToEdgeInsets() {
this?.let { this?.let {
ViewCompat.setOnApplyWindowInsetsListener(it) { view, windowInsets -> ViewCompat.setOnApplyWindowInsetsListener(it) { view, windowInsets ->
val insets = windowInsets.getInsets( val systemBarsInsets =
WindowInsetsCompat.Type.systemBars() windowInsets.getInsets(
or WindowInsetsCompat.Type.displayCutout() WindowInsetsCompat.Type.displayCutout() or
) WindowInsetsCompat.Type.systemBars()
val layoutParams = view.layoutParams as? ViewGroup.MarginLayoutParams )
layoutParams?.topMargin = insets.top view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
view.layoutParams = layoutParams topMargin = systemBarsInsets.top
// Optionally add padding to the bottom to prevent content from being obscured by system bars. leftMargin = systemBarsInsets.left
if (shouldAddBottomPadding) { bottomMargin = systemBarsInsets.bottom
val systemBarsInsets = rightMargin = systemBarsInsets.right
windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
view.updatePadding(bottom = systemBarsInsets.bottom)
} }
WindowInsetsCompat.CONSUMED WindowInsetsCompat.CONSUMED
} }

View File

@ -91,6 +91,8 @@
<item name="colorAccent">?colorSecondary</item> <item name="colorAccent">?colorSecondary</item>
<item name="android:navigationBarColor">@color/black</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> </style>
</resources> </resources>

View File

@ -85,8 +85,7 @@ class CustomMainActivity : CoreMainActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
activityCustomMainBinding = ActivityCustomMainBinding.inflate(layoutInflater) activityCustomMainBinding = ActivityCustomMainBinding.inflate(layoutInflater)
setContentView(activityCustomMainBinding.root) setContentView(activityCustomMainBinding.root)
drawerNavView.applyEdgeToEdgeInsets() activityCustomMainBinding.root.applyEdgeToEdgeInsets()
readerTableOfContentsDrawer.applyEdgeToEdgeInsets()
if (savedInstanceState != null) { if (savedInstanceState != null) {
return return
} }