Getting distinct LiveData

This little code made me happy today. I wanted my animation binded to LiveData to animate only when a number is changed not every time I return to the app. Let’s say I have a notification bell in the toolbar corner with a red circle with a number inside so the user knows how many notifications are unread. If the number changed, between or during a session, I want to animate this red bubble, but only when users start the app, not every time they return to this specific fragment.

private val _notificationCounter = MutableLiveData<Int>()
val notificationCounter: LiveData<Int> = _notificationCounter
val notificationAnimation: LiveData<Int> = MediatorLiveData<Int>().apply {
    addSource(Transformations.distinctUntilChanged(notificationCounter)) { 
        postValue(it) 
    }
}