Skip to content

Instantly share code, notes, and snippets.

View Laimiux's full-sized avatar

Laimonas Turauskas Laimiux

View GitHub Profile
override fun evaluate(
input: Unit,
state: State,
context: FormulaContext<State>
): Evaluation<StopwatchRenderModel> {
return Evaluation(
renderModel = ...,
updates = context.updates {
if (state.isRunning) {
val incrementTimePassedEvents = RxStream.fromObservable {
class StopwatchFormula : Formula<Unit, StopwatchFormula.State, StopwatchRenderModel> {
...
override fun evaluate(
input: Unit,
state: State,
context: FormulaContext<State>
): Evaluation<StopwatchRenderModel> {
return Evaluation(
class StopwatchFormula : Formula<Unit, StopwatchFormula.State, StopwatchRenderModel> {
data class State(
val timePassedInMillis: Long,
val isRunning: Boolean
)
override fun initialState(input: Unit): State = State(
timePassedInMillis = 0,
isRunning = false
data class StopwatchState(
val timePassedInMillis: Long,
val isRunning: Boolean
)
class StopwatchFormula : Formula<Unit, StopwatchState, StopwatchRenderModel> {
override fun initialState(input: Unit) = StopwatchState(
timePassedInMillis = 0,
isRunning = false
)
override fun evaluate(
input: Unit,
state: StopwatchState,
data class StopwatchRenderModel(
val timePassed: String,
val startStopButton: ButtonRenderModel,
val resetButton: ButtonRenderModel
)
data class ButtonRenderModel(
val text: String,
val onSelected: () -> Unit
)
class StopwatchRenderView(root: ViewGroup): RenderView<StopwatchRenderModel> {
private val timePassed: TextView = root.findViewById(R.id.time_passed_text_view)
private val startStopButton: Button = root.findViewById(R.id.start_stop_button)
private val resetButton: Button = root.findViewById(R.id.reset_button)
override val renderer: Renderer<StopwatchRenderModel> = Renderer.create { model ->
timePassed.text = model.timePassed
startStopButton.text = model.startStopButton.text
startStopButton.setOnClickListener {
fun createDataStream(
textChangedEvents: Flowable<TextChangedEvent>,
submitCommentEvents: Flowable<SubmitCommentEvent>
): Flowable<CommentFormData>
class CommentFormPresenter(val viewModel: CommentViewModel) {
fun attach(view: CommentFormView) {
// Don't forget to unsubscribe
viewModel.viewStateStream().subscribe { state ->
view.setViewState(state)
}
}
}
class CommentViewModel(val model: CommentFormModel) {
fun viewStateStream(): Flowable<CommentFormViewState> {
// define the relays that will allow to complete unidirectional data flow
val textChangedEventRelay = PublishRelay.create<TextChangedEvent>()
val submitEventRelay = PublishRelay.create<SubmitCommentEvent>()
return model
.dataStream(
// We pass the user action flowables to the CommentFormModel
textChangedEvents = textChangedEventRelay.toFlowable(Backpressure.LATEST),