Last active
September 24, 2019 00:21
-
-
Save Laimiux/a8fdbda4399ee3e6530c7764d611c212 to your computer and use it in GitHub Desktop.
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 StopwatchFormula : Formula<Unit, StopwatchFormula.State, StopwatchRenderModel> { | |
... | |
override fun evaluate( | |
input: Unit, | |
state: State, | |
context: FormulaContext<State> | |
): Evaluation<StopwatchRenderModel> { | |
return Evaluation( | |
renderModel = StopwatchRenderModel( | |
timePassed = formatTimePassed(state.timePassedInMillis), | |
startStopButton = startStopButton(state, context), | |
resetButton = resetButton(state, context) | |
) | |
) | |
} | |
private fun formatTimePassed(timePassedInMillis: Long): String { | |
// TODO: actually format it. | |
return "$timePassedInMillis ms" | |
} | |
private fun startStopButton(state: State, context: FormulaContext<State>): ButtonRenderModel { | |
return ButtonRenderModel( | |
text = if (state.isRunning) "Stop" else "Start", | |
onSelected = context.callback { | |
transition(state.copy(isRunning = !state.isRunning)) | |
} | |
) | |
} | |
private fun resetButton(state: State, context: FormulaContext<State>): ButtonRenderModel { | |
return ButtonRenderModel( | |
text = "Reset", | |
onSelected = context.callback { | |
transition(state.copy(timePassedInMillis = 0, isRunning = false)) | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment