From 2e816846520b58d08600bc1d96a5f0a0a24f3bd6 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Thu, 14 Mar 2024 14:08:22 +0200 Subject: [PATCH] Fix chapters grid view --- .../details/ui/adapter/ChapterGridItemAD.kt | 2 +- .../ui/pager/chapters/ChapterGridSpanHelper.kt | 17 +++++++++++++++++ .../ui/pager/chapters/ChaptersFragment.kt | 4 +++- .../ui/adapter/TypedListSpacingDecoration.kt | 1 + .../koitharu/kotatsu/reader/ui/ChaptersSheet.kt | 4 +++- 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/adapter/ChapterGridItemAD.kt b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/adapter/ChapterGridItemAD.kt index c5f2a8c1d..7ee67d92d 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/adapter/ChapterGridItemAD.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/adapter/ChapterGridItemAD.kt @@ -24,7 +24,7 @@ fun chapterGridItemAD( bind { payloads -> if (payloads.isEmpty()) { - binding.textViewTitle.text = item.chapter.formatNumber() + binding.textViewTitle.text = item.chapter.formatNumber() ?: "?" } binding.imageViewNew.isVisible = item.isNew binding.imageViewCurrent.isVisible = item.isCurrent diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/pager/chapters/ChapterGridSpanHelper.kt b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/pager/chapters/ChapterGridSpanHelper.kt index 4c19a2a77..5d5b18d1d 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/pager/chapters/ChapterGridSpanHelper.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/pager/chapters/ChapterGridSpanHelper.kt @@ -4,6 +4,7 @@ import android.view.View import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView import org.koitharu.kotatsu.R +import org.koitharu.kotatsu.list.ui.adapter.ListItemType import kotlin.math.roundToInt class ChapterGridSpanHelper private constructor() : View.OnLayoutChangeListener { @@ -29,6 +30,22 @@ class ChapterGridSpanHelper private constructor() : View.OnLayoutChangeListener (rv.layoutManager as? GridLayoutManager)?.spanCount = getSpanCount(rv) } + class SpanSizeLookup( + private val recyclerView: RecyclerView + ) : GridLayoutManager.SpanSizeLookup() { + + override fun getSpanSize(position: Int): Int { + return when (recyclerView.adapter?.getItemViewType(position)) { + ListItemType.CHAPTER_LIST.ordinal, // for smooth transition + ListItemType.HEADER.ordinal -> getTotalSpans() + + else -> 1 + } + } + + private fun getTotalSpans() = (recyclerView.layoutManager as? GridLayoutManager)?.spanCount ?: 1 + } + companion object { fun attach(view: RecyclerView) { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/pager/chapters/ChaptersFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/pager/chapters/ChaptersFragment.kt index 85e4f6c31..641ebbd60 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/pager/chapters/ChaptersFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/pager/chapters/ChaptersFragment.kt @@ -66,7 +66,9 @@ class ChaptersFragment : ) viewModel.isChaptersInGridView.observe(viewLifecycleOwner) { chaptersInGridView -> binding.recyclerViewChapters.layoutManager = if (chaptersInGridView) { - GridLayoutManager(context, ChapterGridSpanHelper.getSpanCount(binding.recyclerViewChapters)) + GridLayoutManager(context, ChapterGridSpanHelper.getSpanCount(binding.recyclerViewChapters)).apply { + spanSizeLookup = ChapterGridSpanHelper.SpanSizeLookup(binding.recyclerViewChapters) + } } else { LinearLayoutManager(context) } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/adapter/TypedListSpacingDecoration.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/adapter/TypedListSpacingDecoration.kt index 7984879f5..9c4c90592 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/adapter/TypedListSpacingDecoration.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/adapter/TypedListSpacingDecoration.kt @@ -86,4 +86,5 @@ class TypedListSpacingDecoration( || this == ListItemType.FILTER_SORT || this == ListItemType.FILTER_TAG || this == ListItemType.CHAPTER_LIST + || this == ListItemType.CHAPTER_GRID } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ChaptersSheet.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ChaptersSheet.kt index a9244c12f..ac63da417 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ChaptersSheet.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ChaptersSheet.kt @@ -93,7 +93,9 @@ class ChaptersSheet : BaseAdaptiveSheet(), } ChapterGridSpanHelper.attach(binding.recyclerView) binding.recyclerView.layoutManager = if (settings.isChaptersGridView) { - GridLayoutManager(context, ChapterGridSpanHelper.getSpanCount(binding.recyclerView)) + GridLayoutManager(context, ChapterGridSpanHelper.getSpanCount(binding.recyclerView)).apply { + spanSizeLookup = ChapterGridSpanHelper.SpanSizeLookup(binding.recyclerView) + } } else { LinearLayoutManager(context) }