From 8c8edd65c7382922d943eeb31d141c65bebf0893 Mon Sep 17 00:00:00 2001 From: Tiberiu Neagu Date: Tue, 21 Apr 2020 09:24:44 +0300 Subject: [PATCH] https://github.com/Co-Epi/app-android/issues/95 --- .../main/java/org/coepi/android/cen/RealmCenDao.kt | 13 ++++++++++--- .../worker/cenfetcher/ContactsFetchWorker.kt | 5 +++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/coepi/android/cen/RealmCenDao.kt b/app/src/main/java/org/coepi/android/cen/RealmCenDao.kt index 07f85669..6e90987b 100644 --- a/app/src/main/java/org/coepi/android/cen/RealmCenDao.kt +++ b/app/src/main/java/org/coepi/android/cen/RealmCenDao.kt @@ -1,6 +1,7 @@ package org.coepi.android.cen import io.realm.kotlin.createObject +import io.realm.kotlin.delete import io.realm.kotlin.oneOf import io.realm.kotlin.where import org.coepi.android.domain.CoEpiDate @@ -32,12 +33,18 @@ class RealmCenDao(private val realmProvider: RealmProvider) { return false } realm.executeTransaction { - val realmObj = realm.createObject(cen.cen.toHex()) // Create a new object + val realmObj = + realm.createObject(cen.cen.toHex()) // Create a new object realmObj.timestamp = cen.date.unixTime } return true } -// @Delete("DELETE FROM cen where :timeStamp > timeStamp") -// fun cleanCENs(timeStamp : Int) + // @Delete("DELETE FROM cen where :timeStamp > timeStamp") + fun cleanCENs(timeStamp: Long) = + realm.where() + .lessThan("timestamp", timeStamp) + .findAll() + .deleteAllFromRealm() + } diff --git a/app/src/main/java/org/coepi/android/worker/cenfetcher/ContactsFetchWorker.kt b/app/src/main/java/org/coepi/android/worker/cenfetcher/ContactsFetchWorker.kt index ab5392f7..bd6373bd 100644 --- a/app/src/main/java/org/coepi/android/worker/cenfetcher/ContactsFetchWorker.kt +++ b/app/src/main/java/org/coepi/android/worker/cenfetcher/ContactsFetchWorker.kt @@ -6,6 +6,7 @@ import androidx.work.ListenableWorker.Result.success import androidx.work.WorkerParameters import androidx.work.workDataOf import kotlinx.coroutines.delay +import org.coepi.android.cen.RealmCenDao import org.coepi.android.cen.RealmCenReportDao import org.coepi.android.cen.ReceivedCenReport import org.coepi.android.common.doIfSuccess @@ -29,6 +30,7 @@ class ContactsFetchWorker( private val coEpiRepo: CoEpiRepo by inject() private val reportsDao: RealmCenReportDao by inject() + private val cenDao: RealmCenDao by inject() private val preferences: Preferences by inject() override suspend fun doWork(): Result { @@ -40,6 +42,9 @@ class ContactsFetchWorker( val nowBeforeRequest: CoEpiDate = now() + //delete old CENs + cenDao.cleanCENs(nowBeforeRequest.unixTime) + val reportsResult = coEpiRepo.reports(fromDate) val reports: List = reportsResult.successOrNull() ?: emptyList()