Kruge is a Kotlin library that provides a user-friendly and flexible way to implement various polling strategies in your Android applications. It simplifies the process of repeatedly executing tasks at specified intervals while offering control over the polling behavior.
- Supports various polling strategies:
- Endless Run: Executes the polling task indefinitely until manually stopped.
- Timeout Run: Executes the polling task for a defined duration.
- Retry Limit Run: Executes the polling task until a set number of retries is reached or a successful result is obtained.
- Provides real-time updates on the polling status through
StateFlow<PollingState> - Offers methods to start, stop, and check the polling status.
Here's a basic example of how to use Kruge with Endless Run strategy:
val scope = CoroutineScope(Dispatchers.IO)
val kruge = Kruge.init<NetworkModel>(
coroutineScope = scope,
pollingStrategy = EndlessStrategy(),
pollInterval = 5000L
)
kruge.poll {
// Your polling logic here
val data = fetchDataFromNetwork()
// Update UI or perform actions based on data
}
// Later, you can stop the polling
kruge.stop()kruge.state.collect { state ->
when (state) {
PollingState.Completed -> TODO()
PollingState.Idle -> TODO()
is PollingState.Polling<*> -> TODO()
PollingState.Terminated -> TODO()
}
}Kruge exposes a StateFlow that provides updates on the current polling state:
- Idle: No polling operation is currently running.
- Polling: Polling is in progress with the latest result (R).
- Terminated: Polling was explicitly stopped.
- Completed: Polling finished naturally (e.g., due to timeout or retry limit).