mirror of
https://github.com/binwiederhier/ntfy-android.git
synced 2024-06-02 18:54:34 +12:00
58 lines
2.2 KiB
Kotlin
58 lines
2.2 KiB
Kotlin
|
package io.heckel.ntfy.ui
|
||
|
|
||
|
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
|
||
|
import io.heckel.ntfy.R
|
||
|
import io.heckel.ntfy.data.Notification
|
||
|
import io.heckel.ntfy.data.Subscription
|
||
|
import io.heckel.ntfy.data.topicShortUrl
|
||
|
import java.time.Instant
|
||
|
import java.util.*
|
||
|
|
||
|
class DetailAdapter(private val onClick: (Notification) -> Unit) :
|
||
|
ListAdapter<Notification, DetailAdapter.DetailViewHolder>(TopicDiffCallback) {
|
||
|
|
||
|
/* Creates and inflates view and return TopicViewHolder. */
|
||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DetailViewHolder {
|
||
|
val view = LayoutInflater.from(parent.context)
|
||
|
.inflate(R.layout.detail_fragment_item, parent, false)
|
||
|
return DetailViewHolder(view, onClick)
|
||
|
}
|
||
|
|
||
|
/* Gets current topic and uses it to bind view. */
|
||
|
override fun onBindViewHolder(holder: DetailViewHolder, position: Int) {
|
||
|
holder.bind(getItem(position))
|
||
|
}
|
||
|
|
||
|
/* ViewHolder for Topic, takes in the inflated view and the onClick behavior. */
|
||
|
class DetailViewHolder(itemView: View, val onClick: (Notification) -> Unit) :
|
||
|
RecyclerView.ViewHolder(itemView) {
|
||
|
private var notification: Notification? = null
|
||
|
private val dateView: TextView = itemView.findViewById(R.id.detail_item_date_text)
|
||
|
private val messageView: TextView = itemView.findViewById(R.id.detail_item_message_text)
|
||
|
|
||
|
fun bind(notification: Notification) {
|
||
|
this.notification = notification
|
||
|
dateView.text = Date(notification.timestamp * 1000).toString()
|
||
|
messageView.text = notification.message
|
||
|
itemView.setOnClickListener { onClick(notification) }
|
||
|
}
|
||
|
}
|
||
|
|
||
|
object TopicDiffCallback : DiffUtil.ItemCallback<Notification>() {
|
||
|
override fun areItemsTheSame(oldItem: Notification, newItem: Notification): Boolean {
|
||
|
return oldItem.id == newItem.id
|
||
|
}
|
||
|
|
||
|
override fun areContentsTheSame(oldItem: Notification, newItem: Notification): Boolean {
|
||
|
return oldItem == newItem
|
||
|
}
|
||
|
}
|
||
|
}
|