diff --git a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt index 711e4ec..b234aa2 100644 --- a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt +++ b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt @@ -14,14 +14,13 @@ import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch -import me.zobrist.tichucounter.databinding.ActivityMainBinding import me.zobrist.tichucounter.data.Round +import me.zobrist.tichucounter.databinding.ActivityMainBinding import me.zobrist.tichucounter.domain.Tichu import me.zobrist.tichucounter.domain.getAbsoluteDifference import me.zobrist.tichucounter.fragments.HistoryListViewModel import me.zobrist.tichucounter.fragments.KeyboardViewModel import me.zobrist.tichucounter.repository.GameRepository -import java.util.* import javax.inject.Inject @AndroidEntryPoint @@ -32,8 +31,10 @@ class MainActivity : AppCompatActivity() { private val keyboardViewModel: KeyboardViewModel by viewModels() private val historyListViewModel: HistoryListViewModel by viewModels() - @Inject lateinit var tichu: Tichu - @Inject lateinit var gameRepository: GameRepository + @Inject + lateinit var tichu: Tichu + @Inject + lateinit var gameRepository: GameRepository private var ignoreNextUpdate: Boolean = false @@ -51,8 +52,7 @@ class MainActivity : AppCompatActivity() { GlobalScope.launch(Dispatchers.IO) { val game = gameRepository.getActiveGame() - if(game == null) - { + if (game == null) { val game = gameRepository.newGame() } } @@ -162,8 +162,7 @@ class MainActivity : AppCompatActivity() { R.id.action_undo -> { GlobalScope.launch(Dispatchers.IO) { val history = gameRepository.getActiveRoundHistory() - if(history.isNotEmpty()) - { + if (history.isNotEmpty()) { gameRepository.removeRoundFromHistory(history.last()) historyListViewModel.updateAll() } diff --git a/app/src/main/java/me/zobrist/tichucounter/data/AppDatabase.kt b/app/src/main/java/me/zobrist/tichucounter/data/AppDatabase.kt index a7b540b..1fcc51a 100644 --- a/app/src/main/java/me/zobrist/tichucounter/data/AppDatabase.kt +++ b/app/src/main/java/me/zobrist/tichucounter/data/AppDatabase.kt @@ -1,9 +1,7 @@ package me.zobrist.tichucounter.data -import DateConverter import androidx.room.Database import androidx.room.RoomDatabase -import androidx.room.TypeConverter @Database(entities = [Round::class, Game::class], version = 1) abstract class AppDatabase : RoomDatabase() { 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 c8abd00..87e1eea 100644 --- a/app/src/main/java/me/zobrist/tichucounter/data/Game.kt +++ b/app/src/main/java/me/zobrist/tichucounter/data/Game.kt @@ -1,13 +1,12 @@ package me.zobrist.tichucounter.data -import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import java.util.Date @Entity data class Game( val active: Boolean, var nameA: String, var nameB: String, - @PrimaryKey(autoGenerate = true) val uid: Long? = 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/Round.kt b/app/src/main/java/me/zobrist/tichucounter/data/Round.kt index d02e016..81c9fe5 100644 --- a/app/src/main/java/me/zobrist/tichucounter/data/Round.kt +++ b/app/src/main/java/me/zobrist/tichucounter/data/Round.kt @@ -1,6 +1,5 @@ package me.zobrist.tichucounter.data -import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey @@ -9,4 +8,5 @@ data class Round( var gameId: Long, var scoreA: Int?, var scoreB: Int?, - @PrimaryKey(autoGenerate = true) val uid: Long? = 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/fragments/HistoryListViewModel.kt b/app/src/main/java/me/zobrist/tichucounter/fragments/HistoryListViewModel.kt index 339e3a4..81d7ce9 100644 --- a/app/src/main/java/me/zobrist/tichucounter/fragments/HistoryListViewModel.kt +++ b/app/src/main/java/me/zobrist/tichucounter/fragments/HistoryListViewModel.kt @@ -20,7 +20,6 @@ class HistoryListViewModel @Inject constructor(val gameRepository: GameRepositor private val _scrollDown: SingleLiveEvent = SingleLiveEvent() - val totalScoreA: LiveData get() { return _totalScoreA 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 fe780e2..e69fd4d 100644 --- a/app/src/main/java/me/zobrist/tichucounter/repository/GameRepository.kt +++ b/app/src/main/java/me/zobrist/tichucounter/repository/GameRepository.kt @@ -8,7 +8,10 @@ import me.zobrist.tichucounter.data.Round import me.zobrist.tichucounter.data.RoundDao import javax.inject.Inject -class GameRepository @Inject constructor(private val gameDao: GameDao, private val roundDao: RoundDao) { +class GameRepository @Inject constructor( + private val gameDao: GameDao, + private val roundDao: RoundDao +) { suspend fun newGame(): Game { return withContext(Dispatchers.IO) { @@ -17,7 +20,6 @@ class GameRepository @Inject constructor(private val gameDao: GameDao, private setActive(id) return@withContext gameDao.getActive() } - } suspend fun getActiveGame(): Game { @@ -26,45 +28,31 @@ class GameRepository @Inject constructor(private val gameDao: GameDao, private } } - suspend fun setActive(game: Game) - { - withContext(Dispatchers.IO) { - game.uid?.let { gameDao.setActive(it) } - game.uid?.let { gameDao.setOthersInactive(it) } - } - } - - suspend fun setActive(id: Long) - { + suspend fun setActive(id: Long) { withContext(Dispatchers.IO) { gameDao.setActive(id) gameDao.setOthersInactive(id) } } - suspend fun getActiveRoundHistory(): List - { + suspend fun getActiveRoundHistory(): List { return withContext(Dispatchers.IO) { val active = getActiveGame() - if(active?.uid != null) - { + if (active?.uid != null) { return@withContext roundDao.getAllForGame(active.uid) - } - else { + } else { return@withContext listOf() } } } - suspend fun removeRoundFromHistory(round: Round) - { + suspend fun removeRoundFromHistory(round: Round) { withContext(Dispatchers.IO) { roundDao.delete(round) } } - suspend fun addRoundToActiveGame(scoreA: Int, scoreB: Int) - { + suspend fun addRoundToActiveGame(scoreA: Int, scoreB: Int) { withContext(Dispatchers.IO) { val active = getActiveGame() val round = Round(active.uid!!, scoreA, scoreB)