Optimize query. Update teamname on 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:
@@ -1,7 +1,9 @@
|
||||
package me.zobrist.tichucounter.data
|
||||
|
||||
import androidx.room.*
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Update
|
||||
|
||||
@Dao
|
||||
interface DaoBase<T> {
|
||||
|
||||
@@ -11,7 +11,7 @@ interface GameDao: DaoBase<Game> {
|
||||
fun getAll(): Flow<List<Game>>
|
||||
|
||||
@Query("SELECT * FROM game WHERE uid is :gameId")
|
||||
fun getGameById(gameId: Long): Game
|
||||
fun getGameById(gameId: Long): Flow<Game>
|
||||
|
||||
@Query("SELECT * FROM game WHERE active is 1")
|
||||
fun getActive(): Game
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
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
|
||||
@@ -15,6 +12,14 @@ interface RoundDao: DaoBase<Round> {
|
||||
@Query("SELECT * FROM round WHERE gameId is :gameId")
|
||||
fun getAllForGame(gameId: Long?): List<Round>
|
||||
|
||||
@Query(
|
||||
"SELECT gameId, scoreA, scoreB, round.uid " +
|
||||
"FROM round " +
|
||||
"LEFT JOIN game ON game.uid = round.gameId " +
|
||||
"WHERE game.active == 1"
|
||||
)
|
||||
fun getForActiveGame(): Flow<List<Round>>
|
||||
|
||||
@Insert
|
||||
fun insertAll(vararg rounds: Round)
|
||||
|
||||
|
||||
@@ -6,34 +6,22 @@ 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(
|
||||
private val roundDao: RoundDao,
|
||||
private val gameDao: GameDao,
|
||||
private val gameRepository: GameRepository
|
||||
) : ViewModel() {
|
||||
class HistoryListViewModel @Inject constructor(private val roundDao: RoundDao) : ViewModel() {
|
||||
|
||||
private val _historyA: MutableLiveData<String> = MutableLiveData<String>()
|
||||
private val _historyB: MutableLiveData<String> = MutableLiveData<String>()
|
||||
private val _scrollDown: SingleLiveEvent<Boolean> = SingleLiveEvent()
|
||||
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
roundDao.getAll().collect {
|
||||
update()
|
||||
}
|
||||
}
|
||||
|
||||
viewModelScope.launch {
|
||||
gameDao.getAll().collect {
|
||||
update()
|
||||
roundDao.getForActiveGame().collect() { scores ->
|
||||
update(scores)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,31 +41,19 @@ class HistoryListViewModel @Inject constructor(
|
||||
return _scrollDown
|
||||
}
|
||||
|
||||
private suspend fun update() {
|
||||
val scores = gameRepository.getActiveRoundHistory()
|
||||
if (scores != null) {
|
||||
getHistoryA(scores)
|
||||
getHistoryB(scores)
|
||||
private fun update(scores: List<Round>) {
|
||||
var historyA = String()
|
||||
var historyB = String()
|
||||
|
||||
scores.forEach {
|
||||
historyA += it.scoreA.toString() + "\n"
|
||||
historyB += it.scoreB.toString() + "\n"
|
||||
}
|
||||
_historyA.value = historyA
|
||||
_historyB.value = historyB
|
||||
scrollDown()
|
||||
}
|
||||
|
||||
private fun getHistoryA(scores: List<Round>) {
|
||||
var tempHistory = String()
|
||||
scores.forEach {
|
||||
tempHistory += it.scoreA.toString() + "\n"
|
||||
}
|
||||
_historyA.value = tempHistory
|
||||
}
|
||||
|
||||
private fun getHistoryB(scores: List<Round>) {
|
||||
var tempHistory = String()
|
||||
scores.forEach {
|
||||
tempHistory += it.scoreB.toString() + "\n"
|
||||
}
|
||||
_historyB.value = tempHistory
|
||||
}
|
||||
|
||||
private fun scrollDown() {
|
||||
_scrollDown.value = true
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.widget.doAfterTextChanged
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import me.zobrist.tichucounter.databinding.FragmentTeamNamesBinding
|
||||
|
||||
@@ -16,25 +17,25 @@ class TeamNames : FragmentBase<FragmentTeamNamesBinding>() {
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
|
||||
binding.nameTeamA.setOnFocusChangeListener { _, focus ->
|
||||
if (!focus) {
|
||||
binding.nameTeamA.doAfterTextChanged {
|
||||
viewModel.setNameA(binding.nameTeamA.text.toString())
|
||||
}
|
||||
}
|
||||
|
||||
binding.nameTeamB.setOnFocusChangeListener { _, focus ->
|
||||
if (!focus) {
|
||||
binding.nameTeamB.doAfterTextChanged {
|
||||
viewModel.setNameB(binding.nameTeamB.text.toString())
|
||||
}
|
||||
}
|
||||
|
||||
viewModel.nameA.observe(viewLifecycleOwner) { value ->
|
||||
if (value != binding.nameTeamA.text.toString()) {
|
||||
binding.nameTeamA.setText(value)
|
||||
}
|
||||
}
|
||||
|
||||
viewModel.nameB.observe(viewLifecycleOwner) { value ->
|
||||
if (value != binding.nameTeamB.text.toString()) {
|
||||
binding.nameTeamB.setText(value)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,36 +6,25 @@ 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.repository.GameRepository
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class TeamScoresViewModel @Inject constructor(
|
||||
private val roundDao: RoundDao,
|
||||
private val gameDao: GameDao,
|
||||
private val gameRepository: GameRepository
|
||||
) : ViewModel() {
|
||||
class TeamScoresViewModel @Inject constructor(private val roundDao: RoundDao) : 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()
|
||||
roundDao.getForActiveGame().collect() { scores ->
|
||||
update(scores)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun update() {
|
||||
val scores = gameRepository.getActiveRoundHistory()
|
||||
private fun update(scores: List<Round>) {
|
||||
|
||||
var scoreA = 0
|
||||
var scoreB = 0
|
||||
|
||||
Reference in New Issue
Block a user