From f6b3f70e1890a4c3df2f71a3487af393d7f1caf3 Mon Sep 17 00:00:00 2001 From: Fabian Zobrist Date: Wed, 28 Dec 2022 12:30:31 +0100 Subject: [PATCH] Split in more fragments. Trigger gui update trough db change --- app/build.gradle | 1 + .../me/zobrist/tichucounter/MainActivity.kt | 31 +------ .../tichucounter/data/DateConverter.kt | 2 + .../me/zobrist/tichucounter/data/GameDao.kt | 14 +-- .../me/zobrist/tichucounter/data/RoundDao.kt | 3 +- .../tichucounter/fragments/HistoryList.kt | 2 +- .../fragments/HistoryListViewModel.kt | 62 +++++-------- .../tichucounter/fragments/Keyboard.kt | 13 ++- .../fragments/KeyboardViewModel.kt | 9 -- .../tichucounter/fragments/TeamNames.kt | 63 +++++++++++++ .../fragments/TeamNamesViewModel.kt | 58 ++++++++++++ .../tichucounter/fragments/TeamScores.kt | 50 +++++++++++ .../fragments/TeamScoresViewModel.kt | 61 +++++++++++++ .../tichucounter/framework/SingleLiveEvent.kt | 8 +- .../tichucounter/repository/GameRepository.kt | 15 +++- app/src/main/res/layout-land/content_main.xml | 78 ++++------------ app/src/main/res/layout/content_main.xml | 88 ++++--------------- .../main/res/layout/fragment_team_names.xml | 44 ++++++++++ .../main/res/layout/fragment_team_scores.xml | 40 +++++++++ 19 files changed, 413 insertions(+), 229 deletions(-) create mode 100644 app/src/main/java/me/zobrist/tichucounter/fragments/TeamNames.kt create mode 100644 app/src/main/java/me/zobrist/tichucounter/fragments/TeamNamesViewModel.kt create mode 100644 app/src/main/java/me/zobrist/tichucounter/fragments/TeamScores.kt create mode 100644 app/src/main/java/me/zobrist/tichucounter/fragments/TeamScoresViewModel.kt create mode 100644 app/src/main/res/layout/fragment_team_names.xml create mode 100644 app/src/main/res/layout/fragment_team_scores.xml diff --git a/app/build.gradle b/app/build.gradle index ec8d04f..990abce 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -80,6 +80,7 @@ dependencies { implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1' + implementation 'androidx.fragment:fragment:1.4.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.4' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' diff --git a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt index b234aa2..584b610 100644 --- a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt +++ b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt @@ -18,7 +18,6 @@ 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 javax.inject.Inject @@ -29,10 +28,10 @@ class MainActivity : AppCompatActivity() { private var currentRound: Round = Round(0, 0, null, null) private val keyboardViewModel: KeyboardViewModel by viewModels() - private val historyListViewModel: HistoryListViewModel by viewModels() @Inject lateinit var tichu: Tichu + @Inject lateinit var gameRepository: GameRepository @@ -53,25 +52,15 @@ class MainActivity : AppCompatActivity() { val game = gameRepository.getActiveGame() if (game == null) { - val game = gameRepository.newGame() + gameRepository.newGame() } } - historyListViewModel.updateAll() - - updateTheme(this.getSharedPreferences("Settings", MODE_PRIVATE).getInt("Theme", 2)) keepScreenOn( this.getSharedPreferences("Settings", MODE_PRIVATE).getBoolean("Screen_On", false) ) - binding.contentMain.nameTeamA.setText( - this.getSharedPreferences("Settings", MODE_PRIVATE).getString("nameTeamA", "TeamA") - ) - binding.contentMain.nameTeamB.setText( - this.getSharedPreferences("Settings", MODE_PRIVATE).getString("nameTeamB", "TeamB") - ) - keyboardViewModel.scoreA.observe(this) { value -> val oldValue = currentRound.scoreA currentRound.scoreA = value @@ -107,26 +96,12 @@ class MainActivity : AppCompatActivity() { keyboardViewModel.setSubmitButtonEnable(tichu.isValidRound(currentRound)) } } - - keyboardViewModel.submitButtonClicked.observe(this) { - historyListViewModel.updateAll() - } - - historyListViewModel.totalScoreA.observe(this) { value -> - binding.contentMain.scoreA.text = value.toString() - } - - historyListViewModel.totalScoreB.observe(this) { value -> - binding.contentMain.scoreB.text = value.toString() - } } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) val prefs = this.getSharedPreferences("Settings", MODE_PRIVATE).edit() - prefs.putString("nameTeamA", binding.contentMain.nameTeamA.text.toString()) - prefs.putString("nameTeamB", binding.contentMain.nameTeamB.text.toString()) prefs.apply() } @@ -150,7 +125,6 @@ class MainActivity : AppCompatActivity() { dialog.dismiss() GlobalScope.launch(Dispatchers.IO) { gameRepository.newGame() - historyListViewModel.updateAll() } }.setNegativeButton(getString(R.string.no)) { dialog, _ -> dialog.cancel() @@ -164,7 +138,6 @@ class MainActivity : AppCompatActivity() { val history = gameRepository.getActiveRoundHistory() if (history.isNotEmpty()) { gameRepository.removeRoundFromHistory(history.last()) - historyListViewModel.updateAll() } } true diff --git a/app/src/main/java/me/zobrist/tichucounter/data/DateConverter.kt b/app/src/main/java/me/zobrist/tichucounter/data/DateConverter.kt index 4afbe99..9bda853 100644 --- a/app/src/main/java/me/zobrist/tichucounter/data/DateConverter.kt +++ b/app/src/main/java/me/zobrist/tichucounter/data/DateConverter.kt @@ -1,3 +1,5 @@ +package me.zobrist.tichucounter.data + import androidx.room.TypeConverter import java.util.* 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 edb51f9..808b165 100644 --- a/app/src/main/java/me/zobrist/tichucounter/data/GameDao.kt +++ b/app/src/main/java/me/zobrist/tichucounter/data/GameDao.kt @@ -1,20 +1,19 @@ package me.zobrist.tichucounter.data -import androidx.room.Dao -import androidx.room.Delete -import androidx.room.Insert -import androidx.room.Query +import androidx.room.* +import kotlinx.coroutines.flow.Flow + @Dao interface GameDao { @Query("SELECT * FROM game") - fun getAll(): List + fun getAll(): Flow> @Query("SELECT * FROM game WHERE uid is :gameId") fun getGameById(gameId: Long): Game - @Query("SELECT * FROM game WHERE active is 1") + @Query("SELECT * FROM game WHERE active is 1") fun getActive(): Game @Query("UPDATE game SET active = 1 WHERE uid is :gameId;") @@ -26,6 +25,9 @@ interface GameDao { @Insert fun insert(game: Game): Long + @Update + fun update(game: Game) + @Delete fun delete(round: Game) } \ 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 d97f81f..15a433b 100644 --- a/app/src/main/java/me/zobrist/tichucounter/data/RoundDao.kt +++ b/app/src/main/java/me/zobrist/tichucounter/data/RoundDao.kt @@ -4,12 +4,13 @@ import androidx.room.Dao import androidx.room.Delete import androidx.room.Insert import androidx.room.Query +import kotlinx.coroutines.flow.Flow @Dao interface RoundDao { @Query("SELECT * FROM round") - fun getAll(): List + fun getAll(): Flow> @Query("SELECT * FROM round WHERE gameId is :gameId") fun getAllForGame(gameId: Long?): List diff --git a/app/src/main/java/me/zobrist/tichucounter/fragments/HistoryList.kt b/app/src/main/java/me/zobrist/tichucounter/fragments/HistoryList.kt index 6d37ac4..a1ed4bf 100644 --- a/app/src/main/java/me/zobrist/tichucounter/fragments/HistoryList.kt +++ b/app/src/main/java/me/zobrist/tichucounter/fragments/HistoryList.kt @@ -26,7 +26,7 @@ class HistoryList : Fragment() { inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { + ): View { _binding = FragmentHistoryListBinding.inflate(inflater, container, false) return binding.root } 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 81d7ce9..2a0c77d 100644 --- a/app/src/main/java/me/zobrist/tichucounter/fragments/HistoryListViewModel.kt +++ b/app/src/main/java/me/zobrist/tichucounter/fragments/HistoryListViewModel.kt @@ -1,34 +1,42 @@ package me.zobrist.tichucounter.fragments -import SingleLiveEvent import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.launch +import me.zobrist.tichucounter.data.GameDao import me.zobrist.tichucounter.data.Round +import me.zobrist.tichucounter.data.RoundDao +import me.zobrist.tichucounter.framework.SingleLiveEvent import me.zobrist.tichucounter.repository.GameRepository import javax.inject.Inject @HiltViewModel -class HistoryListViewModel @Inject constructor(val gameRepository: GameRepository) : ViewModel() { - private val _totalScoreA: MutableLiveData = MutableLiveData() - private val _totalScoreB: MutableLiveData = MutableLiveData() +class HistoryListViewModel @Inject constructor( + private val roundDao: RoundDao, + private val gameDao: GameDao, + private val gameRepository: GameRepository +) : ViewModel() { private val _historyA: MutableLiveData = MutableLiveData() private val _historyB: MutableLiveData = MutableLiveData() private val _scrollDown: SingleLiveEvent = SingleLiveEvent() - val totalScoreA: LiveData - get() { - return _totalScoreA + init { + viewModelScope.launch { + roundDao.getAll().collect { + update() + } } - val totalScoreB: LiveData - get() { - return _totalScoreB + viewModelScope.launch { + gameDao.getAll().collect { + update() + } } + } val historyA: LiveData get() { @@ -45,20 +53,13 @@ class HistoryListViewModel @Inject constructor(val gameRepository: GameRepositor return _scrollDown } - private fun getScoreA(scores: List) { - var tempScore = 0 - scores.forEach { - it.scoreA?.let { it -> tempScore += it } + private suspend fun update() { + val scores = gameRepository.getActiveRoundHistory() + if (scores != null) { + getHistoryA(scores) + getHistoryB(scores) } - _totalScoreA.value = tempScore - } - - private fun getScoreB(scores: List) { - var tempScore = 0 - scores.forEach { - it.scoreB?.let { it -> tempScore += it } - } - _totalScoreB.value = tempScore + scrollDown() } private fun getHistoryA(scores: List) { @@ -77,21 +78,6 @@ class HistoryListViewModel @Inject constructor(val gameRepository: GameRepositor _historyB.value = tempHistory } - fun updateAll() { - viewModelScope.launch { - val scores = gameRepository.getActiveRoundHistory() - if (scores != null) { - getHistoryA(scores) - getHistoryB(scores) - getScoreA(scores) - getScoreB(scores) - } - scrollDown() - } - - - } - private fun scrollDown() { _scrollDown.value = true } diff --git a/app/src/main/java/me/zobrist/tichucounter/fragments/Keyboard.kt b/app/src/main/java/me/zobrist/tichucounter/fragments/Keyboard.kt index b40645d..73cabbb 100644 --- a/app/src/main/java/me/zobrist/tichucounter/fragments/Keyboard.kt +++ b/app/src/main/java/me/zobrist/tichucounter/fragments/Keyboard.kt @@ -10,7 +10,6 @@ import android.view.inputmethod.InputMethodManager import android.widget.EditText import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels -import androidx.lifecycle.Observer import me.zobrist.tichucounter.databinding.FragmentKeyboardBinding class Keyboard : Fragment() { @@ -51,9 +50,9 @@ class Keyboard : Fragment() { disableSubmitButton() - viewModel.enableSubmitButton.observe(viewLifecycleOwner, Observer { enabled -> + viewModel.enableSubmitButton.observe(viewLifecycleOwner) { enabled -> if (enabled) enableSubmitButton() else disableSubmitButton() - }) + } viewModel.scoreA.observe(viewLifecycleOwner) { value -> updateScore(binding.inputTeamA, value) @@ -146,12 +145,12 @@ class Keyboard : Fragment() { var value = getActiveValue() if (value == null) { - if (getActiveText() == "-") { + unhandledNegation = if (getActiveText() == "-") { setActiveText("") - unhandledNegation = false + false } else { setActiveText("-") - unhandledNegation = true + true } } else { value = value?.times(-1) @@ -194,7 +193,7 @@ class Keyboard : Fragment() { return } - var text = try { + val text = try { score.toString() } catch (e: Exception) { "" diff --git a/app/src/main/java/me/zobrist/tichucounter/fragments/KeyboardViewModel.kt b/app/src/main/java/me/zobrist/tichucounter/fragments/KeyboardViewModel.kt index 4bfd298..d97f549 100644 --- a/app/src/main/java/me/zobrist/tichucounter/fragments/KeyboardViewModel.kt +++ b/app/src/main/java/me/zobrist/tichucounter/fragments/KeyboardViewModel.kt @@ -1,6 +1,5 @@ package me.zobrist.tichucounter.fragments -import SingleLiveEvent import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel @@ -15,8 +14,6 @@ class KeyboardViewModel @Inject constructor(val gameRepository: GameRepository) private val _scoreA: MutableLiveData = MutableLiveData() private val _scoreB: MutableLiveData = MutableLiveData() private val _enableSubmitButton: MutableLiveData = MutableLiveData() - private val _submitButtonClicked: SingleLiveEvent = SingleLiveEvent() - val scoreA: LiveData get() { @@ -33,11 +30,6 @@ class KeyboardViewModel @Inject constructor(val gameRepository: GameRepository) return _enableSubmitButton } - val submitButtonClicked: LiveData - get() { - return _submitButtonClicked - } - fun setScoreA(score: Int?) { _scoreA.value = score } @@ -56,7 +48,6 @@ class KeyboardViewModel @Inject constructor(val gameRepository: GameRepository) _scoreA.value = null _scoreB.value = null setSubmitButtonEnable(false) - _submitButtonClicked.value = true } } } \ No newline at end of file diff --git a/app/src/main/java/me/zobrist/tichucounter/fragments/TeamNames.kt b/app/src/main/java/me/zobrist/tichucounter/fragments/TeamNames.kt new file mode 100644 index 0000000..a91d322 --- /dev/null +++ b/app/src/main/java/me/zobrist/tichucounter/fragments/TeamNames.kt @@ -0,0 +1,63 @@ +package me.zobrist.tichucounter.fragments + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.fragment.app.activityViewModels +import me.zobrist.tichucounter.databinding.FragmentTeamNamesBinding + +class TeamNames : Fragment() { + + private var _binding: FragmentTeamNamesBinding? = null + + // This property is only valid between onCreateView and + // onDestroyView. + private val binding get() = _binding!! + + companion object { + fun newInstance() = TeamNames() + } + + private val viewModel: TeamNamesViewModel by activityViewModels() + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + _binding = FragmentTeamNamesBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onActivityCreated(savedInstanceState) + + binding.nameTeamA.setOnFocusChangeListener { _, focus -> + if (!focus) { + viewModel.setNameA(binding.nameTeamA.text.toString()) + } + } + + binding.nameTeamB.setOnFocusChangeListener { _, focus -> + if (!focus) { + viewModel.setNameB(binding.nameTeamB.text.toString()) + } + } + + viewModel.nameA.observe(viewLifecycleOwner) { value -> + binding.nameTeamA.setText(value) + } + + viewModel.nameB.observe(viewLifecycleOwner) { value -> + binding.nameTeamB.setText(value) + } + + } +} \ No newline at end of file diff --git a/app/src/main/java/me/zobrist/tichucounter/fragments/TeamNamesViewModel.kt b/app/src/main/java/me/zobrist/tichucounter/fragments/TeamNamesViewModel.kt new file mode 100644 index 0000000..f62e457 --- /dev/null +++ b/app/src/main/java/me/zobrist/tichucounter/fragments/TeamNamesViewModel.kt @@ -0,0 +1,58 @@ +package me.zobrist.tichucounter.fragments + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.launch +import me.zobrist.tichucounter.data.GameDao +import me.zobrist.tichucounter.repository.GameRepository +import javax.inject.Inject + +@HiltViewModel +class TeamNamesViewModel @Inject constructor( + private val gameDao: GameDao, + private val gameRepository: GameRepository +) : + ViewModel() { + + private val _nameA: MutableLiveData = MutableLiveData() + private val _nameB: MutableLiveData = MutableLiveData() + + val nameA: LiveData + get() { + return _nameA + } + + val nameB: LiveData + get() { + return _nameB + } + + init { + viewModelScope.launch { + gameDao.getAll().collect() { + val game = gameRepository.getActiveGame() + _nameA.value = game.nameA + _nameB.value = game.nameB + } + } + } + + fun setNameA(name: String) { + viewModelScope.launch { + val game = gameRepository.getActiveGame() + game.nameA = name + gameRepository.updateGame(game) + } + } + + fun setNameB(name: String) { + viewModelScope.launch { + val game = gameRepository.getActiveGame() + game.nameB = name + gameRepository.updateGame(game) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/me/zobrist/tichucounter/fragments/TeamScores.kt b/app/src/main/java/me/zobrist/tichucounter/fragments/TeamScores.kt new file mode 100644 index 0000000..0f5e88d --- /dev/null +++ b/app/src/main/java/me/zobrist/tichucounter/fragments/TeamScores.kt @@ -0,0 +1,50 @@ +package me.zobrist.tichucounter.fragments + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.fragment.app.activityViewModels +import me.zobrist.tichucounter.databinding.FragmentTeamScoresBinding + +class TeamScores : Fragment() { + + private var _binding: FragmentTeamScoresBinding? = null + + // This property is only valid between onCreateView and + // onDestroyView. + private val binding get() = _binding!! + + companion object { + fun newInstance() = TeamScores() + } + + private val viewModel: TeamScoresViewModel by activityViewModels() + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + _binding = FragmentTeamScoresBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onActivityCreated(savedInstanceState) + + viewModel.scoreA.observe(viewLifecycleOwner) { + binding.scoreA.text = it?.toString() ?: "" + } + + viewModel.scoreB.observe(viewLifecycleOwner) { + binding.scoreB.text = it?.toString() ?: "" + } + } +} \ No newline at end of file diff --git a/app/src/main/java/me/zobrist/tichucounter/fragments/TeamScoresViewModel.kt b/app/src/main/java/me/zobrist/tichucounter/fragments/TeamScoresViewModel.kt new file mode 100644 index 0000000..7d03f46 --- /dev/null +++ b/app/src/main/java/me/zobrist/tichucounter/fragments/TeamScoresViewModel.kt @@ -0,0 +1,61 @@ +package me.zobrist.tichucounter.fragments + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.launch +import me.zobrist.tichucounter.data.GameDao +import me.zobrist.tichucounter.data.RoundDao +import me.zobrist.tichucounter.repository.GameRepository +import javax.inject.Inject + +@HiltViewModel +class TeamScoresViewModel @Inject constructor( + private val roundDao: RoundDao, + private val gameDao: GameDao, + private val gameRepository: GameRepository +) : ViewModel() { + private var _scoreA: MutableLiveData = MutableLiveData() + private var _scoreB: MutableLiveData = MutableLiveData() + + init { + viewModelScope.launch { + roundDao.getAll().collect { + update() + } + } + + viewModelScope.launch { + gameDao.getAll().collect { + update() + } + } + } + + private suspend fun update() { + val scores = gameRepository.getActiveRoundHistory() + + var scoreA = 0 + var scoreB = 0 + + scores.forEach { + it.scoreA?.let { a -> scoreA += a } + it.scoreB?.let { b -> scoreB += b } + } + + _scoreA.value = scoreA + _scoreB.value = scoreB + } + + val scoreA: LiveData + get() { + return _scoreA + } + + val scoreB: LiveData + get() { + return _scoreB + } +} \ No newline at end of file diff --git a/app/src/main/java/me/zobrist/tichucounter/framework/SingleLiveEvent.kt b/app/src/main/java/me/zobrist/tichucounter/framework/SingleLiveEvent.kt index bff1787..81ad85c 100644 --- a/app/src/main/java/me/zobrist/tichucounter/framework/SingleLiveEvent.kt +++ b/app/src/main/java/me/zobrist/tichucounter/framework/SingleLiveEvent.kt @@ -1,3 +1,5 @@ +package me.zobrist.tichucounter.framework + import android.util.Log import androidx.annotation.MainThread import androidx.lifecycle.LifecycleOwner @@ -26,11 +28,11 @@ class SingleLiveEvent : MutableLiveData() { Log.w(TAG, "Multiple observers registered but only one will be notified of changes.") } // Observe the internal MutableLiveData - super.observe(owner, Observer { t -> + super.observe(owner) { t -> if (pending.compareAndSet(true, false)) { observer.onChanged(t) } - }) + } } @MainThread @@ -48,6 +50,6 @@ class SingleLiveEvent : MutableLiveData() { } companion object { - private val TAG = "SingleLiveEvent" + private const val TAG = "me.zobrist.tichucounter.framework.SingleLiveEvent" } } \ No newline at end of file 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 e69fd4d..ab1c6a7 100644 --- a/app/src/main/java/me/zobrist/tichucounter/repository/GameRepository.kt +++ b/app/src/main/java/me/zobrist/tichucounter/repository/GameRepository.kt @@ -15,20 +15,31 @@ class GameRepository @Inject constructor( suspend fun newGame(): Game { return withContext(Dispatchers.IO) { - val game = Game(true, "TeamA", "TeamB") + val currentGame = gameDao.getActive() + val game = if (currentGame == null) { + Game(true, "TeamA", "TeamB") + } else { + Game(true, currentGame.nameA, currentGame.nameB) + } val id = gameDao.insert(game) setActive(id) return@withContext gameDao.getActive() } } + suspend fun updateGame(game: Game) { + withContext(Dispatchers.IO) { + gameDao.update(game) + } + } + suspend fun getActiveGame(): Game { return withContext(Dispatchers.IO) { return@withContext gameDao.getActive() } } - suspend fun setActive(id: Long) { + private suspend fun setActive(id: Long) { withContext(Dispatchers.IO) { gameDao.setActive(id) gameDao.setOthersInactive(id) diff --git a/app/src/main/res/layout-land/content_main.xml b/app/src/main/res/layout-land/content_main.xml index 9d900d7..beb18cd 100644 --- a/app/src/main/res/layout-land/content_main.xml +++ b/app/src/main/res/layout-land/content_main.xml @@ -17,73 +17,27 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> - + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" + tools:layout="@layout/fragment_team_names" /> - - - - - - - + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/teamNames" + tools:layout="@layout/fragment_team_scores" /> - - - - - - - - - - - - - - - - - + app:layout_constraintTop_toTopOf="parent" + tools:layout="@layout/fragment_team_names" /> - - - - - + - - + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_team_scores.xml b/app/src/main/res/layout/fragment_team_scores.xml new file mode 100644 index 0000000..2652f84 --- /dev/null +++ b/app/src/main/res/layout/fragment_team_scores.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + \ No newline at end of file