Skip to content

Instantly share code, notes, and snippets.

View stopwatch_start_stop_implemented.kt
fun startStopButton(state: State, context: FormulaContext<State>): ButtonRenderModel {
return if (state.isRunning) {
ButtonRenderModel(
text = "Stop",
onSelected = context.callback {
transition(state.copy(isRunning = false))
}
)
} else {
ButtonRenderModel(
View stopwatch_formula_is_running.kt
class StopwatchFormula : Formula<Unit, StopwatchFormula.State, StopwatchRenderModel> {
data class State(
val isRunning: Boolean
)
override fun initialState(input: Unit): State = State(
isRunning = false
)
View stopwatch_formula_skeleton.kt
class StopwatchFormula : Formula<Unit, StopwatchFormula.State, StopwatchRenderModel> {
// We will use this a little later.
object State
override fun initialState(input: Unit): State = State
override fun evaluate(
input: Unit,
state: State,
context: FormulaContext<State>
View stopwatch_internval_observable.kt
Observable
.interval(1, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
View stopwatch_reset_button.kt
fun resetButton(state: State, context: FormulaContext<State>): ButtonRenderModel {
return ButtonRenderModel(
text = "Reset",
onSelected = context.callback {
transition(state.copy(timePassedInMillis = 0, isRunning = false))
}
)
}
View stopwatch_state.kt
data class State(
val timePassedInMillis: Long,
val isRunning: Boolean
)
View format_time_passed.kt
fun formatTimePassed(timePassedInMillis: Long): String {
// TODO: actually format it
return "${timePassedInMillis}ms"
}
View start_stop_button.kt
fun startStopButton(state: State): ButtonRenderModel {
return ButtonRenderModel(
text = if (state.isRunning) "Stop" else "Start",
onSelected = { /* TODO: toggle the stopwatch */ }
)
}
View stopwatch_activity.kt
class StopwatchActivity : FragmentActivity() {
private val disposables = CompositeDisposable()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.stopwatch_activity)
val renderView = StopwatchRenderView(findViewById(R.id.activity_content))
val renderModels: Observable<StopwatchRenderModel> =
You can’t perform that action at this time.