mirror of
https://github.com/wulkanowy/wulkanowy.git
synced 2024-11-22 10:56:31 -06:00
Add auto refresh to wulkanowy repo
This commit is contained in:
parent
24abe47332
commit
859c6ef154
@ -6,6 +6,8 @@ import io.github.wulkanowy.data.api.services.WulkanowyService
|
||||
import io.github.wulkanowy.data.db.dao.AdminMessageDao
|
||||
import io.github.wulkanowy.data.db.entities.AdminMessage
|
||||
import io.github.wulkanowy.data.networkBoundResource
|
||||
import io.github.wulkanowy.utils.AutoRefreshHelper
|
||||
import io.github.wulkanowy.utils.getRefreshKey
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.filterNot
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
@ -18,10 +20,13 @@ class WulkanowyRepository @Inject constructor(
|
||||
private val wulkanowyService: WulkanowyService,
|
||||
private val adminMessageDao: AdminMessageDao,
|
||||
private val preferencesRepository: PreferencesRepository,
|
||||
private val refreshHelper: AutoRefreshHelper,
|
||||
) {
|
||||
|
||||
private val saveFetchResultMutex = Mutex()
|
||||
|
||||
private val cacheKey = "mapping"
|
||||
|
||||
fun getAdminMessages(): Flow<Resource<List<AdminMessage>>> =
|
||||
networkBoundResource(
|
||||
mutex = saveFetchResultMutex,
|
||||
@ -38,7 +43,11 @@ class WulkanowyRepository @Inject constructor(
|
||||
suspend fun getMapping(): Mapping? {
|
||||
var savedMapping = preferencesRepository.mapping
|
||||
|
||||
if (savedMapping == null) {
|
||||
val isExpired = refreshHelper.shouldBeRefreshed(
|
||||
key = getRefreshKey(cacheKey)
|
||||
)
|
||||
|
||||
if (savedMapping == null || isExpired) {
|
||||
fetchMapping()
|
||||
savedMapping = preferencesRepository.mapping
|
||||
}
|
||||
@ -49,6 +58,9 @@ class WulkanowyRepository @Inject constructor(
|
||||
suspend fun fetchMapping() {
|
||||
runCatching { wulkanowyService.getMapping() }
|
||||
.onFailure { Timber.e(it) }
|
||||
.onSuccess { preferencesRepository.mapping = it }
|
||||
.onSuccess {
|
||||
preferencesRepository.mapping = it
|
||||
refreshHelper.updateLastRefreshTimestamp(cacheKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -30,6 +30,10 @@ fun getRefreshKey(name: String, mailbox: Mailbox?, folder: MessageFolder): Strin
|
||||
return "${name}_${mailbox?.globalKey ?: "all"}_${folder.id}"
|
||||
}
|
||||
|
||||
fun getRefreshKey(name: String): String {
|
||||
return name
|
||||
}
|
||||
|
||||
class AutoRefreshHelper @Inject constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
private val sharedPref: SharedPrefProvider
|
||||
|
Loading…
Reference in New Issue
Block a user