|
package com.example.services.timers |
|
|
|
import android.os.CountDownTimer |
|
import com.example.services.timers.listeners.CountDownTimerListenerWrapper |
|
import com.example.services.timers.listeners.CancelListener |
|
import com.example.services.timers.listeners.FinishListener |
|
import com.example.services.timers.listeners.TickListener |
|
|
|
/** |
|
* An infinite **CountDownTimer** that runs each an interval of milliseconds |
|
* |
|
* This Class is based in: |
|
* [CountUpTimer.java: An infinite timer](https://gist.github.com/kasparsj/10599789) |
|
*/ |
|
open class CountDownTimerInfinite @JvmOverloads constructor(val countDownInterval: Long, protected var limitCycles: Int? = null) : CountDownTimer(Long.MAX_VALUE, countDownInterval) { |
|
|
|
protected open var listener = CountDownTimerListenerWrapper() |
|
|
|
var countDownCycle = 1 |
|
protected set |
|
|
|
override fun onTick(millisUntilFinished: Long) { |
|
|
|
limitCycles?.let { |
|
|
|
if (countDownCycle >= it) { |
|
stop() |
|
return |
|
} |
|
} |
|
|
|
listener.onTick(Long.MAX_VALUE * countDownCycle - millisUntilFinished) |
|
countDownCycle++ |
|
} |
|
|
|
override fun onFinish() { |
|
listener.onFinish() |
|
|
|
startTimer() |
|
} |
|
|
|
fun startTimer(): CountDownTimerInfinite { |
|
return start() as CountDownTimerInfinite |
|
} |
|
|
|
fun cancelTimer(): CountDownTimerInfinite { |
|
stop() |
|
countDownCycle = 1 |
|
return this |
|
} |
|
|
|
fun stop(): CountDownTimerInfinite { |
|
cancelTimer() |
|
listener.onCancel() |
|
|
|
return this |
|
} |
|
|
|
/** |
|
* Set listeners defined in [CountDownTimerListenerWrapper] |
|
* as Kotlin DSL, using [Function literals receiver](https://kotlinlang.org/docs/lambdas.html#function-literals-with-receiver). |
|
* |
|
* With this, you can register a listener for all callback methods once, or just one |
|
* like described in SOLID [Interface segregation principle](https://en.wikipedia.org/wiki/Interface_segregation_principle) patterns |
|
* |
|
* ## Reference |
|
* |
|
* - [Listeners with several functions in Kotlin](https://antonioleiva.com/listeners-several-functions-kotlin) |
|
*/ |
|
@Suppress("unused") |
|
fun setListeners(init: CountDownTimerListenerWrapper.() -> Unit): CountDownTimerListenerWrapper { |
|
listener.init() |
|
return listener |
|
} |
|
|
|
//region Listeners Fluent Interfaces |
|
|
|
@JvmName("setLimitCycles") |
|
fun limitCycles(limitCycles: Int): CountDownTimerInfinite { |
|
this.limitCycles = limitCycles |
|
return this |
|
} |
|
|
|
@JvmName("setOnTickListener") |
|
fun tickListener(tickListener: TickListener): CountDownTimerInfinite { |
|
listener.onTick(tickListener) |
|
return this |
|
} |
|
|
|
@JvmName("setOnCancelListener") |
|
fun cancelListener(listener: CancelListener): CountDownTimerInfinite { |
|
this.listener.onCancel(listener) |
|
return this |
|
} |
|
|
|
@JvmName("setOnFinishListener") |
|
fun finishListener(finishListener: FinishListener): CountDownTimerInfinite { |
|
listener.onFinish(finishListener) |
|
return this |
|
} |
|
|
|
//endregion |
|
} |