Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Feature/key retrieval on resume #660

Merged
merged 4 commits into from
Jun 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ package de.rki.coronawarnapp.ui.viewmodel
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import de.rki.coronawarnapp.CoronaWarnApplication
import de.rki.coronawarnapp.exception.ExceptionCategory.INTERNAL
import de.rki.coronawarnapp.exception.TransactionException
import de.rki.coronawarnapp.exception.reporting.report
import de.rki.coronawarnapp.storage.ExposureSummaryRepository
import de.rki.coronawarnapp.storage.LocalData
import de.rki.coronawarnapp.storage.RiskLevelRepository
import de.rki.coronawarnapp.storage.TracingRepository
import de.rki.coronawarnapp.timer.TimerHelper
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction
import de.rki.coronawarnapp.transaction.RiskLevelTransaction
import de.rki.coronawarnapp.util.ConnectivityHelper
import kotlinx.coroutines.launch
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.joda.time.Instant
import timber.log.Timber
import java.util.Date

Expand Down Expand Up @@ -47,13 +54,30 @@ class TracingViewModel : ViewModel() {
var isRefreshing: LiveData<Boolean> = TracingRepository.isRefreshing

/**
* Launches the RiskLevelTransaction in the viewModel scope
* Launches the RetrieveDiagnosisKeysTransaction and RiskLevelTransaction in the viewModel scope
*
* @see RiskLevelTransaction
* @see RiskLevelRepository
*/
fun refreshRiskLevel() {
viewModelScope.launch {
try {
val currentDate = DateTime(Instant.now(), DateTimeZone.getDefault())
val lastFetch = DateTime(
LocalData.lastTimeDiagnosisKeysFromServerFetch(),
DateTimeZone.getDefault()
)
val keysWereNotRetrievedToday = LocalData.lastTimeDiagnosisKeysFromServerFetch() == null ||
currentDate.withTimeAtStartOfDay() != lastFetch.withTimeAtStartOfDay()
val isNetworkEnabled = ConnectivityHelper.isNetworkEnabled(CoronaWarnApplication.getAppContext())
if (keysWereNotRetrievedToday && isNetworkEnabled) {
RetrieveDiagnosisKeysTransaction.start()
refreshLastTimeDiagnosisKeysFetchedDate()
TimerHelper.checkManualKeyRetrievalTimer()
}
} catch (e: TransactionException) {
e.cause?.report(INTERNAL)
}
try {
RiskLevelTransaction.start()
} catch (e: TransactionException) {
Expand Down