Better preview state management

This commit is contained in:
J-Jamet
2021-02-08 16:30:19 +01:00
parent 30f0de83d3
commit 21cf49f4f8
2 changed files with 32 additions and 16 deletions

View File

@@ -21,7 +21,6 @@ package com.kunzisoft.keepass.adapters
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Bitmap
import android.graphics.Color
import android.text.format.Formatter
import android.util.TypedValue
@@ -47,9 +46,6 @@ class EntryAttachmentsItemsAdapter(context: Context)
var binaryCipherKey: Database.LoadedKey? = null
var onItemClickListener: ((item: EntryAttachmentState)->Unit)? = null
// To manage image preview loading
private val imageBinariesSet = HashMap<Int, Bitmap?>()
private var mTitleColor: Int
init {
@@ -71,19 +67,32 @@ class EntryAttachmentsItemsAdapter(context: Context)
holder.itemView.visibility = View.VISIBLE
holder.binaryFileThumbnail.apply {
visibility = View.GONE
// Show the bitmap image if loaded
if (imageBinariesSet.containsKey(position)) {
if (imageBinariesSet[position] != null) {
setImageBitmap(imageBinariesSet[position])
when (entryAttachmentState.previewItemState) {
AttachmentState.NULL -> {
visibility = View.GONE
entryAttachmentState.previewItemState = AttachmentState.IN_PROGRESS
// Load the bitmap image
Attachment.loadBitmap(entryAttachmentState.attachment, binaryCipherKey) { imageLoaded ->
entryAttachmentState.previewItem = imageLoaded
if (imageLoaded == null) {
entryAttachmentState.previewItemState = AttachmentState.ERROR
} else {
entryAttachmentState.previewItemState = AttachmentState.COMPLETE
}
notifyItemChanged(position)
}
}
AttachmentState.IN_PROGRESS,
AttachmentState.START -> {
// Do nothing if in progress
}
AttachmentState.COMPLETE -> {
setImageBitmap(entryAttachmentState.previewItem)
visibility = View.VISIBLE
}
} else {
imageBinariesSet[position] = null
// Load the bitmap image
Attachment.loadBitmap(entryAttachmentState.attachment, binaryCipherKey) { imageLoaded ->
imageBinariesSet[position] = imageLoaded
notifyItemChanged(position)
AttachmentState.ERROR -> {
visibility = View.GONE
}
}
this.setOnClickListener {

View File

@@ -19,6 +19,7 @@
*/
package com.kunzisoft.keepass.model
import android.graphics.Bitmap
import android.os.Parcel
import android.os.Parcelable
import com.kunzisoft.keepass.database.element.Attachment
@@ -29,19 +30,25 @@ import com.kunzisoft.keepass.utils.writeEnum
data class EntryAttachmentState(var attachment: Attachment,
var streamDirection: StreamDirection,
var downloadState: AttachmentState = AttachmentState.NULL,
var downloadProgression: Int = 0) : Parcelable {
var downloadProgression: Int = 0,
var previewItem: Bitmap? = null,
var previewItemState: AttachmentState = AttachmentState.NULL) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readParcelable(Attachment::class.java.classLoader) ?: Attachment("", BinaryAttachment()),
parcel.readEnum<StreamDirection>() ?: StreamDirection.DOWNLOAD,
parcel.readEnum<AttachmentState>() ?: AttachmentState.NULL,
parcel.readInt())
parcel.readInt(),
parcel.readParcelable(Bitmap::class.java.classLoader),
parcel.readEnum<AttachmentState>() ?: AttachmentState.NULL)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeParcelable(attachment, flags)
parcel.writeEnum(streamDirection)
parcel.writeEnum(downloadState)
parcel.writeInt(downloadProgression)
parcel.writeParcelable(previewItem, flags)
parcel.writeEnum(previewItemState)
}
override fun describeContents(): Int {