SearchActivityViewModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchActivityViewModel @Inject | |
constructor(private val githubUserRepository: GithubUserRepository, private val historyRepository: HistoryRepository): ViewModel() { | |
val login: MutableLiveData<String> = MutableLiveData() | |
var githubUserLiveData: LiveData<Resource<GithubUser>> = MutableLiveData() | |
val historiesLiveData: MutableLiveData<List<History>> = MutableLiveData() | |
val toast: MutableLiveData<String> = MutableLiveData() | |
init { | |
Timber.d("Injection SearchActivityViewModel") | |
githubUserLiveData = Transformations.switchMap(login, { | |
login.value?.let { githubUserRepository.loadUser(it) } | |
?: AbsentLiveData.create() | |
}) | |
githubUserLiveData.observeForever { | |
it?.let { if(it.isOnError()) toast.postValue(it.message) } | |
} | |
} | |
fun insertHistory(search: String) = historyRepository.insertHistory(search) | |
fun selectHistories() { | |
historyRepository.selectHistories().observeForever { | |
it?.let { | |
if(it.isNotEmpty()) historiesLiveData.postValue(it) | |
} | |
} | |
} | |
fun deleteHistory(history: History) = historyRepository.deleteHistory(history) | |
fun getPreferenceUserKeyName() = githubUserRepository.getUserKeyName() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment