@@ -228,10 +228,13 @@ class Repository : KoinComponent {
228
228
229
229
@SuppressLint(" CheckResult" )
230
230
fun saveAnsweredQuestion (answeredQuestion : AnsweredQuestion , answers : List <SelectedAnswer >) {
231
- Observable .create< Unit > {
231
+ Observable .fromCallable< Boolean > {
232
232
db.formDetailsDao().insertAnsweredQuestion(answeredQuestion, answers)
233
+ true
233
234
}.subscribeOn(Schedulers .io())
234
- .observeOn(AndroidSchedulers .mainThread()).subscribe({}, {
235
+ .observeOn(AndroidSchedulers .mainThread()).subscribe({
236
+ Log .d(TAG , " Saving answered question: $answeredQuestion (answers: $answers )" )
237
+ }, {
235
238
Log .i(TAG , it.message.orEmpty())
236
239
})
237
240
}
@@ -249,14 +252,20 @@ class Repository : KoinComponent {
249
252
fun syncAnswers (countyCode : String , pollingStationNumber : Int , formId : Int ) {
250
253
db.formDetailsDao().getNotSyncedQuestionsForForm(countyCode, pollingStationNumber, formId)
251
254
.toObservable()
252
- .subscribeOn(Schedulers .io()).flatMap {
253
- syncAnswers(it)
254
- }.observeOn(AndroidSchedulers .mainThread()).subscribe({
255
- Observable .create<Unit > {
256
- db.formDetailsDao()
257
- .updateAnsweredQuestions(countyCode, pollingStationNumber, formId)
258
- }.subscribeOn(Schedulers .io()).observeOn(AndroidSchedulers .mainThread())
259
- .subscribe()
255
+ .subscribeOn(Schedulers .io()).flatMap(
256
+ {
257
+ Log .d(TAG , " Syncing answers: ${it.size} answers to sync" )
258
+ syncAnswers(it)
259
+ },
260
+ { answersList, response -> Pair (answersList, response) }
261
+ ).observeOn(AndroidSchedulers .mainThread()).subscribe({
262
+ if (it.first.isNotEmpty()) {
263
+ Observable .fromCallable {
264
+ db.formDetailsDao()
265
+ .updateAnsweredQuestions(countyCode, pollingStationNumber, formId)
266
+ }.subscribeOn(Schedulers .io()).observeOn(AndroidSchedulers .mainThread())
267
+ .subscribe()
268
+ }
260
269
}, {
261
270
Log .i(TAG , it.message ? : " Error on synchronizing data" )
262
271
})
@@ -431,7 +440,7 @@ class Repository : KoinComponent {
431
440
db.pollingStationDao().deleteAll()
432
441
}
433
442
}
434
-
443
+
435
444
fun getVisitedStations () = db.pollingStationDao().getVisitedPollingStations()
436
445
}
437
446
0 commit comments