Download File
Download Project
Settings
Line Wrap
Themes
default
ambiance
bespin
dracula
eclipse
material
mbo
mdn-like
neat
solarized dark
ttcn
zenburn
VideosInSameCategoryViewModel.java
/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.support.v17.leanback.supportleanbackshowcase.app.room.viewmodel; import android.app.Application; import android.arch.lifecycle.AndroidViewModel; import android.arch.lifecycle.LiveData; import android.arch.lifecycle.ViewModel; import android.arch.lifecycle.ViewModelProvider; import android.support.annotation.NonNull; import android.support.v17.leanback.supportleanbackshowcase.app.room.db.repo.VideosRepository; import android.support.v17.leanback.supportleanbackshowcase.app.room.db.entity.VideoEntity; import java.util.List; import javax.inject.Inject; public class VideosInSameCategoryViewModel extends AndroidViewModel { // instance of the repository // TODO not injected into here private VideosRepository mRepository; @Inject public VideosInSameCategoryViewModel(@NonNull Application application, VideosRepository repository) { super(application); mRepository = repository; } /** * Return the video entity list in same category using live data * * @return live data */ public LiveData
> getVideosInSameCategory(String category) { // The design here is: The view model will talk to repository to fetch the live data. // The data base is created on the main thread (The database creation won't block the UI) // If the database is not prepared (i.e. no valid data existed in the database), it will // return an empty live data automatically, there is no need to declare an empty live data // explicitly return mRepository.getVideosInSameCategoryLiveData(category); } }