2021-10-27 03:44:12 +02:00
|
|
|
package io.heckel.ntfy
|
2021-10-25 15:01:10 +02:00
|
|
|
|
2021-10-27 04:41:19 +02:00
|
|
|
import android.content.Context
|
2021-10-25 15:01:10 +02:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.TextView
|
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
|
|
import androidx.recyclerview.widget.ListAdapter
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2021-10-27 04:41:19 +02:00
|
|
|
import io.heckel.ntfy.data.Status
|
2021-10-27 22:15:59 +02:00
|
|
|
import io.heckel.ntfy.data.Subscription
|
2021-10-27 04:41:19 +02:00
|
|
|
import io.heckel.ntfy.data.topicUrl
|
2021-10-25 15:01:10 +02:00
|
|
|
|
2021-10-27 22:15:59 +02:00
|
|
|
class TopicsAdapter(private val onClick: (Subscription) -> Unit) :
|
|
|
|
ListAdapter<Subscription, TopicsAdapter.TopicViewHolder>(TopicDiffCallback) {
|
2021-10-25 15:01:10 +02:00
|
|
|
|
2021-10-25 19:45:56 +02:00
|
|
|
/* ViewHolder for Topic, takes in the inflated view and the onClick behavior. */
|
2021-10-27 22:15:59 +02:00
|
|
|
class TopicViewHolder(itemView: View, val onClick: (Subscription) -> Unit) :
|
2021-10-25 15:01:10 +02:00
|
|
|
RecyclerView.ViewHolder(itemView) {
|
2021-10-27 22:15:59 +02:00
|
|
|
private var topic: Subscription? = null
|
2021-10-27 04:41:19 +02:00
|
|
|
private val context: Context = itemView.context
|
|
|
|
private val nameView: TextView = itemView.findViewById(R.id.topic_text)
|
|
|
|
private val statusView: TextView = itemView.findViewById(R.id.topic_status)
|
2021-10-25 15:01:10 +02:00
|
|
|
|
|
|
|
init {
|
|
|
|
itemView.setOnClickListener {
|
2021-10-27 04:41:19 +02:00
|
|
|
topic?.let {
|
2021-10-25 15:01:10 +02:00
|
|
|
onClick(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 22:15:59 +02:00
|
|
|
fun bind(subscription: Subscription) {
|
|
|
|
println("bind sub: $subscription")
|
|
|
|
this.topic = subscription
|
|
|
|
val statusText = when (subscription.status) {
|
2021-10-27 04:41:19 +02:00
|
|
|
Status.CONNECTING -> context.getString(R.string.status_connecting)
|
2021-10-27 22:15:59 +02:00
|
|
|
else -> context.getString(R.string.status_connected)
|
2021-10-27 04:41:19 +02:00
|
|
|
}
|
2021-10-27 22:15:59 +02:00
|
|
|
val statusMessage = if (subscription.messages == 1) {
|
|
|
|
context.getString(R.string.status_text_one, statusText, subscription.messages)
|
2021-10-27 04:41:19 +02:00
|
|
|
} else {
|
2021-10-27 22:15:59 +02:00
|
|
|
context.getString(R.string.status_text_not_one, statusText, subscription.messages)
|
2021-10-27 04:41:19 +02:00
|
|
|
}
|
2021-10-27 22:15:59 +02:00
|
|
|
nameView.text = topicUrl(subscription)
|
2021-10-27 04:41:19 +02:00
|
|
|
statusView.text = statusMessage
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 19:45:56 +02:00
|
|
|
/* Creates and inflates view and return TopicViewHolder. */
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TopicViewHolder {
|
2021-10-25 15:01:10 +02:00
|
|
|
val view = LayoutInflater.from(parent.context)
|
2021-10-25 19:45:56 +02:00
|
|
|
.inflate(R.layout.topic_item, parent, false)
|
|
|
|
return TopicViewHolder(view, onClick)
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
|
|
|
|
2021-10-25 19:45:56 +02:00
|
|
|
/* Gets current topic and uses it to bind view. */
|
|
|
|
override fun onBindViewHolder(holder: TopicViewHolder, position: Int) {
|
|
|
|
val topic = getItem(position)
|
|
|
|
holder.bind(topic)
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 22:15:59 +02:00
|
|
|
object TopicDiffCallback : DiffUtil.ItemCallback<Subscription>() {
|
|
|
|
override fun areItemsTheSame(oldItem: Subscription, newItem: Subscription): Boolean {
|
|
|
|
println("areItemsTheSame: $oldItem.id ==? $newItem.id")
|
2021-10-27 04:41:19 +02:00
|
|
|
return oldItem.id == newItem.id
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 22:15:59 +02:00
|
|
|
override fun areContentsTheSame(oldItem: Subscription, newItem: Subscription): Boolean {
|
|
|
|
println("areContentsTheSame: $oldItem ==? $newItem")
|
2021-10-27 04:41:19 +02:00
|
|
|
return oldItem == newItem
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
|
|
|
}
|