Skip to content

Instantly share code, notes, and snippets.

@aleweichandt
Created August 16, 2021 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aleweichandt/168e81c2c50ec0e6aea92078e5bca9ba to your computer and use it in GitHub Desktop.
Save aleweichandt/168e81c2c50ec0e6aea92078e5bca9ba to your computer and use it in GitHub Desktop.
Refresh Control
package com.example.todo.base.data
import java.util.*
import java.util.concurrent.TimeUnit
class RefreshControl(
rate: Long = DEFAULT_REFRESH_RATE_MS,
private var lastUpdateDate: Date? = null
) : ITimeLimitedResource {
companion object {
val DEFAULT_REFRESH_RATE_MS = TimeUnit.MINUTES.toMillis(5)
}
interface Listener {
suspend fun cleanup()
}
private val listeners: MutableList<Listener> = mutableListOf()
private val children: MutableList<RefreshControl> = mutableListOf()
// ITimeLimitedResource
override var refreshRate: Long = rate
override val lastUpdate: Date?
get() = lastUpdateDate
override suspend fun evict(cleanup: Boolean) {
lastUpdateDate = null
children.forEach { it.evict(cleanup) }
if (cleanup) {
listeners.forEach { it.cleanup() }
}
}
// Public API
fun createChild(): RefreshControl =
RefreshControl(refreshRate, lastUpdateDate).also { children.add(it) }
fun addListener(listener: Listener) {
listeners.add(listener)
}
fun refresh() {
lastUpdateDate = Date()
}
fun isExpired() = lastUpdateDate?.let { (Date().time - it.time) > refreshRate } ?: true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment