@@ -280,45 +280,51 @@ class TaskRepository(
280280 val remoteTasks = withContext(Dispatchers .IO ) {
281281 tasksApi.listAll(remoteListId, showHidden = true , showCompleted = true )
282282 }
283- remoteTasks.onEach { remoteTask ->
283+
284+ val taskEntities = remoteTasks.map { remoteTask ->
284285 val existingEntity = taskDao.getByRemoteId(remoteTask.id)
285- taskDao.upsert( remoteTask.asTaskEntity(localListId, existingEntity?.id) )
286+ remoteTask.asTaskEntity(localListId, existingEntity?.id)
286287 }
288+ taskDao.upsertAll(taskEntities)
289+
287290 taskDao.deleteStaleTasks(localListId, remoteTasks.map(Task ::id))
291+
288292 val localOnlyTasks = taskDao.getLocalOnlyTasks(localListId)
289293 val sortedRootTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == null })
290294 var previousTaskId: String? = null
295+ val syncedTasks = mutableListOf<TaskEntity >()
291296 sortedRootTasks.onEach { localRootTask ->
292- val remoteTask = syncLocalTask(localListId, remoteListId, localRootTask, null , previousTaskId)
293- val sortedSubTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == localRootTask.id })
294- var previousSubTaskId: String? = null
295- sortedSubTasks.onEach { localSubTask ->
296- val remoteSubTask = syncLocalTask(localListId, remoteListId, localSubTask, remoteTask?.id, previousSubTaskId)
297- previousSubTaskId = remoteSubTask?.id
297+ val remoteRootTask = withContext(Dispatchers .IO ) {
298+ try {
299+ tasksApi.insert(remoteListId, localRootTask.asTask(), null , previousTaskId).also {
300+ syncedTasks.add(it.asTaskEntity(localListId, localRootTask.id))
301+ }
302+ } catch (e: Exception ) {
303+ null
304+ }
298305 }
299- previousTaskId = remoteTask?.id
300- }
301- }
302- }
303306
304- private suspend fun syncLocalTask (
305- localTaskListId : Long ,
306- remoteTaskListId : String ,
307- localTask : TaskEntity ,
308- parentTaskId : String? ,
309- previousTaskId : String?
310- ): Task ? {
311- val remoteTask = withContext(Dispatchers .IO ) {
312- try {
313- tasksApi.insert(remoteTaskListId, localTask.asTask(), parentTaskId, previousTaskId)
314- } catch (e: Exception ) {
315- null
307+ // don't try syncing sub tasks if root task failed, it would break hierarchy on remote side
308+ if (remoteRootTask != null ) {
309+ val sortedSubTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == localRootTask.id })
310+ var previousSubTaskId: String? = null
311+ sortedSubTasks.onEach { localSubTask ->
312+ val remoteSubTask = withContext(Dispatchers .IO ) {
313+ try {
314+ tasksApi.insert(remoteListId, localSubTask.asTask(), remoteRootTask.id, previousSubTaskId).also {
315+ syncedTasks.add(it.asTaskEntity(localListId, localSubTask.id))
316+ }
317+ } catch (e: Exception ) {
318+ null
319+ }
320+ }
321+ previousSubTaskId = remoteSubTask?.id
322+ }
323+ }
324+ previousTaskId = remoteRootTask?.id
316325 }
326+ taskDao.upsertAll(syncedTasks)
317327 }
318- if (remoteTask != null ) {
319- taskDao.upsert(remoteTask.asTaskEntity(localTaskListId, localTask.id))
320- }
321- return remoteTask
322328 }
323329
324330 suspend fun createTaskList (title : String ) {
0 commit comments