Based in CountUpTimer.java: An infinite timer, I did this fork to
improve the implementation and use Kotlin
language. You can use that for pooling requests on any action that
needs run for an uncertain time.
Additionally, this implementation use Kotlin DSL as a flexible way to register listeners :)
That was tested in a Android app example using Android Studio Dolphin and latest api's for that moment (API 32 and 33) :)
Java
// Instantiate the class and pass the each tick time: (30000 = 30 seconds)
CountDownTimerInfinite poolingTimer = new CountDownTimerInfinite(30000);
// Run some action on "onTickListener" event. You can use segregation interface
// principle (from Java classes) or Kotlin DSL (from Kotlin classes)
poolingTimer.setOnTickListener((millis) -> {
// Your action here: (e.g API REST request)
myRepository.fetchSomething();
}).startTimer();
Kotlin
// Instantiate the class and pass the each tick time: (30000 = 30 seconds)
val poolingTimer = CountDownTimerInfinite(30000);
// Run some action on "onTickListener" event. You can use segregation interface
// principle (from Java classes) or Kotlin DSL (from Kotlin classes)
poolingTimer.setListeners {
onTick { millis ->
// Your action here: (e.g API REST request)
myRepository.fetchSomething();
}
}.startTimer();