From 77f67d4c8f9a12fca1c370a9fdb488832ec68d93 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Mon, 30 Dec 2024 18:58:44 +0530 Subject: [PATCH] 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). --- .../kiwixmobile/main/KiwixMainActivity.kt | 3 +- .../kiwixmobile/core/base/BaseFragment.kt | 4 -- .../kiwixmobile/core/error/ErrorActivity.kt | 2 +- .../core/extensions/ViewExtensions.kt | 70 +++++++++++-------- core/src/main/res/values/themes.xml | 2 + .../custom/main/CustomMainActivity.kt | 3 +- 6 files changed, 46 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt b/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt index c210c8b76..5a3d644a1 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt @@ -129,7 +129,6 @@ class KiwixMainActivity : CoreMainActivity() { closeNavigationDrawer() onNavigationItemSelected(item) } - applyEdgeToEdgeInsets() } activityKiwixMainBinding.bottomNavView.setupWithNavController(navController) lifecycleScope.launch { @@ -138,7 +137,7 @@ class KiwixMainActivity : CoreMainActivity() { handleZimFileIntent(intent) handleNotificationIntent(intent) handleGetContentIntent(intent) - readerTableOfContentsDrawer.applyEdgeToEdgeInsets() + activityKiwixMainBinding.root.applyEdgeToEdgeInsets() } private suspend fun migrateInternalToPublicAppDirectory() { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/base/BaseFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/base/BaseFragment.kt index dff0f4000..c6d4e7e2d 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/base/BaseFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/base/BaseFragment.kt @@ -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. @@ -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 diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/error/ErrorActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/error/ErrorActivity.kt index e76dd37f1..bf504906f 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/error/ErrorActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/error/ErrorActivity.kt @@ -86,7 +86,7 @@ open class ErrorActivity : BaseActivity() { } setupReportButton() activityKiwixErrorBinding?.restartButton?.setOnClickListener { restartApp() } - activityKiwixErrorBinding?.root.applyEdgeToEdgeInsets(true) + activityKiwixErrorBinding?.root.applyEdgeToEdgeInsets() } override fun onDestroy() { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ViewExtensions.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ViewExtensions.kt index ad18774f2..5b5e3255c 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ViewExtensions.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ViewExtensions.kt @@ -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 @@ -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 { + topMargin = systemBarsInsets.top + leftMargin = systemBarsInsets.left + bottomMargin = systemBarsInsets.bottom + rightMargin = systemBarsInsets.right } WindowInsetsCompat.CONSUMED } diff --git a/core/src/main/res/values/themes.xml b/core/src/main/res/values/themes.xml index 5431a0432..c5869f938 100644 --- a/core/src/main/res/values/themes.xml +++ b/core/src/main/res/values/themes.xml @@ -91,6 +91,8 @@ ?colorSecondary @color/black + + never diff --git a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt index b4163ad21..f9a41b7aa 100644 --- a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt +++ b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt @@ -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 }