Add feature to toggle between list and grid view for chapters

This commit is contained in:
jsericksk
2024-02-17 02:09:35 -03:00
parent 44adbde536
commit 0c823f1056
4 changed files with 27 additions and 6 deletions

View File

@@ -10,14 +10,24 @@ import org.koitharu.kotatsu.list.ui.adapter.listHeaderAD
import org.koitharu.kotatsu.list.ui.model.ListModel import org.koitharu.kotatsu.list.ui.model.ListModel
class ChaptersAdapter( class ChaptersAdapter(
onItemClickListener: OnListItemClickListener<ChapterListItem>, private val onItemClickListener: OnListItemClickListener<ChapterListItem>,
chaptersInGridView: Boolean,
) : BaseListAdapter<ListModel>(), FastScroller.SectionIndexer { ) : BaseListAdapter<ListModel>(), FastScroller.SectionIndexer {
init { init {
addDelegate(ListItemType.CHAPTER, chapterListItemAD(onItemClickListener)) setChapterAdapterDelegate(chaptersInGridView)
addDelegate(ListItemType.HEADER, listHeaderAD(null)) addDelegate(ListItemType.HEADER, listHeaderAD(null))
} }
fun setChapterAdapterDelegate(chaptersInGridView: Boolean) {
delegatesManager.removeDelegate(ListItemType.CHAPTER.ordinal)
if (chaptersInGridView) {
addDelegate(ListItemType.CHAPTER, chapterGridItemAD(onItemClickListener))
} else {
addDelegate(ListItemType.CHAPTER, chapterListItemAD(onItemClickListener))
}
}
override fun getSectionText(context: Context, position: Int): CharSequence? { override fun getSectionText(context: Context, position: Int): CharSequence? {
return findHeader(position)?.getText(context) return findHeader(position)?.getText(context)
} }

View File

@@ -1,6 +1,7 @@
package org.koitharu.kotatsu.details.ui.pager.chapters package org.koitharu.kotatsu.details.ui.pager.chapters
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
@@ -11,6 +12,8 @@ import androidx.core.graphics.Insets
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
@@ -55,7 +58,7 @@ class ChaptersFragment :
override fun onViewBindingCreated(binding: FragmentChaptersBinding, savedInstanceState: Bundle?) { override fun onViewBindingCreated(binding: FragmentChaptersBinding, savedInstanceState: Bundle?) {
super.onViewBindingCreated(binding, savedInstanceState) super.onViewBindingCreated(binding, savedInstanceState)
chaptersAdapter = ChaptersAdapter(this) chaptersAdapter = ChaptersAdapter(this, viewModel.isChaptersInGridView.value)
selectionController = ListSelectionController( selectionController = ListSelectionController(
activity = requireActivity(), activity = requireActivity(),
decoration = ChaptersSelectionDecoration(binding.root.context), decoration = ChaptersSelectionDecoration(binding.root.context),
@@ -67,7 +70,15 @@ class ChaptersFragment :
checkNotNull(selectionController).attachToRecyclerView(this) checkNotNull(selectionController).attachToRecyclerView(this)
setHasFixedSize(true) setHasFixedSize(true)
isNestedScrollingEnabled = false isNestedScrollingEnabled = false
adapter = chaptersAdapter }
viewModel.isChaptersInGridView.observe(viewLifecycleOwner) { chaptersInGridView ->
chaptersAdapter?.setChapterAdapterDelegate(chaptersInGridView)
binding.recyclerViewChapters.adapter = chaptersAdapter
binding.recyclerViewChapters.layoutManager = if (chaptersInGridView) {
GridLayoutManager(context, 4)
} else {
LinearLayoutManager(context)
}
} }
viewModel.isLoading.observe(viewLifecycleOwner, this::onLoadingStateChanged) viewModel.isLoading.observe(viewLifecycleOwner, this::onLoadingStateChanged)
viewModel.chapters viewModel.chapters

View File

@@ -75,7 +75,7 @@ class ChaptersSheet : BaseAdaptiveSheet<SheetChaptersBinding>(),
-1 -1
} }
binding.recyclerView.addItemDecoration(TypedListSpacingDecoration(binding.recyclerView.context, true)) binding.recyclerView.addItemDecoration(TypedListSpacingDecoration(binding.recyclerView.context, true))
binding.recyclerView.adapter = ChaptersAdapter(this).also { adapter -> binding.recyclerView.adapter = ChaptersAdapter(this, false).also { adapter ->
if (currentPosition >= 0) { if (currentPosition >= 0) {
val targetPosition = (currentPosition - 1).coerceAtLeast(0) val targetPosition = (currentPosition - 1).coerceAtLeast(0)
val offset = val offset =

View File

@@ -9,9 +9,9 @@
<com.google.android.material.card.MaterialCardView <com.google.android.material.card.MaterialCardView
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="6dp"
android:clipChildren="false" android:clipChildren="false"
app:cardCornerRadius="16dp" app:cardCornerRadius="16dp"
app:cardUseCompatPadding="true"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="H,1:1" app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"