I have a custom Android TextView which shows the amount of time left in a game via a CountDownTimer
class CountdownTextView(context: Context, attrs: AttributeSet) : TextView(context, attrs) {
private lateinit var countDownTimer: CountDownTimer
private lateinit var onFinishObservable: Observable<Unit>
fun setTime(initTime: Int) {
this.text = "$initTime:00"
countDownTimer = object : CountDownTimer((initTime *1000).toLong(), 1000) {
override fun onTick(millisUntilFinished: Long) {
val minutes = millisUntilFinished / 60000
val seconds = (millisUntilFinished % 60000) / 1000
if (seconds / 10 > 0) {
text = "$minutes:${(millisUntilFinished % 60000) / 1000}"
} else {
text = "$minutes:0${(millisUntilFinished % 60000) / 1000}"
}
}
override fun onFinish() {
}
}
fun startCountdown() {
countDownTimer.start()
}
}
How do I set up an observable that emits a value when the countDownTimer's onFinish() method is called? I need this so that on the main activity, I can subscribe to that observable and perform the necessary actions when the countdowntimer expires.