forked from upstream-mirrors/quik
changes to save scheduled message attachment data in app local dir so external delete of attachment file doesn't result in it being unable to be found when sending message
This commit is contained in:
+15
-76
@@ -19,19 +19,16 @@
|
||||
package dev.octoshrimpy.quik.feature.scheduled
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.media.MediaMetadataRetriever
|
||||
import android.net.Uri
|
||||
import android.provider.MediaStore.MATCH_DEFAULT
|
||||
import android.provider.OpenableColumns
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.bumptech.glide.Glide
|
||||
import com.moez.QKSMS.feature.extensions.LoadBestIconIntoImageView
|
||||
import com.moez.QKSMS.feature.extensions.loadBestIconIntoImageView
|
||||
import dev.octoshrimpy.quik.R
|
||||
import dev.octoshrimpy.quik.common.base.QkAdapter
|
||||
import dev.octoshrimpy.quik.common.base.QkViewHolder
|
||||
import dev.octoshrimpy.quik.extensions.resourceExists
|
||||
import dev.octoshrimpy.quik.extensions.getName
|
||||
import kotlinx.android.synthetic.main.scheduled_message_image_list_item.fileName
|
||||
import kotlinx.android.synthetic.main.scheduled_message_image_list_item.thumbnail
|
||||
import javax.inject.Inject
|
||||
@@ -52,78 +49,20 @@ class ScheduledMessageAttachmentAdapter @Inject constructor(
|
||||
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
|
||||
val uri = getItem(position)
|
||||
|
||||
// if uri target doesn't exist anymore
|
||||
if (!uri.resourceExists(context)) {
|
||||
holder.thumbnail.setImageResource(android.R.drawable.ic_delete)
|
||||
holder.fileName.text = context.getString(R.string.attachment_missing)
|
||||
holder.fileName.visibility = View.VISIBLE
|
||||
return
|
||||
}
|
||||
|
||||
val mimeType = context.contentResolver.getType(uri)
|
||||
|
||||
// if attachment mime type is image/* or video/*, use image/video-frame
|
||||
if ((mimeType?.startsWith("image/") == true) ||
|
||||
(mimeType?.startsWith("video/") == true)
|
||||
) {
|
||||
Glide
|
||||
.with(context)
|
||||
.load(uri)
|
||||
.into(holder.thumbnail)
|
||||
return
|
||||
}
|
||||
|
||||
// if audio mime type, try and use embedded image if one exists
|
||||
if (mimeType?.startsWith("audio/") == true) {
|
||||
val metaDataRetriever = MediaMetadataRetriever()
|
||||
metaDataRetriever.setDataSource(context, uri)
|
||||
val embeddedPicture = metaDataRetriever.embeddedPicture
|
||||
if (embeddedPicture != null) {
|
||||
Glide
|
||||
.with(context)
|
||||
.load(embeddedPicture)
|
||||
.into(holder.thumbnail)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// try and use icon from default app for type
|
||||
val intent = Intent(Intent.ACTION_VIEW).setDataAndType(uri, mimeType)
|
||||
val appIcon = context
|
||||
.packageManager
|
||||
.resolveActivity(intent, MATCH_DEFAULT)
|
||||
?.loadIcon(context.packageManager)
|
||||
if (appIcon != null)
|
||||
Glide
|
||||
.with(context)
|
||||
.load(appIcon)
|
||||
.into(holder.thumbnail)
|
||||
else if (mimeType?.startsWith("audio/") == true)
|
||||
// else, if audio, use default local audio icon
|
||||
Glide
|
||||
.with(context)
|
||||
.load(R.drawable.ic_round_volume_up_24)
|
||||
.into(holder.thumbnail)
|
||||
else
|
||||
// else, use default attachment icon
|
||||
Glide
|
||||
.with(context)
|
||||
.load(R.drawable.ic_attachment_black_24dp)
|
||||
.into(holder.thumbnail)
|
||||
|
||||
// else, show file name
|
||||
val cursor = context.contentResolver.query(
|
||||
uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null
|
||||
)
|
||||
cursor?.use {
|
||||
try {
|
||||
cursor.moveToFirst()
|
||||
holder.fileName.text = cursor.getString(
|
||||
cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) ?: 0
|
||||
)
|
||||
// set best image and text to use for icon
|
||||
when (getItem(position).loadBestIconIntoImageView(context, holder.thumbnail)) {
|
||||
LoadBestIconIntoImageView.Missing -> {
|
||||
holder.fileName.text = context.getString(R.string.attachment_missing)
|
||||
holder.fileName.visibility = View.VISIBLE
|
||||
} catch (e: Exception) {
|
||||
}
|
||||
LoadBestIconIntoImageView.ActivityIcon,
|
||||
LoadBestIconIntoImageView.DefaultAudioIcon,
|
||||
LoadBestIconIntoImageView.GenericIcon -> {
|
||||
// generic style icon used, also show name
|
||||
holder.fileName.text = uri.getName(context)
|
||||
holder.fileName.visibility = View.VISIBLE
|
||||
}
|
||||
else -> holder.fileName.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import dev.octoshrimpy.quik.repository.ScheduledMessageRepository
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.autoDisposable
|
||||
import dev.octoshrimpy.quik.common.util.ClipboardUtils
|
||||
import dev.octoshrimpy.quik.interactor.DeleteScheduledMessages
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import io.reactivex.rxkotlin.withLatestFrom
|
||||
@@ -22,6 +23,7 @@ class ScheduledViewModel @Inject constructor(
|
||||
private val navigator: Navigator,
|
||||
private val scheduledMessageRepo: ScheduledMessageRepository,
|
||||
private val sendScheduledMessageInteractor: SendScheduledMessage,
|
||||
private val deleteScheduledMessagesInteractor: DeleteScheduledMessages,
|
||||
) : QkViewModel<ScheduledView, ScheduledState>(ScheduledState(
|
||||
scheduledMessages = scheduledMessageRepo.getScheduledMessages()
|
||||
)) {
|
||||
@@ -87,7 +89,7 @@ class ScheduledViewModel @Inject constructor(
|
||||
.subscribeOn(Schedulers.io())
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe {
|
||||
scheduledMessageRepo.deleteScheduledMessages(it)
|
||||
deleteScheduledMessagesInteractor.execute(it)
|
||||
view.clearSelection()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user