diff --git a/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt b/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt index b777d7ea2..1c8f4f010 100644 --- a/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt +++ b/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt @@ -63,57 +63,49 @@ class EntryViewModel: ViewModel() { private val _historySelected = SingleLiveEvent() fun loadEntry(database: Database?, entryId: NodeId?, historyPosition: Int) { - if (entryId != null) { + if (database != null && entryId != null) { IOActionTask( { - database?.getEntryById(entryId) - }, - { mainEntry -> - // Manage current version and history - _mainEntryId.value = mainEntry?.nodeId - _historyPosition.value = historyPosition + val mainEntry = database.getEntryById(entryId) + val currentEntry = if (historyPosition > -1) { + mainEntry?.getHistory()?.get(historyPosition) + } else { + mainEntry + } - IOActionTask( - { - val currentEntry = if (historyPosition > -1) { - mainEntry?.getHistory()?.get(historyPosition) - } else { - mainEntry + val entryTemplate = currentEntry?.let { + database.getTemplate(it) + } ?: Template.STANDARD + + // To simplify template field visibility + currentEntry?.let { entry -> + // Add mainEntry to check the parent and define the template state + database.decodeEntryWithTemplateConfiguration(entry, mainEntry).let { + // To update current modification time + it.touch(modified = false, touchParents = false) + + // Build history info + val entryInfoHistory = it.getHistory().map { entryHistory -> + entryHistory.getEntryInfo(database) } - val entryTemplate = currentEntry?.let { - database?.getTemplate(it) - } ?: Template.STANDARD - - // To simplify template field visibility - currentEntry?.let { entry -> - // Add mainEntry to check the parent and define the template state - database?.decodeEntryWithTemplateConfiguration(entry, mainEntry) - ?.let { - // To update current modification time - it.touch(modified = false, touchParents = false) - - // Build history info - val entryInfoHistory = it.getHistory().map { entryHistory -> - entryHistory.getEntryInfo(database) - } - - EntryInfoHistory( - entryTemplate, - it.getEntryInfo(database), - entryInfoHistory - ) - } - } - }, - { entryInfoHistory -> - if (entryInfoHistory != null) { - _template.value = entryInfoHistory.template - _entryInfo.value = entryInfoHistory.entryInfo - _entryHistory.value = entryInfoHistory.entryHistory - } + EntryInfoHistory( + mainEntry!!.nodeId, + entryTemplate, + it.getEntryInfo(database), + entryInfoHistory + ) } - ).execute() + } + }, + { entryInfoHistory -> + if (entryInfoHistory != null) { + _mainEntryId.value = entryInfoHistory.mainEntryId + _historyPosition.value = historyPosition + _template.value = entryInfoHistory.template + _entryInfo.value = entryInfoHistory.entryInfo + _entryHistory.value = entryInfoHistory.entryHistory + } } ).execute() } @@ -139,7 +131,8 @@ class EntryViewModel: ViewModel() { _historySelected.value = EntryHistory(NodeIdUUID(item.id), null, item, position) } - data class EntryInfoHistory(val template: Template, + data class EntryInfoHistory(var mainEntryId: NodeId, + val template: Template, val entryInfo: EntryInfo, val entryHistory: List) // Custom data class to manage entry to retrieve and define is it's an history item (!= -1)