Skip to content

Instantly share code, notes, and snippets.

View Laimiux's full-sized avatar

Laimonas Turauskas Laimiux

View GitHub Profile
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
)
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 {
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, 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
)
data class StopwatchRenderModel(
val timePassed: String,
val startStopButton: ButtonRenderModel,
val resetButton: ButtonRenderModel
)
data class ButtonRenderModel(
val text: String,
val onSelected: () -> Unit
)
@Laimiux
Laimiux / ViewStatePagerAdapter.java
Created January 9, 2015 23:03
PagerAdapter for views instead of Fragments.
package com.laimiux.dayahead.ui.util;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
fun reduce(event: TextChangedEvent): Reducer<CommentFormData> {
// implementation
}
fun reduce(event: Lce<Comment>): Reducer<CommentFormData> {
// implementation
}
fun formState(
fun reduce(event: TextChangedEvent): (CommentFormData) -> CommentFormData {
// implementation
return { state ->
// Let's check comment validity
val isValid = event.enteredText.length > 5
// Update the current state with the changes
return state.copy(comment = event.enteredText, isCommentValid = isValid)
}
}