From f2f662869395456c4ec3f574180705c5ea1aa4c5 Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 24 Jun 2020 22:48:20 -0400 Subject: [PATCH] Separate out chapters heading into separate adapter --- .../chapter/MangaChaptersHeaderAdapter.kt | 47 +++++++++++++++++++ .../chapter/MangaInfoChaptersController.kt | 19 ++++---- .../manga/chapter/MangaInfoHeaderAdapter.kt | 17 ------- .../main/res/layout/manga_chapters_header.xml | 20 ++++++++ app/src/main/res/layout/manga_info_header.xml | 18 ------- 5 files changed, 78 insertions(+), 43 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/MangaChaptersHeaderAdapter.kt create mode 100644 app/src/main/res/layout/manga_chapters_header.xml diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/MangaChaptersHeaderAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/MangaChaptersHeaderAdapter.kt new file mode 100644 index 000000000..bb0ad2d39 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/MangaChaptersHeaderAdapter.kt @@ -0,0 +1,47 @@ +package eu.kanade.tachiyomi.ui.manga.chapter + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.databinding.MangaChaptersHeaderBinding +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job + +class MangaChaptersHeaderAdapter : + RecyclerView.Adapter() { + + private var numChapters: Int? = null + + private val scope = CoroutineScope(Job() + Dispatchers.Main) + private lateinit var binding: MangaChaptersHeaderBinding + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HeaderViewHolder { + binding = MangaChaptersHeaderBinding.inflate(LayoutInflater.from(parent.context), parent, false) + return HeaderViewHolder(binding.root) + } + + override fun getItemCount(): Int = 1 + + override fun onBindViewHolder(holder: HeaderViewHolder, position: Int) { + holder.bind() + } + + fun setNumChapters(numChapters: Int) { + this.numChapters = numChapters + + notifyDataSetChanged() + } + + inner class HeaderViewHolder(private val view: View) : RecyclerView.ViewHolder(view) { + fun bind() { + binding.chaptersLabel.text = if (numChapters == null) { + view.context.getString(R.string.chapters) + } else { + view.context.resources.getQuantityString(R.plurals.manga_num_chapters, numChapters!!, numChapters) + } + } + } +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/MangaInfoChaptersController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/MangaInfoChaptersController.kt index 33965a7e0..5a2d1ab10 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/MangaInfoChaptersController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/MangaInfoChaptersController.kt @@ -68,7 +68,8 @@ class MangaInfoChaptersController(private val fromSource: Boolean = false) : private val preferences: PreferencesHelper by injectLazy() - private var headerAdapter: MangaInfoHeaderAdapter? = null + private var mangaInfoAdapter: MangaInfoHeaderAdapter? = null + private var chaptersHeaderAdapter: MangaChaptersHeaderAdapter? = null private var chaptersAdapter: ChaptersAdapter? = null /** @@ -110,10 +111,11 @@ class MangaInfoChaptersController(private val fromSource: Boolean = false) : if (ctrl.manga == null || ctrl.source == null) return // Init RecyclerView and adapter - headerAdapter = MangaInfoHeaderAdapter(this, fromSource) + mangaInfoAdapter = MangaInfoHeaderAdapter(this, fromSource) + chaptersHeaderAdapter = MangaChaptersHeaderAdapter() chaptersAdapter = ChaptersAdapter(this, view.context) - binding.recycler.adapter = MergeAdapter(headerAdapter, chaptersAdapter) + binding.recycler.adapter = MergeAdapter(mangaInfoAdapter, chaptersHeaderAdapter, chaptersAdapter) binding.recycler.layoutManager = LinearLayoutManager(view.context) binding.recycler.addItemDecoration(DividerItemDecoration(view.context, DividerItemDecoration.VERTICAL)) binding.recycler.setHasFixedSize(true) @@ -157,7 +159,8 @@ class MangaInfoChaptersController(private val fromSource: Boolean = false) : override fun onDestroyView(view: View) { destroyActionModeIfNeeded() binding.actionToolbar.destroy() - headerAdapter = null + mangaInfoAdapter = null + chaptersHeaderAdapter = null chaptersAdapter = null super.onDestroyView(view) } @@ -300,7 +303,7 @@ class MangaInfoChaptersController(private val fromSource: Boolean = false) : fun onNextMangaInfo(manga: Manga, source: Source) { if (manga.initialized) { // Update view. - headerAdapter?.update(manga, source) + mangaInfoAdapter?.update(manga, source) // Skips directly to chapters list by default if navigated to from the library if (!fromSource) { @@ -419,7 +422,7 @@ class MangaInfoChaptersController(private val fromSource: Boolean = false) : } } - headerAdapter?.notifyDataSetChanged() + mangaInfoAdapter?.notifyDataSetChanged() } fun onCategoriesClick() { @@ -511,8 +514,8 @@ class MangaInfoChaptersController(private val fromSource: Boolean = false) : fetchChaptersFromSource() } - val header = headerAdapter ?: return - header.setNumChapters(chapters.size) + val chaptersHeader = chaptersHeaderAdapter ?: return + chaptersHeader.setNumChapters(chapters.size) val adapter = chaptersAdapter ?: return adapter.updateDataSet(chapters) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/MangaInfoHeaderAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/MangaInfoHeaderAdapter.kt index 2adfe4a02..731f5791e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/MangaInfoHeaderAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/MangaInfoHeaderAdapter.kt @@ -42,7 +42,6 @@ class MangaInfoHeaderAdapter( private var manga: Manga = controller.presenter.manga private var source: Source = controller.presenter.source - private var numChapters: Int? = null private val scope = CoroutineScope(Job() + Dispatchers.Main) private lateinit var binding: MangaInfoHeaderBinding @@ -73,12 +72,6 @@ class MangaInfoHeaderAdapter( notifyDataSetChanged() } - fun setNumChapters(numChapters: Int) { - this.numChapters = numChapters - - notifyDataSetChanged() - } - inner class HeaderViewHolder(private val view: View) : RecyclerView.ViewHolder(view) { fun bind() { // For rounded corners @@ -174,7 +167,6 @@ class MangaInfoHeaderAdapter( .launchIn(scope) setMangaInfo(manga, source) - setChapterInfo() } /** @@ -345,14 +337,5 @@ class MangaInfoHeaderAdapter( isChecked = isFavorite } } - - private fun setChapterInfo() { - // Chapters heading - binding.chaptersLabel.text = if (numChapters == null) { - view.context.getString(R.string.chapters) - } else { - view.context.resources.getQuantityString(R.plurals.manga_num_chapters, numChapters!!, numChapters) - } - } } } diff --git a/app/src/main/res/layout/manga_chapters_header.xml b/app/src/main/res/layout/manga_chapters_header.xml new file mode 100644 index 000000000..f23333a69 --- /dev/null +++ b/app/src/main/res/layout/manga_chapters_header.xml @@ -0,0 +1,20 @@ + + + + + + diff --git a/app/src/main/res/layout/manga_info_header.xml b/app/src/main/res/layout/manga_info_header.xml index 58630b462..35590e01d 100644 --- a/app/src/main/res/layout/manga_info_header.xml +++ b/app/src/main/res/layout/manga_info_header.xml @@ -252,22 +252,4 @@ - - - - - -