From 309380bdd57fa8fa74673292e6ca977cb88c2e3a Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Mon, 8 Feb 2021 19:40:29 +0100 Subject: [PATCH] Perform binary preview animation --- .../keepass/activities/EntryEditActivity.kt | 6 +-- .../keepass/activities/EntryEditFragment.kt | 6 ++- .../adapters/EntryAttachmentsItemsAdapter.kt | 38 ++++++++----------- .../keepass/model/EntryAttachmentState.kt | 8 +--- app/src/main/res/layout/item_attachment.xml | 1 + 5 files changed, 26 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt index 1124ec2f4..7a8e41cf7 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt @@ -369,10 +369,8 @@ class EntryEditActivity : LockingActivity(), entryEditFragment?.putAttachment(entryAttachmentState) } AttachmentState.COMPLETE -> { - entryEditFragment?.apply { - putAttachment(entryAttachmentState) - // Scroll to the attachment position - getAttachmentViewPosition(entryAttachmentState) { + entryEditFragment?.putAttachment(entryAttachmentState) { + entryEditFragment?.getAttachmentViewPosition(entryAttachmentState) { scrollView?.smoothScrollTo(0, it.toInt()) } } diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditFragment.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditFragment.kt index f3b56d3d9..afbb66cef 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditFragment.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditFragment.kt @@ -505,9 +505,13 @@ class EntryEditFragment: StylishFragment() { return attachmentsAdapter.contains(attachment) } - fun putAttachment(attachment: EntryAttachmentState) { + fun putAttachment(attachment: EntryAttachmentState, + onPreviewLoaded: (()-> Unit)? = null) { attachmentsContainerView.visibility = View.VISIBLE attachmentsAdapter.putItem(attachment) + attachmentsAdapter.onBinaryPreviewLoaded = { + onPreviewLoaded?.invoke() + } } fun removeAttachment(attachment: EntryAttachmentState) { diff --git a/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsItemsAdapter.kt b/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsItemsAdapter.kt index c5fe32996..45b16b3e5 100644 --- a/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsItemsAdapter.kt +++ b/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsItemsAdapter.kt @@ -38,6 +38,7 @@ import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm import com.kunzisoft.keepass.model.AttachmentState import com.kunzisoft.keepass.model.EntryAttachmentState import com.kunzisoft.keepass.model.StreamDirection +import com.kunzisoft.keepass.view.expand class EntryAttachmentsItemsAdapter(context: Context) @@ -45,6 +46,7 @@ class EntryAttachmentsItemsAdapter(context: Context) var binaryCipherKey: Database.LoadedKey? = null var onItemClickListener: ((item: EntryAttachmentState)->Unit)? = null + var onBinaryPreviewLoaded: ((item: EntryAttachmentState) -> Unit)? = null private var mTitleColor: Int @@ -67,35 +69,27 @@ class EntryAttachmentsItemsAdapter(context: Context) holder.itemView.visibility = View.VISIBLE holder.binaryFileThumbnail.apply { - // Show the bitmap image if loaded - when (entryAttachmentState.previewItemState) { - AttachmentState.NULL -> { - visibility = View.GONE - entryAttachmentState.previewItemState = AttachmentState.IN_PROGRESS + // Perform image loading only if upload is finished + if (entryAttachmentState.downloadState != AttachmentState.START + && entryAttachmentState.downloadState != AttachmentState.IN_PROGRESS) { + // Show the bitmap image if loaded + if (entryAttachmentState.previewState == AttachmentState.NULL) { + entryAttachmentState.previewState = AttachmentState.IN_PROGRESS // Load the bitmap image Attachment.loadBitmap(entryAttachmentState.attachment, binaryCipherKey) { imageLoaded -> - entryAttachmentState.previewItem = imageLoaded if (imageLoaded == null) { - entryAttachmentState.previewItemState = AttachmentState.ERROR + entryAttachmentState.previewState = AttachmentState.ERROR + visibility = View.GONE + onBinaryPreviewLoaded?.invoke(entryAttachmentState) } else { - entryAttachmentState.previewItemState = AttachmentState.COMPLETE + entryAttachmentState.previewState = AttachmentState.COMPLETE + setImageBitmap(imageLoaded) + expand(true, resources.getDimensionPixelSize(R.dimen.item_file_info_height)) { + onBinaryPreviewLoaded?.invoke(entryAttachmentState) + } } - notifyItemChanged(position) } } - AttachmentState.IN_PROGRESS, - AttachmentState.START -> { - // Do nothing if in progress - } - AttachmentState.COMPLETE -> { - setImageBitmap(entryAttachmentState.previewItem) - visibility = View.VISIBLE - entryAttachmentState.previewItemState = AttachmentState.NULL - } - AttachmentState.ERROR -> { - visibility = View.GONE - entryAttachmentState.previewItemState = AttachmentState.NULL - } } this.setOnClickListener { ImageViewerActivity.getInstance(context, entryAttachmentState.attachment) diff --git a/app/src/main/java/com/kunzisoft/keepass/model/EntryAttachmentState.kt b/app/src/main/java/com/kunzisoft/keepass/model/EntryAttachmentState.kt index 2b65ac538..b1ad9df53 100644 --- a/app/src/main/java/com/kunzisoft/keepass/model/EntryAttachmentState.kt +++ b/app/src/main/java/com/kunzisoft/keepass/model/EntryAttachmentState.kt @@ -19,7 +19,6 @@ */ package com.kunzisoft.keepass.model -import android.graphics.Bitmap import android.os.Parcel import android.os.Parcelable import com.kunzisoft.keepass.database.element.Attachment @@ -31,15 +30,13 @@ data class EntryAttachmentState(var attachment: Attachment, var streamDirection: StreamDirection, var downloadState: AttachmentState = AttachmentState.NULL, var downloadProgression: Int = 0, - var previewItem: Bitmap? = null, - var previewItemState: AttachmentState = AttachmentState.NULL) : Parcelable { + var previewState: AttachmentState = AttachmentState.NULL) : Parcelable { constructor(parcel: Parcel) : this( parcel.readParcelable(Attachment::class.java.classLoader) ?: Attachment("", BinaryAttachment()), parcel.readEnum() ?: StreamDirection.DOWNLOAD, parcel.readEnum() ?: AttachmentState.NULL, parcel.readInt(), - parcel.readParcelable(Bitmap::class.java.classLoader), parcel.readEnum() ?: AttachmentState.NULL) override fun writeToParcel(parcel: Parcel, flags: Int) { @@ -47,8 +44,7 @@ data class EntryAttachmentState(var attachment: Attachment, parcel.writeEnum(streamDirection) parcel.writeEnum(downloadState) parcel.writeInt(downloadProgression) - parcel.writeParcelable(previewItem, flags) - parcel.writeEnum(previewItemState) + parcel.writeEnum(previewState) } override fun describeContents(): Int { diff --git a/app/src/main/res/layout/item_attachment.xml b/app/src/main/res/layout/item_attachment.xml index 7f19fbf26..95d69f18b 100644 --- a/app/src/main/res/layout/item_attachment.xml +++ b/app/src/main/res/layout/item_attachment.xml @@ -34,6 +34,7 @@ android:layout_height="144dp" android:layout_marginTop="12dp" android:layout_centerHorizontal="true" + android:visibility="gone" android:background="?android:attr/windowBackground" android:scaleType="fitStart" />