From 8895acbf6b86d0b8d5d762e5ad632efddae47deb Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Mon, 6 Nov 2023 01:16:09 -0600 Subject: [PATCH] Prevent flashing empty inbox when switching lists --- .../astrid/activity/TaskListFragment.kt | 20 ++--- .../java/org/tasks/compose/edit/SubtaskRow.kt | 81 ++++++++++++------- .../java/org/tasks/ui/TaskListViewModel.kt | 9 ++- 3 files changed, 71 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt index 78305e9d6..643131e91 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt @@ -280,16 +280,18 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL lifecycleScope.launch { viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { listViewModel.state.collect { - submitList(it.tasks) - if (it.tasks.isEmpty()) { - swipeRefreshLayout.visibility = View.GONE - emptyRefreshLayout.visibility = View.VISIBLE - } else { - swipeRefreshLayout.visibility = View.VISIBLE - emptyRefreshLayout.visibility = View.GONE + if (it.tasks is TaskListViewModel.TasksResults.Results) { + submitList(it.tasks.tasks) + if (it.tasks.tasks.isEmpty()) { + swipeRefreshLayout.visibility = View.GONE + emptyRefreshLayout.visibility = View.VISIBLE + } else { + swipeRefreshLayout.visibility = View.VISIBLE + emptyRefreshLayout.visibility = View.GONE + } + swipeRefreshLayout.isRefreshing = it.syncOngoing + emptyRefreshLayout.isRefreshing = it.syncOngoing } - swipeRefreshLayout.isRefreshing = it.syncOngoing - emptyRefreshLayout.isRefreshing = it.syncOngoing } } } diff --git a/app/src/main/java/org/tasks/compose/edit/SubtaskRow.kt b/app/src/main/java/org/tasks/compose/edit/SubtaskRow.kt index 293953f49..1b4e5fcb0 100644 --- a/app/src/main/java/org/tasks/compose/edit/SubtaskRow.kt +++ b/app/src/main/java/org/tasks/compose/edit/SubtaskRow.kt @@ -3,14 +3,24 @@ package org.tasks.compose.edit import android.content.res.Configuration import androidx.compose.foundation.clickable import androidx.compose.foundation.focusable -import androidx.compose.foundation.layout.* +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width import androidx.compose.foundation.text.BasicTextField import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material.ContentAlpha import androidx.compose.material.MaterialTheme import androidx.compose.material.Text -import androidx.compose.runtime.* +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha @@ -27,8 +37,14 @@ import com.google.android.material.composethemeadapter.MdcTheme import com.todoroo.astrid.api.Filter import com.todoroo.astrid.api.GtasksFilter import com.todoroo.astrid.data.Task -import org.tasks.compose.* +import org.tasks.compose.CheckBox +import org.tasks.compose.ClearButton +import org.tasks.compose.DisabledText +import org.tasks.compose.SubtaskChip +import org.tasks.compose.TaskEditIcon +import org.tasks.compose.TaskEditRow import org.tasks.data.TaskContainer +import org.tasks.ui.TaskListViewModel @Composable fun SubtaskRow( @@ -36,7 +52,7 @@ fun SubtaskRow( filter: Filter?, hasParent: Boolean, desaturate: Boolean, - existingSubtasks: List, + existingSubtasks: TaskListViewModel.TasksResults, newSubtasks: List, openSubtask: (Task) -> Unit, completeExistingSubtask: (Task, Boolean) -> Unit, @@ -73,15 +89,22 @@ fun SubtaskRow( ) } else { Spacer(modifier = Modifier.height(height = 8.dp)) - existingSubtasks.forEach { task -> - ExistingSubtaskRow( - task = task, - desaturate = desaturate, - indent = if (filter !is GtasksFilter) task.indent else 0, - onRowClick = { openSubtask(task.task) }, - onCompleteClick = { completeExistingSubtask(task.task, !task.isCompleted) }, - onToggleSubtaskClick = { toggleSubtask(task.id, !task.isCollapsed) } - ) + if (existingSubtasks is TaskListViewModel.TasksResults.Results) { + existingSubtasks.tasks.forEach { task -> + ExistingSubtaskRow( + task = task, + desaturate = desaturate, + indent = if (filter !is GtasksFilter) task.indent else 0, + onRowClick = { openSubtask(task.task) }, + onCompleteClick = { + completeExistingSubtask( + task.task, + !task.isCompleted + ) + }, + onToggleSubtaskClick = { toggleSubtask(task.id, !task.isCollapsed) } + ) + } } newSubtasks.forEach { subtask -> NewSubtaskRow( @@ -214,7 +237,7 @@ fun NoSubtasks() { filter = null, hasParent = false, desaturate = true, - existingSubtasks = emptyList(), + existingSubtasks = TaskListViewModel.TasksResults.Results(emptyList()), newSubtasks = emptyList(), openSubtask = {}, completeExistingSubtask = { _, _ -> }, @@ -236,20 +259,22 @@ fun SubtasksPreview() { filter = null, hasParent = false, desaturate = true, - existingSubtasks = listOf( - TaskContainer( - task = Task().apply { - title = "Existing subtask 1" - priority = Task.Priority.HIGH - }, - indent = 0 - ), - TaskContainer( - task = Task().apply { - title = "Existing subtask 2 with a really long title" - priority = Task.Priority.LOW - }, - indent = 1 + existingSubtasks = TaskListViewModel.TasksResults.Results( + listOf( + TaskContainer( + task = Task( + title = "Existing subtask 1", + priority = Task.Priority.HIGH, + ), + indent = 0 + ), + TaskContainer( + task = Task( + title = "Existing subtask 2 with a really long title", + priority = Task.Priority.LOW, + ), + indent = 1 + ) ) ), newSubtasks = listOf( diff --git a/app/src/main/java/org/tasks/ui/TaskListViewModel.kt b/app/src/main/java/org/tasks/ui/TaskListViewModel.kt index 748ff2a47..765489be4 100644 --- a/app/src/main/java/org/tasks/ui/TaskListViewModel.kt +++ b/app/src/main/java/org/tasks/ui/TaskListViewModel.kt @@ -47,11 +47,16 @@ class TaskListViewModel @Inject constructor( private val firebase: Firebase, ) : ViewModel() { + sealed interface TasksResults { + data object Loading : TasksResults + data class Results(val tasks: List) : TasksResults + } + data class State( val filter: Filter? = null, val now: Long = DateUtilities.now(), val searchQuery: String? = null, - val tasks: List = emptyList(), + val tasks: TasksResults = TasksResults.Loading, val begForSubscription: Boolean = false, val syncOngoing: Boolean = false, ) @@ -116,7 +121,7 @@ class TaskListViewModel @Inject constructor( } .onEach { tasks -> _state.update { - it.copy(tasks = tasks) + it.copy(tasks = TasksResults.Results(tasks)) } } .flowOn(Dispatchers.Default)