From 1703cd87cb31bb7be5d51beee442fd6e954ff94e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= <oli_r@fg4f.de>
Date: Thu, 19 Aug 2021 11:27:25 +0200
Subject: [PATCH] Extract Board to Composable

---
 src/main/kotlin/GameBoard.kt          | 55 +++++++++++++++++++++
 src/main/kotlin/Main.kt               | 69 +++++++++++----------------
 src/main/kotlin/components/EndCard.kt |  9 +++-
 3 files changed, 90 insertions(+), 43 deletions(-)
 create mode 100644 src/main/kotlin/GameBoard.kt

diff --git a/src/main/kotlin/GameBoard.kt b/src/main/kotlin/GameBoard.kt
new file mode 100644
index 0000000..176fcea
--- /dev/null
+++ b/src/main/kotlin/GameBoard.kt
@@ -0,0 +1,55 @@
+import androidx.compose.foundation.layout.*
+import androidx.compose.material.Button
+import androidx.compose.material.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.unit.dp
+import components.GameHeader
+import components.PlayerBar
+import components.QuestionGrid
+import data.Player
+
+@Composable
+@Suppress("FunctionName")
+fun GameBoard(
+    reviewModeActive: Boolean,
+    secondRoundDouble: Boolean,
+    playerPointMap: MutableMap<Player, Long>,
+    onDisableReviewMode: () -> Unit,
+    onChangeQuestionsPlayed: () -> Unit
+) {
+    Column(modifier = Modifier.fillMaxHeight().fillMaxWidth()) {
+        Row(
+            modifier = Modifier.fillMaxHeight(0.2f)
+        ) {
+            GameHeader(
+                title = game.title,
+                color = game.headerColor
+            )
+        }
+        Spacer(modifier = Modifier.height(5.dp))
+        Row(
+            modifier = Modifier.fillMaxHeight()
+        ) {
+            if (reviewModeActive) {
+                Button(onClick = onDisableReviewMode) {
+                    Text(text = "Back")
+                }
+            }
+            Column(Modifier.fillMaxWidth(0.8f)) {
+                QuestionGrid(
+                    game,
+                    secondRoundDouble,
+                    onResolveQuestion = onChangeQuestionsPlayed,
+                    onPointsChange = { player, points ->
+                        playerPointMap[player] = playerPointMap[player]?.plus(points) ?: 0
+                    }
+                )
+            }
+            Spacer(modifier = Modifier.width(5.dp))
+            Column(Modifier.fillMaxWidth()) {
+                PlayerBar(players = playerPointMap)
+            }
+        }
+    }
+}
diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt
index 97d144e..b1a7583 100644
--- a/src/main/kotlin/Main.kt
+++ b/src/main/kotlin/Main.kt
@@ -1,14 +1,8 @@
-import androidx.compose.foundation.layout.*
 import androidx.compose.material.MaterialTheme
 import androidx.compose.runtime.*
-import androidx.compose.ui.Modifier
-import androidx.compose.ui.unit.dp
 import androidx.compose.ui.window.Window
 import androidx.compose.ui.window.application
 import components.EndCard
-import components.GameHeader
-import components.PlayerBar
-import components.QuestionGrid
 import data.*
 
 val game = Game(
@@ -17,7 +11,8 @@ val game = Game(
     endGameAfter = 2,
     players = listOf(
         Player(name = "Oli"),
-        Player(name = "Gesina")
+        Player(name = "Gesina"),
+        Player(name = "Siggi")
     ),
     topics = listOf(
         Topic(
@@ -26,7 +21,7 @@ val game = Game(
                 green = 0x44,
                 blue = 0xAA
             ),
-            topic = "Wer MACKt's?",
+            topic = "Wer?",
             questions = listOf(
                 QuestionData(
                     hint = "Mack",
@@ -90,6 +85,21 @@ val game = Game(
                     points = 5u
                 )
             )
+        ),
+        Topic(
+            topic = "Topic4",
+            questions = listOf(
+                QuestionData(
+                    hint = "Some",
+                    answer = "thing",
+                    points = 1000u
+                ),
+                QuestionData(
+                    hint = "Other",
+                    answer = "Thing",
+                    points = 5u
+                )
+            )
         )
     )
 )
@@ -108,43 +118,20 @@ fun main() = application {
             var questionsPlayed by remember { mutableStateOf(0) }
             val secondRoundDouble by remember { derivedStateOf { questionsPlayed >= game.doubleAfter } }
             val gameEnded by remember { derivedStateOf { questionsPlayed >= game.endGameAfter } }
+            var reviewModeActive by remember { mutableStateOf(false) }
 
-            if (!gameEnded) {
-                Column(modifier = Modifier.fillMaxHeight().fillMaxWidth()) {
-                    Row(
-                        modifier = Modifier.fillMaxHeight(0.2f)
-                    ) {
-                        GameHeader(
-                            title = game.title,
-                            color = game.headerColor
-                        )
-                    }
-                    Spacer(modifier = Modifier.height(5.dp))
-                    Row(
-                        modifier = Modifier.fillMaxHeight()
-                    ) {
-                        Column(Modifier.fillMaxWidth(0.8f)) {
-                            QuestionGrid(
-                                game,
-                                secondRoundDouble,
-                                onResolveQuestion = {
-                                    questionsPlayed++
-                                    println("Questions: $questionsPlayed, Double: $secondRoundDouble")
-                                },
-                                onPointsChange = { player, points ->
-                                    playerPointMap[player] = playerPointMap[player]?.plus(points) ?: 0
-                                }
-                            )
-                        }
-                        Spacer(modifier = Modifier.width(5.dp))
-                        Column(Modifier.fillMaxWidth()) {
-                            PlayerBar(players = playerPointMap)
-                        }
-                    }
-                }
+            if (!gameEnded || reviewModeActive) {
+                GameBoard(
+                    reviewModeActive,
+                    secondRoundDouble,
+                    playerPointMap,
+                    onDisableReviewMode = { reviewModeActive = false },
+                    onChangeQuestionsPlayed = { questionsPlayed++ }
+                )
             } else {
                 EndCard(
                     playerPointMap = playerPointMap,
+                    reviewBoard = { reviewModeActive = true },
                     close = ::exitApplication
                 )
             }
diff --git a/src/main/kotlin/components/EndCard.kt b/src/main/kotlin/components/EndCard.kt
index 0770bde..239a223 100644
--- a/src/main/kotlin/components/EndCard.kt
+++ b/src/main/kotlin/components/EndCard.kt
@@ -14,6 +14,7 @@ import data.toColor
 @Composable
 fun EndCard(
     playerPointMap: MutableMap<Player, Long>,
+    reviewBoard: () -> Unit,
     close: () -> Unit
 ) {
     // TODO: Style
@@ -35,7 +36,7 @@ fun EndCard(
             for ((index, data) in sortedPlayersPoints.withIndex()) {
                 val player = data.key
                 Row(horizontalArrangement = Arrangement.SpaceBetween) {
-                    Text( text = "${index+1}.")
+                    Text(text = "${index + 1}.")
                     Surface(
                         color = player.color.toColor()
                     ) {
@@ -43,7 +44,11 @@ fun EndCard(
                     }
                 }
             }
-
+            Button(
+                onClick = reviewBoard
+            ) {
+                Text(text = "Review")
+            }
             Button(
                 onClick = close
             ) {