-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #347 from novoda/gol/model-app-component
GoL: MVP for App component
- Loading branch information
Showing
12 changed files
with
166 additions
and
33 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
game-of-life-multiplatform/common/src/main/kotlin/com/novoda/gol/Logger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.novoda.gol | ||
|
||
expect object Logger { | ||
|
||
fun log(o: Any?) | ||
} |
28 changes: 28 additions & 0 deletions
28
game-of-life-multiplatform/common/src/main/kotlin/com/novoda/gol/presentation/AppModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.novoda.gol.presentation | ||
|
||
import com.novoda.gol.patterns.PatternEntity | ||
import kotlin.properties.Delegates.observable | ||
|
||
class AppModel { | ||
|
||
private var boardViewState by observable(BoardViewState(true)) { _, _, newValue -> | ||
onBoardStateChanged(newValue) | ||
} | ||
|
||
var onSimulationStateChanged: (isIdle: Boolean) -> Unit by observable<(Boolean) -> Unit>({}) { _, _, newValue -> | ||
newValue(boardViewState.isIdle) | ||
} | ||
|
||
var onBoardStateChanged: (BoardViewState) -> Unit by observable<(BoardViewState) -> Unit>({}) { _, _, newValue -> | ||
newValue(boardViewState) | ||
} | ||
|
||
fun toggleSimulation() { | ||
boardViewState = BoardViewState(isIdle = boardViewState.isIdle.not()) | ||
onSimulationStateChanged(boardViewState.isIdle) | ||
} | ||
|
||
fun selectPattern(pattern: PatternEntity) { | ||
boardViewState = boardViewState.copy(selectedPattern = pattern) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...-of-life-multiplatform/common/src/main/kotlin/com/novoda/gol/presentation/AppPresenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.novoda.gol.presentation | ||
|
||
class AppPresenter { | ||
|
||
private val model = AppModel() | ||
|
||
fun bind(view: AppView) { | ||
|
||
model.onSimulationStateChanged = { isIdle -> | ||
view.renderControlButtonLabel(if (isIdle) "Start simulation" else "Stop Simulation") | ||
view.renderPatternSelectionVisibility(visibility = isIdle) | ||
} | ||
|
||
model.onBoardStateChanged = view::renderBoard | ||
|
||
view.onControlButtonClicked = model::toggleSimulation | ||
|
||
view.onPatternSelected = model::selectPattern | ||
} | ||
|
||
fun unbind(view: AppView) { | ||
model.onSimulationStateChanged = {} | ||
view.onControlButtonClicked = {} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
game-of-life-multiplatform/common/src/main/kotlin/com/novoda/gol/presentation/AppView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.novoda.gol.presentation | ||
|
||
import com.novoda.gol.patterns.PatternEntity | ||
|
||
interface AppView { | ||
|
||
var onControlButtonClicked : () -> Unit | ||
var onPatternSelected: (pattern : PatternEntity) -> Unit | ||
|
||
fun renderControlButtonLabel(controlButtonLabel: String) | ||
fun renderPatternSelectionVisibility(visibility: Boolean) | ||
fun renderBoard(boardViewState: BoardViewState) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...f-life-multiplatform/common/src/main/kotlin/com/novoda/gol/presentation/BoardViewState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.novoda.gol.presentation | ||
|
||
import com.novoda.gol.patterns.PatternEntity | ||
|
||
data class BoardViewState( | ||
val isIdle: Boolean, | ||
val selectedPattern: PatternEntity? = null | ||
) |
8 changes: 8 additions & 0 deletions
8
...life-multiplatform/common/src/main/kotlin/com/novoda/gol/presentation/PatternViewState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.novoda.gol.presentation | ||
|
||
import com.novoda.gol.patterns.PatternEntity | ||
|
||
data class PatternViewState( | ||
var shouldDisplay: Boolean, | ||
val patternEntities: List<PatternEntity> | ||
) |
8 changes: 8 additions & 0 deletions
8
game-of-life-multiplatform/game-of-life-js/src/main/kotlin/com/novoda/gol/Logger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.novoda.gol | ||
|
||
actual object Logger { | ||
|
||
actual fun log(o: Any?) { | ||
console.log(o) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,4 +43,4 @@ fun RBuilder.pattern(patternEntity: PatternEntity, onPatternSelected: () -> Unit | |
} | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
game-of-life-multiplatform/game-of-life-jvm/src/main/kotlin/com/novoda/gol/Logger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.novoda.gol | ||
|
||
actual object Logger { | ||
|
||
actual fun log(o: Any?) { | ||
System.out.println(o) | ||
} | ||
|
||
} |