e-ntfy-android/app/src/main/java/io/heckel/ntfy/ui/MainAdapter.kt

104 lines
4.6 KiB
Kotlin
Raw Normal View History

2021-10-28 05:04:14 +02:00
package io.heckel.ntfy.ui
2021-10-27 04:41:19 +02:00
import android.content.Context
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-28 05:04:14 +02:00
import io.heckel.ntfy.R
2021-11-15 03:42:41 +01:00
import io.heckel.ntfy.data.ConnectionState
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
import java.text.SimpleDateFormat
import java.util.*
2021-11-03 17:48:13 +01:00
class MainAdapter(private val onClick: (Subscription) -> Unit, private val onLongClick: (Subscription) -> Unit) :
ListAdapter<Subscription, MainAdapter.SubscriptionViewHolder>(TopicDiffCallback) {
2021-11-03 18:56:08 +01:00
val selected = mutableSetOf<Long>() // Subscription IDs
2021-10-30 03:13:58 +02:00
/* Creates and inflates view and return TopicViewHolder. */
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubscriptionViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.main_fragment_item, parent, false)
2021-11-03 17:48:13 +01:00
return SubscriptionViewHolder(view, selected, onClick, onLongClick)
2021-10-30 03:13:58 +02:00
}
/* Gets current topic and uses it to bind view. */
override fun onBindViewHolder(holder: SubscriptionViewHolder, position: Int) {
val subscription = getItem(position)
holder.bind(subscription)
}
2021-11-03 17:48:13 +01:00
fun toggleSelection(subscriptionId: Long) {
if (selected.contains(subscriptionId)) {
selected.remove(subscriptionId)
} else {
selected.add(subscriptionId)
}
}
2021-10-25 19:45:56 +02:00
/* ViewHolder for Topic, takes in the inflated view and the onClick behavior. */
2021-11-03 18:56:08 +01:00
class SubscriptionViewHolder(itemView: View, private val selected: Set<Long>, val onClick: (Subscription) -> Unit, val onLongClick: (Subscription) -> Unit) :
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
2021-10-31 20:19:25 +01:00
private val nameView: TextView = itemView.findViewById(R.id.main_item_text)
private val statusView: TextView = itemView.findViewById(R.id.main_item_status)
private val dateView: TextView = itemView.findViewById(R.id.main_item_date)
private val instantImageView: View = itemView.findViewById(R.id.main_item_instant_image)
private val newItemsView: TextView = itemView.findViewById(R.id.main_item_new)
2021-10-27 22:15:59 +02:00
fun bind(subscription: Subscription) {
2021-10-28 17:45:34 +02:00
this.subscription = subscription
var statusMessage = if (subscription.totalCount == 1) {
context.getString(R.string.main_item_status_text_one, subscription.totalCount)
2021-10-27 04:41:19 +02:00
} else {
context.getString(R.string.main_item_status_text_not_one, subscription.totalCount)
2021-10-28 17:45:34 +02:00
}
2021-11-15 03:42:41 +01:00
if (subscription.instant && subscription.state == ConnectionState.RECONNECTING) {
statusMessage += ", " + context.getString(R.string.main_item_status_reconnecting)
}
val dateText = if (subscription.lastActive == 0L) {
""
} else if (System.currentTimeMillis()/1000 - subscription.lastActive < 24 * 60 * 60) {
SimpleDateFormat("HH:mm").format(Date(subscription.lastActive*1000))
} else {
SimpleDateFormat("M/d/yy").format(Date(subscription.lastActive*1000))
}
2021-10-30 03:13:58 +02:00
nameView.text = topicShortUrl(subscription.baseUrl, subscription.topic)
statusView.text = statusMessage
dateView.text = dateText
if (subscription.instant) {
instantImageView.visibility = View.VISIBLE
} else {
instantImageView.visibility = View.GONE
}
if (subscription.newCount > 0) {
newItemsView.visibility = View.VISIBLE
newItemsView.text = if (subscription.newCount <= 99) subscription.newCount.toString() else "99+"
} else {
newItemsView.visibility = View.GONE
}
2021-10-31 20:19:25 +01:00
itemView.setOnClickListener { onClick(subscription) }
2021-11-03 17:48:13 +01:00
itemView.setOnLongClickListener { onLongClick(subscription); true }
if (selected.contains(subscription.id)) {
itemView.setBackgroundResource(R.color.primarySelectedRowColor);
}
}
}
2021-10-30 03:13:58 +02:00
object TopicDiffCallback : DiffUtil.ItemCallback<Subscription>() {
override fun areItemsTheSame(oldItem: Subscription, newItem: Subscription): Boolean {
return oldItem.id == newItem.id
}
2021-10-30 03:13:58 +02:00
override fun areContentsTheSame(oldItem: Subscription, newItem: Subscription): Boolean {
return oldItem == newItem
}
}
}