mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-30 22:02:57 -08:00
Manage attachments
This commit is contained in:
1 parent
dbde23fb7b
commit
bf6cb04fe8
4 files changed
+89
-66
No files matched your search
@@ -224,6 +224,10 @@ class EntryEditActivity : LockingActivity(),
|
||||
.show()
|
||||
}
|
||||
}
|
||||
mEntryEditViewModel.onStartUploadAttachment.observe(this) {
|
||||
// Start uploading in service
|
||||
mAttachmentFileBinderManager?.startUploadAttachment(it.attachmentToUploadUri, it.attachment)
|
||||
}
|
||||
mEntryEditViewModel.onAttachmentAction.observe(this) { attachmentState ->
|
||||
when (attachmentState?.downloadState) {
|
||||
AttachmentState.ERROR -> {
|
||||
@@ -475,36 +479,13 @@ class EntryEditActivity : LockingActivity(),
|
||||
|
||||
override fun onValidateUploadFileTooBig(attachmentToUploadUri: Uri?, fileName: String?) {
|
||||
if (attachmentToUploadUri != null && fileName != null) {
|
||||
buildNewAttachment(attachmentToUploadUri, fileName)
|
||||
mEntryEditViewModel.buildNewAttachment(attachmentToUploadUri, fileName)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onValidateReplaceFile(attachmentToUploadUri: Uri?, attachment: Attachment?) {
|
||||
startUploadAttachment(attachmentToUploadUri, attachment)
|
||||
}
|
||||
|
||||
private fun startUploadAttachment(attachmentToUploadUri: Uri?, attachment: Attachment?) {
|
||||
if (attachmentToUploadUri != null && attachment != null) {
|
||||
// Start uploading in service
|
||||
mAttachmentFileBinderManager?.startUploadAttachment(attachmentToUploadUri, attachment)
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildNewAttachment(attachmentToUploadUri: Uri, fileName: String) {
|
||||
mDatabase?.buildNewBinaryAttachment()?.let { binaryAttachment ->
|
||||
val entryAttachment = Attachment(fileName, binaryAttachment)
|
||||
/*
|
||||
TODO fragment
|
||||
// Ask to replace the current attachment
|
||||
if ((mDatabase?.allowMultipleAttachments == false && entryEditFragment?.containsAttachment() == true) ||
|
||||
entryEditFragment?.containsAttachment(EntryAttachmentState(entryAttachment, StreamDirection.UPLOAD)) == true) {
|
||||
ReplaceFileDialogFragment.build(attachmentToUploadUri, entryAttachment)
|
||||
.show(supportFragmentManager, "replacementFileFragment")
|
||||
} else {
|
||||
startUploadAttachment(attachmentToUploadUri, entryAttachment)
|
||||
}
|
||||
|
||||
*/
|
||||
mEntryEditViewModel.startUploadAttachment(attachmentToUploadUri, attachment)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -523,7 +504,7 @@ class EntryEditActivity : LockingActivity(),
|
||||
FileTooBigDialogFragment.build(attachmentToUploadUri, fileName)
|
||||
.show(supportFragmentManager, "fileTooBigFragment")
|
||||
} else {
|
||||
buildNewAttachment(attachmentToUploadUri, fileName)
|
||||
mEntryEditViewModel.buildNewAttachment(attachmentToUploadUri, fileName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.recyclerview.widget.SimpleItemAnimator
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.activities.dialogs.ReplaceFileDialogFragment
|
||||
import com.kunzisoft.keepass.activities.dialogs.SetOTPDialogFragment
|
||||
import com.kunzisoft.keepass.activities.lock.resetAppTimeoutWhenViewFocusedOrChanged
|
||||
import com.kunzisoft.keepass.adapters.EntryAttachmentsItemsAdapter
|
||||
@@ -178,6 +179,23 @@ class EntryEditFragment: DatabaseFragment(), SetOTPDialogFragment.CreateOtpListe
|
||||
}
|
||||
}
|
||||
|
||||
mEntryEditViewModel.onBuildNewAttachment.observe(viewLifecycleOwner) {
|
||||
val attachmentToUploadUri = it.attachmentToUploadUri
|
||||
val fileName = it.fileName
|
||||
mDatabase?.buildNewBinaryAttachment()?.let { binaryAttachment ->
|
||||
val entryAttachment = Attachment(fileName, binaryAttachment)
|
||||
// Ask to replace the current attachment
|
||||
if ((mDatabase?.allowMultipleAttachments == false
|
||||
&& containsAttachment()) ||
|
||||
containsAttachment(EntryAttachmentState(entryAttachment, StreamDirection.UPLOAD))) {
|
||||
ReplaceFileDialogFragment.build(attachmentToUploadUri, entryAttachment)
|
||||
.show(parentFragmentManager, "replacementFileFragment")
|
||||
} else {
|
||||
mEntryEditViewModel.startUploadAttachment(attachmentToUploadUri, entryAttachment)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mEntryEditViewModel.onAttachmentAction.observe(viewLifecycleOwner) { entryAttachmentState ->
|
||||
when (entryAttachmentState?.downloadState) {
|
||||
AttachmentState.START -> {
|
||||
@@ -219,6 +237,11 @@ class EntryEditFragment: DatabaseFragment(), SetOTPDialogFragment.CreateOtpListe
|
||||
drawFactory = mDatabase?.iconDrawableFactory
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
mEntryEditViewModel.updateEntryInfo(retrieveEntryInfo())
|
||||
}
|
||||
|
||||
override fun onDetach() {
|
||||
super.onDetach()
|
||||
|
||||
@@ -302,11 +325,11 @@ class EntryEditFragment: DatabaseFragment(), SetOTPDialogFragment.CreateOtpListe
|
||||
}
|
||||
}
|
||||
|
||||
fun containsAttachment(): Boolean {
|
||||
private fun containsAttachment(): Boolean {
|
||||
return attachmentsAdapter?.isEmpty() != true
|
||||
}
|
||||
|
||||
fun containsAttachment(attachment: EntryAttachmentState): Boolean {
|
||||
private fun containsAttachment(attachment: EntryAttachmentState): Boolean {
|
||||
return attachmentsAdapter?.contains(attachment) ?: false
|
||||
}
|
||||
|
||||
|
||||
@@ -575,7 +575,6 @@ class TemplateView @JvmOverloads constructor(context: Context,
|
||||
return
|
||||
} else {
|
||||
mTemplate = state.template
|
||||
mEntryInfo = state.entryInfo
|
||||
buildTemplateAndPopulateInfo()
|
||||
super.onRestoreInstanceState(state.superState)
|
||||
}
|
||||
@@ -586,7 +585,6 @@ class TemplateView @JvmOverloads constructor(context: Context,
|
||||
val saveState = SavedState(superSave)
|
||||
populateEntryInfoWithViews()
|
||||
saveState.template = this.mTemplate
|
||||
saveState.entryInfo = this.mEntryInfo
|
||||
return saveState
|
||||
}
|
||||
|
||||
@@ -595,21 +593,17 @@ class TemplateView @JvmOverloads constructor(context: Context,
|
||||
|
||||
internal class SavedState : BaseSavedState {
|
||||
var template: Template? = null
|
||||
var entryInfo: EntryInfo? = null
|
||||
|
||||
constructor(superState: Parcelable?) : super(superState)
|
||||
|
||||
private constructor(parcel: Parcel) : super(parcel) {
|
||||
template = parcel.readParcelable(Template::class.java.classLoader)
|
||||
?: template
|
||||
entryInfo = parcel.readParcelable(EntryInfo::class.java.classLoader)
|
||||
?: entryInfo
|
||||
}
|
||||
|
||||
override fun writeToParcel(out: Parcel, flags: Int) {
|
||||
super.writeToParcel(out, flags)
|
||||
out.writeParcelable(template, flags)
|
||||
out.writeParcelable(entryInfo, flags)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.kunzisoft.keepass.viewmodels
|
||||
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
@@ -18,7 +19,6 @@ import com.kunzisoft.keepass.view.TemplateView
|
||||
|
||||
class EntryEditViewModel: ViewModel() {
|
||||
|
||||
private var mTempEntryInfo = EntryInfo()
|
||||
private var mEntryInfoLoaded = false
|
||||
val entryInfoLoaded : LiveData<EntryInfo> get() = _entryInfoLoaded
|
||||
private val _entryInfoLoaded = SingleLiveEvent<EntryInfo>()
|
||||
@@ -56,6 +56,10 @@ class EntryEditViewModel: ViewModel() {
|
||||
private val _onTimeSelected = SingleLiveEvent<TemplateView.Time>()
|
||||
|
||||
private val mTempAttachments = mutableListOf<EntryAttachmentState>()
|
||||
val onBuildNewAttachment : LiveData<AttachmentBuild> get() = _onBuildNewAttachment
|
||||
private val _onBuildNewAttachment = SingleLiveEvent<AttachmentBuild>()
|
||||
val onStartUploadAttachment : LiveData<AttachmentUpload> get() = _onStartUploadAttachment
|
||||
private val _onStartUploadAttachment = SingleLiveEvent<AttachmentUpload>()
|
||||
val attachmentDeleted : LiveData<Attachment> get() = _attachmentDeleted
|
||||
private val _attachmentDeleted = SingleLiveEvent<Attachment>()
|
||||
val onAttachmentAction : LiveData<EntryAttachmentState?> get() = _onAttachmentAction
|
||||
@@ -70,42 +74,53 @@ class EntryEditViewModel: ViewModel() {
|
||||
fun loadEntryInfo(entryInfo: EntryInfo) {
|
||||
if (!mEntryInfoLoaded) {
|
||||
mEntryInfoLoaded = true
|
||||
IOActionTask(
|
||||
{
|
||||
internalUpdateEntryInfo(entryInfo)
|
||||
},
|
||||
{
|
||||
_entryInfoLoaded.value = mTempEntryInfo
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
}
|
||||
|
||||
private fun internalUpdateEntryInfo(entryInfo: EntryInfo) {
|
||||
mTempEntryInfo = entryInfo
|
||||
// Do not save entry in upload progression
|
||||
mTempAttachments.forEach { attachmentState ->
|
||||
if (attachmentState.streamDirection == StreamDirection.UPLOAD) {
|
||||
when (attachmentState.downloadState) {
|
||||
AttachmentState.START,
|
||||
AttachmentState.IN_PROGRESS,
|
||||
AttachmentState.CANCELED,
|
||||
AttachmentState.ERROR -> {
|
||||
// Remove attachment not finished from info
|
||||
mTempEntryInfo.attachments = mTempEntryInfo.attachments.toMutableList().apply {
|
||||
remove(attachmentState.attachment)
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
internalUpdateEntryInfo(entryInfo) {
|
||||
_entryInfoLoaded.value = it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateEntryInfo(entryInfo: EntryInfo) {
|
||||
internalUpdateEntryInfo(entryInfo) {
|
||||
_entryInfoLoaded.value = it
|
||||
}
|
||||
}
|
||||
|
||||
fun saveEntryInfo(entryInfo: EntryInfo) {
|
||||
internalUpdateEntryInfo(entryInfo)
|
||||
_onEntryInfoSaved.value = EntryInfoTempAttachments(mTempEntryInfo, mTempAttachments)
|
||||
internalUpdateEntryInfo(entryInfo) {
|
||||
_onEntryInfoSaved.value = EntryInfoTempAttachments(it, mTempAttachments)
|
||||
}
|
||||
}
|
||||
|
||||
private fun internalUpdateEntryInfo(entryInfo: EntryInfo,
|
||||
actionOnFinish: (entryInfo: EntryInfo) -> Unit) {
|
||||
IOActionTask(
|
||||
{
|
||||
// Do not save entry in upload progression
|
||||
mTempAttachments.forEach { attachmentState ->
|
||||
if (attachmentState.streamDirection == StreamDirection.UPLOAD) {
|
||||
when (attachmentState.downloadState) {
|
||||
AttachmentState.START,
|
||||
AttachmentState.IN_PROGRESS,
|
||||
AttachmentState.CANCELED,
|
||||
AttachmentState.ERROR -> {
|
||||
// Remove attachment not finished from info
|
||||
entryInfo.attachments = entryInfo.attachments.toMutableList().apply {
|
||||
remove(attachmentState.attachment)
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
entryInfo
|
||||
},
|
||||
{
|
||||
if (it != null)
|
||||
actionOnFinish.invoke(it)
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
|
||||
fun assignTemplate(template: Template) {
|
||||
@@ -161,6 +176,14 @@ class EntryEditViewModel: ViewModel() {
|
||||
_onTimeSelected.value = TemplateView.Time(hours, minutes)
|
||||
}
|
||||
|
||||
fun buildNewAttachment(attachmentToUploadUri: Uri, fileName: String) {
|
||||
_onBuildNewAttachment.value = AttachmentBuild(attachmentToUploadUri, fileName)
|
||||
}
|
||||
|
||||
fun startUploadAttachment(attachmentToUploadUri: Uri, attachment: Attachment) {
|
||||
_onStartUploadAttachment.value = AttachmentUpload(attachmentToUploadUri, attachment)
|
||||
}
|
||||
|
||||
fun deleteAttachment(attachment: Attachment) {
|
||||
_attachmentDeleted.value = attachment
|
||||
}
|
||||
@@ -179,6 +202,8 @@ class EntryEditViewModel: ViewModel() {
|
||||
|
||||
data class EntryInfoTempAttachments(val entryInfo: EntryInfo, val tempAttachments: List<EntryAttachmentState>)
|
||||
data class FieldEdition(val oldField: Field?, val newField: Field?)
|
||||
data class AttachmentBuild(val attachmentToUploadUri: Uri, val fileName: String)
|
||||
data class AttachmentUpload(val attachmentToUploadUri: Uri, val attachment: Attachment)
|
||||
data class AttachmentPosition(val entryAttachmentState: EntryAttachmentState, val viewPosition: Float)
|
||||
|
||||
companion object {
|
||||
|
||||
Reference in new issue
Block a user