Skip to content

Instantly share code, notes, and snippets.

View Laimiux's full-sized avatar

Laimonas Turauskas Laimiux

View GitHub Profile
fun createDataStream(
textChangedEvents: Flowable<TextChangedEvent>,
submitCommentEvents: Flowable<SubmitCommentEvent>
): Flowable<CommentFormData>
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 {
data class StopwatchRenderModel(
val timePassed: String,
val startStopButton: ButtonRenderModel,
val resetButton: ButtonRenderModel
)
data class ButtonRenderModel(
val text: String,
val onSelected: () -> Unit
)
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 StopwatchState(
val timePassedInMillis: Long,
val isRunning: Boolean
)
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
class StopwatchFormula : Formula<Unit, StopwatchFormula.State, StopwatchRenderModel> {
...
override fun evaluate(
input: Unit,
state: State,
context: FormulaContext<State>
): Evaluation<StopwatchRenderModel> {
return Evaluation(
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 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> =
fun startStopButton(state: State): ButtonRenderModel {
return ButtonRenderModel(
text = if (state.isRunning) "Stop" else "Start",
onSelected = { /* TODO: toggle the stopwatch */ }
)
}