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
|
|
|
|
import android.widget.TextView
|
2022-04-28 02:58:23 +02:00
|
|
|
import androidx.cardview.widget.CardView
|
2021-10-25 15:01:10 +02:00
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
|
|
import androidx.recyclerview.widget.ListAdapter
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2022-01-13 05:31:37 +01:00
|
|
|
import io.heckel.ntfy.BuildConfig
|
2021-10-28 05:04:14 +02:00
|
|
|
import io.heckel.ntfy.R
|
2022-01-18 20:28:48 +01:00
|
|
|
import io.heckel.ntfy.db.ConnectionState
|
|
|
|
import io.heckel.ntfy.db.Repository
|
|
|
|
import io.heckel.ntfy.db.Subscription
|
2021-11-27 22:18:09 +01:00
|
|
|
import io.heckel.ntfy.util.topicShortUrl
|
2021-11-17 21:30:57 +01:00
|
|
|
import java.text.DateFormat
|
2021-11-12 01:41:29 +01:00
|
|
|
import java.util.*
|
2021-10-25 15:01:10 +02:00
|
|
|
|
2022-01-01 16:56:18 +01:00
|
|
|
class MainAdapter(private val repository: Repository, private val onClick: (Subscription) -> Unit, private val onLongClick: (Subscription) -> Unit) :
|
2021-11-03 17:48:13 +01:00
|
|
|
ListAdapter<Subscription, MainAdapter.SubscriptionViewHolder>(TopicDiffCallback) {
|
2021-11-03 18:56:08 +01:00
|
|
|
val selected = mutableSetOf<Long>() // Subscription IDs
|
2021-10-25 15:01:10 +02:00
|
|
|
|
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)
|
2021-11-22 21:45:43 +01:00
|
|
|
.inflate(R.layout.fragment_main_item, parent, false)
|
2022-01-01 16:56:18 +01:00
|
|
|
return SubscriptionViewHolder(view, repository, 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. */
|
2022-01-01 16:56:18 +01:00
|
|
|
class SubscriptionViewHolder(itemView: View, private val repository: Repository, private val selected: Set<Long>, val onClick: (Subscription) -> Unit, val onLongClick: (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
|
2022-04-28 02:58:23 +02:00
|
|
|
private val cardView: CardView = itemView.findViewById(R.id.main_item_card)
|
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)
|
2021-11-12 01:41:29 +01:00
|
|
|
private val dateView: TextView = itemView.findViewById(R.id.main_item_date)
|
2021-11-22 21:45:43 +01:00
|
|
|
private val notificationDisabledUntilImageView: View = itemView.findViewById(R.id.main_item_notification_disabled_until_image)
|
|
|
|
private val notificationDisabledForeverImageView: View = itemView.findViewById(R.id.main_item_notification_disabled_forever_image)
|
2021-11-14 19:54:48 +01:00
|
|
|
private val instantImageView: View = itemView.findViewById(R.id.main_item_instant_image)
|
2021-11-15 22:24:31 +01:00
|
|
|
private val newItemsView: TextView = itemView.findViewById(R.id.main_item_new)
|
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
|
2022-01-01 17:09:00 +01:00
|
|
|
val isUnifiedPush = subscription.upAppId != null
|
|
|
|
var statusMessage = if (isUnifiedPush) {
|
2021-12-30 14:23:47 +01:00
|
|
|
context.getString(R.string.main_item_status_unified_push, subscription.upAppId)
|
|
|
|
} else if (subscription.totalCount == 1) {
|
2021-11-15 22:24:31 +01:00
|
|
|
context.getString(R.string.main_item_status_text_one, subscription.totalCount)
|
2021-10-27 04:41:19 +02:00
|
|
|
} else {
|
2021-11-15 22:24:31 +01:00
|
|
|
context.getString(R.string.main_item_status_text_not_one, subscription.totalCount)
|
2021-10-28 17:45:34 +02:00
|
|
|
}
|
2021-11-16 20:08:52 +01:00
|
|
|
if (subscription.instant && subscription.state == ConnectionState.CONNECTING) {
|
2021-11-15 03:42:41 +01:00
|
|
|
statusMessage += ", " + context.getString(R.string.main_item_status_reconnecting)
|
|
|
|
}
|
2021-11-17 21:30:57 +01:00
|
|
|
val date = Date(subscription.lastActive * 1000)
|
|
|
|
val dateStr = DateFormat.getDateInstance(DateFormat.SHORT).format(date)
|
|
|
|
val moreThanOneDay = System.currentTimeMillis()/1000 - subscription.lastActive > 24 * 60 * 60
|
|
|
|
val sameDay = dateStr == DateFormat.getDateInstance(DateFormat.SHORT).format(Date()) // Omg this is horrible
|
2021-11-12 04:14:28 +01:00
|
|
|
val dateText = if (subscription.lastActive == 0L) {
|
|
|
|
""
|
2021-11-17 21:30:57 +01:00
|
|
|
} else if (sameDay) {
|
|
|
|
DateFormat.getTimeInstance(DateFormat.SHORT).format(date)
|
|
|
|
} else if (!moreThanOneDay) {
|
|
|
|
context.getString(R.string.main_item_date_yesterday)
|
2021-11-12 01:41:29 +01:00
|
|
|
} else {
|
2021-11-17 21:30:57 +01:00
|
|
|
dateStr
|
2021-11-12 01:41:29 +01:00
|
|
|
}
|
2022-01-01 16:56:18 +01:00
|
|
|
val globalMutedUntil = repository.getGlobalMutedUntil()
|
2022-01-01 17:09:00 +01:00
|
|
|
val showMutedForeverIcon = (subscription.mutedUntil == 1L || globalMutedUntil == 1L) && !isUnifiedPush
|
|
|
|
val showMutedUntilIcon = !showMutedForeverIcon && (subscription.mutedUntil > 1L || globalMutedUntil > 1L) && !isUnifiedPush
|
2021-10-30 03:13:58 +02:00
|
|
|
nameView.text = topicShortUrl(subscription.baseUrl, subscription.topic)
|
|
|
|
statusView.text = statusMessage
|
2021-11-12 01:41:29 +01:00
|
|
|
dateView.text = dateText
|
2022-01-01 17:09:00 +01:00
|
|
|
dateView.visibility = if (isUnifiedPush) View.GONE else View.VISIBLE
|
2022-01-01 16:56:18 +01:00
|
|
|
notificationDisabledUntilImageView.visibility = if (showMutedUntilIcon) View.VISIBLE else View.GONE
|
|
|
|
notificationDisabledForeverImageView.visibility = if (showMutedForeverIcon) View.VISIBLE else View.GONE
|
2022-01-13 05:31:37 +01:00
|
|
|
instantImageView.visibility = if (subscription.instant && BuildConfig.FIREBASE_AVAILABLE) View.VISIBLE else View.GONE
|
2022-01-01 17:09:00 +01:00
|
|
|
if (isUnifiedPush || subscription.newCount == 0) {
|
2021-12-30 14:23:47 +01:00
|
|
|
newItemsView.visibility = View.GONE
|
|
|
|
} else {
|
2021-11-15 22:24:31 +01:00
|
|
|
newItemsView.visibility = View.VISIBLE
|
|
|
|
newItemsView.text = if (subscription.newCount <= 99) subscription.newCount.toString() else "99+"
|
|
|
|
}
|
2022-04-28 02:58:23 +02:00
|
|
|
cardView.setOnClickListener { onClick(subscription) }
|
|
|
|
cardView.setOnLongClickListener { onLongClick(subscription); true }
|
2021-11-03 17:48:13 +01:00
|
|
|
if (selected.contains(subscription.id)) {
|
2022-04-28 02:58:23 +02:00
|
|
|
cardView.setCardBackgroundColor(Colors.itemSelectedBackgroundColor(context))
|
2021-11-03 17:48:13 +01:00
|
|
|
}
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-25 15:01:10 +02:00
|
|
|
|
2021-10-30 03:13:58 +02:00
|
|
|
override fun areContentsTheSame(oldItem: Subscription, newItem: Subscription): Boolean {
|
|
|
|
return oldItem == newItem
|
|
|
|
}
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
|
|
|
}
|