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