What I'm looking to do is create a Cooking Alarm app.

I need to update the List in three cases:

1). Every second the text that represents second left has to be updated

2). Every time a new item is created the RecyclerView should also update

I'm using the MVVM architecture and would like to update it via LiveData.

Also, I'm still new to android development so I might be missing some necessary knowledge

This i how i setup my alarm, the alarm is the thing recycler view will hold in each of it's an element. There will be more stuff inside but for now, this is all it have.

class Alarm(val item: Item, private val context: Context, val notifyID: Int) 
{

// region LiveDatas
private val _timeLeft = MutableLiveData<Int>(item.timeInSec)
val timeLeft: LiveData<Int>
    get() = _timeLeft

private val _isFinished = MutableLiveData<Boolean>(false)
val isFinished: LiveData<Boolean>
    get() = _isFinished
// endregion

// count down goes down every second
fun tick() {
    _timeLeft.value = (_timeLeft.value ?: 0) - 1
    if ((_timeLeft.value ?: 0) <= 0) {
        onFinish()
    }
}

fun onFinish() {
    _isFinished.value = true
}

}   

Related posts

Recent Viewed