Guest User

Untitled

a guest
Mar 24th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. class SearchActivityViewModel @Inject
  2. constructor(private val githubUserRepository: GithubUserRepository, private val historyRepository: HistoryRepository): ViewModel() {
  3.  
  4. val login: MutableLiveData<String> = MutableLiveData()
  5. var githubUserLiveData: LiveData<Resource<GithubUser>> = MutableLiveData()
  6. val historiesLiveData: MutableLiveData<List<History>> = MutableLiveData()
  7.  
  8. val toast: MutableLiveData<String> = MutableLiveData()
  9.  
  10. init {
  11. Timber.d("Injection SearchActivityViewModel")
  12.  
  13. githubUserLiveData = Transformations.switchMap(login, {
  14. login.value?.let { githubUserRepository.loadUser(it) }
  15. ?: AbsentLiveData.create()
  16. })
  17.  
  18. githubUserLiveData.observeForever {
  19. it?.let { if(it.isOnError()) toast.postValue(it.message) }
  20. }
  21. }
  22.  
  23. fun insertHistory(search: String) = historyRepository.insertHistory(search)
  24.  
  25. fun selectHistories() {
  26. historyRepository.selectHistories().observeForever {
  27. it?.let {
  28. if(it.isNotEmpty()) historiesLiveData.postValue(it)
  29. }
  30. }
  31. }
  32.  
  33. fun deleteHistory(history: History) = historyRepository.deleteHistory(history)
  34.  
  35. fun getPreferenceUserKeyName() = githubUserRepository.getUserKeyName()
  36. }
Add Comment
Please, Sign In to add comment