Skip to content

Commit

Permalink
fix(#133): gives a little chance to the progress indicator to be disp…
Browse files Browse the repository at this point in the history
…layed when synchronizing observation records
  • Loading branch information
sgrimault committed Mar 22, 2023
1 parent d0d9a70 commit 24b77ab
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import fr.geonature.occtax.features.record.domain.ObservationRecord
import fr.geonature.occtax.features.record.domain.SynchronizationStatus
import fr.geonature.occtax.features.record.repository.IObservationRecordRepository
import fr.geonature.occtax.features.record.repository.ISynchronizeObservationRecordRepository
import kotlinx.coroutines.delay
import org.tinylog.Logger
import java.util.Date
import java.util.UUID
Expand Down Expand Up @@ -54,19 +55,19 @@ class SynchronizeObservationRecordsWorker @AssistedInject constructor(
return Result.retry()
}

val observationRecordToSynchronize = observationRecordRepository.readAll()
val observationRecordsToSynchronize = observationRecordRepository.readAll()
.getOrDefault(emptyList())
.filter { it.status == ObservationRecord.Status.TO_SYNC }

if (observationRecordToSynchronize.isEmpty()) {
if (observationRecordsToSynchronize.isEmpty()) {
Logger.info { "no observation records to synchronize" }

return Result.success(workData(state = WorkInfo.State.SUCCEEDED))
}

val observationRecordsSynchronized = mutableListOf<ObservationRecord>()

observationRecordToSynchronize.forEach { observationRecordToSync ->
observationRecordsToSynchronize.forEach { observationRecordToSync ->
setProgress(
workData(
WorkInfo.State.RUNNING,
Expand All @@ -75,6 +76,8 @@ class SynchronizeObservationRecordsWorker @AssistedInject constructor(
)
)

delay(500)

synchronizeObservationRecordRepository.synchronize(observationRecordToSync)
.fold(
onSuccess = {
Expand All @@ -100,10 +103,10 @@ class SynchronizeObservationRecordsWorker @AssistedInject constructor(
}

Logger.info {
"observation records synchronization ${if (observationRecordsSynchronized.size == observationRecordToSynchronize.size) "successfully finished" else "finished with errors"} in ${Date().time - startTime.time}ms"
"observation records synchronization ${if (observationRecordsSynchronized.size == observationRecordsToSynchronize.size) "successfully finished" else "finished with errors"} in ${Date().time - startTime.time}ms"
}

return if (observationRecordsSynchronized.size == observationRecordToSynchronize.size) {
return if (observationRecordsSynchronized.size == observationRecordsToSynchronize.size) {
Result.success(
workData(state = WorkInfo.State.SUCCEEDED)
)
Expand Down

0 comments on commit 24b77ab

Please sign in to comment.