Split in more fragments. Trigger gui update trough db change
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -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'
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
package me.zobrist.tichucounter.data
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
import java.util.*
|
||||
|
||||
|
||||
@@ -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<Game>
|
||||
fun getAll(): Flow<List<Game>>
|
||||
|
||||
@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)
|
||||
}
|
||||
@@ -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<Round>
|
||||
fun getAll(): Flow<List<Round>>
|
||||
|
||||
@Query("SELECT * FROM round WHERE gameId is :gameId")
|
||||
fun getAllForGame(gameId: Long?): List<Round>
|
||||
|
||||
@@ -26,7 +26,7 @@ class HistoryList : Fragment() {
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
): View {
|
||||
_binding = FragmentHistoryListBinding.inflate(inflater, container, false)
|
||||
return binding.root
|
||||
}
|
||||
|
||||
@@ -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<Int> = MutableLiveData<Int>()
|
||||
private val _totalScoreB: MutableLiveData<Int> = MutableLiveData<Int>()
|
||||
class HistoryListViewModel @Inject constructor(
|
||||
private val roundDao: RoundDao,
|
||||
private val gameDao: GameDao,
|
||||
private val gameRepository: GameRepository
|
||||
) : ViewModel() {
|
||||
private val _historyA: MutableLiveData<String> = MutableLiveData<String>()
|
||||
private val _historyB: MutableLiveData<String> = MutableLiveData<String>()
|
||||
private val _scrollDown: SingleLiveEvent<Boolean> = SingleLiveEvent()
|
||||
|
||||
|
||||
val totalScoreA: LiveData<Int>
|
||||
get() {
|
||||
return _totalScoreA
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
roundDao.getAll().collect {
|
||||
update()
|
||||
}
|
||||
}
|
||||
|
||||
val totalScoreB: LiveData<Int>
|
||||
get() {
|
||||
return _totalScoreB
|
||||
viewModelScope.launch {
|
||||
gameDao.getAll().collect {
|
||||
update()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val historyA: LiveData<String>
|
||||
get() {
|
||||
@@ -45,20 +53,13 @@ class HistoryListViewModel @Inject constructor(val gameRepository: GameRepositor
|
||||
return _scrollDown
|
||||
}
|
||||
|
||||
private fun getScoreA(scores: List<Round>) {
|
||||
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<Round>) {
|
||||
var tempScore = 0
|
||||
scores.forEach {
|
||||
it.scoreB?.let { it -> tempScore += it }
|
||||
}
|
||||
_totalScoreB.value = tempScore
|
||||
scrollDown()
|
||||
}
|
||||
|
||||
private fun getHistoryA(scores: List<Round>) {
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
""
|
||||
|
||||
@@ -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<Int?> = MutableLiveData()
|
||||
private val _scoreB: MutableLiveData<Int?> = MutableLiveData()
|
||||
private val _enableSubmitButton: MutableLiveData<Boolean> = MutableLiveData()
|
||||
private val _submitButtonClicked: SingleLiveEvent<Boolean> = SingleLiveEvent()
|
||||
|
||||
|
||||
val scoreA: LiveData<Int?>
|
||||
get() {
|
||||
@@ -33,11 +30,6 @@ class KeyboardViewModel @Inject constructor(val gameRepository: GameRepository)
|
||||
return _enableSubmitButton
|
||||
}
|
||||
|
||||
val submitButtonClicked: LiveData<Boolean>
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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<String> = MutableLiveData()
|
||||
private val _nameB: MutableLiveData<String> = MutableLiveData()
|
||||
|
||||
val nameA: LiveData<String>
|
||||
get() {
|
||||
return _nameA
|
||||
}
|
||||
|
||||
val nameB: LiveData<String>
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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() ?: ""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Int> = MutableLiveData()
|
||||
private var _scoreB: MutableLiveData<Int> = 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<Int>
|
||||
get() {
|
||||
return _scoreA
|
||||
}
|
||||
|
||||
val scoreB: LiveData<Int>
|
||||
get() {
|
||||
return _scoreB
|
||||
}
|
||||
}
|
||||
@@ -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<T> : MutableLiveData<T>() {
|
||||
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<T> : MutableLiveData<T>() {
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val TAG = "SingleLiveEvent"
|
||||
private const val TAG = "me.zobrist.tichucounter.framework.SingleLiveEvent"
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -17,73 +17,27 @@
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/viewNames"
|
||||
<androidx.fragment.app.FragmentContainerView
|
||||
android:id="@+id/teamNames"
|
||||
android:name="me.zobrist.tichucounter.fragments.TeamNames"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:layout="@layout/fragment_team_names" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/nameTeamA"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:autofillHints=""
|
||||
android:gravity="center"
|
||||
android:imeOptions="actionDone"
|
||||
android:inputType="text"
|
||||
android:selectAllOnFocus="true"
|
||||
android:singleLine="true"
|
||||
android:text="@string/team_a"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/nameTeamB"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:autofillHints=""
|
||||
android:gravity="center"
|
||||
android:imeOptions="actionDone"
|
||||
android:inputType="text"
|
||||
android:selectAllOnFocus="true"
|
||||
android:singleLine="true"
|
||||
android:text="@string/team_b"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/viewScore"
|
||||
<androidx.fragment.app.FragmentContainerView
|
||||
android:id="@+id/teamScores"
|
||||
android:name="me.zobrist.tichucounter.fragments.TeamScores"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/teamNames"
|
||||
tools:layout="@layout/fragment_team_scores" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/scoreA"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:gravity="center"
|
||||
android:text="0"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/scoreB"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:gravity="center"
|
||||
android:text="0"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<fragment
|
||||
<androidx.fragment.app.FragmentContainerView
|
||||
android:id="@+id/scrollHistory"
|
||||
android:name="me.zobrist.tichucounter.fragments.HistoryList"
|
||||
android:layout_width="match_parent"
|
||||
@@ -103,7 +57,7 @@
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/left">
|
||||
|
||||
<fragment
|
||||
<androidx.fragment.app.FragmentContainerView
|
||||
android:id="@+id/keyboard"
|
||||
android:name="me.zobrist.tichucounter.fragments.Keyboard"
|
||||
android:layout_width="wrap_content"
|
||||
|
||||
@@ -6,79 +6,25 @@
|
||||
android:layout_height="match_parent"
|
||||
app:layout_behavior="@string/appbar_scrolling_view_behavior">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/viewNames"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:orientation="horizontal"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/nameTeamA"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:autofillHints=""
|
||||
android:gravity="center"
|
||||
android:imeOptions="actionDone"
|
||||
android:inputType="text"
|
||||
android:selectAllOnFocus="true"
|
||||
android:singleLine="true"
|
||||
android:text="@string/team_a"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/nameTeamB"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:autofillHints=""
|
||||
android:gravity="center"
|
||||
android:imeOptions="actionDone"
|
||||
android:inputType="text"
|
||||
android:selectAllOnFocus="true"
|
||||
android:singleLine="true"
|
||||
android:text="@string/team_b"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/viewScore"
|
||||
<androidx.fragment.app.FragmentContainerView
|
||||
android:id="@+id/teamNames"
|
||||
android:name="me.zobrist.tichucounter.fragments.TeamNames"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/viewNames">
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:layout="@layout/fragment_team_names" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/scoreA"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:gravity="center"
|
||||
android:text="0"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/scoreB"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:gravity="center"
|
||||
android:text="0"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
</LinearLayout>
|
||||
<androidx.fragment.app.FragmentContainerView
|
||||
android:id="@+id/teamScores"
|
||||
android:name="me.zobrist.tichucounter.fragments.TeamScores"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/teamNames"
|
||||
tools:layout="@layout/fragment_team_scores" />
|
||||
|
||||
|
||||
<View
|
||||
@@ -89,11 +35,11 @@
|
||||
android:background="?android:attr/listDivider"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/viewScore"
|
||||
app:layout_constraintTop_toBottomOf="@id/teamScores"
|
||||
tools:layout_editor_absoluteY="50dp" />
|
||||
|
||||
|
||||
<fragment
|
||||
<androidx.fragment.app.FragmentContainerView
|
||||
android:id="@+id/scrollHistory"
|
||||
android:name="me.zobrist.tichucounter.fragments.HistoryList"
|
||||
android:layout_width="match_parent"
|
||||
@@ -114,7 +60,7 @@
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
<fragment
|
||||
<androidx.fragment.app.FragmentContainerView
|
||||
android:id="@+id/keyboard"
|
||||
android:name="me.zobrist.tichucounter.fragments.Keyboard"
|
||||
android:layout_width="0dp"
|
||||
|
||||
44
app/src/main/res/layout/fragment_team_names.xml
Normal file
44
app/src/main/res/layout/fragment_team_names.xml
Normal file
@@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".fragments.TeamNames">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/viewNames"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/nameTeamA"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:autofillHints=""
|
||||
android:gravity="center"
|
||||
android:imeOptions="actionDone"
|
||||
android:inputType="text"
|
||||
android:selectAllOnFocus="true"
|
||||
android:singleLine="true"
|
||||
android:text="@string/team_a"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/nameTeamB"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:autofillHints=""
|
||||
android:gravity="center"
|
||||
android:imeOptions="actionDone"
|
||||
android:inputType="text"
|
||||
android:selectAllOnFocus="true"
|
||||
android:singleLine="true"
|
||||
android:text="@string/team_b"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</FrameLayout>
|
||||
40
app/src/main/res/layout/fragment_team_scores.xml
Normal file
40
app/src/main/res/layout/fragment_team_scores.xml
Normal file
@@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".fragments.TeamScores">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/viewScore"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/scoreA"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:gravity="center"
|
||||
android:text="0"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/scoreB"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:gravity="center"
|
||||
android:text="0"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</FrameLayout>
|
||||
Reference in New Issue
Block a user