Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
- }
Add Comment
Please, Sign In to add comment