From de35a4c62a7636650b1ff88c33de0cc0695f6411 Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 14 May 2021 09:14:42 -0400 Subject: [PATCH] Initial tablet NavigationRailView implementation TODO: - Make the side nav go beside the toolbar too - Extract out common main_activity stuff to remove duplicated code --- app/build.gradle.kts | 2 +- .../tachiyomi/ui/library/LibraryController.kt | 4 +- .../kanade/tachiyomi/ui/main/MainActivity.kt | 86 +++++++++------- .../ui/recent/updates/UpdatesController.kt | 4 +- ...ttom_nav_selector.xml => nav_selector.xml} | 0 .../main/res/layout-sw600dp/main_activity.xml | 99 +++++++++++++++++++ app/src/main/res/layout/main_activity.xml | 6 +- .../res/menu/{bottom_nav.xml => main_nav.xml} | 0 8 files changed, 157 insertions(+), 44 deletions(-) rename app/src/main/res/color/{bottom_nav_selector.xml => nav_selector.xml} (100%) create mode 100644 app/src/main/res/layout-sw600dp/main_activity.xml rename app/src/main/res/menu/{bottom_nav.xml => main_nav.xml} (100%) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index e1cb74f651..6e624fa2d9 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -142,7 +142,7 @@ dependencies { implementation("androidx.work:work-runtime-ktx:2.5.0") // UI library - implementation("com.google.android.material:material:1.3.0") + implementation("com.google.android.material:material:1.4.0-beta01") "standardImplementation"("com.google.firebase:firebase-core:19.0.0") diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index 18023a2351..eb616bc0dd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -367,7 +367,7 @@ class LibraryController( actionMode!!, R.menu.library_selection ) { onActionItemClicked(it!!) } - (activity as? MainActivity)?.showBottomNav(visible = false, collapse = true) + (activity as? MainActivity)?.showNav(visible = false, collapse = true) } } @@ -476,7 +476,7 @@ class LibraryController( selectionRelay.call(LibrarySelectionEvent.Cleared()) binding.actionToolbar.hide() - (activity as? MainActivity)?.showBottomNav(visible = true, collapse = true) + (activity as? MainActivity)?.showNav(visible = true, collapse = true) actionMode = null } 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 2fbd0baa42..fa626c8435 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 @@ -24,6 +24,7 @@ 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.navigation.NavigationBarView import dev.chrisbanes.insetter.applyInsetter import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.Migrations @@ -76,7 +77,7 @@ class MainActivity : BaseViewBindingActivity() { } lateinit var tabAnimator: ViewHeightAnimator - private lateinit var bottomNavAnimator: ViewHeightAnimator + private var bottomNavAnimator: ViewHeightAnimator? = null private var isConfirmingExit: Boolean = false private var isHandlingShortcut: Boolean = false @@ -109,7 +110,7 @@ class MainActivity : BaseViewBindingActivity() { margin() } } - binding.bottomNav.applyInsetter { + binding.bottomNav?.applyInsetter { type(navigationBars = true) { padding() } @@ -131,23 +132,26 @@ class MainActivity : BaseViewBindingActivity() { } tabAnimator = ViewHeightAnimator(binding.tabs, 0L) - bottomNavAnimator = ViewHeightAnimator(binding.bottomNav) - // If bottom nav is hidden, make it visible again when the app bar is expanded - binding.appbar.addOnOffsetChangedListener( - AppBarLayout.OnOffsetChangedListener { _, verticalOffset -> - if (verticalOffset == 0) { - showBottomNav(true) + if (binding.bottomNav != null) { + bottomNavAnimator = ViewHeightAnimator(binding.bottomNav!!) + + // If bottom nav is hidden, make it visible again when the app bar is expanded + binding.appbar.addOnOffsetChangedListener( + AppBarLayout.OnOffsetChangedListener { _, verticalOffset -> + if (verticalOffset == 0) { + showNav(true) + } } - } - ) + ) - // Set behavior of bottom nav - preferences.hideBottomBar() - .asImmediateFlow { setBottomNavBehaviorOnScroll() } - .launchIn(lifecycleScope) + // Set behavior of bottom nav + preferences.hideBottomBar() + .asImmediateFlow { setBottomNavBehaviorOnScroll() } + .launchIn(lifecycleScope) + } - binding.bottomNav.setOnNavigationItemSelectedListener { item -> + nav.setOnItemSelectedListener { item -> val id = item.itemId val currentRoot = router.backstack.firstOrNull() @@ -256,9 +260,9 @@ class MainActivity : BaseViewBindingActivity() { private fun setExtensionsBadge() { val updates = preferences.extensionUpdatesCount().get() if (updates > 0) { - binding.bottomNav.getOrCreateBadge(R.id.nav_browse).number = updates + nav.getOrCreateBadge(R.id.nav_browse).number = updates } else { - binding.bottomNav.removeBadge(R.id.nav_browse) + nav.removeBadge(R.id.nav_browse) } } @@ -350,7 +354,7 @@ class MainActivity : BaseViewBindingActivity() { super.onDestroy() // Binding sometimes isn't actually instantiated yet somehow - binding?.bottomNav.setOnNavigationItemSelectedListener(null) + nav.setOnItemSelectedListener(null) binding?.toolbar.setNavigationOnClickListener(null) } @@ -385,7 +389,7 @@ class MainActivity : BaseViewBindingActivity() { fun setSelectedNavItem(itemId: Int) { if (!isFinishing) { - binding.bottomNav.selectedItemId = itemId + nav.selectedItemId = itemId } } @@ -407,11 +411,11 @@ class MainActivity : BaseViewBindingActivity() { binding.appbar.setExpanded(true) if ((from == null || from is RootController) && to !is RootController) { - showBottomNav(visible = false, collapse = true) + showNav(visible = false, collapse = true) } if (to is RootController) { // Always show bottom nav again when returning to a RootController - showBottomNav(visible = true, collapse = from !is RootController) + showNav(visible = true, collapse = from !is RootController) } if (from is TabbedController) { @@ -447,21 +451,28 @@ class MainActivity : BaseViewBindingActivity() { } } - fun showBottomNav(visible: Boolean, collapse: Boolean = false) { - val layoutParams = binding.bottomNav.layoutParams as CoordinatorLayout.LayoutParams - val bottomViewNavigationBehavior = layoutParams.behavior as? HideBottomViewOnScrollBehavior - if (visible) { - if (collapse) { - bottomNavAnimator.expand() - } + fun showNav(visible: Boolean, collapse: Boolean = false) { + binding.bottomNav?.let { + val layoutParams = it.layoutParams as CoordinatorLayout.LayoutParams + val bottomViewNavigationBehavior = + layoutParams.behavior as? HideBottomViewOnScrollBehavior + if (visible) { + if (collapse) { + bottomNavAnimator?.expand() + } - bottomViewNavigationBehavior?.slideUp(binding.bottomNav) - } else { - if (collapse) { - bottomNavAnimator.collapse() - } + bottomViewNavigationBehavior?.slideUp(it) + } else { + if (collapse) { + bottomNavAnimator?.collapse() + } - bottomViewNavigationBehavior?.slideDown(binding.bottomNav) + bottomViewNavigationBehavior?.slideDown(it) + } + } + + binding.sideNav?.let { + it.isVisible = visible } } @@ -484,9 +495,9 @@ class MainActivity : BaseViewBindingActivity() { } private fun setBottomNavBehaviorOnScroll() { - showBottomNav(visible = true) + showNav(visible = true) - binding.bottomNav.updateLayoutParams { + binding.bottomNav?.updateLayoutParams { behavior = when { preferences.hideBottomBar().get() -> HideBottomViewOnScrollBehavior() else -> null @@ -494,6 +505,9 @@ class MainActivity : BaseViewBindingActivity() { } } + private val nav: NavigationBarView + get() = binding.bottomNav ?: binding.sideNav!! + companion object { // Shortcut actions const val SHORTCUT_LIBRARY = "eu.kanade.tachiyomi.SHOW_LIBRARY" diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt index 33077968ae..11e8ab777f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt @@ -182,7 +182,7 @@ class UpdatesController : actionMode!!, R.menu.updates_chapter_selection ) { onActionItemClicked(it!!) } - (activity as? MainActivity)?.showBottomNav(visible = false, collapse = true) + (activity as? MainActivity)?.showNav(visible = false, collapse = true) } toggleSelection(position) @@ -380,7 +380,7 @@ class UpdatesController : adapter?.clearSelection() binding.actionToolbar.hide() - (activity as? MainActivity)?.showBottomNav(visible = true, collapse = true) + (activity as? MainActivity)?.showNav(visible = true, collapse = true) actionMode = null } diff --git a/app/src/main/res/color/bottom_nav_selector.xml b/app/src/main/res/color/nav_selector.xml similarity index 100% rename from app/src/main/res/color/bottom_nav_selector.xml rename to app/src/main/res/color/nav_selector.xml diff --git a/app/src/main/res/layout-sw600dp/main_activity.xml b/app/src/main/res/layout-sw600dp/main_activity.xml new file mode 100644 index 0000000000..ab0896696a --- /dev/null +++ b/app/src/main/res/layout-sw600dp/main_activity.xml @@ -0,0 +1,99 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/main_activity.xml b/app/src/main/res/layout/main_activity.xml index 6606e12cdf..559d555221 100644 --- a/app/src/main/res/layout/main_activity.xml +++ b/app/src/main/res/layout/main_activity.xml @@ -85,10 +85,10 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" - app:itemIconTint="@color/bottom_nav_selector" - app:itemTextColor="@color/bottom_nav_selector" + app:itemIconTint="@color/nav_selector" + app:itemTextColor="@color/nav_selector" app:labelVisibilityMode="labeled" app:layout_insetEdge="bottom" - app:menu="@menu/bottom_nav" /> + app:menu="@menu/main_nav" /> diff --git a/app/src/main/res/menu/bottom_nav.xml b/app/src/main/res/menu/main_nav.xml similarity index 100% rename from app/src/main/res/menu/bottom_nav.xml rename to app/src/main/res/menu/main_nav.xml