Use view binding in TrackAdapter

This commit is contained in:
arkon 2020-06-17 22:45:40 -04:00
parent cbb8f25645
commit a768280d82
2 changed files with 32 additions and 41 deletions

View File

@ -1,12 +1,15 @@
package eu.kanade.tachiyomi.ui.manga.track package eu.kanade.tachiyomi.ui.manga.track
import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.databinding.TrackItemBinding
import eu.kanade.tachiyomi.util.view.inflate import eu.kanade.tachiyomi.util.view.inflate
class TrackAdapter(controller: TrackController) : RecyclerView.Adapter<TrackHolder>() { class TrackAdapter(controller: TrackController) : RecyclerView.Adapter<TrackHolder>() {
private lateinit var binding: TrackItemBinding
var items = emptyList<TrackItem>() var items = emptyList<TrackItem>()
set(value) { set(value) {
if (field !== value) { if (field !== value) {
@ -26,8 +29,8 @@ class TrackAdapter(controller: TrackController) : RecyclerView.Adapter<TrackHold
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TrackHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TrackHolder {
val view = parent.inflate(R.layout.track_item) binding = TrackItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return TrackHolder(view, this) return TrackHolder(binding, this)
} }
override fun onBindViewHolder(holder: TrackHolder, position: Int) { override fun onBindViewHolder(holder: TrackHolder, position: Int) {

View File

@ -1,27 +1,15 @@
package eu.kanade.tachiyomi.ui.manga.track package eu.kanade.tachiyomi.ui.manga.track
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.view.View
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.databinding.TrackItemBinding
import eu.kanade.tachiyomi.ui.base.holder.BaseViewHolder import eu.kanade.tachiyomi.ui.base.holder.BaseViewHolder
import eu.kanade.tachiyomi.util.view.gone import eu.kanade.tachiyomi.util.view.gone
import eu.kanade.tachiyomi.util.view.visibleIf import eu.kanade.tachiyomi.util.view.visibleIf
import java.text.DateFormat import java.text.DateFormat
import kotlinx.android.synthetic.main.track_item.bottom_divider
import kotlinx.android.synthetic.main.track_item.logo_container
import kotlinx.android.synthetic.main.track_item.track_chapters
import kotlinx.android.synthetic.main.track_item.track_details
import kotlinx.android.synthetic.main.track_item.track_finish_date
import kotlinx.android.synthetic.main.track_item.track_logo
import kotlinx.android.synthetic.main.track_item.track_score
import kotlinx.android.synthetic.main.track_item.track_set
import kotlinx.android.synthetic.main.track_item.track_start_date
import kotlinx.android.synthetic.main.track_item.track_status
import kotlinx.android.synthetic.main.track_item.track_title
import kotlinx.android.synthetic.main.track_item.vert_divider_3
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
class TrackHolder(view: View, adapter: TrackAdapter) : BaseViewHolder(view) { class TrackHolder(private val binding: TrackItemBinding, adapter: TrackAdapter) : BaseViewHolder(binding.root) {
private val preferences: PreferencesHelper by injectLazy() private val preferences: PreferencesHelper by injectLazy()
@ -32,47 +20,47 @@ class TrackHolder(view: View, adapter: TrackAdapter) : BaseViewHolder(view) {
init { init {
val listener = adapter.rowClickListener val listener = adapter.rowClickListener
logo_container.setOnClickListener { listener.onLogoClick(bindingAdapterPosition) } binding.logoContainer.setOnClickListener { listener.onLogoClick(bindingAdapterPosition) }
track_set.setOnClickListener { listener.onSetClick(bindingAdapterPosition) } binding.trackSet.setOnClickListener { listener.onSetClick(bindingAdapterPosition) }
track_title.setOnClickListener { listener.onSetClick(bindingAdapterPosition) } binding.trackTitle.setOnClickListener { listener.onSetClick(bindingAdapterPosition) }
track_title.setOnLongClickListener { binding.trackTitle.setOnLongClickListener {
listener.onTitleLongClick(bindingAdapterPosition) listener.onTitleLongClick(bindingAdapterPosition)
true true
} }
track_status.setOnClickListener { listener.onStatusClick(bindingAdapterPosition) } binding.trackStatus.setOnClickListener { listener.onStatusClick(bindingAdapterPosition) }
track_chapters.setOnClickListener { listener.onChaptersClick(bindingAdapterPosition) } binding.trackChapters.setOnClickListener { listener.onChaptersClick(bindingAdapterPosition) }
track_score.setOnClickListener { listener.onScoreClick(bindingAdapterPosition) } binding.trackScore.setOnClickListener { listener.onScoreClick(bindingAdapterPosition) }
track_start_date.setOnClickListener { listener.onStartDateClick(bindingAdapterPosition) } binding.trackStartDate.setOnClickListener { listener.onStartDateClick(bindingAdapterPosition) }
track_finish_date.setOnClickListener { listener.onFinishDateClick(bindingAdapterPosition) } binding.trackFinishDate.setOnClickListener { listener.onFinishDateClick(bindingAdapterPosition) }
} }
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
fun bind(item: TrackItem) { fun bind(item: TrackItem) {
val track = item.track val track = item.track
track_logo.setImageResource(item.service.getLogo()) binding.trackLogo.setImageResource(item.service.getLogo())
logo_container.setBackgroundColor(item.service.getLogoColor()) binding.logoContainer.setBackgroundColor(item.service.getLogoColor())
track_set.visibleIf { track == null } binding.trackSet.visibleIf { track == null }
track_title.visibleIf { track != null } binding.trackTitle.visibleIf { track != null }
track_details.visibleIf { track != null } binding.trackDetails.visibleIf { track != null }
if (track != null) { if (track != null) {
track_title.text = track.title binding.trackTitle.text = track.title
track_chapters.text = "${track.last_chapter_read}/" + binding.trackChapters.text = "${track.last_chapter_read}/" +
if (track.total_chapters > 0) track.total_chapters else "-" if (track.total_chapters > 0) track.total_chapters else "-"
track_status.text = item.service.getStatus(track.status) binding.trackStatus.text = item.service.getStatus(track.status)
track_score.text = if (track.score == 0f) "-" else item.service.displayScore(track) binding.trackScore.text = if (track.score == 0f) "-" else item.service.displayScore(track)
if (item.service.supportsReadingDates) { if (item.service.supportsReadingDates) {
track_start_date.text = binding.trackStartDate.text =
if (track.started_reading_date != 0L) dateFormat.format(track.started_reading_date) else "-" if (track.started_reading_date != 0L) dateFormat.format(track.started_reading_date) else "-"
track_finish_date.text = binding.trackFinishDate.text =
if (track.finished_reading_date != 0L) dateFormat.format(track.finished_reading_date) else "-" if (track.finished_reading_date != 0L) dateFormat.format(track.finished_reading_date) else "-"
} else { } else {
bottom_divider.gone() binding.bottomDivider.gone()
vert_divider_3.gone() binding.vertDivider3.gone()
track_start_date.gone() binding.trackStartDate.gone()
track_finish_date.gone() binding.trackFinishDate.gone()
} }
} }
} }