Hi, I’m showing countdown timer on ui served from ...
# orbit-mvi
r
Hi, I’m showing countdown timer on ui served from viewModel .
MVI state
Copy code
data class VideoState(val currentTime: Long, 
                      val totalTime: Long,
                      val title: String)
UI (Fragment)
Copy code
renderData(state: VideoState) {
 binding.timerTextView.text = "$state.currentTime"
 binding.durationTextView.text = "$state.totalTime"
 binding.title = "$state.title"
}
only
timerTextView
will update but
durationTextView
&
title
will remain same still
renderData
will set everything. Is there any performance again if we want to update part of the view? If so whats the better to do that? Hoping for a
onBindViewHolder
with
payload
kind of approach
I’ve checked PostDetailsViewModel , please let me know if below approach is correct or not .
Copy code
data class VideoData(
    val url: String = "",
    val title: String = "",
    val isPlaying: Boolean = false,
    val totalDuration: Long = 0L,
)

sealed class VideoState {

    abstract val videoData: VideoData

    data class VideoMeta(override val videoData: VideoData) : VideoState()

    data class VideTimer(val currentTime: Long, override val videoData: VideoData) : VideoState()
}

class VideoViewModel(private val someSubject: SomeSubject, private val someRepo: SomeRepo) : ViewModel(), ContainerHost<VideoState, Nothing> {

    override val container by lazy {
        container<VideoState, Nothing>(
            initialState = VideoState.VideoMeta(VideoData()),
        ) { initData() }
    }

    private fun initData() = intent {
        val videoData: VideoData = someRepo.fetchVideoMeta()
        reduce {
            with(videoData) {
                VideoState.VideoMeta(state.videoData.copy(url = url, title = title, totalDuration = totalDuration))
            }
        }
        repeatOnSubscription {
            val timer: Long = someSubject.listentoTimer()
            reduce {
                VideoState.VideTimer(currentTime = timer, videoData = state.videoData)
            }
        }
    }
}

class VideoFragment : Fragment() {

    fun renderData(state: VideoState) {
        when (state) {
            is VideoState.VideTimer -> binding.timerTv.text = "${state.currentTime}"
            is VideoState.VideoMeta -> with(state.videoData) {
                binding.titleTv.text = "$title"
                binding.totalTv.text = "$totalDuration"
                binding.playIcon.background = if (isPlaying) playIcon else pauseIcon
            }
        }
    }
}
100 Views