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:
@@ -16,7 +16,7 @@ android {
|
||||
minSdk = 26
|
||||
targetSdk = 35
|
||||
versionCode = 1
|
||||
versionName = "0.0.10"
|
||||
versionName = "0.0.11"
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
// 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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user