Created
July 7, 2021 18:13
-
-
Save mrahimygk/df9402440a4805b7b93bbcafcd8a43a5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
abstract class BaseFragment<VM : BaseViewModel, DB : ViewDataBinding> : Fragment() { | |
abstract val viewModel: VM | |
@CallSuper | |
open fun bindObservables() { | |
(viewModel as? EmptyObservableHost)?.let { | |
it.getObservableLiveDataList().forEach { | |
it.observe(viewLifecycleOwner, {}) | |
} | |
} | |
} | |
} |
This file contains hidden or 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
import androidx.lifecycle.LiveData | |
interface EmptyObservableHost { | |
fun getObservableLiveDataList(): List<LiveData<Any>> | |
} |
This file contains hidden or 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 GithubSearchViewModel( | |
private val searchGithubUseCase: SearchGithubUseCase, | |
private val likeGithubUseCase: LikeGithubRepoUseCase | |
) : BaseViewModel(), EmptyObservableHost { | |
private val searchEvent: MediatorLiveData<String?> = searchQuery.map { | |
if (it.isNullOrBlank() || (restoredSearchQuery != null && restoredSearchQuery == it)) null | |
else { | |
pageNumber = 1 | |
currentGithubQueryList.clear() | |
it.trim() | |
} | |
}.debounce(SEARCH_DEBOUNCE_TIME, viewModelScope) | |
private val searchEventAction = searchEvent.map { | |
if (it.isNullOrEmpty()) { | |
_onEmptySearchEvent.postValue(LiveEvent()) | |
} else { | |
searchJob?.cancel() | |
searchJob = doSearch(it, false) | |
} | |
return@map Any() | |
} | |
override fun getObservableLiveDataList(): List<LiveData<Any>> { | |
return listOf(searchEventAction) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment