Move all fragments to ui package. Store created and modified date to game.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-01-06 11:09:06 +01:00
parent 94cdbcad0b
commit 1e428e854e
29 changed files with 99 additions and 91 deletions

View File

@@ -10,7 +10,8 @@
android:localeConfig="@xml/locales_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
android:dataExtractionRules="@xml/data_extraction_rules">
<activity
android:name=".DrawerActivity"
android:exported="false"

View File

@@ -36,7 +36,7 @@ abstract class BaseActivity : AppCompatActivity(),
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String?) {
when(key) {
when (key) {
"language" -> setLanguage(sharedPreferences.getString(key, null))
"screen_on" -> keepScreenOn(sharedPreferences.getBoolean(key, false))
"theme" -> updateTheme(sharedPreferences.getString(key, null))

View File

@@ -2,8 +2,10 @@ package me.zobrist.tichucounter.data
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
@Database(entities = [Round::class, Game::class], version = 1)
@TypeConverters(DateConverter::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun roundDao(): RoundDao
abstract fun gameDao(): GameDao

View File

@@ -1,8 +1,10 @@
package me.zobrist.tichucounter.data
import androidx.room.ProvidedTypeConverter
import androidx.room.TypeConverter
import java.util.*
@ProvidedTypeConverter
object DateConverter {
@TypeConverter
fun toDate(dateLong: Long?): Date? {

View File

@@ -2,11 +2,14 @@ package me.zobrist.tichucounter.data
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.*
@Entity
data class Game(
val active: Boolean,
var nameA: String,
var nameB: String,
val created: Date,
var modified: Date,
@PrimaryKey(autoGenerate = true) val uid: Long? = null
)

View File

@@ -7,14 +7,11 @@ import kotlinx.coroutines.flow.Flow
@Dao
interface GameDao : DaoBase<Game> {
@Query("SELECT * FROM game")
fun getAll(): Flow<List<Game>>
@Query("SELECT * FROM game WHERE uid is :gameId")
fun getGameById(gameId: Long): Flow<Game>
@Query("SELECT * FROM game WHERE active is 1")
fun getActive(): Game
fun getActive(): Flow<Game?>
@Query("UPDATE game SET active = 1 WHERE uid is :gameId;")
fun setActive(gameId: Long)

View File

@@ -6,9 +6,6 @@ import kotlinx.coroutines.flow.Flow
@Dao
interface RoundDao : DaoBase<Round> {
@Query("SELECT * FROM round")
fun getAll(): Flow<List<Round>>
@Query("SELECT * FROM round WHERE gameId is :gameId")
fun getAllForGame(gameId: Long?): List<Round>
@@ -19,8 +16,4 @@ interface RoundDao : DaoBase<Round> {
"WHERE game.active == 1"
)
fun getForActiveGame(): Flow<List<Round>>
@Insert
fun insertAll(vararg rounds: Round)
}

View File

@@ -1,11 +1,14 @@
package me.zobrist.tichucounter.repository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.zobrist.tichucounter.data.Game
import me.zobrist.tichucounter.data.GameDao
import me.zobrist.tichucounter.data.Round
import me.zobrist.tichucounter.data.RoundDao
import java.util.*
import javax.inject.Inject
class GameRepository @Inject constructor(
@@ -13,32 +16,40 @@ class GameRepository @Inject constructor(
private val roundDao: RoundDao
) {
suspend fun newGame(): Game {
return withContext(Dispatchers.IO) {
val currentGame = gameDao.getActive()
val game = if (currentGame == null) {
Game(true, "TeamA", "TeamB")
} else {
Game(true, currentGame.nameA, currentGame.nameB)
private var _activeGame: Game? = null
val activeGame: Game
get() {
return _activeGame!!
}
init {
CoroutineScope(Dispatchers.IO).launch {
gameDao.getActive().collect {
if (it == null) {
gameDao.insert(Game(true, "TeamA", "TeamB", Date(), Date()))
} else {
_activeGame = it
}
}
val id = gameDao.insert(game)
}
}
suspend fun newGame() {
withContext(Dispatchers.IO) {
val id =
gameDao.insert(Game(true, activeGame.nameA, activeGame.nameB, Date(), Date()))
setActive(id)
return@withContext gameDao.getActive()
}
}
suspend fun updateGame(game: Game) {
game.modified = Date()
withContext(Dispatchers.IO) {
gameDao.update(game)
}
}
suspend fun getActiveGame(): Game {
return withContext(Dispatchers.IO) {
return@withContext gameDao.getActive()
}
}
private suspend fun setActive(id: Long) {
withContext(Dispatchers.IO) {
gameDao.setActive(id)
@@ -46,28 +57,23 @@ class GameRepository @Inject constructor(
}
}
suspend fun getActiveRoundHistory(): List<Round> {
return withContext(Dispatchers.IO) {
val active = getActiveGame()
if (active?.uid != null) {
return@withContext roundDao.getAllForGame(active.uid)
} else {
return@withContext listOf<Round>()
}
}
}
suspend fun removeRoundFromHistory(round: Round) {
suspend fun revertLastRound() {
withContext(Dispatchers.IO) {
roundDao.delete(round)
try {
val lastRound = roundDao.getAllForGame(activeGame.uid).last()
roundDao.delete(lastRound)
} catch (_: NoSuchElementException) {
}
}
}
suspend fun addRoundToActiveGame(scoreA: Int, scoreB: Int) {
withContext(Dispatchers.IO) {
val active = getActiveGame()
val active = activeGame
active.modified = Date()
val round = Round(active.uid!!, scoreA, scoreB)
roundDao.insertAll(round)
roundDao.insert(round)
gameDao.update(active)
}
}
}

View File

@@ -1,4 +1,4 @@
package me.zobrist.tichucounter.fragments
package me.zobrist.tichucounter.ui
import android.os.Bundle
import android.view.LayoutInflater

View File

@@ -15,9 +15,8 @@ import me.zobrist.tichucounter.data.Round
import me.zobrist.tichucounter.databinding.FragmentCounterBinding
import me.zobrist.tichucounter.domain.Tichu
import me.zobrist.tichucounter.domain.getAbsoluteDifference
import me.zobrist.tichucounter.fragments.FragmentBase
import me.zobrist.tichucounter.fragments.KeyboardViewModel
import me.zobrist.tichucounter.repository.GameRepository
import me.zobrist.tichucounter.ui.FragmentBase
import javax.inject.Inject
@AndroidEntryPoint
@@ -107,10 +106,7 @@ class CounterFragment : FragmentBase<FragmentCounterBinding>(), MenuProvider {
}
R.id.action_undo -> {
viewLifecycleOwner.lifecycleScope.launch {
val history = gameRepository.getActiveRoundHistory()
if (history.isNotEmpty()) {
gameRepository.removeRoundFromHistory(history.last())
}
gameRepository.revertLastRound()
}
true
}

View File

@@ -1,4 +1,4 @@
package me.zobrist.tichucounter.fragments
package me.zobrist.tichucounter.ui.counter
import android.os.Bundle
import android.view.LayoutInflater
@@ -7,6 +7,7 @@ import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import dagger.hilt.android.AndroidEntryPoint
import me.zobrist.tichucounter.databinding.FragmentHistoryListBinding
import me.zobrist.tichucounter.ui.FragmentBase
@AndroidEntryPoint
class HistoryList : FragmentBase<FragmentHistoryListBinding>() {

View File

@@ -1,4 +1,4 @@
package me.zobrist.tichucounter.fragments
package me.zobrist.tichucounter.ui.counter
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
@@ -20,7 +20,7 @@ class HistoryListViewModel @Inject constructor(private val roundDao: RoundDao) :
init {
viewModelScope.launch {
roundDao.getForActiveGame().collect() { scores ->
roundDao.getForActiveGame().collect { scores ->
update(scores)
}
}

View File

@@ -1,4 +1,4 @@
package me.zobrist.tichucounter.fragments
package me.zobrist.tichucounter.ui.counter
import android.content.Context
import android.os.Bundle
@@ -11,6 +11,7 @@ import android.widget.EditText
import androidx.fragment.app.activityViewModels
import dagger.hilt.android.AndroidEntryPoint
import me.zobrist.tichucounter.databinding.FragmentKeyboardBinding
import me.zobrist.tichucounter.ui.FragmentBase
@AndroidEntryPoint
class Keyboard : FragmentBase<FragmentKeyboardBinding>() {

View File

@@ -1,4 +1,4 @@
package me.zobrist.tichucounter.fragments
package me.zobrist.tichucounter.ui.counter
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData

View File

@@ -1,4 +1,4 @@
package me.zobrist.tichucounter.fragments
package me.zobrist.tichucounter.ui.counter
import android.os.Bundle
import android.view.LayoutInflater
@@ -8,6 +8,7 @@ import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.activityViewModels
import dagger.hilt.android.AndroidEntryPoint
import me.zobrist.tichucounter.databinding.FragmentTeamNamesBinding
import me.zobrist.tichucounter.ui.FragmentBase
@AndroidEntryPoint
class TeamNames : FragmentBase<FragmentTeamNamesBinding>() {

View File

@@ -1,4 +1,4 @@
package me.zobrist.tichucounter.fragments
package me.zobrist.tichucounter.ui.counter
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
@@ -32,17 +32,16 @@ class TeamNamesViewModel @Inject constructor(
init {
viewModelScope.launch {
gameDao.getAll().collect() {
val game = gameRepository.getActiveGame()
_nameA.value = game.nameA
_nameB.value = game.nameB
gameDao.getActive().collect {
_nameA.value = it?.nameA
_nameB.value = it?.nameB
}
}
}
fun setNameA(name: String) {
viewModelScope.launch {
val game = gameRepository.getActiveGame()
val game = gameRepository.activeGame
game.nameA = name
gameRepository.updateGame(game)
}
@@ -50,7 +49,7 @@ class TeamNamesViewModel @Inject constructor(
fun setNameB(name: String) {
viewModelScope.launch {
val game = gameRepository.getActiveGame()
val game = gameRepository.activeGame
game.nameB = name
gameRepository.updateGame(game)
}

View File

@@ -1,4 +1,4 @@
package me.zobrist.tichucounter.fragments
package me.zobrist.tichucounter.ui.counter
import android.os.Bundle
import android.view.LayoutInflater
@@ -7,6 +7,7 @@ import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import dagger.hilt.android.AndroidEntryPoint
import me.zobrist.tichucounter.databinding.FragmentTeamScoresBinding
import me.zobrist.tichucounter.ui.FragmentBase
@AndroidEntryPoint
class TeamScores : FragmentBase<FragmentTeamScoresBinding>() {

View File

@@ -1,4 +1,4 @@
package me.zobrist.tichucounter.fragments
package me.zobrist.tichucounter.ui.counter
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
@@ -18,7 +18,7 @@ class TeamScoresViewModel @Inject constructor(private val roundDao: RoundDao) :
init {
viewModelScope.launch {
roundDao.getForActiveGame().collect() { scores ->
roundDao.getForActiveGame().collect { scores ->
update(scores)
}
}

View File

@@ -22,12 +22,11 @@ class SlideshowFragment : Fragment() {
savedInstanceState: Bundle?
): View {
val slideshowViewModel =
ViewModelProvider(this).get(SlideshowViewModel::class.java)
ViewModelProvider(this)[SlideshowViewModel::class.java]
_binding = FragmentSlideshowBinding.inflate(inflater, container, false)
val root: View = binding.root
return root
return binding.root
}
override fun onDestroyView() {

View File

@@ -19,7 +19,7 @@
<androidx.fragment.app.FragmentContainerView
android:id="@+id/teamNames"
android:name="me.zobrist.tichucounter.fragments.TeamNames"
android:name="me.zobrist.tichucounter.ui.counter.TeamNames"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
@@ -29,7 +29,7 @@
<androidx.fragment.app.FragmentContainerView
android:id="@+id/teamScores"
android:name="me.zobrist.tichucounter.fragments.TeamScores"
android:name="me.zobrist.tichucounter.ui.counter.TeamScores"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
@@ -39,7 +39,7 @@
<androidx.fragment.app.FragmentContainerView
android:id="@+id/scrollHistory"
android:name="me.zobrist.tichucounter.fragments.HistoryList"
android:name="me.zobrist.tichucounter.ui.counter.HistoryList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
@@ -59,7 +59,7 @@
<androidx.fragment.app.FragmentContainerView
android:id="@+id/keyboard"
android:name="me.zobrist.tichucounter.fragments.Keyboard"
android:name="me.zobrist.tichucounter.ui.counter.Keyboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"

View File

@@ -8,7 +8,7 @@
<androidx.fragment.app.FragmentContainerView
android:id="@+id/teamNames"
android:name="me.zobrist.tichucounter.fragments.TeamNames"
android:name="me.zobrist.tichucounter.ui.counter.TeamNames"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
@@ -18,7 +18,7 @@
<androidx.fragment.app.FragmentContainerView
android:id="@+id/teamScores"
android:name="me.zobrist.tichucounter.fragments.TeamScores"
android:name="me.zobrist.tichucounter.ui.counter.TeamScores"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
@@ -41,7 +41,7 @@
<androidx.fragment.app.FragmentContainerView
android:id="@+id/scrollHistory"
android:name="me.zobrist.tichucounter.fragments.HistoryList"
android:name="me.zobrist.tichucounter.ui.counter.HistoryList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="16dp"
@@ -62,7 +62,7 @@
<androidx.fragment.app.FragmentContainerView
android:id="@+id/keyboard"
android:name="me.zobrist.tichucounter.fragments.Keyboard"
android:name="me.zobrist.tichucounter.ui.counter.Keyboard"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"

View File

@@ -3,7 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.HistoryList">
tools:context=".ui.counter.HistoryList">
<ScrollView
android:id="@+id/scrollViewHistory"
@@ -12,7 +12,7 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout

View File

@@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragments.Keyboard">
tools:context=".ui.counter.Keyboard">
<LinearLayout
android:layout_width="match_parent"

View File

@@ -3,7 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.TeamNames">
tools:context=".ui.counter.TeamNames">
<LinearLayout
android:id="@+id/viewNames"

View File

@@ -3,7 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.TeamScores">
tools:context=".ui.counter.TeamScores">
<LinearLayout
android:id="@+id/viewScore"

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<data-extraction-rules>
<cloud-backup>
</cloud-backup>
</data-extraction-rules>

View File

@@ -17,7 +17,7 @@ class TichuUnitTest {
var inputScoreA = 125
var inputScoreB = -25
val tichu: Tichu = Tichu()
val tichu = Tichu()
// Normal round range -25 to 125 as input
while (inputScoreB <= 125) {