Move binding source binding logic to the viewholder.
This commit is contained in:
parent
3ad2ad402f
commit
ba4b27651b
@ -6,10 +6,8 @@ import android.content.Intent
|
|||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.Button
|
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.constraintlayout.widget.ConstraintLayout
|
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import bou.amine.apps.readerforselfossv2.android.R
|
import bou.amine.apps.readerforselfossv2.android.R
|
||||||
import bou.amine.apps.readerforselfossv2.android.UpsertSourceActivity
|
import bou.amine.apps.readerforselfossv2.android.UpsertSourceActivity
|
||||||
@ -33,47 +31,15 @@ class SourcesListAdapter(
|
|||||||
private val items: ArrayList<SelfossModel.SourceDetail>,
|
private val items: ArrayList<SelfossModel.SourceDetail>,
|
||||||
) : RecyclerView.Adapter<SourcesListAdapter.ViewHolder>(),
|
) : RecyclerView.Adapter<SourcesListAdapter.ViewHolder>(),
|
||||||
DIAware {
|
DIAware {
|
||||||
private val c: Context = app.baseContext
|
|
||||||
private lateinit var binding: SourceListItemBinding
|
|
||||||
|
|
||||||
override val di: DI by closestDI(app)
|
override val di: DI by closestDI(app)
|
||||||
private val repository: Repository by instance()
|
|
||||||
private val appSettingsService: AppSettingsService by instance()
|
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||||
binding = SourceListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
val binding = SourceListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
||||||
return ViewHolder(binding.root)
|
return ViewHolder(binding)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||||
holder: ViewHolder,
|
holder.bind(items[position], position)
|
||||||
position: Int,
|
|
||||||
) {
|
|
||||||
val source = items[position]
|
|
||||||
|
|
||||||
val deleteBtn: Button = holder.mView.findViewById(R.id.deleteBtn)
|
|
||||||
|
|
||||||
deleteBtn.setOnClickListener { this.showDeleteConfirmationDialog(source, position) }
|
|
||||||
|
|
||||||
holder.mView.setOnClickListener {
|
|
||||||
repository.setSelectedSource(source)
|
|
||||||
app.startActivity(Intent(app, UpsertSourceActivity::class.java))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (source.getIcon(repository.baseUrl).isEmpty()) {
|
|
||||||
binding.itemImage.setBackgroundAndText(source.title.getHtmlDecoded())
|
|
||||||
} else {
|
|
||||||
c.circularDrawable(source.getIcon(repository.baseUrl), binding.itemImage, appSettingsService)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!source.error.isNullOrBlank()) {
|
|
||||||
binding.errorText.visibility = View.VISIBLE
|
|
||||||
binding.errorText.text = source.error
|
|
||||||
} else {
|
|
||||||
binding.errorText.visibility = View.GONE
|
|
||||||
}
|
|
||||||
|
|
||||||
binding.sourceTitle.text = source.title.getHtmlDecoded()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getItemId(position: Int) = position.toLong()
|
override fun getItemId(position: Int) = position.toLong()
|
||||||
@ -83,6 +49,35 @@ class SourcesListAdapter(
|
|||||||
override fun getItemCount(): Int = items.size
|
override fun getItemCount(): Int = items.size
|
||||||
|
|
||||||
|
|
||||||
|
inner class ViewHolder(val binding: SourceListItemBinding) : RecyclerView.ViewHolder(binding.root) {
|
||||||
|
|
||||||
|
private val context: Context = app.applicationContext
|
||||||
|
private val repository: Repository by instance()
|
||||||
|
private val appSettingsService: AppSettingsService by instance()
|
||||||
|
|
||||||
|
fun bind(source: SelfossModel.SourceDetail, position: Int) {
|
||||||
|
binding.apply {
|
||||||
|
sourceTitle.text = source.title.getHtmlDecoded()
|
||||||
|
if (source.getIcon(repository.baseUrl).isEmpty()) {
|
||||||
|
itemImage.setBackgroundAndText(source.title.getHtmlDecoded())
|
||||||
|
} else {
|
||||||
|
context.circularDrawable(source.getIcon(repository.baseUrl), itemImage, appSettingsService)
|
||||||
|
}
|
||||||
|
|
||||||
|
errorText.apply {
|
||||||
|
visibility = if (!source.error.isNullOrBlank()) View.VISIBLE else View.GONE
|
||||||
|
text = source.error
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteBtn.setOnClickListener { showDeleteConfirmationDialog(source, position) }
|
||||||
|
|
||||||
|
root.setOnClickListener {
|
||||||
|
repository.setSelectedSource(source)
|
||||||
|
app.startActivity(Intent(app, UpsertSourceActivity::class.java))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun showDeleteConfirmationDialog(source: SelfossModel.SourceDetail, position: Int) {
|
private fun showDeleteConfirmationDialog(source: SelfossModel.SourceDetail, position: Int) {
|
||||||
AlertDialog.Builder(app)
|
AlertDialog.Builder(app)
|
||||||
.setTitle(app.getString(R.string.confirm_delete_title))
|
.setTitle(app.getString(R.string.confirm_delete_title))
|
||||||
@ -110,8 +105,5 @@ class SourcesListAdapter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
inner class ViewHolder(
|
|
||||||
val mView: ConstraintLayout,
|
|
||||||
) : RecyclerView.ViewHolder(mView)
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user