Browse Source

Hide app/nav bars on scroll

arkon 4 years ago
parent
commit
d72c51c8dd

+ 3 - 0
app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt

@@ -31,6 +31,7 @@ import eu.kanade.tachiyomi.source.online.HttpSource
 import eu.kanade.tachiyomi.ui.base.controller.NucleusController
 import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction
 import eu.kanade.tachiyomi.ui.library.ChangeMangaCategoriesDialog
+import eu.kanade.tachiyomi.ui.main.offsetFabAppbarHeight
 import eu.kanade.tachiyomi.ui.manga.MangaController
 import eu.kanade.tachiyomi.ui.webview.WebViewActivity
 import eu.kanade.tachiyomi.util.system.connectivityManager
@@ -157,6 +158,8 @@ open class BrowseSourceController(bundle: Bundle) :
         filterSheet?.setOnDismissListener { binding.fabFilter.visible() }
 
         binding.fabFilter.setOnClickListener { filterSheet?.show() }
+
+        binding.fabFilter.offsetFabAppbarHeight(activity!!)
         binding.fabFilter.visible()
     }
 

+ 3 - 0
app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryController.kt

@@ -17,6 +17,7 @@ import eu.kanade.tachiyomi.R
 import eu.kanade.tachiyomi.data.database.models.Category
 import eu.kanade.tachiyomi.databinding.CategoriesControllerBinding
 import eu.kanade.tachiyomi.ui.base.controller.NucleusController
+import eu.kanade.tachiyomi.ui.main.offsetFabAppbarHeight
 import eu.kanade.tachiyomi.util.system.toast
 import kotlinx.coroutines.flow.launchIn
 import kotlinx.coroutines.flow.onEach
@@ -93,6 +94,8 @@ class CategoryController :
                 CategoryCreateDialog(this@CategoryController).showDialog(router, null)
             }
             .launchIn(scope)
+
+        binding.fab.offsetFabAppbarHeight(activity!!)
     }
 
     /**

+ 3 - 0
app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt

@@ -13,6 +13,7 @@ import eu.kanade.tachiyomi.data.download.model.Download
 import eu.kanade.tachiyomi.databinding.DownloadControllerBinding
 import eu.kanade.tachiyomi.source.model.Page
 import eu.kanade.tachiyomi.ui.base.controller.NucleusController
+import eu.kanade.tachiyomi.ui.main.offsetFabAppbarHeight
 import java.util.HashMap
 import java.util.concurrent.TimeUnit
 import kotlinx.coroutines.flow.launchIn
@@ -92,6 +93,8 @@ class DownloadController :
             }
             .launchIn(scope)
 
+        binding.fab.offsetFabAppbarHeight(activity!!)
+
         // Subscribe to changes
         DownloadService.runningRelay
             .observeOn(AndroidSchedulers.mainThread())

+ 33 - 2
app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt

@@ -1,15 +1,21 @@
 package eu.kanade.tachiyomi.ui.main
 
+import android.app.Activity
 import android.app.SearchManager
 import android.content.Intent
 import android.os.Bundle
+import android.view.View
 import android.view.ViewGroup
 import android.widget.Toast
+import androidx.coordinatorlayout.widget.CoordinatorLayout
 import com.bluelinelabs.conductor.Conductor
 import com.bluelinelabs.conductor.Controller
 import com.bluelinelabs.conductor.ControllerChangeHandler
 import com.bluelinelabs.conductor.Router
 import com.bluelinelabs.conductor.RouterTransaction
+import com.google.android.material.appbar.AppBarLayout
+import com.google.android.material.behavior.HideBottomViewOnScrollBehavior
+import com.google.android.material.tabs.TabLayout
 import eu.kanade.tachiyomi.Migrations
 import eu.kanade.tachiyomi.R
 import eu.kanade.tachiyomi.data.notification.NotificationReceiver
@@ -318,11 +324,21 @@ class MainActivity : BaseActivity<MainActivityBinding>() {
 
         supportActionBar?.setDisplayHomeAsUpEnabled(router.backstackSize != 1)
 
+        // Always show appbar again when changing controllers
+        binding.appbar.setExpanded(true)
+
         if ((from == null || from is RootController) && to !is RootController) {
             bottomNavAnimator.collapse()
         }
-        if (to is RootController && from !is RootController) {
-            bottomNavAnimator.expand()
+        if (to is RootController) {
+            if (from !is RootController) {
+                bottomNavAnimator.expand()
+            }
+
+            // Always show bottom nav again when returning to a RootController
+            val layoutParams = binding.bottomNav.layoutParams as CoordinatorLayout.LayoutParams
+            val bottomViewNavigationBehavior = layoutParams.behavior as HideBottomViewOnScrollBehavior
+            bottomViewNavigationBehavior.slideUp(binding.bottomNav)
         }
 
         if (from is TabbedController) {
@@ -358,3 +374,18 @@ class MainActivity : BaseActivity<MainActivityBinding>() {
         const val INTENT_SEARCH_FILTER = "filter"
     }
 }
+
+/**
+ * Used to manually offset a FAB within child views that might be cut off due to the collapsing
+ * AppBarLayout.
+ */
+fun View.offsetFabAppbarHeight(activity: Activity) {
+    val appbar: AppBarLayout = activity.findViewById(R.id.appbar)
+    val tabs: TabLayout = activity.findViewById(R.id.tabs)
+    appbar.addOnOffsetChangedListener(
+        AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
+            val maxAbsOffset = appBarLayout.measuredHeight - tabs.measuredHeight
+            translationY = -maxAbsOffset - verticalOffset.toFloat()
+        }
+    )
+}

+ 2 - 0
app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt

@@ -24,6 +24,7 @@ import eu.kanade.tachiyomi.data.database.models.Manga
 import eu.kanade.tachiyomi.data.download.model.Download
 import eu.kanade.tachiyomi.databinding.ChaptersControllerBinding
 import eu.kanade.tachiyomi.ui.base.controller.NucleusController
+import eu.kanade.tachiyomi.ui.main.offsetFabAppbarHeight
 import eu.kanade.tachiyomi.ui.manga.MangaController
 import eu.kanade.tachiyomi.ui.reader.ReaderActivity
 import eu.kanade.tachiyomi.util.system.getResourceColor
@@ -120,6 +121,7 @@ class ChaptersController :
             }
             .launchIn(scope)
 
+        binding.fab.offsetFabAppbarHeight(activity!!)
         binding.fab.shrinkOnScroll(binding.recycler)
     }
 

+ 8 - 5
app/src/main/res/layout/main_activity.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
@@ -15,7 +15,8 @@
             android:layout_width="match_parent"
             android:layout_height="?attr/actionBarSize"
             android:background="?attr/colorPrimary"
-            android:theme="?attr/actionBarTheme" />
+            android:theme="?attr/actionBarTheme"
+            app:layout_scrollFlags="scroll|enterAlways|snap" />
 
         <com.google.android.material.tabs.TabLayout
             android:id="@+id/tabs"
@@ -28,8 +29,8 @@
     <com.bluelinelabs.conductor.ChangeHandlerFrameLayout
         android:id="@+id/controller_container"
         android:layout_width="match_parent"
-        android:layout_height="0dp"
-        android:layout_weight="1" />
+        android:layout_height="match_parent"
+        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
 
     <com.google.android.material.bottomnavigation.BottomNavigationView
         android:id="@+id/bottom_nav"
@@ -38,6 +39,8 @@
         android:layout_height="wrap_content"
         android:layout_gravity="bottom"
         app:labelVisibilityMode="labeled"
+        app:layout_insetEdge="bottom"
+        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
         app:menu="@menu/bottom_nav" />
 
-</LinearLayout>
+</androidx.coordinatorlayout.widget.CoordinatorLayout>