Separate out chapters heading into separate adapter

This commit is contained in:
arkon 2020-06-24 22:48:20 -04:00
parent 1c33032721
commit f2f6628693
5 changed files with 78 additions and 43 deletions

View File

@ -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<MangaChaptersHeaderAdapter.HeaderViewHolder>() {
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)
}
}
}
}

View File

@ -68,7 +68,8 @@ class MangaInfoChaptersController(private val fromSource: Boolean = false) :
private val preferences: PreferencesHelper by injectLazy() 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 private var chaptersAdapter: ChaptersAdapter? = null
/** /**
@ -110,10 +111,11 @@ class MangaInfoChaptersController(private val fromSource: Boolean = false) :
if (ctrl.manga == null || ctrl.source == null) return if (ctrl.manga == null || ctrl.source == null) return
// Init RecyclerView and adapter // Init RecyclerView and adapter
headerAdapter = MangaInfoHeaderAdapter(this, fromSource) mangaInfoAdapter = MangaInfoHeaderAdapter(this, fromSource)
chaptersHeaderAdapter = MangaChaptersHeaderAdapter()
chaptersAdapter = ChaptersAdapter(this, view.context) 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.layoutManager = LinearLayoutManager(view.context)
binding.recycler.addItemDecoration(DividerItemDecoration(view.context, DividerItemDecoration.VERTICAL)) binding.recycler.addItemDecoration(DividerItemDecoration(view.context, DividerItemDecoration.VERTICAL))
binding.recycler.setHasFixedSize(true) binding.recycler.setHasFixedSize(true)
@ -157,7 +159,8 @@ class MangaInfoChaptersController(private val fromSource: Boolean = false) :
override fun onDestroyView(view: View) { override fun onDestroyView(view: View) {
destroyActionModeIfNeeded() destroyActionModeIfNeeded()
binding.actionToolbar.destroy() binding.actionToolbar.destroy()
headerAdapter = null mangaInfoAdapter = null
chaptersHeaderAdapter = null
chaptersAdapter = null chaptersAdapter = null
super.onDestroyView(view) super.onDestroyView(view)
} }
@ -300,7 +303,7 @@ class MangaInfoChaptersController(private val fromSource: Boolean = false) :
fun onNextMangaInfo(manga: Manga, source: Source) { fun onNextMangaInfo(manga: Manga, source: Source) {
if (manga.initialized) { if (manga.initialized) {
// Update view. // Update view.
headerAdapter?.update(manga, source) mangaInfoAdapter?.update(manga, source)
// Skips directly to chapters list by default if navigated to from the library // Skips directly to chapters list by default if navigated to from the library
if (!fromSource) { if (!fromSource) {
@ -419,7 +422,7 @@ class MangaInfoChaptersController(private val fromSource: Boolean = false) :
} }
} }
headerAdapter?.notifyDataSetChanged() mangaInfoAdapter?.notifyDataSetChanged()
} }
fun onCategoriesClick() { fun onCategoriesClick() {
@ -511,8 +514,8 @@ class MangaInfoChaptersController(private val fromSource: Boolean = false) :
fetchChaptersFromSource() fetchChaptersFromSource()
} }
val header = headerAdapter ?: return val chaptersHeader = chaptersHeaderAdapter ?: return
header.setNumChapters(chapters.size) chaptersHeader.setNumChapters(chapters.size)
val adapter = chaptersAdapter ?: return val adapter = chaptersAdapter ?: return
adapter.updateDataSet(chapters) adapter.updateDataSet(chapters)

View File

@ -42,7 +42,6 @@ class MangaInfoHeaderAdapter(
private var manga: Manga = controller.presenter.manga private var manga: Manga = controller.presenter.manga
private var source: Source = controller.presenter.source private var source: Source = controller.presenter.source
private var numChapters: Int? = null
private val scope = CoroutineScope(Job() + Dispatchers.Main) private val scope = CoroutineScope(Job() + Dispatchers.Main)
private lateinit var binding: MangaInfoHeaderBinding private lateinit var binding: MangaInfoHeaderBinding
@ -73,12 +72,6 @@ class MangaInfoHeaderAdapter(
notifyDataSetChanged() notifyDataSetChanged()
} }
fun setNumChapters(numChapters: Int) {
this.numChapters = numChapters
notifyDataSetChanged()
}
inner class HeaderViewHolder(private val view: View) : RecyclerView.ViewHolder(view) { inner class HeaderViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
fun bind() { fun bind() {
// For rounded corners // For rounded corners
@ -174,7 +167,6 @@ class MangaInfoHeaderAdapter(
.launchIn(scope) .launchIn(scope)
setMangaInfo(manga, source) setMangaInfo(manga, source)
setChapterInfo()
} }
/** /**
@ -345,14 +337,5 @@ class MangaInfoHeaderAdapter(
isChecked = isFavorite 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)
}
}
} }
} }

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:paddingBottom="8dp"
tools:context=".ui.browse.source.browse.BrowseSourceController">
<TextView
android:id="@+id/chapters_label"
style="@style/TextAppearance.Regular.SubHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chapters"
android:textIsSelectable="false" />
</RelativeLayout>

View File

@ -252,22 +252,4 @@
</FrameLayout> </FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:paddingTop="16dp"
android:paddingBottom="8dp">
<TextView
android:id="@+id/chapters_label"
style="@style/TextAppearance.Regular.SubHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chapters"
android:textIsSelectable="false" />
</RelativeLayout>
</LinearLayout> </LinearLayout>