This file contains 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
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) | |
} |
This file contains 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
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) { |
This file contains 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
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) |
This file contains 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
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) { |
This file contains 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 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> | |
... | |
} |
This file contains 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
override fun onActivityResult(...) { | |
// process success/ failure, extract data | |
viewModel.onActivityResult(...) | |
} | |
fun onRefreshed() { | |
viewModel.onRefreshed() | |
} | |
fun onSomeButtonClicked() { | |
viewModel.onSomeButtonClicked() | |
} |
This file contains 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
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) |
This file contains 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
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 | |
} |
This file contains 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 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 |
This file contains 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 SomeActivity: AppCompatActivity { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
//... | |
val profileRepository = ProfileRepository(...) | |
val someRepository = SomeRepository(...) | |
val somePresenter = SomePresenter(profileRepository, someRepository) | |
somePresenter.doSomeWork() | |
} | |
} |