MainActivity fixes (#6591)
* Reduce notifyDataSetChanged calls when category count is disabled * Fix category tabs briefly showing when it's supposed to be disabled Also fix tabs showing when activity recreated * Lift appbar when tab is hidden Check against tab visibility instead of viewpager * Restore selected nav item after recreate * Simplify SHORTCUT_MANGA intent handling Don't need to change controller if the topmost controller is the target
This commit is contained in:
@@ -28,24 +28,13 @@ class LibraryAdapter(
|
||||
* The categories to bind in the adapter.
|
||||
*/
|
||||
var categories: List<Category> = emptyList()
|
||||
// This setter helps to not refresh the adapter if the reference to the list doesn't change.
|
||||
set(value) {
|
||||
if (field !== value) {
|
||||
field = value
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
private set
|
||||
|
||||
/**
|
||||
* The number of manga in each category.
|
||||
* List order must be the same as [categories]
|
||||
*/
|
||||
var itemsPerCategory: Map<Int, Int> = emptyMap()
|
||||
set(value) {
|
||||
if (field !== value) {
|
||||
field = value
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
private var itemsPerCategory: List<Int> = emptyList()
|
||||
|
||||
private var boundViews = arrayListOf<View>()
|
||||
|
||||
@@ -62,6 +51,29 @@ class LibraryAdapter(
|
||||
.launchIn(controller.viewScope)
|
||||
}
|
||||
|
||||
/**
|
||||
* Pair of category and size of category
|
||||
*/
|
||||
fun updateCategories(new: List<Pair<Category, Int>>) {
|
||||
var updated = false
|
||||
|
||||
val newCategories = new.map { it.first }
|
||||
if (categories != newCategories) {
|
||||
categories = newCategories
|
||||
updated = true
|
||||
}
|
||||
|
||||
val newItemsPerCategory = new.map { it.second }
|
||||
if (itemsPerCategory !== newItemsPerCategory) {
|
||||
itemsPerCategory = newItemsPerCategory
|
||||
updated = true
|
||||
}
|
||||
|
||||
if (updated) {
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new view for this adapter.
|
||||
*
|
||||
@@ -112,10 +124,11 @@ class LibraryAdapter(
|
||||
* @return the title to display.
|
||||
*/
|
||||
override fun getPageTitle(position: Int): CharSequence {
|
||||
if (preferences.categoryNumberOfItems().get()) {
|
||||
return categories[position].let { "${it.name} (${itemsPerCategory[it.id]})" }
|
||||
return if (!preferences.categoryNumberOfItems().get()) {
|
||||
categories[position].name
|
||||
} else {
|
||||
categories[position].let { "${it.name} (${itemsPerCategory[position]})" }
|
||||
}
|
||||
return categories[position].name
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,6 +8,7 @@ import android.view.MenuInflater
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import androidx.appcompat.view.ActionMode
|
||||
import androidx.core.view.doOnAttach
|
||||
import androidx.core.view.isVisible
|
||||
import com.bluelinelabs.conductor.ControllerChangeHandler
|
||||
import com.bluelinelabs.conductor.ControllerChangeType
|
||||
@@ -234,8 +235,9 @@ class LibraryController(
|
||||
super.onDestroyView(view)
|
||||
}
|
||||
|
||||
override fun configureTabs(tabs: TabLayout) {
|
||||
override fun configureTabs(tabs: TabLayout): Boolean {
|
||||
with(tabs) {
|
||||
isVisible = false
|
||||
tabGravity = TabLayout.GRAVITY_START
|
||||
tabMode = TabLayout.MODE_SCROLLABLE
|
||||
}
|
||||
@@ -247,6 +249,8 @@ class LibraryController(
|
||||
mangaCountVisibilitySubscription = mangaCountVisibilityRelay.subscribe {
|
||||
adapter?.notifyDataSetChanged()
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
override fun cleanupTabs(tabs: TabLayout) {
|
||||
@@ -291,22 +295,17 @@ class LibraryController(
|
||||
}
|
||||
|
||||
// Set the categories
|
||||
adapter.categories = categories
|
||||
adapter.itemsPerCategory = adapter.categories
|
||||
.map { (it.id ?: -1) to (mangaMap[it.id]?.size ?: 0) }
|
||||
.toMap()
|
||||
adapter.updateCategories(categories.map { it to (mangaMap[it.id]?.size ?: 0) })
|
||||
|
||||
// Restore active category.
|
||||
binding.libraryPager.setCurrentItem(activeCat, false)
|
||||
|
||||
// Trigger display of tabs
|
||||
onTabsSettingsChanged()
|
||||
onTabsSettingsChanged(firstLaunch = true)
|
||||
|
||||
// Delay the scroll position to allow the view to be properly measured.
|
||||
view.post {
|
||||
if (isAttached) {
|
||||
(activity as? MainActivity)?.binding?.tabs?.setScrollPosition(binding.libraryPager.currentItem, 0f, true)
|
||||
}
|
||||
view.doOnAttach {
|
||||
(activity as? MainActivity)?.binding?.tabs?.setScrollPosition(binding.libraryPager.currentItem, 0f, true)
|
||||
}
|
||||
|
||||
// Send the manga map to child fragments after the adapter is updated.
|
||||
@@ -338,9 +337,11 @@ class LibraryController(
|
||||
presenter.requestBadgesUpdate()
|
||||
}
|
||||
|
||||
private fun onTabsSettingsChanged() {
|
||||
private fun onTabsSettingsChanged(firstLaunch: Boolean = false) {
|
||||
if (!firstLaunch) {
|
||||
mangaCountVisibilityRelay.call(preferences.categoryNumberOfItems().get())
|
||||
}
|
||||
tabsVisibilityRelay.call(preferences.categoryTabs().get() && adapter?.categories?.size ?: 0 > 1)
|
||||
mangaCountVisibilityRelay.call(preferences.categoryNumberOfItems().get())
|
||||
updateTitle()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user