in Android MVVM, how many repositories and networking clients that I need to make to serve different queries?

Viewed 1891

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

2 Answers

You need one repository for each model (data).

Video model has a VideoRepository interface.

Your repositories could be one of below:

  • InMemoryRepository
  • DatabaseRepository
  • RemoteRepository

When working with MVVM think about the model (data) first.

  1. I have some data (videos) to save, so I need a model for that.
  2. I need to save read/write that model so I need a repository.
  3. My repository needs to save/read from database so I need VideoDatabaseRepostory
interface VideoRepository {
  fun getVideos(type: VideoType)
  fun getVideo(videoId: Int)
  fun uploadVideo(file: File)
}
class VideoDatabaseRepository : VideoRepository {

    // Accepts an enum to decide which API to call.
    fun getVideos(type: VideoType) {
        when (type) {
            FavoriteVideos -> {   }
            RecentVideos -> { }
            ...  
        }
    }

}

In network mode, you need some web API that filters results based on your query to the API.

First of all, in your query you can distingues them by passing parameter which can be enum type. then you can call only one repository method by just passing different type in a parameter. If it's no possible then you have to create three different queries for repository also in client.

Related