Simpler load entry implementation

This commit is contained in:
J-Jamet committed 2021-08-27 16:16:55 +02:00
1 parent dc70918648
commit b4aee17f53
1 file changed
+39 -46
@@ -63,57 +63,49 @@ class EntryViewModel: ViewModel() {
private val _historySelected = SingleLiveEvent<EntryHistory>()
fun loadEntry(database: Database?, entryId: NodeId<UUID>?, 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<UUID>,
val template: Template,
val entryInfo: EntryInfo,
val entryHistory: List<EntryInfo>)
// Custom data class to manage entry to retrieve and define is it's an history item (!= -1)