diff --git a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt index debc04e..e64f60a 100644 --- a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt +++ b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt @@ -178,7 +178,10 @@ class MainActivity : AppCompatActivity() { } private fun clearAll() { - //historyListViewModel.clearAll() + GlobalScope.launch(Dispatchers.IO) { + gameRepository.newGame() + historyListViewModel.updateAll() + } } diff --git a/app/src/main/java/me/zobrist/tichucounter/data/Game.kt b/app/src/main/java/me/zobrist/tichucounter/data/Game.kt index 948b454..c8abd00 100644 --- a/app/src/main/java/me/zobrist/tichucounter/data/Game.kt +++ b/app/src/main/java/me/zobrist/tichucounter/data/Game.kt @@ -10,4 +10,4 @@ data class Game( val active: Boolean, var nameA: String, var nameB: String, - @PrimaryKey(autoGenerate = true) val uid: Int? = null) \ No newline at end of file + @PrimaryKey(autoGenerate = true) val uid: Long? = null) \ No newline at end of file diff --git a/app/src/main/java/me/zobrist/tichucounter/data/GameDao.kt b/app/src/main/java/me/zobrist/tichucounter/data/GameDao.kt index a2a5b69..edb51f9 100644 --- a/app/src/main/java/me/zobrist/tichucounter/data/GameDao.kt +++ b/app/src/main/java/me/zobrist/tichucounter/data/GameDao.kt @@ -12,19 +12,19 @@ interface GameDao { fun getAll(): List @Query("SELECT * FROM game WHERE uid is :gameId") - fun getGameById(gameId: Int): Game + fun getGameById(gameId: Long): Game @Query("SELECT * FROM game WHERE active is 1") fun getActive(): Game @Query("UPDATE game SET active = 1 WHERE uid is :gameId;") - fun setActive(gameId: Int) + fun setActive(gameId: Long) @Query("UPDATE game SET active = 0 WHERE uid is not :gameId;") - fun setOthersInactive(gameId: Int) + fun setOthersInactive(gameId: Long) @Insert - fun insertAll(vararg users: Game) + fun insert(game: Game): Long @Delete fun delete(round: Game) diff --git a/app/src/main/java/me/zobrist/tichucounter/data/Round.kt b/app/src/main/java/me/zobrist/tichucounter/data/Round.kt index 6d6aab9..d02e016 100644 --- a/app/src/main/java/me/zobrist/tichucounter/data/Round.kt +++ b/app/src/main/java/me/zobrist/tichucounter/data/Round.kt @@ -6,7 +6,7 @@ import androidx.room.PrimaryKey @Entity data class Round( - var gameId: Int, + var gameId: Long, var scoreA: Int?, var scoreB: Int?, - @PrimaryKey(autoGenerate = true) val uid: Int? = null) \ No newline at end of file + @PrimaryKey(autoGenerate = true) val uid: Long? = null) \ No newline at end of file diff --git a/app/src/main/java/me/zobrist/tichucounter/data/RoundDao.kt b/app/src/main/java/me/zobrist/tichucounter/data/RoundDao.kt index 3d0a7ee..d97f81f 100644 --- a/app/src/main/java/me/zobrist/tichucounter/data/RoundDao.kt +++ b/app/src/main/java/me/zobrist/tichucounter/data/RoundDao.kt @@ -12,7 +12,7 @@ interface RoundDao { fun getAll(): List @Query("SELECT * FROM round WHERE gameId is :gameId") - fun getAllForGame(gameId: Int?): List + fun getAllForGame(gameId: Long?): List @Insert fun insertAll(vararg rounds: Round) diff --git a/app/src/main/java/me/zobrist/tichucounter/repository/GameRepository.kt b/app/src/main/java/me/zobrist/tichucounter/repository/GameRepository.kt index 1ef4c0d..8bd8ed3 100644 --- a/app/src/main/java/me/zobrist/tichucounter/repository/GameRepository.kt +++ b/app/src/main/java/me/zobrist/tichucounter/repository/GameRepository.kt @@ -13,8 +13,8 @@ class GameRepository @Inject constructor(private val gameDao: GameDao, private suspend fun newGame(): Game { return withContext(Dispatchers.IO) { val game = Game(true, "TeamA", "TeamB") - gameDao.insertAll(game) - setActive(game) + val id = gameDao.insert(game) + setActive(id) return@withContext gameDao.getActive() } @@ -34,6 +34,14 @@ class GameRepository @Inject constructor(private val gameDao: GameDao, private } } + suspend fun setActive(id: Long) + { + withContext(Dispatchers.IO) { + gameDao.setActive(id) + gameDao.setOthersInactive(id) + } + } + suspend fun getActiveRoundHistory(): List { return withContext(Dispatchers.IO) {