From 122b2b1a8eb0bbc198439b78061ed44c53043cc9 Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 9 Nov 2020 17:39:22 -0500 Subject: [PATCH] Draw under navbar in Android 9+ --- .../ui/base/activity/BaseActivity.kt | 25 +++++++++++------ .../kanade/tachiyomi/ui/main/MainActivity.kt | 20 ++++++++++++- .../tachiyomi/util/view/WindowExtensions.kt | 28 ++++++++++++++++--- app/src/main/res/layout/main_activity.xml | 3 +- app/src/main/res/layout/webview_activity.xml | 1 + 5 files changed, 62 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt index 7635f07bb..49f276cea 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt @@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate import eu.kanade.tachiyomi.util.system.LocaleHelper +import eu.kanade.tachiyomi.util.view.edgeToEdge import uy.kohesive.injekt.injectLazy import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values @@ -23,6 +24,15 @@ abstract class BaseActivity : AppCompatActivity() { @Suppress("LeakingThis") private val secureActivityDelegate = SecureActivityDelegate(this) + private val isDarkMode: Boolean by lazy { + val themeMode = preferences.themeMode().get() + (themeMode == Values.ThemeMode.dark) || + ( + themeMode == Values.ThemeMode.system && + (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES) + ) + } + private val lightTheme: Int by lazy { when (preferences.themeLight().get()) { Values.LightThemeVariant.blue -> R.style.Theme_Tachiyomi_LightBlue @@ -60,15 +70,8 @@ abstract class BaseActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { setTheme( - when (preferences.themeMode().get()) { - Values.ThemeMode.system -> { - if (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES) { - darkTheme - } else { - lightTheme - } - } - Values.ThemeMode.dark -> darkTheme + when { + isDarkMode -> darkTheme else -> lightTheme } ) @@ -76,6 +79,10 @@ abstract class BaseActivity : AppCompatActivity() { super.onCreate(savedInstanceState) secureActivityDelegate.onCreate() + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + window.edgeToEdge(!isDarkMode && lightTheme != R.style.Theme_Tachiyomi_LightBlue) + } } override fun onResume() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index 95f7f7fb3..b2628da1b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -3,13 +3,16 @@ package eu.kanade.tachiyomi.ui.main import android.app.Activity import android.app.SearchManager import android.content.Intent +import android.os.Build import android.os.Bundle import android.view.View import android.view.ViewGroup import android.widget.Toast import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.core.view.isVisible +import androidx.core.view.marginBottom import androidx.core.view.updateLayoutParams +import androidx.core.view.updatePadding import androidx.preference.PreferenceDialogController import com.bluelinelabs.conductor.Conductor import com.bluelinelabs.conductor.Controller @@ -83,9 +86,24 @@ class MainActivity : BaseActivity() { } setContentView(binding.root) - setSupportActionBar(binding.toolbar) + // Inset paddings when drawing edge-to-edge in Android 9+ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + binding.bottomNav.setOnApplyWindowInsetsListener { view, insets -> + view.updatePadding(bottom = insets.systemWindowInsetBottom) + insets + } + + val initialFabBottomMargin = binding.rootFab.marginBottom + binding.rootFab.setOnApplyWindowInsetsListener { view, insets -> + view.updateLayoutParams { + bottomMargin = initialFabBottomMargin + insets.systemWindowInsetBottom + } + insets + } + } + tabAnimator = ViewHeightAnimator(binding.tabs, 0L) bottomNavAnimator = ViewHeightAnimator(binding.bottomNav) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/view/WindowExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/view/WindowExtensions.kt index 3ae882af3..1d5b42ac6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/view/WindowExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/view/WindowExtensions.kt @@ -1,22 +1,42 @@ package eu.kanade.tachiyomi.util.view +import android.graphics.Color +import android.os.Build import android.view.View import android.view.Window +import androidx.annotation.RequiresApi + +@RequiresApi(Build.VERSION_CODES.P) +fun Window.edgeToEdge(lightSystemUi: Boolean = false) { + decorView.systemUiVisibility = when { + // Handle light status and navigation bars programmatically to avoid duplicate themes + lightSystemUi -> { + View.SYSTEM_UI_FLAG_LAYOUT_STABLE or + View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or + View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or + View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR + } + else -> { + View.SYSTEM_UI_FLAG_LAYOUT_STABLE or + View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION + } + } + + navigationBarColor = Color.TRANSPARENT +} fun Window.showBar() { - val uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or + decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_STABLE - decorView.systemUiVisibility = uiFlags } fun Window.hideBar() { - val uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or + decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY - decorView.systemUiVisibility = uiFlags } fun Window.defaultBar() { diff --git a/app/src/main/res/layout/main_activity.xml b/app/src/main/res/layout/main_activity.xml index 46f95eced..cbd72ac50 100644 --- a/app/src/main/res/layout/main_activity.xml +++ b/app/src/main/res/layout/main_activity.xml @@ -10,7 +10,8 @@ + android:layout_height="wrap_content" + android:fitsSystemWindows="true">