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