2021-10-28 05:04:14 +02:00
|
|
|
package io.heckel.ntfy.ui
|
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
|
2021-10-28 17:45:34 +02:00
|
|
|
import android.widget.PopupMenu
|
2021-10-25 15:01:10 +02:00
|
|
|
import android.widget.TextView
|
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
|
|
import androidx.recyclerview.widget.ListAdapter
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2021-10-28 05:04:14 +02:00
|
|
|
import io.heckel.ntfy.R
|
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-28 17:45:34 +02:00
|
|
|
import io.heckel.ntfy.data.topicShortUrl
|
2021-10-25 15:01:10 +02:00
|
|
|
|
2021-10-28 17:45:34 +02:00
|
|
|
class SubscriptionsAdapter(private val context: Context, private val onClick: (Subscription) -> Unit) :
|
|
|
|
ListAdapter<Subscription, SubscriptionsAdapter.SubscriptionViewHolder>(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-28 17:45:34 +02:00
|
|
|
class SubscriptionViewHolder(itemView: View, val onUnsubscribe: (Subscription) -> Unit) :
|
2021-10-25 15:01:10 +02:00
|
|
|
RecyclerView.ViewHolder(itemView) {
|
2021-10-28 17:45:34 +02:00
|
|
|
private var subscription: 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 {
|
2021-10-28 17:45:34 +02:00
|
|
|
val popup = PopupMenu(context, itemView)
|
|
|
|
popup.inflate(R.menu.main_item_popup_menu)
|
|
|
|
popup.setOnMenuItemClickListener { item ->
|
|
|
|
when (item.itemId) {
|
|
|
|
R.id.main_item_popup_unsubscribe -> {
|
|
|
|
subscription?.let { s -> onUnsubscribe(s) }
|
|
|
|
true
|
|
|
|
}
|
|
|
|
else -> false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
itemView.setOnLongClickListener {
|
|
|
|
subscription?.let { popup.show() }
|
|
|
|
true
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 22:15:59 +02:00
|
|
|
fun bind(subscription: Subscription) {
|
2021-10-28 17:45:34 +02:00
|
|
|
this.subscription = subscription
|
|
|
|
val notificationsCountMessage = if (subscription.messages == 1) {
|
|
|
|
context.getString(R.string.main_item_status_text_one, subscription.messages)
|
2021-10-27 04:41:19 +02:00
|
|
|
} else {
|
2021-10-28 17:45:34 +02:00
|
|
|
context.getString(R.string.main_item_status_text_not_one, subscription.messages)
|
|
|
|
}
|
|
|
|
val statusText = when (subscription.status) {
|
|
|
|
Status.CONNECTING -> notificationsCountMessage + ", " + context.getString(R.string.main_item_status_connecting)
|
|
|
|
Status.RECONNECTING -> notificationsCountMessage + ", " + context.getString(R.string.main_item_status_reconnecting)
|
|
|
|
else -> notificationsCountMessage
|
2021-10-27 04:41:19 +02:00
|
|
|
}
|
2021-10-28 17:45:34 +02:00
|
|
|
nameView.text = topicShortUrl(subscription)
|
|
|
|
statusView.text = statusText
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 19:45:56 +02:00
|
|
|
/* Creates and inflates view and return TopicViewHolder. */
|
2021-10-28 17:45:34 +02:00
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubscriptionViewHolder {
|
2021-10-25 15:01:10 +02:00
|
|
|
val view = LayoutInflater.from(parent.context)
|
2021-10-28 14:28:22 +02:00
|
|
|
.inflate(R.layout.main_fragment_item, parent, false)
|
2021-10-28 17:45:34 +02:00
|
|
|
return SubscriptionViewHolder(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. */
|
2021-10-28 17:45:34 +02:00
|
|
|
override fun onBindViewHolder(holder: SubscriptionViewHolder, position: Int) {
|
|
|
|
val subscription = getItem(position)
|
|
|
|
holder.bind(subscription)
|
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 {
|
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 {
|
2021-10-27 04:41:19 +02:00
|
|
|
return oldItem == newItem
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
|
|
|
}
|