I am new in using MVVM and Android architecture, and I am little bit confused.
say for example I want to make an app like Youtube, as you know youtube has these tabs in their bottom navigation: Home, subscription, Trending(explore)
those 3 tabs will show recylerview of items (video object), those 3 will show data from 3 different queries.
in MVVM using viewmodel and livedata, the data stream will be like this
fragment <-- viewModel <-- repository <-- client
so for those 3 tabs I will have 3 different fragments, and it also mean I will have 3 different viewModels. now I am not sure how many repositories and networking clients do I need to have
am I right if I only have 1 repository and 1 networking client ? how many repositories and clients do I need to have ?
each viewModel will retrieve liveData from this VideoRepository below code below, and eventually each fragment will observe data from each viewModel
object VideoRepository {
val homeVideos: LiveData<List<Video>> = VideoRetrofitClient.homeVideos
val subscriptionVideos: LiveData<List<Video>> = VideoRetrofitClient.subscriptionVideos
val trendingVideos: LiveData<List<Video>> = VideoRetrofitClient.trendingVideos
fun searchHomeVideos() {
VideoRetrofitClient.searchHomeVideos()
}
fun searchSubscriptionVideos() {
VideoRetrofitClient.searchHomeVideos()
}
fun searchTrendingVideos() {
VideoRetrofitClient.searchHomeVideos()
}
}
and VideoRepository will get LiveData from this VideoRetrofitClient like this code
object VideoRetrofitClient {
val homeVideos = MutableLiveData<List<Video>>()
val subscriptionVideos = MutableLiveData<List<Video>>()
val trendingVideos = MutableLiveData<List<Video>>()
fun searchHomeVideos() {
// perform networking ....
homeVideos.postValue(videos)
}
fun searchSubscriptionVideos() {
// perform networking ....
subscriptionVideos.postValue(videos)
}
fun searchTrendingVideos() {
// perform networking ....
trendingVideos.postValue(videos)
}
}
java or kotlin are ok