@@ -276,45 +276,51 @@ class TaskRepository(
276276 val remoteTasks = withContext(Dispatchers .IO ) {
277277 tasksApi.listAll(remoteListId, showHidden = true , showCompleted = true )
278278 }
279- remoteTasks.onEach { remoteTask ->
279+
280+ val taskEntities = remoteTasks.map { remoteTask ->
280281 val existingEntity = taskDao.getByRemoteId(remoteTask.id)
281- taskDao.upsert( remoteTask.asTaskEntity(localListId, existingEntity?.id) )
282+ remoteTask.asTaskEntity(localListId, existingEntity?.id)
282283 }
284+ taskDao.upsertAll(taskEntities)
285+
283286 taskDao.deleteStaleTasks(localListId, remoteTasks.map(Task ::id))
287+
284288 val localOnlyTasks = taskDao.getLocalOnlyTasks(localListId)
285289 val sortedRootTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == null })
286290 var previousTaskId: String? = null
291+ val syncedTasks = mutableListOf<TaskEntity >()
287292 sortedRootTasks.onEach { localRootTask ->
288- val remoteTask = syncLocalTask(localListId, remoteListId, localRootTask, null , previousTaskId)
289- val sortedSubTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == localRootTask.id })
290- var previousSubTaskId: String? = null
291- sortedSubTasks.onEach { localSubTask ->
292- val remoteSubTask = syncLocalTask(localListId, remoteListId, localSubTask, remoteTask?.id, previousSubTaskId)
293- previousSubTaskId = remoteSubTask?.id
293+ val remoteRootTask = withContext(Dispatchers .IO ) {
294+ try {
295+ tasksApi.insert(remoteListId, localRootTask.asTask(), null , previousTaskId).also {
296+ syncedTasks.add(it.asTaskEntity(localListId, localRootTask.id))
297+ }
298+ } catch (e: Exception ) {
299+ null
300+ }
294301 }
295- previousTaskId = remoteTask?.id
296- }
297- }
298- }
299302
300- private suspend fun syncLocalTask (
301- localTaskListId : Long ,
302- remoteTaskListId : String ,
303- localTask : TaskEntity ,
304- parentTaskId : String? ,
305- previousTaskId : String?
306- ): Task ? {
307- val remoteTask = withContext(Dispatchers .IO ) {
308- try {
309- tasksApi.insert(remoteTaskListId, localTask.asTask(), parentTaskId, previousTaskId)
310- } catch (e: Exception ) {
311- null
303+ // don't try syncing sub tasks if root task failed, it would break hierarchy on remote side
304+ if (remoteRootTask != null ) {
305+ val sortedSubTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == localRootTask.id })
306+ var previousSubTaskId: String? = null
307+ sortedSubTasks.onEach { localSubTask ->
308+ val remoteSubTask = withContext(Dispatchers .IO ) {
309+ try {
310+ tasksApi.insert(remoteListId, localSubTask.asTask(), remoteRootTask.id, previousSubTaskId).also {
311+ syncedTasks.add(it.asTaskEntity(localListId, localSubTask.id))
312+ }
313+ } catch (e: Exception ) {
314+ null
315+ }
316+ }
317+ previousSubTaskId = remoteSubTask?.id
318+ }
319+ }
320+ previousTaskId = remoteRootTask?.id
312321 }
322+ taskDao.upsertAll(syncedTasks)
313323 }
314- if (remoteTask != null ) {
315- taskDao.upsert(remoteTask.asTaskEntity(localTaskListId, localTask.id))
316- }
317- return remoteTask
318324 }
319325
320326 suspend fun createTaskList (title : String ) {
0 commit comments