Skip to content

Commit

Permalink
GoL: Replace observable properties in View with methods.
Browse files Browse the repository at this point in the history
With this, the default value and the property (as state) is also moved to Model
  • Loading branch information
Said Tahsin Dane committed Jan 10, 2018
1 parent 72db5a5 commit 82ee550
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package com.novoda.gol.presentation

import com.novoda.gol.patterns.PatternEntity
import kotlin.properties.Delegates.observable

class AppModel {

private var isIdle by observable(true) { _, _, newValue ->
onSimulationStateChanged(newValue)
private var boardViewState by observable(BoardViewState(true)) { _, _, newValue ->
onBoardStateChanged(newValue)
}

var onSimulationStateChanged: (isIdle: Boolean) -> Unit by observable<(Boolean) -> Unit>({}) { _, _, newValue ->
newValue(isIdle)
newValue(boardViewState.isIdle)
}

var onBoardStateChanged: (BoardViewState) -> Unit by observable<(BoardViewState) -> Unit>({}) { _, _, newValue ->
newValue(boardViewState)
}

fun toggleSimulation() {
isIdle = isIdle.not()
boardViewState = BoardViewState(isIdle = boardViewState.isIdle.not())
onSimulationStateChanged(boardViewState.isIdle)
}

fun selectPattern(pattern: PatternEntity) {
boardViewState = boardViewState.copy(selectedPattern = pattern)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ class AppPresenter {
fun bind(view: AppView) {

model.onSimulationStateChanged = { isIdle ->
view.controlButtonLabel = if (isIdle) "Start simulation" else "Stop Simulation"
view.patternSelectionVisibility = isIdle
view.board = view.board.copy(isIdle = isIdle)
view.renderControlButtonLabel(if (isIdle) "Start simulation" else "Stop Simulation")
view.renderPatternSelectionVisibility(visibility = isIdle)
}

view.onControlButtonClicked = {
model.toggleSimulation()
}
model.onBoardStateChanged = view::renderBoard

view.onPatternSelected = { pattern ->
view.board = view.board.copy(selectedPattern = pattern)
}
view.onControlButtonClicked = model::toggleSimulation

view.onPatternSelected = model::selectPattern
}

fun unbind(view: AppView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import com.novoda.gol.patterns.PatternEntity

interface AppView {

var controlButtonLabel: String
var patternSelectionVisibility: Boolean
var board: BoardViewState

var onControlButtonClicked : () -> Unit
var onPatternSelected: (pattern : PatternEntity) -> Unit

fun renderControlButtonLabel(controlButtonLabel: String)
fun renderPatternSelectionVisibility(visibility: Boolean)
fun renderBoard(boardViewState: BoardViewState)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class BoardModelImpl private constructor(initialBoard: BoardEntity, private val
}

override fun selectPattern(pattern: PatternEntity) {
if (gameLoop.isLooping() || this.pattern == pattern) {
if (gameLoop.isLooping()) {
return
}
this.pattern = pattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import com.novoda.gol.patterns.PatternEntity

data class BoardViewState(
val isIdle: Boolean,
val selectedPattern: PatternEntity?
val selectedPattern: PatternEntity? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,12 @@ import kotlinx.html.style
import react.*
import react.dom.div
import react.dom.h2
import kotlin.properties.Delegates.observable

class App : RComponent<RProps, State>(), AppView {

override var onControlButtonClicked: () -> Unit = {}
override var onPatternSelected: (pattern: PatternEntity) -> Unit = {}

override var controlButtonLabel by observable("") { _, _, newValue ->
setState {
controlButtonLabel = newValue
}
}

override var patternSelectionVisibility by observable(true) { _, _, newValue ->
setState {
patternViewState.shouldDisplay = newValue
}
}

override var board by observable(BoardViewState(true, null)) { _, _, newValue ->
setState {
boardViewState = newValue
}
}

private val presenter: AppPresenter = AppPresenter()

override fun componentWillMount() {
Expand All @@ -51,6 +32,24 @@ class App : RComponent<RProps, State>(), AppView {
patternViewState = PatternViewState(true, PatternRepository.patterns())
}

override fun renderControlButtonLabel(controlButtonLabel: String) {
setState {
this.controlButtonLabel = controlButtonLabel
}
}

override fun renderPatternSelectionVisibility(visibility: Boolean) {
setState {
patternViewState.shouldDisplay = visibility
}
}

override fun renderBoard(boardViewState: BoardViewState) {
setState {
this.boardViewState = boardViewState
}
}

override fun RBuilder.render(): ReactElement? =
div {
attrs.style = kotlinext.js.js {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ fun RBuilder.pattern(patternEntity: PatternEntity, onPatternSelected: () -> Unit
}
}
}
}
}

0 comments on commit 82ee550

Please sign in to comment.