From 0c823f1056579d6d6beb40413aab7b42b9b82bec Mon Sep 17 00:00:00 2001 From: jsericksk Date: Sat, 17 Feb 2024 02:09:35 -0300 Subject: [PATCH] Add feature to toggle between list and grid view for chapters --- .../kotatsu/details/ui/adapter/ChaptersAdapter.kt | 14 ++++++++++++-- .../details/ui/pager/chapters/ChaptersFragment.kt | 15 +++++++++++++-- .../koitharu/kotatsu/reader/ui/ChaptersSheet.kt | 2 +- app/src/main/res/layout/item_chapter_grid.xml | 2 +- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/adapter/ChaptersAdapter.kt b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/adapter/ChaptersAdapter.kt index 2cdfbd26c..c14824000 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/adapter/ChaptersAdapter.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/adapter/ChaptersAdapter.kt @@ -10,14 +10,24 @@ import org.koitharu.kotatsu.list.ui.adapter.listHeaderAD import org.koitharu.kotatsu.list.ui.model.ListModel class ChaptersAdapter( - onItemClickListener: OnListItemClickListener, + private val onItemClickListener: OnListItemClickListener, + chaptersInGridView: Boolean, ) : BaseListAdapter(), FastScroller.SectionIndexer { init { - addDelegate(ListItemType.CHAPTER, chapterListItemAD(onItemClickListener)) + setChapterAdapterDelegate(chaptersInGridView) 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? { return findHeader(position)?.getText(context) } 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 671576b52..d9ada0fb3 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 @@ -1,6 +1,7 @@ package org.koitharu.kotatsu.details.ui.pager.chapters import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.Menu import android.view.MenuItem @@ -11,6 +12,8 @@ import androidx.core.graphics.Insets import androidx.core.view.isVisible import androidx.fragment.app.activityViewModels import androidx.lifecycle.Lifecycle +import androidx.recyclerview.widget.GridLayoutManager +import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.snackbar.Snackbar import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.flowOn @@ -55,7 +58,7 @@ class ChaptersFragment : override fun onViewBindingCreated(binding: FragmentChaptersBinding, savedInstanceState: Bundle?) { super.onViewBindingCreated(binding, savedInstanceState) - chaptersAdapter = ChaptersAdapter(this) + chaptersAdapter = ChaptersAdapter(this, viewModel.isChaptersInGridView.value) selectionController = ListSelectionController( activity = requireActivity(), decoration = ChaptersSelectionDecoration(binding.root.context), @@ -67,7 +70,15 @@ class ChaptersFragment : checkNotNull(selectionController).attachToRecyclerView(this) setHasFixedSize(true) 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.chapters 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 e32516fe2..f8bff285f 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 @@ -75,7 +75,7 @@ class ChaptersSheet : BaseAdaptiveSheet(), -1 } 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) { val targetPosition = (currentPosition - 1).coerceAtLeast(0) val offset = diff --git a/app/src/main/res/layout/item_chapter_grid.xml b/app/src/main/res/layout/item_chapter_grid.xml index be845c63d..670de9ff8 100644 --- a/app/src/main/res/layout/item_chapter_grid.xml +++ b/app/src/main/res/layout/item_chapter_grid.xml @@ -9,9 +9,9 @@