2021-11-01 08:19:25 +13:00
|
|
|
package io.heckel.ntfy.ui
|
|
|
|
|
2022-01-10 16:08:29 +13:00
|
|
|
import android.app.DownloadManager
|
|
|
|
import android.content.ClipData
|
|
|
|
import android.content.ClipboardManager
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
2022-01-09 16:17:41 +13:00
|
|
|
import android.graphics.BitmapFactory
|
|
|
|
import android.net.Uri
|
2021-11-29 13:28:58 +13:00
|
|
|
import android.util.Log
|
2021-11-01 08:19:25 +13:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2022-01-10 16:08:29 +13:00
|
|
|
import android.widget.*
|
2021-11-28 10:18:09 +13:00
|
|
|
import androidx.core.content.ContextCompat
|
2021-11-01 08:19:25 +13:00
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
|
|
import androidx.recyclerview.widget.ListAdapter
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2022-01-10 16:08:29 +13:00
|
|
|
import androidx.work.OneTimeWorkRequest
|
|
|
|
import androidx.work.WorkManager
|
|
|
|
import androidx.work.workDataOf
|
2021-11-01 08:19:25 +13:00
|
|
|
import io.heckel.ntfy.R
|
|
|
|
import io.heckel.ntfy.data.Notification
|
2022-01-10 16:08:29 +13:00
|
|
|
import io.heckel.ntfy.msg.AttachmentDownloadWorker
|
|
|
|
import io.heckel.ntfy.msg.NotificationDispatcher
|
2021-11-29 13:28:58 +13:00
|
|
|
import io.heckel.ntfy.util.*
|
2021-11-01 08:19:25 +13:00
|
|
|
import java.util.*
|
|
|
|
|
2022-01-10 16:08:29 +13:00
|
|
|
|
2021-11-04 06:56:08 +13:00
|
|
|
class DetailAdapter(private val onClick: (Notification) -> Unit, private val onLongClick: (Notification) -> Unit) :
|
2021-11-01 08:19:25 +13:00
|
|
|
ListAdapter<Notification, DetailAdapter.DetailViewHolder>(TopicDiffCallback) {
|
2021-11-04 06:56:08 +13:00
|
|
|
val selected = mutableSetOf<String>() // Notification IDs
|
2021-11-01 08:19:25 +13:00
|
|
|
|
|
|
|
/* Creates and inflates view and return TopicViewHolder. */
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DetailViewHolder {
|
|
|
|
val view = LayoutInflater.from(parent.context)
|
2021-11-23 09:45:43 +13:00
|
|
|
.inflate(R.layout.fragment_detail_item, parent, false)
|
2021-11-04 06:56:08 +13:00
|
|
|
return DetailViewHolder(view, selected, onClick, onLongClick)
|
2021-11-01 08:19:25 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Gets current topic and uses it to bind view. */
|
|
|
|
override fun onBindViewHolder(holder: DetailViewHolder, position: Int) {
|
|
|
|
holder.bind(getItem(position))
|
|
|
|
}
|
|
|
|
|
2021-11-04 06:56:08 +13:00
|
|
|
fun toggleSelection(notificationId: String) {
|
|
|
|
if (selected.contains(notificationId)) {
|
|
|
|
selected.remove(notificationId)
|
|
|
|
} else {
|
|
|
|
selected.add(notificationId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 08:19:25 +13:00
|
|
|
/* ViewHolder for Topic, takes in the inflated view and the onClick behavior. */
|
2021-11-04 06:56:08 +13:00
|
|
|
class DetailViewHolder(itemView: View, private val selected: Set<String>, val onClick: (Notification) -> Unit, val onLongClick: (Notification) -> Unit) :
|
2021-11-01 08:19:25 +13:00
|
|
|
RecyclerView.ViewHolder(itemView) {
|
|
|
|
private var notification: Notification? = null
|
2021-11-28 10:18:09 +13:00
|
|
|
private val priorityImageView: ImageView = itemView.findViewById(R.id.detail_item_priority_image)
|
2021-11-01 08:19:25 +13:00
|
|
|
private val dateView: TextView = itemView.findViewById(R.id.detail_item_date_text)
|
2021-11-28 10:18:09 +13:00
|
|
|
private val titleView: TextView = itemView.findViewById(R.id.detail_item_title_text)
|
2021-11-01 08:19:25 +13:00
|
|
|
private val messageView: TextView = itemView.findViewById(R.id.detail_item_message_text)
|
2021-11-28 10:18:09 +13:00
|
|
|
private val newImageView: View = itemView.findViewById(R.id.detail_item_new_dot)
|
2022-01-10 16:08:29 +13:00
|
|
|
private val tagsView: TextView = itemView.findViewById(R.id.detail_item_tags_text)
|
2022-01-09 16:17:41 +13:00
|
|
|
private val imageView: ImageView = itemView.findViewById(R.id.detail_item_image)
|
2022-01-10 16:08:29 +13:00
|
|
|
private val attachmentView: TextView = itemView.findViewById(R.id.detail_item_attachment_text)
|
|
|
|
private val menuButton: ImageButton = itemView.findViewById(R.id.detail_item_menu_button)
|
2021-11-01 08:19:25 +13:00
|
|
|
|
|
|
|
fun bind(notification: Notification) {
|
|
|
|
this.notification = notification
|
2021-11-28 10:18:09 +13:00
|
|
|
|
2022-01-10 16:08:29 +13:00
|
|
|
val context = itemView.context
|
2021-11-29 13:28:58 +13:00
|
|
|
val unmatchedTags = unmatchedTags(splitTags(notification.tags))
|
|
|
|
|
2021-11-01 08:19:25 +13:00
|
|
|
dateView.text = Date(notification.timestamp * 1000).toString()
|
2021-11-28 10:18:09 +13:00
|
|
|
messageView.text = formatMessage(notification)
|
2021-11-23 09:45:43 +13:00
|
|
|
newImageView.visibility = if (notification.notificationId == 0) View.GONE else View.VISIBLE
|
2021-11-01 08:19:25 +13:00
|
|
|
itemView.setOnClickListener { onClick(notification) }
|
2021-11-04 06:56:08 +13:00
|
|
|
itemView.setOnLongClickListener { onLongClick(notification); true }
|
2021-11-28 10:18:09 +13:00
|
|
|
if (notification.title != "") {
|
|
|
|
titleView.visibility = View.VISIBLE
|
|
|
|
titleView.text = formatTitle(notification)
|
|
|
|
} else {
|
|
|
|
titleView.visibility = View.GONE
|
|
|
|
}
|
2021-11-29 13:28:58 +13:00
|
|
|
if (unmatchedTags.isNotEmpty()) {
|
|
|
|
tagsView.visibility = View.VISIBLE
|
2022-01-10 16:08:29 +13:00
|
|
|
tagsView.text = context.getString(R.string.detail_item_tags, unmatchedTags.joinToString(", "))
|
2021-11-29 13:28:58 +13:00
|
|
|
} else {
|
|
|
|
tagsView.visibility = View.GONE
|
|
|
|
}
|
2021-11-04 06:56:08 +13:00
|
|
|
if (selected.contains(notification.id)) {
|
|
|
|
itemView.setBackgroundResource(R.color.primarySelectedRowColor);
|
|
|
|
}
|
2021-11-28 10:18:09 +13:00
|
|
|
when (notification.priority) {
|
|
|
|
1 -> {
|
|
|
|
priorityImageView.visibility = View.VISIBLE
|
2022-01-10 16:08:29 +13:00
|
|
|
priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_1_24dp))
|
2021-11-28 10:18:09 +13:00
|
|
|
}
|
|
|
|
2 -> {
|
|
|
|
priorityImageView.visibility = View.VISIBLE
|
2022-01-10 16:08:29 +13:00
|
|
|
priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_2_24dp))
|
2021-11-28 10:18:09 +13:00
|
|
|
}
|
|
|
|
3 -> {
|
|
|
|
priorityImageView.visibility = View.GONE
|
|
|
|
}
|
|
|
|
4 -> {
|
|
|
|
priorityImageView.visibility = View.VISIBLE
|
2022-01-10 16:08:29 +13:00
|
|
|
priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_4_24dp))
|
2021-11-28 10:18:09 +13:00
|
|
|
}
|
|
|
|
5 -> {
|
|
|
|
priorityImageView.visibility = View.VISIBLE
|
2022-01-10 16:08:29 +13:00
|
|
|
priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_5_24dp))
|
2021-11-28 10:18:09 +13:00
|
|
|
}
|
|
|
|
}
|
2022-01-09 16:17:41 +13:00
|
|
|
val contentUri = notification.attachment?.contentUri
|
2022-01-10 16:08:29 +13:00
|
|
|
val fileExists = if (contentUri != null) fileExists(context, contentUri) else false
|
|
|
|
if (contentUri != null && fileExists && supportedImage(notification.attachment.type)) {
|
2022-01-09 16:17:41 +13:00
|
|
|
try {
|
2022-01-10 16:08:29 +13:00
|
|
|
val resolver = context.applicationContext.contentResolver
|
2022-01-09 16:17:41 +13:00
|
|
|
val bitmapStream = resolver.openInputStream(Uri.parse(contentUri))
|
|
|
|
val bitmap = BitmapFactory.decodeStream(bitmapStream)
|
|
|
|
imageView.setImageBitmap(bitmap)
|
|
|
|
imageView.visibility = View.VISIBLE
|
|
|
|
} catch (_: Exception) {
|
|
|
|
imageView.visibility = View.GONE
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
imageView.visibility = View.GONE
|
|
|
|
}
|
2022-01-10 16:08:29 +13:00
|
|
|
if (notification.attachment != null) {
|
|
|
|
attachmentView.text = formatAttachmentInfo(notification, fileExists)
|
|
|
|
attachmentView.visibility = View.VISIBLE
|
|
|
|
menuButton.visibility = View.VISIBLE
|
|
|
|
menuButton.setOnClickListener { menuView ->
|
|
|
|
val popup = PopupMenu(context, menuView)
|
|
|
|
popup.menuInflater.inflate(R.menu.menu_detail_attachment, popup.menu)
|
|
|
|
|
|
|
|
val downloadItem = popup.menu.findItem(R.id.detail_item_menu_download)
|
|
|
|
val openItem = popup.menu.findItem(R.id.detail_item_menu_open)
|
|
|
|
val browseItem = popup.menu.findItem(R.id.detail_item_menu_browse)
|
|
|
|
val copyUrlItem = popup.menu.findItem(R.id.detail_item_menu_copy_url)
|
|
|
|
if (contentUri != null && fileExists) {
|
|
|
|
openItem.setOnMenuItemClickListener {
|
|
|
|
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(contentUri))) // FIXME try/catch
|
|
|
|
true
|
|
|
|
}
|
|
|
|
browseItem.setOnMenuItemClickListener {
|
|
|
|
context.startActivity(Intent(DownloadManager.ACTION_VIEW_DOWNLOADS))
|
|
|
|
true
|
|
|
|
}
|
|
|
|
copyUrlItem.setOnMenuItemClickListener {
|
|
|
|
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
|
|
|
val clip = ClipData.newPlainText("attachment url", notification.attachment.url)
|
|
|
|
clipboard.setPrimaryClip(clip)
|
|
|
|
Toast
|
|
|
|
.makeText(context, context.getString(R.string.detail_copied_to_clipboard_message), Toast.LENGTH_LONG)
|
|
|
|
.show()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
downloadItem.isVisible = false
|
|
|
|
} else {
|
|
|
|
openItem.isVisible = false
|
|
|
|
browseItem.isVisible = false
|
|
|
|
downloadItem.setOnMenuItemClickListener {
|
|
|
|
scheduleAttachmentDownload(context, notification)
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
popup.show()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
attachmentView.visibility = View.GONE
|
|
|
|
menuButton.visibility = View.GONE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun scheduleAttachmentDownload(context: Context, notification: Notification) {
|
|
|
|
Log.d(TAG, "Enqueuing work to download attachment")
|
|
|
|
val workManager = WorkManager.getInstance(context)
|
|
|
|
val workRequest = OneTimeWorkRequest.Builder(AttachmentDownloadWorker::class.java)
|
|
|
|
.setInputData(workDataOf("id" to notification.id))
|
|
|
|
.build()
|
|
|
|
workManager.enqueue(workRequest)
|
2021-11-01 08:19:25 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 16:08:29 +13:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val TAG = "NtfyDetailAdapter"
|
|
|
|
}
|
2021-11-01 08:19:25 +13:00
|
|
|
}
|