mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-31 14:27:57 +01:00 
			
		
		
		
	Separate out chapters heading into separate adapter
This commit is contained in:
		| @@ -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) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user