Update TaskEditViewModel initialization

pull/3348/head
Alex Baker 10 months ago
parent 69b189b29c
commit 9b80a02d03

@ -32,6 +32,7 @@ import kotlinx.collections.immutable.persistentSetOf
import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentList
import kotlinx.collections.immutable.toPersistentSet import kotlinx.collections.immutable.toPersistentSet
import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
@ -126,7 +127,7 @@ class TaskEditViewModel @Inject constructor(
val tags: ImmutableSet<TagData>, val tags: ImmutableSet<TagData>,
val calendar: String?, val calendar: String?,
val attachments: ImmutableSet<TaskAttachment> = persistentSetOf(), val attachments: ImmutableSet<TaskAttachment> = persistentSetOf(),
val alarms: ImmutableSet<Alarm>, val alarms: ImmutableSet<Alarm> = persistentSetOf(),
val newSubtasks: ImmutableList<Task> = persistentListOf(), val newSubtasks: ImmutableList<Task> = persistentListOf(),
val multilineTitle: Boolean, val multilineTitle: Boolean,
) { ) {
@ -177,24 +178,6 @@ class TaskEditViewModel @Inject constructor(
.mapNotNull { controlSetStrings[it] } .mapNotNull { controlSetStrings[it] }
.toPersistentList() .toPersistentList()
}, },
alarms = if (task.isNew) {
ArrayList<Alarm>().apply {
if (task.isNotifyAtStart) {
add(whenStarted(0))
}
if (task.isNotifyAtDeadline) {
add(whenDue(0))
}
if (task.isNotifyAfterDeadline) {
add(whenOverdue(0))
}
if (task.randomReminder > 0) {
add(Alarm(time = task.randomReminder, type = Alarm.TYPE_RANDOM))
}
}
} else {
emptyList()
}.toPersistentSet(),
multilineTitle = preferences.multilineTitle, multilineTitle = preferences.multilineTitle,
location = null, location = null,
tags = persistentSetOf(), tags = persistentSetOf(),
@ -613,39 +596,52 @@ class TaskEditViewModel @Inject constructor(
init { init {
viewModelScope.launch { viewModelScope.launch {
val attachments = async {
taskAttachmentDao taskAttachmentDao
.getAttachments(task.id) .getAttachments(task.id)
.filter { FileHelper.fileExists(context, Uri.parse(it.uri)) } .filter { FileHelper.fileExists(context, Uri.parse(it.uri)) }
.toPersistentSet() .toPersistentSet()
.let { attachments ->
_originalState.update { it.copy(attachments = attachments) }
_viewState.update { it.copy(attachments = attachments) }
}
}
if (!task.isNew) {
viewModelScope.launch {
alarmDao.getAlarms(task.id).toPersistentSet().let { alarms ->
_originalState.update { it.copy(alarms = alarms) }
_viewState.update { it.copy(alarms = alarms) }
} }
val alarms = async {
if (task.isNew) {
ArrayList<Alarm>().apply {
if (task.isNotifyAtStart) {
add(whenStarted(0))
} }
if (task.isNotifyAtDeadline) {
add(whenDue(0))
} }
viewModelScope.launch { if (task.isNotifyAfterDeadline) {
defaultFilterProvider.getList(task).let { list -> add(whenOverdue(0))
_originalState.update { it.copy(list = list) }
_viewState.update { it.copy(list = list) }
} }
if (task.randomReminder > 0) {
add(Alarm(time = task.randomReminder, type = Alarm.TYPE_RANDOM))
} }
viewModelScope.launch {
locationDao.getLocation(task, preferences)?.let { location ->
_originalState.update { it.copy(location = location) }
_viewState.update { it.copy(location = location) }
} }
} else {
alarmDao.getAlarms(task.id)
}.toPersistentSet()
}
val list = async { defaultFilterProvider.getList(task) }
val location = async { locationDao.getLocation(task, preferences) }
val tags = async { tagDataDao.getTags(task).toPersistentSet() }
_originalState.update {
it.copy(
attachments = attachments.await(),
alarms = alarms.await(),
list = list.await(),
location = location.await(),
tags = tags.await(),
)
} }
viewModelScope.launch { _viewState.update {
tagDataDao.getTags(task).toPersistentSet().let { tags -> it.copy(
_originalState.update { it.copy(tags = tags) } attachments = originalState.value.attachments,
_viewState.update { it.copy(tags = tags) } alarms = originalState.value.alarms,
list = originalState.value.list,
location = originalState.value.location,
tags = originalState.value.tags,
)
} }
} }
} }

Loading…
Cancel
Save