Skip to content

Commit

Permalink
Use upsertAll after sync instead of several individual inserts
Browse files Browse the repository at this point in the history
  • Loading branch information
opatry committed Nov 5, 2024
1 parent 5176462 commit 5412239
Showing 1 changed file with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,45 +276,51 @@ class TaskRepository(
val remoteTasks = withContext(Dispatchers.IO) {
tasksApi.listAll(remoteListId, showHidden = true, showCompleted = true)
}
remoteTasks.onEach { remoteTask ->

val taskEntities = remoteTasks.map { remoteTask ->
val existingEntity = taskDao.getByRemoteId(remoteTask.id)
taskDao.upsert(remoteTask.asTaskEntity(localListId, existingEntity?.id))
remoteTask.asTaskEntity(localListId, existingEntity?.id)
}
taskDao.upsertAll(taskEntities)

taskDao.deleteStaleTasks(localListId, remoteTasks.map(Task::id))

val localOnlyTasks = taskDao.getLocalOnlyTasks(localListId)
val sortedRootTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == null })
var previousTaskId: String? = null
val syncedTasks = mutableListOf<TaskEntity>()
sortedRootTasks.onEach { localRootTask ->
val remoteTask = syncLocalTask(localListId, remoteListId, localRootTask, null, previousTaskId)
val sortedSubTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == localRootTask.id })
var previousSubTaskId: String? = null
sortedSubTasks.onEach { localSubTask ->
val remoteSubTask = syncLocalTask(localListId, remoteListId, localSubTask, remoteTask?.id, previousSubTaskId)
previousSubTaskId = remoteSubTask?.id
val remoteRootTask = withContext(Dispatchers.IO) {
try {
tasksApi.insert(remoteListId, localRootTask.asTask(), null, previousTaskId).also {
syncedTasks.add(it.asTaskEntity(localListId, localRootTask.id))
}
} catch (e: Exception) {
null
}
}
previousTaskId = remoteTask?.id
}
}
}

private suspend fun syncLocalTask(
localTaskListId: Long,
remoteTaskListId: String,
localTask: TaskEntity,
parentTaskId: String?,
previousTaskId: String?
): Task? {
val remoteTask = withContext(Dispatchers.IO) {
try {
tasksApi.insert(remoteTaskListId, localTask.asTask(), parentTaskId, previousTaskId)
} catch (e: Exception) {
null
// don't try syncing sub tasks if root task failed, it would break hierarchy on remote side
if (remoteRootTask != null) {
val sortedSubTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == localRootTask.id })
var previousSubTaskId: String? = null
sortedSubTasks.onEach { localSubTask ->
val remoteSubTask = withContext(Dispatchers.IO) {
try {
tasksApi.insert(remoteListId, localSubTask.asTask(), remoteRootTask.id, previousSubTaskId).also {
syncedTasks.add(it.asTaskEntity(localListId, localSubTask.id))
}
} catch (e: Exception) {
null
}
}
previousSubTaskId = remoteSubTask?.id
}
}
previousTaskId = remoteRootTask?.id
}
taskDao.upsertAll(syncedTasks)
}
if (remoteTask != null) {
taskDao.upsert(remoteTask.asTaskEntity(localTaskListId, localTask.id))
}
return remoteTask
}

suspend fun createTaskList(title: String) {
Expand Down

0 comments on commit 5412239

Please sign in to comment.