Skip to content

Instantly share code, notes, and snippets.

View ericntd's full-sized avatar

Eric Nguyen ericntd

View GitHub Profile
val viewState = MutableLiveData<ViewState>()
init {
viewState.value = ViewState.Loading
interactor.getProfile().let { getProfileState ->
when (getProfileState) {
is GetProfileInteractor.State.Success -> {
// render updates on UI
viewState.value = ViewState.ProfileLoaded(getProfileState.profile)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setUpObservation()
}
private fun setUpObservation() {
viewModel.viewState.observe(this, Observer { state ->
when (state) {
val viewState = MutableLiveData<ViewState>()
val actionState = SingleLiveEvent<ActionState>() // each value emit only once
init {
viewState.value = ViewState.Loading
interactor.getProfile().let { getProfileState ->
when (getProfileState) {
is GetProfileInteractor.State.Success -> {
// render updates on UI
viewState.value = ViewState.ProfileLoaded(getProfileState.profile)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setUpObservation()
}
private fun setUpObservation() {
viewModel.viewState.observe(this, Observer { state ->
when (state) {
class ProfileViewModel(val interactor: GetProfileInteractor) : ViewModel() {
private val viewState = MutableLiveData<ViewState>()
private val actionState = SingleLiveEvent<ActionState>()
val viewStateReadOnly: LiveData<ViewState> = viewState as LiveData<ViewState>
val actionStateReadOnly: LiveData<ActionState> = actionState as LiveData<ActionState>
...
}
override fun onActivityResult(...) {
// process success/ failure, extract data
viewModel.onActivityResult(...)
}
fun onRefreshed() {
viewModel.onRefreshed()
}
fun onSomeButtonClicked() {
viewModel.onSomeButtonClicked()
}
private val viewState = MutableLiveData<ViewState>()
private var someUsefulVariable: Int? = null // mutable!
init {
doStep1()
//...
.doStepK()
.map { stepKResult ->
someUsefulVariable = ... // derived from stepKresult
return@map processData(stepKResult, someUsefulVariable)
val inputSubject = PublishSubject.create<String>() // immutable!
val inputSubjectReadOnly = inputSubject.debounce(500L, TimeUnit.MILLISECONDS)
val someViewModel = SomeViewModel(inputSubjectReadOnly, ...)
override fun onSomeButtonClicked(...) {
// process success/ failure, extract data
inputSubject.onNext(data) // emit to input stream ViewModel consumes
}
class SomeViewModel(val inputStream: Observable<SomeDataType>, ...) {
private val viewState = MutableLiveData<ViewState>()
init {
doStep1()
//...
.doStepK()
.zipWith(inputStream) // combine/ merge 2 streams into 1 using Pair
.map { pair ->
val stepKresult = pair.first
class SomeActivity: AppCompatActivity {
override fun onCreate(savedInstanceState: Bundle?) {
//...
val profileRepository = ProfileRepository(...)
val someRepository = SomeRepository(...)
val somePresenter = SomePresenter(profileRepository, someRepository)
somePresenter.doSomeWork()
}
}