Procházet zdrojové kódy

Less hacky way to make sure bottom action toolbar doesn't scroll down (#5871)

* Less hacky way to make sure bottom action toolbar doesn't scroll down

* Fix action toolbar overlapping on landscape

* Disable app bar transparency when ActionMode is present
Ivan Iskandar před 3 roky
rodič
revize
7b3c18bb97

+ 1 - 4
app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt

@@ -169,7 +169,7 @@ class LibraryController(
 
         binding.actionToolbar.applyInsetter {
             type(navigationBars = true) {
-                margin(bottom = true)
+                margin(bottom = true, horizontal = true)
             }
         }
 
@@ -216,8 +216,6 @@ class LibraryController(
                 )
             }
             .launchIn(viewScope)
-
-        (activity as? MainActivity)?.fixViewToBottom(binding.actionToolbar)
     }
 
     override fun onChangeStarted(handler: ControllerChangeHandler, type: ControllerChangeType) {
@@ -230,7 +228,6 @@ class LibraryController(
 
     override fun onDestroyView(view: View) {
         destroyActionModeIfNeeded()
-        (activity as? MainActivity)?.clearFixViewToBottom(binding.actionToolbar)
         binding.actionToolbar.destroy()
         adapter?.onDestroy()
         adapter = null

+ 26 - 16
app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt

@@ -7,9 +7,9 @@ import android.graphics.Color
 import android.os.Build
 import android.os.Bundle
 import android.view.Gravity
-import android.view.View
 import android.view.ViewGroup
 import android.widget.Toast
+import androidx.appcompat.view.ActionMode
 import androidx.coordinatorlayout.widget.CoordinatorLayout
 import androidx.core.animation.doOnEnd
 import androidx.core.splashscreen.SplashScreen
@@ -91,8 +91,6 @@ class MainActivity : BaseViewBindingActivity<MainActivityBinding>() {
     private var isConfirmingExit: Boolean = false
     private var isHandlingShortcut: Boolean = false
 
-    private var fixedViewsToBottom = mutableMapOf<View, AppBarLayout.OnOffsetChangedListener>()
-
     /**
      * App bar lift state for backstack
      */
@@ -477,6 +475,24 @@ class MainActivity : BaseViewBindingActivity<MainActivityBinding>() {
         }
     }
 
+    override fun onSupportActionModeStarted(mode: ActionMode) {
+        binding.appbar.apply {
+            tag = isTransparentWhenNotLifted
+            isTransparentWhenNotLifted = false
+        }
+        setToolbarScrolls(false)
+        super.onSupportActionModeStarted(mode)
+    }
+
+    override fun onSupportActionModeFinished(mode: ActionMode) {
+        binding.appbar.apply {
+            isTransparentWhenNotLifted = (tag as? Boolean) ?: false
+            tag = null
+        }
+        setToolbarScrolls(true)
+        super.onSupportActionModeFinished(mode)
+    }
+
     private suspend fun resetExitConfirmation() {
         isConfirmingExit = true
         val toast = toast(R.string.confirm_exit, Toast.LENGTH_LONG)
@@ -595,21 +611,15 @@ class MainActivity : BaseViewBindingActivity<MainActivityBinding>() {
     }
 
     /**
-     * Used to manually offset a view within the activity's child views that might be cut off due to
-     * the collapsing AppBarLayout.
+     * Sets toolbar CoordinatorLayout scroll flags
      */
-    fun fixViewToBottom(view: View) {
-        val listener = AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
-            val maxAbsOffset = appBarLayout.measuredHeight - binding.tabs.measuredHeight
-            view.translationY = -maxAbsOffset - verticalOffset.toFloat() + appBarLayout.paddingTop
+    private fun setToolbarScrolls(enabled: Boolean) = binding.toolbar.updateLayoutParams<AppBarLayout.LayoutParams> {
+        if (isTablet()) return@updateLayoutParams
+        scrollFlags = if (enabled) {
+            AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL or AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS
+        } else {
+            0
         }
-        binding.appbar.addOnOffsetChangedListener(listener)
-        fixedViewsToBottom[view] = listener
-    }
-
-    fun clearFixViewToBottom(view: View) {
-        val listener = fixedViewsToBottom.remove(view)
-        binding.appbar.removeOnOffsetChangedListener(listener)
     }
 
     private fun setBottomNavBehaviorOnScroll() {

+ 1 - 4
app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt

@@ -235,7 +235,7 @@ class MangaController :
             }
         binding.actionToolbar.applyInsetter {
             type(navigationBars = true) {
-                margin(bottom = true)
+                margin(bottom = true, horizontal = true)
             }
         }
 
@@ -329,8 +329,6 @@ class MangaController :
             }
             .launchIn(viewScope)
 
-        (activity as? MainActivity)?.fixViewToBottom(binding.actionToolbar)
-
         settingsSheet = ChaptersSettingsSheet(router, presenter) { group ->
             if (group is ChaptersSettingsSheet.Filter.FilterGroup) {
                 updateFilterIconState()
@@ -402,7 +400,6 @@ class MangaController :
 
     override fun onDestroyView(view: View) {
         destroyActionModeIfNeeded()
-        (activity as? MainActivity)?.clearFixViewToBottom(binding.actionToolbar)
         binding.actionToolbar.destroy()
         mangaInfoAdapter = null
         chaptersHeaderAdapter = null

+ 1 - 4
app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt

@@ -82,7 +82,7 @@ class UpdatesController :
         }
         binding.actionToolbar.applyInsetter {
             type(navigationBars = true) {
-                margin(bottom = true)
+                margin(bottom = true, horizontal = true)
             }
         }
 
@@ -113,13 +113,10 @@ class UpdatesController :
                 binding.swipeRefresh.isRefreshing = false
             }
             .launchIn(viewScope)
-
-        (activity as? MainActivity)?.fixViewToBottom(binding.actionToolbar)
     }
 
     override fun onDestroyView(view: View) {
         destroyActionModeIfNeeded()
-        (activity as? MainActivity)?.clearFixViewToBottom(binding.actionToolbar)
         binding.actionToolbar.destroy()
         adapter = null
         super.onDestroyView(view)