Change primary key to long. Implement newGame menu option.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-12-27 18:16:14 +01:00
parent 09739ccc8e
commit bdf3c0a98e
6 changed files with 22 additions and 11 deletions

View File

@@ -178,7 +178,10 @@ class MainActivity : AppCompatActivity() {
} }
private fun clearAll() { private fun clearAll() {
//historyListViewModel.clearAll() GlobalScope.launch(Dispatchers.IO) {
gameRepository.newGame()
historyListViewModel.updateAll()
}
} }

View File

@@ -10,4 +10,4 @@ data class Game(
val active: Boolean, val active: Boolean,
var nameA: String, var nameA: String,
var nameB: String, var nameB: String,
@PrimaryKey(autoGenerate = true) val uid: Int? = null) @PrimaryKey(autoGenerate = true) val uid: Long? = null)

View File

@@ -12,19 +12,19 @@ interface GameDao {
fun getAll(): List<Game> fun getAll(): List<Game>
@Query("SELECT * FROM game WHERE uid is :gameId") @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") @Query("SELECT * FROM game WHERE active is 1")
fun getActive(): Game fun getActive(): Game
@Query("UPDATE game SET active = 1 WHERE uid is :gameId;") @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;") @Query("UPDATE game SET active = 0 WHERE uid is not :gameId;")
fun setOthersInactive(gameId: Int) fun setOthersInactive(gameId: Long)
@Insert @Insert
fun insertAll(vararg users: Game) fun insert(game: Game): Long
@Delete @Delete
fun delete(round: Game) fun delete(round: Game)

View File

@@ -6,7 +6,7 @@ import androidx.room.PrimaryKey
@Entity @Entity
data class Round( data class Round(
var gameId: Int, var gameId: Long,
var scoreA: Int?, var scoreA: Int?,
var scoreB: Int?, var scoreB: Int?,
@PrimaryKey(autoGenerate = true) val uid: Int? = null) @PrimaryKey(autoGenerate = true) val uid: Long? = null)

View File

@@ -12,7 +12,7 @@ interface RoundDao {
fun getAll(): List<Round> fun getAll(): List<Round>
@Query("SELECT * FROM round WHERE gameId is :gameId") @Query("SELECT * FROM round WHERE gameId is :gameId")
fun getAllForGame(gameId: Int?): List<Round> fun getAllForGame(gameId: Long?): List<Round>
@Insert @Insert
fun insertAll(vararg rounds: Round) fun insertAll(vararg rounds: Round)

View File

@@ -13,8 +13,8 @@ class GameRepository @Inject constructor(private val gameDao: GameDao, private
suspend fun newGame(): Game { suspend fun newGame(): Game {
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {
val game = Game(true, "TeamA", "TeamB") val game = Game(true, "TeamA", "TeamB")
gameDao.insertAll(game) val id = gameDao.insert(game)
setActive(game) setActive(id)
return@withContext gameDao.getActive() 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<Round> suspend fun getActiveRoundHistory(): List<Round>
{ {
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {