Fix chapters grid view

This commit is contained in:
Koitharu
2024-03-14 14:08:22 +02:00
parent 2573d150f9
commit 2e81684652
5 changed files with 25 additions and 3 deletions

View File

@@ -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

View File

@@ -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) {

View File

@@ -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)
}

View File

@@ -86,4 +86,5 @@ class TypedListSpacingDecoration(
|| this == ListItemType.FILTER_SORT
|| this == ListItemType.FILTER_TAG
|| this == ListItemType.CHAPTER_LIST
|| this == ListItemType.CHAPTER_GRID
}

View File

@@ -93,7 +93,9 @@ class ChaptersSheet : BaseAdaptiveSheet<SheetChaptersBinding>(),
}
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)
}