Skip to content

Instantly share code, notes, and snippets.

View Laimiux's full-sized avatar

Laimonas Turauskas Laimiux

View GitHub Profile
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(
class StopwatchFormula : Formula<Unit, StopwatchFormula.State, StopwatchRenderModel> {
data class State(
val isRunning: Boolean
)
override fun initialState(input: Unit): State = State(
isRunning = false
)
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>
data class State(
val isRunning: Boolean
)
Observable
.interval(1, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
fun resetButton(state: State, context: FormulaContext<State>): ButtonRenderModel {
return ButtonRenderModel(
text = "Reset",
onSelected = context.callback {
transition(state.copy(timePassedInMillis = 0, isRunning = false))
}
)
}
data class State(
val timePassedInMillis: Long,
val isRunning: Boolean
)
fun formatTimePassed(timePassedInMillis: Long): String {
// TODO: actually format it
return "${timePassedInMillis}ms"
}
fun startStopButton(state: State): ButtonRenderModel {
return ButtonRenderModel(
text = if (state.isRunning) "Stop" else "Start",
onSelected = { /* TODO: toggle the stopwatch */ }
)
}
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> =