fix: crash FOREIGN KEY lors de la création de tâche avant sync

Si les projets ne sont pas encore en base Room au moment de l'insertion
(première connexion, sync en cours), vérifie que le projet existe avant
d'insérer la tâche localement. Sinon, déclenche une sync — la tâche est
déjà créée sur l'API et apparaîtra après rafraîchissement.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-06 09:46:42 +02:00
parent b268fc13c5
commit 221cf4f80d
2 changed files with 15 additions and 4 deletions
@@ -7,6 +7,7 @@ import com.planify.mobile.data.bonsai.BonsaiApiClient
import com.planify.mobile.data.bonsai.BonsaiAuthManager
import com.planify.mobile.data.bonsai.BonsaiSyncManager
import com.planify.mobile.data.bonsai.dto.BonsaIssueRequest
import com.planify.mobile.domain.repository.ProjectRepository
import com.planify.mobile.data.notification.ReminderScheduler
import com.planify.mobile.domain.model.DueDate
import com.planify.mobile.domain.model.Reminder
@@ -47,6 +48,8 @@ class TaskEditViewModel @Inject constructor(
private val reminderScheduler: ReminderScheduler,
private val apiClient: BonsaiApiClient,
private val authManager: BonsaiAuthManager,
private val syncManager: BonsaiSyncManager,
private val projectRepository: ProjectRepository,
) : ViewModel() {
private val _state = MutableStateFlow(TaskEditState())
@@ -147,9 +150,17 @@ class TaskEditViewModel @Inject constructor(
addedAt = now,
updatedAt = now,
)
if (st.taskId == null) taskRepository.insertTask(task)
else taskRepository.updateTask(task)
saveReminders(task.id, st, task)
// Insert locally only if the project already exists in Room.
// If not (sync not yet complete), the next sync will populate both.
val projectExists = projectRepository.getProjectById(task.projectId) != null
if (projectExists) {
if (st.taskId == null) taskRepository.insertTask(task)
else taskRepository.updateTask(task)
saveReminders(task.id, st, task)
} else {
// Sync will bring the task; trigger it now.
viewModelScope.launch { syncManager.sync() }
}
_state.update { it.copy(isSaving = false) }
onDone()
}