1 parent
2c37ebcfd2
commit
b0ffbf86e8
3 files changed
+289
-9
No files matched your search
@@ -149,6 +149,12 @@ class TaskEditViewModel @Inject constructor(
|
||||
private var loadedDue: Instant? = null
|
||||
private var loadedAllDay = false
|
||||
|
||||
/** Whether [loadedReminders] are all-day ones; a task without dates converts them once it gets one. */
|
||||
private var loadedKindAllDay = false
|
||||
|
||||
/** The reminders the user picked before clearing the task's last date, until a date comes back. */
|
||||
private val setAside = mutableListOf<SetAside>()
|
||||
|
||||
/** Start a fresh task, optionally pre-selecting a list / parent. */
|
||||
fun bindNew(
|
||||
presetListId: Long? = null,
|
||||
@@ -161,6 +167,7 @@ class TaskEditViewModel @Inject constructor(
|
||||
if (bound) return
|
||||
bound = true
|
||||
editingTaskId = null
|
||||
setAside.clear()
|
||||
baselineLastModified = null
|
||||
viewModelScope.launch {
|
||||
val settings = settingsPrefs.settings.first()
|
||||
@@ -214,6 +221,7 @@ class TaskEditViewModel @Inject constructor(
|
||||
bound = true
|
||||
editingTaskId = taskId
|
||||
editingOccurrence = occurrenceStart
|
||||
setAside.clear()
|
||||
viewModelScope.launch {
|
||||
val settings = settingsPrefs.settings.first()
|
||||
this@TaskEditViewModel.settings = settings
|
||||
@@ -230,6 +238,7 @@ class TaskEditViewModel @Inject constructor(
|
||||
loadedStart = task.start
|
||||
loadedDue = task.due
|
||||
loadedAllDay = task.isAllDay
|
||||
loadedKindAllDay = task.isAllDay
|
||||
_state.value = withFields(
|
||||
TaskEditUiState(
|
||||
loading = false,
|
||||
@@ -255,7 +264,12 @@ class TaskEditViewModel @Inject constructor(
|
||||
isOccurrence = task.isOccurrence,
|
||||
lists = lists,
|
||||
parentCandidates = loadParents(task.listId, selfId = taskId),
|
||||
),
|
||||
).let {
|
||||
// Reminders stored on a task without dates can't ring; they don't come back with a date either.
|
||||
if (it.hasDates) return@let it
|
||||
loadedReminders = emptyList()
|
||||
it.withoutReminders()
|
||||
},
|
||||
).also { baseline = it.toForm() }
|
||||
editingOccurrence = task.occurrenceStart
|
||||
}
|
||||
@@ -309,15 +323,22 @@ class TaskEditViewModel @Inject constructor(
|
||||
/** Switching list invalidates the parent (it lived in the old list); reload candidates. */
|
||||
fun onListChange(listId: Long) {
|
||||
if (_state.value.listId == listId) return // re-picking the same list keeps the parent
|
||||
update { it.copy(listId = listId, parentId = null, errors = emptySet()).withDefaultReminders() }
|
||||
update { it.copy(listId = listId, parentId = null, errors = emptySet()).withDatedReminders() }
|
||||
viewModelScope.launch {
|
||||
val candidates = loadParents(listId, selfId = editingTaskId)
|
||||
update { it.copy(parentCandidates = candidates) }
|
||||
}
|
||||
}
|
||||
|
||||
fun onStartChange(value: Instant?) = update { it.copy(start = value).withDefaultReminders() }
|
||||
fun onDueChange(value: Instant?) = update { it.copy(due = value).withDefaultReminders() }
|
||||
fun onStartChange(value: Instant?) = update {
|
||||
val next = it.copy(start = value)
|
||||
next.withDatedReminders(lostDates = it.hasDates && !next.hasDates)
|
||||
}
|
||||
|
||||
fun onDueChange(value: Instant?) = update {
|
||||
val next = it.copy(due = value)
|
||||
next.withDatedReminders(lostDates = it.hasDates && !next.hasDates)
|
||||
}
|
||||
/**
|
||||
* All-day and timed values use different conventions (UTC midnight vs. a real
|
||||
* instant in the device zone), so the switch has to move the timestamps too —
|
||||
@@ -333,10 +354,7 @@ class TaskEditViewModel @Inject constructor(
|
||||
start = it.start?.rebasedForAllDay(value),
|
||||
due = it.due?.rebasedForAllDay(value),
|
||||
)
|
||||
if (!it.remindersTouched) {
|
||||
loadedReminders = carried(loadedReminders, switched)
|
||||
return@update switched.withDefaultReminders()
|
||||
}
|
||||
if (!it.remindersTouched) return@update switched.withDatedReminders()
|
||||
switched.copy(reminders = carried(it.reminders, switched).sortedForDisplay(switched))
|
||||
}
|
||||
|
||||
@@ -346,12 +364,40 @@ class TaskEditViewModel @Inject constructor(
|
||||
val carried = if (previous != null && previous.second == reminders) {
|
||||
previous.first
|
||||
} else {
|
||||
StoredReminders.switched(reminders, switched.toForm(), settings, nowMillis(), ZoneId.systemDefault())
|
||||
switchedFor(reminders, switched)
|
||||
}
|
||||
lastSwitch = reminders to carried
|
||||
return carried
|
||||
}
|
||||
|
||||
/** [reminders] made [state]'s kind, timed or all-day. */
|
||||
private fun switchedFor(reminders: List<TaskReminder>, state: TaskEditUiState): List<TaskReminder> =
|
||||
StoredReminders.switched(reminders, state.toForm(), settings, nowMillis(), ZoneId.systemDefault())
|
||||
|
||||
/**
|
||||
* [withDefaultReminders] for a task that may have lost its dates: with none
|
||||
* left its reminders have nothing to count from and go (#159). Those the user
|
||||
* picked are set aside when the last date goes and come back with the next
|
||||
* one; untouched ones come back from [loadedReminders] anyway. Reminders
|
||||
* picked while the task has no date stay, for the form to point out.
|
||||
*/
|
||||
private fun TaskEditUiState.withDatedReminders(lostDates: Boolean = false): TaskEditUiState {
|
||||
if (!hasDates) {
|
||||
if (!remindersTouched) return if (reminders.isEmpty()) this else withoutReminders()
|
||||
if (!lostDates || reminders.isEmpty()) return this
|
||||
setAside += SetAside(reminders, isAllDay)
|
||||
return withoutReminders()
|
||||
}
|
||||
if (loadedKindAllDay != isAllDay) {
|
||||
loadedReminders = carried(loadedReminders, this)
|
||||
loadedKindAllDay = isAllDay
|
||||
}
|
||||
if (setAside.isEmpty()) return withDefaultReminders()
|
||||
val restored = setAside.flatMap { if (it.allDay == isAllDay) it.reminders else switchedFor(it.reminders, this) }
|
||||
setAside.clear()
|
||||
return copy(reminders = (reminders + restored).distinct().sortedForDisplay(this), remindersChosen = true)
|
||||
}
|
||||
|
||||
/**
|
||||
* The reminders the task will be saved with while the user has not
|
||||
* touched them: those it was loaded with, plus the defaults of the dates
|
||||
@@ -473,6 +519,14 @@ class TaskEditViewModel @Inject constructor(
|
||||
private fun List<TaskReminder>.sortedForDisplay(state: TaskEditUiState): List<TaskReminder> =
|
||||
sortedWith(taskReminderDisplayOrder(state.isAllDay, state.allDayMinuteOfDay))
|
||||
|
||||
private val TaskEditUiState.hasDates: Boolean get() = start != null || due != null
|
||||
|
||||
/** No reminders shown; saved, the task keeps none of its own without turning reminders off. */
|
||||
private fun TaskEditUiState.withoutReminders(): TaskEditUiState = copy(reminders = emptyList(), remindersChosen = false)
|
||||
|
||||
/** Reminders put away while the task has no date, as the kind they were picked in. */
|
||||
private class SetAside(val reminders: List<TaskReminder>, val allDay: Boolean)
|
||||
|
||||
/** Snapshot the editable state as a validated [TaskForm]. */
|
||||
private fun TaskEditUiState.toForm(): TaskForm = TaskForm(
|
||||
title = title,
|
||||
|
||||
@@ -0,0 +1,223 @@
|
||||
package de.jeanlucmakiola.agendula.ui.edit
|
||||
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.emptyPreferences
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs
|
||||
import de.jeanlucmakiola.agendula.data.tasks.ProviderEnvironment
|
||||
import de.jeanlucmakiola.agendula.data.tasks.ProviderResolver
|
||||
import de.jeanlucmakiola.agendula.data.tasks.TasksRepository
|
||||
import de.jeanlucmakiola.agendula.domain.Priority
|
||||
import de.jeanlucmakiola.agendula.domain.Task
|
||||
import de.jeanlucmakiola.agendula.domain.TaskDetail
|
||||
import de.jeanlucmakiola.agendula.domain.TaskFilter
|
||||
import de.jeanlucmakiola.agendula.domain.TaskForm
|
||||
import de.jeanlucmakiola.agendula.domain.TaskList
|
||||
import de.jeanlucmakiola.agendula.domain.TaskReminder
|
||||
import de.jeanlucmakiola.agendula.domain.TaskStatus
|
||||
import de.jeanlucmakiola.agendula.domain.recurrence.RecurringScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.lang.reflect.Proxy
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Duration.Companion.days
|
||||
import kotlin.time.Instant
|
||||
|
||||
/** Reminders on a task that loses or regains its dates in the editor (#159). */
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class TaskEditViewModelTest {
|
||||
|
||||
private val due = Clock.System.now() + 3.days
|
||||
private val reminder = TaskReminder(30)
|
||||
private val repository = FakeRepository()
|
||||
|
||||
@BeforeEach fun setUp() = Dispatchers.setMain(UnconfinedTestDispatcher())
|
||||
|
||||
@AfterEach fun tearDown() = Dispatchers.resetMain()
|
||||
|
||||
private fun editing(
|
||||
start: Instant? = null,
|
||||
due: Instant? = this.due,
|
||||
reminders: List<TaskReminder> = listOf(reminder),
|
||||
): TaskEditViewModel {
|
||||
repository.task = task(start, due)
|
||||
repository.reminders = reminders
|
||||
return TaskEditViewModel(repository, settingsPrefs()).apply { bindEdit(TASK_ID) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `clearing the last date drops the reminders and the task saves`() {
|
||||
val vm = editing()
|
||||
vm.onDueChange(null)
|
||||
vm.save()
|
||||
|
||||
assertThat(vm.state.value.errors).isEmpty()
|
||||
assertThat(repository.saved?.reminders).isEmpty()
|
||||
// Empty but not chosen: no reminders of its own, without turning the default off for good.
|
||||
assertThat(repository.saved?.remindersChosen).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `untouched reminders survive an all-day switch while the task has no date`() {
|
||||
val vm = editing(reminders = listOf(TaskReminder(0)))
|
||||
vm.onDueChange(null)
|
||||
vm.onAllDayChange(true)
|
||||
vm.onDueChange(due)
|
||||
|
||||
assertThat(vm.state.value.reminders).isNotEmpty()
|
||||
assertThat(vm.state.value.reminders.all { it.allDay.minuteOfDay != null || it.minutesBefore % (24 * 60) == 0 })
|
||||
.isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a reminder added without a date stays, and the set-aside ones come back with it`() {
|
||||
val vm = editing()
|
||||
val picked = TaskReminder(120)
|
||||
val added = TaskReminder(240)
|
||||
vm.addReminder(picked)
|
||||
vm.onDueChange(null)
|
||||
vm.addReminder(added)
|
||||
vm.onListChange(LIST_ID + 1)
|
||||
|
||||
assertThat(vm.state.value.reminders).containsExactly(added)
|
||||
|
||||
vm.onDueChange(due)
|
||||
assertThat(vm.state.value.reminders).containsExactly(reminder, picked, added)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `reminders stored on a task without dates do not come back with a date`() {
|
||||
val vm = editing(due = null)
|
||||
vm.onDueChange(due)
|
||||
|
||||
assertThat(vm.state.value.reminders).doesNotContain(reminder)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an all-day switch or another list does not bring them back`() {
|
||||
val vm = editing()
|
||||
vm.onDueChange(null)
|
||||
vm.onAllDayChange(true)
|
||||
vm.onListChange(LIST_ID + 1)
|
||||
|
||||
assertThat(vm.state.value.reminders).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `untouched reminders come back with a new date`() {
|
||||
val vm = editing()
|
||||
vm.onDueChange(null)
|
||||
vm.onDueChange(due + 1.days)
|
||||
|
||||
assertThat(vm.state.value.reminders).contains(reminder)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `picked reminders come back with a new date`() {
|
||||
val vm = editing()
|
||||
val picked = TaskReminder(120)
|
||||
vm.addReminder(picked)
|
||||
vm.onDueChange(null)
|
||||
vm.onDueChange(due + 1.days)
|
||||
|
||||
assertThat(vm.state.value.reminders).containsExactly(reminder, picked)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `clearing one of two dates keeps the reminders`() {
|
||||
val vm = editing(start = due - 1.days)
|
||||
vm.onDueChange(null)
|
||||
|
||||
assertThat(vm.state.value.reminders).containsExactly(reminder)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a task stored without dates opens without its reminders`() {
|
||||
val vm = editing(due = null)
|
||||
|
||||
assertThat(vm.state.value.reminders).isEmpty()
|
||||
assertThat(vm.state.value.isDirty).isFalse()
|
||||
}
|
||||
|
||||
private fun task(start: Instant?, due: Instant?) = Task(
|
||||
taskId = TASK_ID,
|
||||
listId = LIST_ID,
|
||||
title = "Task",
|
||||
description = null,
|
||||
location = null,
|
||||
url = null,
|
||||
priority = Priority.NONE,
|
||||
status = TaskStatus.NEEDS_ACTION,
|
||||
percentComplete = null,
|
||||
start = start,
|
||||
due = due,
|
||||
isAllDay = false,
|
||||
timeZone = null,
|
||||
completedAt = null,
|
||||
listColor = 0,
|
||||
taskColor = null,
|
||||
listName = null,
|
||||
accountName = null,
|
||||
parentId = null,
|
||||
isRecurring = false,
|
||||
distanceFromCurrent = null,
|
||||
created = null,
|
||||
lastModified = null,
|
||||
)
|
||||
|
||||
private class FakeRepository : TasksRepository by unsupported() {
|
||||
var task: Task? = null
|
||||
var reminders: List<TaskReminder> = emptyList()
|
||||
var saved: TaskForm? = null
|
||||
|
||||
override fun taskLists() = flowOf(emptyList<TaskList>())
|
||||
override fun tasks(filter: TaskFilter): Flow<List<Task>> = flowOf(emptyList())
|
||||
override fun taskDetail(taskId: Long, occurrenceStart: Instant?) =
|
||||
flowOf(task?.let { TaskDetail(it, emptyList()) })
|
||||
override suspend fun remindersFor(taskId: Long) = reminders
|
||||
override suspend fun updateTask(
|
||||
taskId: Long,
|
||||
form: TaskForm,
|
||||
expectedLastModified: Instant?,
|
||||
occurrenceStart: Instant?,
|
||||
scope: RecurringScope,
|
||||
) {
|
||||
saved = form
|
||||
}
|
||||
}
|
||||
|
||||
private class MemoryDataStore : DataStore<Preferences> {
|
||||
override val data = MutableStateFlow(emptyPreferences())
|
||||
override suspend fun updateData(transform: suspend (t: Preferences) -> Preferences) =
|
||||
transform(data.value).also { data.value = it }
|
||||
}
|
||||
|
||||
private object NoProviders : ProviderEnvironment {
|
||||
override fun packageDeclaring(authority: String): String? = null
|
||||
override fun isGranted(permission: String) = false
|
||||
override fun appLabel(packageName: String): String? = null
|
||||
}
|
||||
|
||||
private fun settingsPrefs() = SettingsPrefs(MemoryDataStore(), ProviderResolver(NoProviders))
|
||||
|
||||
private companion object {
|
||||
const val TASK_ID = 1L
|
||||
const val LIST_ID = 7L
|
||||
|
||||
/** Fails any call the test does not expect. */
|
||||
inline fun <reified T> unsupported(): T =
|
||||
Proxy.newProxyInstance(T::class.java.classLoader, arrayOf(T::class.java)) { _, method, _ ->
|
||||
throw UnsupportedOperationException("Unexpected call: ${method.name}")
|
||||
} as T
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user