2021-10-31 20:19:25 +01:00
|
|
|
package io.heckel.ntfy.ui
|
|
|
|
|
2022-01-11 23:00:18 +01:00
|
|
|
import android.Manifest
|
|
|
|
import android.app.Activity
|
2022-01-10 19:46:31 +01:00
|
|
|
import android.content.*
|
2022-01-11 23:00:18 +01:00
|
|
|
import android.content.pm.PackageManager
|
2022-01-10 21:36:50 +01:00
|
|
|
import android.graphics.Bitmap
|
2022-01-09 04:17:41 +01:00
|
|
|
import android.graphics.BitmapFactory
|
|
|
|
import android.net.Uri
|
2022-01-11 23:00:18 +01:00
|
|
|
import android.os.Build
|
2021-10-31 20:19:25 +01:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2022-01-10 04:08:29 +01:00
|
|
|
import android.widget.*
|
2022-01-11 23:00:18 +01:00
|
|
|
import androidx.core.app.ActivityCompat
|
2021-11-27 22:18:09 +01:00
|
|
|
import androidx.core.content.ContextCompat
|
2021-10-31 20:19:25 +01:00
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
|
|
import androidx.recyclerview.widget.ListAdapter
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2022-01-10 21:36:50 +01:00
|
|
|
import com.stfalcon.imageviewer.StfalconImageViewer
|
2021-10-31 20:19:25 +01:00
|
|
|
import io.heckel.ntfy.R
|
2022-01-18 20:28:48 +01:00
|
|
|
import io.heckel.ntfy.db.*
|
2022-01-12 00:21:30 +01:00
|
|
|
import io.heckel.ntfy.msg.DownloadManager
|
2021-11-29 01:28:58 +01:00
|
|
|
import io.heckel.ntfy.util.*
|
2022-01-12 02:47:28 +01:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.GlobalScope
|
|
|
|
import kotlinx.coroutines.launch
|
2021-10-31 20:19:25 +01:00
|
|
|
|
2022-01-12 02:47:28 +01:00
|
|
|
class DetailAdapter(private val activity: Activity, private val repository: Repository, private val onClick: (Notification) -> Unit, private val onLongClick: (Notification) -> Unit) :
|
2021-10-31 20:19:25 +01:00
|
|
|
ListAdapter<Notification, DetailAdapter.DetailViewHolder>(TopicDiffCallback) {
|
2021-11-03 18:56:08 +01:00
|
|
|
val selected = mutableSetOf<String>() // Notification IDs
|
2021-10-31 20:19:25 +01:00
|
|
|
|
|
|
|
/* Creates and inflates view and return TopicViewHolder. */
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DetailViewHolder {
|
|
|
|
val view = LayoutInflater.from(parent.context)
|
2021-11-22 21:45:43 +01:00
|
|
|
.inflate(R.layout.fragment_detail_item, parent, false)
|
2022-01-12 02:47:28 +01:00
|
|
|
return DetailViewHolder(activity, repository, view, selected, onClick, onLongClick)
|
2021-10-31 20:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Gets current topic and uses it to bind view. */
|
|
|
|
override fun onBindViewHolder(holder: DetailViewHolder, position: Int) {
|
|
|
|
holder.bind(getItem(position))
|
|
|
|
}
|
|
|
|
|
2022-02-06 03:02:05 +01:00
|
|
|
fun get(position: Int): Notification {
|
|
|
|
return getItem(position)
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:56:08 +01:00
|
|
|
fun toggleSelection(notificationId: String) {
|
|
|
|
if (selected.contains(notificationId)) {
|
|
|
|
selected.remove(notificationId)
|
|
|
|
} else {
|
|
|
|
selected.add(notificationId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 20:19:25 +01:00
|
|
|
/* ViewHolder for Topic, takes in the inflated view and the onClick behavior. */
|
2022-01-12 02:47:28 +01:00
|
|
|
class DetailViewHolder(private val activity: Activity, private val repository: Repository, itemView: View, private val selected: Set<String>, val onClick: (Notification) -> Unit, val onLongClick: (Notification) -> Unit) :
|
2021-10-31 20:19:25 +01:00
|
|
|
RecyclerView.ViewHolder(itemView) {
|
|
|
|
private var notification: Notification? = null
|
2021-11-27 22:18:09 +01:00
|
|
|
private val priorityImageView: ImageView = itemView.findViewById(R.id.detail_item_priority_image)
|
2021-10-31 20:19:25 +01:00
|
|
|
private val dateView: TextView = itemView.findViewById(R.id.detail_item_date_text)
|
2021-11-27 22:18:09 +01:00
|
|
|
private val titleView: TextView = itemView.findViewById(R.id.detail_item_title_text)
|
2021-10-31 20:19:25 +01:00
|
|
|
private val messageView: TextView = itemView.findViewById(R.id.detail_item_message_text)
|
2022-01-10 19:46:31 +01:00
|
|
|
private val newDotImageView: View = itemView.findViewById(R.id.detail_item_new_dot)
|
2022-01-10 04:08:29 +01:00
|
|
|
private val tagsView: TextView = itemView.findViewById(R.id.detail_item_tags_text)
|
|
|
|
private val menuButton: ImageButton = itemView.findViewById(R.id.detail_item_menu_button)
|
2022-01-10 19:46:31 +01:00
|
|
|
private val attachmentImageView: ImageView = itemView.findViewById(R.id.detail_item_attachment_image)
|
2022-02-13 15:26:26 +01:00
|
|
|
private val attachmentBoxView: View = itemView.findViewById(R.id.detail_item_attachment_file_box)
|
|
|
|
private val attachmentIconView: ImageView = itemView.findViewById(R.id.detail_item_attachment_file_icon)
|
|
|
|
private val attachmentInfoView: TextView = itemView.findViewById(R.id.detail_item_attachment_file_info)
|
2021-10-31 20:19:25 +01:00
|
|
|
|
|
|
|
fun bind(notification: Notification) {
|
|
|
|
this.notification = notification
|
2021-11-27 22:18:09 +01:00
|
|
|
|
2022-01-10 04:08:29 +01:00
|
|
|
val context = itemView.context
|
2021-11-29 01:28:58 +01:00
|
|
|
val unmatchedTags = unmatchedTags(splitTags(notification.tags))
|
|
|
|
|
2022-03-14 01:15:58 +01:00
|
|
|
dateView.text = formatDateShort(notification.timestamp)
|
2021-11-27 22:18:09 +01:00
|
|
|
messageView.text = formatMessage(notification)
|
2022-01-10 19:46:31 +01:00
|
|
|
newDotImageView.visibility = if (notification.notificationId == 0) View.GONE else View.VISIBLE
|
2021-10-31 20:19:25 +01:00
|
|
|
itemView.setOnClickListener { onClick(notification) }
|
2021-11-03 18:56:08 +01:00
|
|
|
itemView.setOnLongClickListener { onLongClick(notification); true }
|
2021-11-27 22:18:09 +01:00
|
|
|
if (notification.title != "") {
|
|
|
|
titleView.visibility = View.VISIBLE
|
|
|
|
titleView.text = formatTitle(notification)
|
|
|
|
} else {
|
|
|
|
titleView.visibility = View.GONE
|
|
|
|
}
|
2021-11-29 01:28:58 +01:00
|
|
|
if (unmatchedTags.isNotEmpty()) {
|
|
|
|
tagsView.visibility = View.VISIBLE
|
2022-01-10 04:08:29 +01:00
|
|
|
tagsView.text = context.getString(R.string.detail_item_tags, unmatchedTags.joinToString(", "))
|
2021-11-29 01:28:58 +01:00
|
|
|
} else {
|
|
|
|
tagsView.visibility = View.GONE
|
|
|
|
}
|
2021-11-03 18:56:08 +01:00
|
|
|
if (selected.contains(notification.id)) {
|
2022-02-08 00:35:36 +01:00
|
|
|
itemView.setBackgroundResource(Colors.itemSelectedBackground(context))
|
2021-11-03 18:56:08 +01:00
|
|
|
}
|
2022-03-21 00:53:51 +01:00
|
|
|
val attachment = notification.attachment
|
|
|
|
val exists = if (attachment?.contentUri != null) fileExists(context, attachment.contentUri) else false
|
2022-01-10 19:46:31 +01:00
|
|
|
renderPriority(context, notification)
|
2022-03-21 00:53:51 +01:00
|
|
|
maybeRenderMenu(context, notification, exists)
|
|
|
|
maybeRenderAttachment(context, notification, exists)
|
2022-01-10 19:46:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun renderPriority(context: Context, notification: Notification) {
|
2021-11-27 22:18:09 +01:00
|
|
|
when (notification.priority) {
|
|
|
|
1 -> {
|
|
|
|
priorityImageView.visibility = View.VISIBLE
|
2022-01-10 04:08:29 +01:00
|
|
|
priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_1_24dp))
|
2021-11-27 22:18:09 +01:00
|
|
|
}
|
|
|
|
2 -> {
|
|
|
|
priorityImageView.visibility = View.VISIBLE
|
2022-01-10 04:08:29 +01:00
|
|
|
priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_2_24dp))
|
2021-11-27 22:18:09 +01:00
|
|
|
}
|
|
|
|
3 -> {
|
|
|
|
priorityImageView.visibility = View.GONE
|
|
|
|
}
|
|
|
|
4 -> {
|
|
|
|
priorityImageView.visibility = View.VISIBLE
|
2022-01-10 04:08:29 +01:00
|
|
|
priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_4_24dp))
|
2021-11-27 22:18:09 +01:00
|
|
|
}
|
|
|
|
5 -> {
|
|
|
|
priorityImageView.visibility = View.VISIBLE
|
2022-01-10 04:08:29 +01:00
|
|
|
priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_5_24dp))
|
2021-11-27 22:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-10 19:46:31 +01:00
|
|
|
}
|
|
|
|
|
2022-03-21 00:53:51 +01:00
|
|
|
private fun maybeRenderAttachment(context: Context, notification: Notification, exists: Boolean) {
|
2022-01-10 19:46:31 +01:00
|
|
|
if (notification.attachment == null) {
|
|
|
|
attachmentImageView.visibility = View.GONE
|
|
|
|
attachmentBoxView.visibility = View.GONE
|
|
|
|
return
|
2022-01-09 04:17:41 +01:00
|
|
|
}
|
2022-01-10 19:46:31 +01:00
|
|
|
val attachment = notification.attachment
|
2022-01-10 21:36:50 +01:00
|
|
|
val image = attachment.contentUri != null && exists && supportedImage(attachment.type)
|
|
|
|
maybeRenderAttachmentImage(context, attachment, image)
|
|
|
|
maybeRenderAttachmentBox(context, notification, attachment, exists, image)
|
2022-01-10 19:46:31 +01:00
|
|
|
}
|
|
|
|
|
2022-03-21 00:53:51 +01:00
|
|
|
private fun maybeRenderMenu(context: Context, notification: Notification, exists: Boolean) {
|
|
|
|
val menuButtonPopupMenu = maybeCreateMenuPopup(context, menuButton, notification, exists) // Heavy lifting not during on-click
|
2022-01-10 19:46:31 +01:00
|
|
|
if (menuButtonPopupMenu != null) {
|
|
|
|
menuButton.setOnClickListener { menuButtonPopupMenu.show() }
|
2022-01-10 04:08:29 +01:00
|
|
|
menuButton.visibility = View.VISIBLE
|
2022-01-10 19:46:31 +01:00
|
|
|
} else {
|
|
|
|
menuButton.visibility = View.GONE
|
|
|
|
}
|
2022-01-10 21:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun maybeRenderAttachmentBox(context: Context, notification: Notification, attachment: Attachment, exists: Boolean, image: Boolean) {
|
|
|
|
if (image) {
|
|
|
|
attachmentBoxView.visibility = View.GONE
|
|
|
|
return
|
|
|
|
}
|
|
|
|
attachmentInfoView.text = formatAttachmentDetails(context, attachment, exists)
|
2022-02-12 02:34:08 +01:00
|
|
|
attachmentIconView.setImageResource(mimeTypeToIconResource(attachment.type))
|
2022-03-21 00:53:51 +01:00
|
|
|
val attachmentBoxPopupMenu = maybeCreateMenuPopup(context, attachmentBoxView, notification, exists) // Heavy lifting not during on-click
|
2022-01-10 19:46:31 +01:00
|
|
|
if (attachmentBoxPopupMenu != null) {
|
|
|
|
attachmentBoxView.setOnClickListener { attachmentBoxPopupMenu.show() }
|
|
|
|
} else {
|
|
|
|
attachmentBoxView.setOnClickListener {
|
|
|
|
Toast
|
|
|
|
.makeText(context, context.getString(R.string.detail_item_cannot_download), Toast.LENGTH_LONG)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
attachmentBoxView.visibility = View.VISIBLE
|
|
|
|
}
|
|
|
|
|
2022-03-21 00:53:51 +01:00
|
|
|
private fun maybeCreateMenuPopup(context: Context, anchor: View?, notification: Notification, exists: Boolean): PopupMenu? {
|
2022-01-10 19:46:31 +01:00
|
|
|
val popup = PopupMenu(context, anchor)
|
|
|
|
popup.menuInflater.inflate(R.menu.menu_detail_attachment, popup.menu)
|
2022-03-21 00:53:51 +01:00
|
|
|
val attachment = notification.attachment // May be null
|
|
|
|
val hasAttachment = attachment != null
|
2022-01-10 19:46:31 +01:00
|
|
|
val downloadItem = popup.menu.findItem(R.id.detail_item_menu_download)
|
2022-01-12 00:21:30 +01:00
|
|
|
val cancelItem = popup.menu.findItem(R.id.detail_item_menu_cancel)
|
2022-01-10 19:46:31 +01:00
|
|
|
val openItem = popup.menu.findItem(R.id.detail_item_menu_open)
|
|
|
|
val browseItem = popup.menu.findItem(R.id.detail_item_menu_browse)
|
2022-01-12 02:47:28 +01:00
|
|
|
val deleteItem = popup.menu.findItem(R.id.detail_item_menu_delete)
|
2022-01-10 19:46:31 +01:00
|
|
|
val copyUrlItem = popup.menu.findItem(R.id.detail_item_menu_copy_url)
|
2022-03-21 00:53:51 +01:00
|
|
|
val copyContentsItem = popup.menu.findItem(R.id.detail_item_menu_contents)
|
|
|
|
val expired = attachment?.expires != null && attachment.expires < System.currentTimeMillis()/1000
|
|
|
|
val inProgress = attachment?.progress in 0..99
|
|
|
|
if (attachment != null) {
|
|
|
|
if (attachment.contentUri != null) {
|
|
|
|
openItem.setOnMenuItemClickListener {
|
|
|
|
try {
|
|
|
|
val contentUri = Uri.parse(attachment.contentUri)
|
|
|
|
val intent = Intent(Intent.ACTION_VIEW, contentUri)
|
|
|
|
intent.setDataAndType(contentUri, attachment.type ?: "application/octet-stream") // Required for Android <= P
|
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
|
|
context.startActivity(intent)
|
|
|
|
} catch (e: ActivityNotFoundException) {
|
|
|
|
Toast
|
|
|
|
.makeText(context, context.getString(R.string.detail_item_cannot_open_not_found), Toast.LENGTH_LONG)
|
|
|
|
.show()
|
|
|
|
} catch (e: Exception) {
|
|
|
|
Toast
|
|
|
|
.makeText(context, context.getString(R.string.detail_item_cannot_open, e.message), Toast.LENGTH_LONG)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
true
|
2022-01-10 04:08:29 +01:00
|
|
|
}
|
2022-03-21 00:53:51 +01:00
|
|
|
}
|
|
|
|
browseItem.setOnMenuItemClickListener {
|
|
|
|
val intent = Intent(android.app.DownloadManager.ACTION_VIEW_DOWNLOADS)
|
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
|
|
context.startActivity(intent)
|
2022-01-10 19:46:31 +01:00
|
|
|
true
|
|
|
|
}
|
2022-03-21 00:53:51 +01:00
|
|
|
if (attachment.contentUri != null) {
|
|
|
|
deleteItem.setOnMenuItemClickListener {
|
|
|
|
try {
|
|
|
|
val contentUri = Uri.parse(attachment.contentUri)
|
|
|
|
val resolver = context.applicationContext.contentResolver
|
|
|
|
val deleted = resolver.delete(contentUri, null, null) > 0
|
|
|
|
if (!deleted) throw Exception("no rows deleted")
|
|
|
|
val newAttachment = attachment.copy(progress = PROGRESS_DELETED)
|
|
|
|
val newNotification = notification.copy(attachment = newAttachment)
|
|
|
|
GlobalScope.launch(Dispatchers.IO) {
|
|
|
|
repository.updateNotification(newNotification)
|
|
|
|
}
|
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.w(TAG, "Failed to update notification: ${e.message}", e)
|
|
|
|
Toast
|
|
|
|
.makeText(context, context.getString(R.string.detail_item_delete_failed, e.message), Toast.LENGTH_LONG)
|
|
|
|
.show()
|
2022-01-12 02:47:28 +01:00
|
|
|
}
|
2022-03-21 00:53:51 +01:00
|
|
|
true
|
2022-01-12 02:47:28 +01:00
|
|
|
}
|
2022-03-21 00:53:51 +01:00
|
|
|
}
|
|
|
|
copyUrlItem.setOnMenuItemClickListener {
|
|
|
|
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
|
|
|
val clip = ClipData.newPlainText("attachment url", attachment.url)
|
|
|
|
clipboard.setPrimaryClip(clip)
|
|
|
|
Toast
|
|
|
|
.makeText(context, context.getString(R.string.detail_item_menu_copy_url_copied), Toast.LENGTH_LONG)
|
|
|
|
.show()
|
2022-01-12 02:47:28 +01:00
|
|
|
true
|
|
|
|
}
|
2022-03-21 00:53:51 +01:00
|
|
|
downloadItem.setOnMenuItemClickListener {
|
|
|
|
val requiresPermission = Build.VERSION.SDK_INT <= Build.VERSION_CODES.P && ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED
|
|
|
|
if (requiresPermission) {
|
|
|
|
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_CODE_WRITE_STORAGE_PERMISSION_FOR_DOWNLOAD)
|
|
|
|
return@setOnMenuItemClickListener true
|
|
|
|
}
|
|
|
|
DownloadManager.enqueue(context, notification.id, userAction = true)
|
|
|
|
true
|
|
|
|
}
|
|
|
|
cancelItem.setOnMenuItemClickListener {
|
|
|
|
DownloadManager.cancel(context, notification.id)
|
|
|
|
true
|
2022-01-11 23:00:18 +01:00
|
|
|
}
|
2022-01-12 00:21:30 +01:00
|
|
|
}
|
2022-03-21 00:53:51 +01:00
|
|
|
if (notification.click != "") {
|
|
|
|
copyContentsItem.setOnMenuItemClickListener {
|
|
|
|
copyToClipboard(context, notification)
|
|
|
|
true
|
|
|
|
}
|
2022-01-10 19:46:31 +01:00
|
|
|
}
|
2022-03-21 00:53:51 +01:00
|
|
|
openItem.isVisible = hasAttachment && exists
|
|
|
|
browseItem.isVisible = hasAttachment && exists
|
|
|
|
downloadItem.isVisible = hasAttachment && !exists && !expired && !inProgress
|
|
|
|
deleteItem.isVisible = hasAttachment && exists
|
|
|
|
copyUrlItem.isVisible = hasAttachment && !expired
|
|
|
|
cancelItem.isVisible = hasAttachment && inProgress
|
|
|
|
copyContentsItem.isVisible = notification.click != ""
|
|
|
|
val noOptions = !openItem.isVisible && !browseItem.isVisible && !downloadItem.isVisible
|
|
|
|
&& !copyUrlItem.isVisible && !cancelItem.isVisible && !deleteItem.isVisible
|
|
|
|
&& !copyContentsItem.isVisible
|
2022-01-10 19:46:31 +01:00
|
|
|
if (noOptions) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return popup
|
|
|
|
}
|
2022-01-10 04:08:29 +01:00
|
|
|
|
2022-01-10 19:46:31 +01:00
|
|
|
private fun formatAttachmentDetails(context: Context, attachment: Attachment, exists: Boolean): String {
|
2022-02-12 02:34:08 +01:00
|
|
|
val name = fileName(context, attachment.contentUri, attachment.name)
|
2022-01-10 19:46:31 +01:00
|
|
|
val notYetDownloaded = !exists && attachment.progress == PROGRESS_NONE
|
|
|
|
val downloading = !exists && attachment.progress in 0..99
|
2022-01-12 02:47:28 +01:00
|
|
|
val deleted = !exists && (attachment.progress == PROGRESS_DONE || attachment.progress == PROGRESS_DELETED)
|
2022-01-11 23:00:18 +01:00
|
|
|
val failed = !exists && attachment.progress == PROGRESS_FAILED
|
2022-01-10 19:46:31 +01:00
|
|
|
val expired = attachment.expires != null && attachment.expires < System.currentTimeMillis()/1000
|
|
|
|
val expires = attachment.expires != null && attachment.expires > System.currentTimeMillis()/1000
|
|
|
|
val infos = mutableListOf<String>()
|
|
|
|
if (attachment.size != null) {
|
|
|
|
infos.add(formatBytes(attachment.size))
|
|
|
|
}
|
|
|
|
if (notYetDownloaded) {
|
|
|
|
if (expired) {
|
2022-01-11 23:00:18 +01:00
|
|
|
infos.add(context.getString(R.string.detail_item_download_info_not_downloaded_expired))
|
2022-01-10 19:46:31 +01:00
|
|
|
} else if (expires) {
|
2022-01-11 23:00:18 +01:00
|
|
|
infos.add(context.getString(R.string.detail_item_download_info_not_downloaded_expires_x, formatDateShort(attachment.expires!!)))
|
2022-01-10 19:46:31 +01:00
|
|
|
} else {
|
2022-01-11 23:00:18 +01:00
|
|
|
infos.add(context.getString(R.string.detail_item_download_info_not_downloaded))
|
2022-01-10 19:46:31 +01:00
|
|
|
}
|
|
|
|
} else if (downloading) {
|
2022-01-11 23:00:18 +01:00
|
|
|
infos.add(context.getString(R.string.detail_item_download_info_downloading_x_percent, attachment.progress))
|
2022-01-10 19:46:31 +01:00
|
|
|
} else if (deleted) {
|
|
|
|
if (expired) {
|
2022-01-11 23:00:18 +01:00
|
|
|
infos.add(context.getString(R.string.detail_item_download_info_deleted_expired))
|
2022-01-10 19:46:31 +01:00
|
|
|
} else if (expires) {
|
2022-01-11 23:00:18 +01:00
|
|
|
infos.add(context.getString(R.string.detail_item_download_info_deleted_expires_x, formatDateShort(attachment.expires!!)))
|
2022-01-10 19:46:31 +01:00
|
|
|
} else {
|
2022-01-11 23:00:18 +01:00
|
|
|
infos.add(context.getString(R.string.detail_item_download_info_deleted))
|
2022-01-10 04:08:29 +01:00
|
|
|
}
|
2022-01-11 23:00:18 +01:00
|
|
|
} else if (failed) {
|
2022-02-10 18:10:27 +01:00
|
|
|
if (expired) {
|
|
|
|
infos.add(context.getString(R.string.detail_item_download_info_download_failed_expired))
|
|
|
|
} else if (expires) {
|
|
|
|
infos.add(context.getString(R.string.detail_item_download_info_download_failed_expires_x, formatDateShort(attachment.expires!!)))
|
|
|
|
} else {
|
|
|
|
infos.add(context.getString(R.string.detail_item_download_info_download_failed))
|
|
|
|
}
|
2022-01-10 19:46:31 +01:00
|
|
|
}
|
|
|
|
return if (infos.size > 0) {
|
|
|
|
"$name\n${infos.joinToString(", ")}"
|
2022-01-10 04:08:29 +01:00
|
|
|
} else {
|
2022-01-10 19:46:31 +01:00
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 21:36:50 +01:00
|
|
|
private fun maybeRenderAttachmentImage(context: Context, attachment: Attachment, image: Boolean) {
|
|
|
|
if (!image) {
|
2022-01-10 19:46:31 +01:00
|
|
|
attachmentImageView.visibility = View.GONE
|
|
|
|
return
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
val resolver = context.applicationContext.contentResolver
|
2022-01-10 21:36:50 +01:00
|
|
|
val bitmapStream = resolver.openInputStream(Uri.parse(attachment.contentUri))
|
2022-01-10 19:46:31 +01:00
|
|
|
val bitmap = BitmapFactory.decodeStream(bitmapStream)
|
|
|
|
attachmentImageView.setImageBitmap(bitmap)
|
2022-01-10 21:36:50 +01:00
|
|
|
attachmentImageView.setOnClickListener {
|
|
|
|
val loadImage = { view: ImageView, image: Bitmap -> view.setImageBitmap(image) }
|
|
|
|
StfalconImageViewer.Builder(context, listOf(bitmap), loadImage)
|
|
|
|
.allowZooming(true)
|
|
|
|
.withTransitionFrom(attachmentImageView)
|
|
|
|
.withHiddenStatusBar(false)
|
|
|
|
.show()
|
|
|
|
}
|
2022-01-10 19:46:31 +01:00
|
|
|
attachmentImageView.visibility = View.VISIBLE
|
|
|
|
} catch (_: Exception) {
|
|
|
|
attachmentImageView.visibility = View.GONE
|
2022-01-10 04:08:29 +01:00
|
|
|
}
|
|
|
|
}
|
2021-10-31 20:19:25 +01: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 04:08:29 +01:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val TAG = "NtfyDetailAdapter"
|
2022-01-11 23:00:18 +01:00
|
|
|
const val REQUEST_CODE_WRITE_STORAGE_PERMISSION_FOR_DOWNLOAD = 9876
|
2022-01-10 04:08:29 +01:00
|
|
|
}
|
2021-10-31 20:19:25 +01:00
|
|
|
}
|