Use recursive queries if filter can be sorted

pull/2332/head
Alex Baker 2 years ago
parent 7945160536
commit d4192803f8

@ -13,7 +13,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import org.tasks.LocalBroadcastManager
import org.tasks.data.SubtaskInfo
import org.tasks.data.TaskContainer
import org.tasks.data.TaskDao
import org.tasks.date.DateTimeUtils.isAfterNow
@ -151,7 +150,7 @@ class TaskDao @Inject constructor(
internal suspend fun insert(task: Task): Long = taskDao.insert(task)
internal suspend fun fetchTasks(callback: suspend (SubtaskInfo) -> List<String>): List<TaskContainer> =
internal suspend fun fetchTasks(callback: suspend () -> List<String>): List<TaskContainer> =
taskDao.fetchTasks(callback)
internal suspend fun getAll(): List<Task> = taskDao.getAll()

@ -1,8 +0,0 @@
package org.tasks.data;
public class SubtaskInfo {
public boolean hasSubtasks;
public boolean usesSubtasks() {
return hasSubtasks;
}
}

@ -95,14 +95,10 @@ abstract class TaskDao(private val database: Database) {
+ "WHERE completed > 0 AND calendarUri IS NOT NULL AND calendarUri != ''")
abstract suspend fun clearCompletedCalendarEvents(): Int
open suspend fun fetchTasks(callback: suspend (SubtaskInfo) -> List<String>): List<TaskContainer> {
return fetchTasks(getSubtaskInfo(), callback)
}
open suspend fun fetchTasks(subtasks: SubtaskInfo, callback: suspend (SubtaskInfo) -> List<String>): List<TaskContainer> =
open suspend fun fetchTasks(callback: suspend () -> List<String>): List<TaskContainer> =
database.withTransaction {
val start = if (BuildConfig.DEBUG) now() else 0
val queries = callback(subtasks)
val queries = callback()
val last = queries.size - 1
for (i in 0 until last) {
query(SimpleSQLiteQuery(queries[i]))
@ -114,7 +110,7 @@ abstract class TaskDao(private val database: Database) {
suspend fun fetchTasks(preferences: Preferences, filter: Filter): List<TaskContainer> =
fetchTasks {
TaskListQuery.getQuery(preferences, filter, it)
TaskListQuery.getQuery(preferences, filter)
}
@RawQuery
@ -126,9 +122,6 @@ abstract class TaskDao(private val database: Database) {
@RawQuery
abstract suspend fun count(query: SimpleSQLiteQuery): Int
@Query("SELECT EXISTS(SELECT 1 FROM tasks WHERE parent > 0 AND deleted = 0) AS hasSubtasks")
abstract suspend fun getSubtaskInfo(): SubtaskInfo
suspend fun touch(ids: List<Long>, now: Long = currentTimeMillis()) =
ids.eachChunk { internalTouch(it, now) }

@ -33,13 +33,12 @@ object TaskListQuery {
fun getQuery(
preferences: QueryPreferences,
filter: Filter,
subtasks: SubtaskInfo
): MutableList<String> = when {
filter.supportsManualSort() && preferences.isManualSort ->
getRecursiveQuery(filter, preferences)
filter.supportsAstridSorting() && preferences.isAstridSort ->
getNonRecursiveQuery(filter, preferences)
filter.supportsSubtasks() && subtasks.usesSubtasks() ->
filter.supportsSorting() ->
getRecursiveQuery(filter, preferences)
else -> getNonRecursiveQuery(filter, preferences)
}

@ -9,7 +9,6 @@ import com.todoroo.andlib.utility.AndroidUtilities
import com.todoroo.astrid.api.Filter
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import org.tasks.data.SubtaskInfo
import org.tasks.data.TaskContainer
import org.tasks.data.TaskDao
import org.tasks.data.TaskListQuery.getQuery
@ -53,18 +52,13 @@ class TaskListViewModel @Inject constructor(
}
try {
viewModelScope.launch {
val subtasks = taskDao.getSubtaskInfo()
performNonPagedQuery(subtasks)
_tasks.value = taskDao.fetchTasks { getQuery(preferences, filter!!) }
}
} catch (e: Exception) {
Timber.e(e)
}
}
private suspend fun performNonPagedQuery(subtasks: SubtaskInfo) {
_tasks.value = taskDao.fetchTasks(subtasks) { getQuery(preferences, filter!!, it) }
}
val value: List<TaskContainer>
get() = _tasks.value ?: emptyList()
}

@ -18,7 +18,6 @@ import kotlinx.coroutines.runBlocking
import org.tasks.BuildConfig
import org.tasks.LocalBroadcastManager
import org.tasks.R
import org.tasks.data.SubtaskInfo
import org.tasks.data.TaskContainer
import org.tasks.data.TaskDao
import org.tasks.data.TaskListQuery.getQuery
@ -99,7 +98,7 @@ internal class ScrollableViewsFactory(
runBlocking {
updateSettings()
tasks = SectionedDataSource(
taskDao.fetchTasks { getQuery(filter, it) },
taskDao.fetchTasks { getQuery(filter) },
disableGroups,
groupMode,
collapsed,
@ -295,8 +294,8 @@ internal class ScrollableViewsFactory(
private fun getTask(position: Int): TaskContainer? = tasks.getItem(position)
private suspend fun getQuery(filter: Filter?, subtasks: SubtaskInfo): List<String> {
val queries = getQuery(widgetPreferences, filter!!, subtasks)
private suspend fun getQuery(filter: Filter?): List<String> {
val queries = getQuery(widgetPreferences, filter!!)
val last = queries.size - 1
queries[last] =
subtasksHelper.applySubtasksToWidgetFilter(filter, widgetPreferences, queries[last])

Loading…
Cancel
Save