Skip to content

Commit

Permalink
Change currency rates update interval to 2 hours (#116)
Browse files Browse the repository at this point in the history
* Change currency rates update interval to 2 hours

* Extract rates update interval as constant and reuse in bg workers
  • Loading branch information
mdrlzy authored Oct 5, 2024
1 parent 59c12e0 commit 8ec1677
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.arkbuilders.rate.data.repo.currency
import arrow.core.Either
import arrow.core.left
import arrow.core.right
import dev.arkbuilders.rate.domain.AppConfig
import dev.arkbuilders.rate.domain.model.CurrencyName
import dev.arkbuilders.rate.domain.model.CurrencyRate
import dev.arkbuilders.rate.domain.model.CurrencyType
Expand All @@ -18,7 +19,6 @@ import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import java.time.Duration
import java.time.OffsetDateTime
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -96,7 +96,8 @@ class CurrencyRepoImpl @Inject constructor(
private fun hasUpdateIntervalPassed(updatedDate: OffsetDateTime?) =
updatedDate == null ||
Duration.between(updatedDate, OffsetDateTime.now())
.toMillis() > dayInMillis
.toMillis() > updateInterval

private val dayInMillis = TimeUnit.DAYS.toMillis(1)
private val updateInterval =
Duration.ofHours(AppConfig.CURRENCY_RATES_UPDATE_INTERVAL_HOURS).toMillis()
}
5 changes: 5 additions & 0 deletions app/src/main/java/dev/arkbuilders/rate/domain/AppConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.arkbuilders.rate.domain

object AppConfig {
const val CURRENCY_RATES_UPDATE_INTERVAL_HOURS = 2L
}
3 changes: 2 additions & 1 deletion app/src/main/java/dev/arkbuilders/rate/presentation/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import dev.arkbuilders.rate.BuildConfig
import dev.arkbuilders.rate.data.worker.CurrencyMonitorWorker
import dev.arkbuilders.rate.data.worker.QuickPairsWidgetRefreshWorker
import dev.arkbuilders.rate.di.DIManager
import dev.arkbuilders.rate.domain.AppConfig
import dev.arkbuilders.rate.domain.repo.PreferenceKey
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -57,7 +58,7 @@ class App : Application(), Configuration.Provider {
val workRequest =
PeriodicWorkRequest.Builder(
workerClass,
8L,
AppConfig.CURRENCY_RATES_UPDATE_INTERVAL_HOURS,
TimeUnit.HOURS,
).setConstraints(constraints)
.build()
Expand Down

0 comments on commit 8ec1677

Please sign in to comment.