Move temp attachments in view model

This commit is contained in:
J-Jamet committed 2021-06-09 17:34:14 +02:00
1 parent cdfbcd873c
commit 0ec72bb013
3 files changed
+34 -30

No files matched your search

@@ -101,7 +101,6 @@ class EntryEditActivity : LockingActivity(),
private var mEntry : Entry? = null
private var mIsTemplate: Boolean = false
private var mEntryTemplate: Template = Template.STANDARD
private var mTempAttachments = mutableListOf<EntryAttachmentState>()
private val mEntryEditViewModel: EntryEditViewModel by viewModels()
// To manage attachments
@@ -243,10 +242,6 @@ class EntryEditActivity : LockingActivity(),
}
mEntryEditViewModel.onAttachmentAction.observe(this) { attachmentState ->
when (attachmentState?.downloadState) {
AttachmentState.START -> {
// Add in temp list
mTempAttachments.add(attachmentState)
}
AttachmentState.ERROR -> {
coordinatorLayout?.let {
Snackbar.make(it, R.string.error_file_not_create, Snackbar.LENGTH_LONG).asError().show()
@@ -290,40 +285,21 @@ class EntryEditActivity : LockingActivity(),
}
// Build new entry from the entry info retrieved
mEntryEditViewModel.onEntryInfoUpdated.observe(this) { entryInfo ->
mEntryEditViewModel.onEntryInfoUpdated.observe(this) { entryInfoTempAttachments ->
mEntry?.let { oldEntry ->
// Create a clone
var newEntry = Entry(oldEntry)
// 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 -> {
}
}
}
}
// Build info
newEntry.setEntryInfo(mDatabase, entryInfo)
newEntry.setEntryInfo(mDatabase, entryInfoTempAttachments.entryInfo)
// Encode entry properties for template
newEntry = mDatabase?.encodeEntryWithTemplateConfiguration(newEntry, mEntryTemplate)
?: newEntry
// Delete temp attachment if not used
mTempAttachments.forEach { tempAttachmentState ->
entryInfoTempAttachments.tempAttachments.forEach { tempAttachmentState ->
val tempAttachment = tempAttachmentState.attachment
mDatabase?.attachmentPool?.let { binaryPool ->
if (!newEntry.getAttachments(binaryPool).contains(tempAttachment)) {
@@ -87,6 +87,7 @@ class EntryEditFragment: DatabaseFragment(), SetOTPDialogFragment.CreateOtpListe
private var mTemplate = Template.STANDARD
private var mCustomFieldIds = mutableListOf<FieldId>()
// Current date time selection
@IdRes
private var mTempDateTimeViewId: Int? = null
@@ -8,16 +8,18 @@ import com.kunzisoft.keepass.database.element.DateInstant
import com.kunzisoft.keepass.database.element.Field
import com.kunzisoft.keepass.database.element.icon.IconImage
import com.kunzisoft.keepass.database.element.template.Template
import com.kunzisoft.keepass.model.AttachmentState
import com.kunzisoft.keepass.model.EntryAttachmentState
import com.kunzisoft.keepass.model.EntryInfo
import com.kunzisoft.keepass.model.StreamDirection
class EntryEditViewModel: ViewModel() {
val requestEntryInfoUpdate : LiveData<Void?> get() = _requestEntryInfoUpdate
private val _requestEntryInfoUpdate = SingleLiveEvent<Void?>()
val onEntryInfoUpdated : LiveData<EntryInfo> get() = _onEntryInfoUpdated
private val _onEntryInfoUpdated = SingleLiveEvent<EntryInfo>()
val onEntryInfoUpdated : LiveData<EntryInfoTempAttachments> get() = _onEntryInfoUpdated
private val _onEntryInfoUpdated = SingleLiveEvent<EntryInfoTempAttachments>()
val onTemplateChanged : LiveData<Template> get() = _onTemplateChanged
private val _onTemplateChanged = SingleLiveEvent<Template>()
@@ -46,6 +48,7 @@ class EntryEditViewModel: ViewModel() {
val onTimeSelected : LiveData<Time> get() = _onTimeSelected
private val _onTimeSelected = SingleLiveEvent<Time>()
private val mTempAttachments = mutableListOf<EntryAttachmentState>()
val attachmentDeleted : LiveData<Attachment> get() = _attachmentDeleted
private val _attachmentDeleted = SingleLiveEvent<Attachment>()
val onAttachmentAction : LiveData<EntryAttachmentState?> get() = _onAttachmentAction
@@ -58,7 +61,26 @@ class EntryEditViewModel: ViewModel() {
}
fun updateEntryInfo(entryInfo: EntryInfo) {
_onEntryInfoUpdated.value = 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
entryInfo.attachments = entryInfo.attachments.toMutableList().apply {
remove(attachmentState.attachment)
}
}
else -> {
}
}
}
}
_onEntryInfoUpdated.value = EntryInfoTempAttachments(entryInfo, mTempAttachments)
}
fun assignTemplate(template: Template) {
@@ -120,6 +142,10 @@ class EntryEditViewModel: ViewModel() {
}
fun onAttachmentAction(entryAttachmentState: EntryAttachmentState?) {
if (entryAttachmentState?.downloadState == AttachmentState.START) {
// Add in temp list
mTempAttachments.add(entryAttachmentState)
}
_onAttachmentAction.value = entryAttachmentState
}
@@ -127,6 +153,7 @@ class EntryEditViewModel: ViewModel() {
_onBinaryPreviewLoaded.value = AttachmentPosition(entryAttachmentState, viewPosition)
}
data class EntryInfoTempAttachments(val entryInfo: EntryInfo, val tempAttachments: List<EntryAttachmentState>)
data class Date(val year: Int, val month: Int, val day: Int)
data class Time(val hours: Int, val minutes: Int)
data class FieldEdition(val oldField: Field?, val newField: Field?)