Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SearchActivityViewModel
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