Flow on new game.
Now review is always requested regardless of where the new game was started.
This commit is contained in:
@@ -43,7 +43,9 @@ import androidx.navigation.compose.rememberNavController
|
||||
import com.google.accompanist.systemuicontroller.rememberSystemUiController
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.launch
|
||||
import me.zobrist.tichucounter.data.entity.Game
|
||||
import me.zobrist.tichucounter.domain.DrawerItem
|
||||
import me.zobrist.tichucounter.domain.KeepScreenOn
|
||||
import me.zobrist.tichucounter.domain.Language
|
||||
@@ -54,6 +56,7 @@ import me.zobrist.tichucounter.domain.Theme
|
||||
import me.zobrist.tichucounter.domain.TopBarAction
|
||||
import me.zobrist.tichucounter.domain.TopBarState
|
||||
import me.zobrist.tichucounter.domain.navigate
|
||||
import me.zobrist.tichucounter.repository.GameRepository
|
||||
import me.zobrist.tichucounter.ui.AppTheme
|
||||
import me.zobrist.tichucounter.ui.MainViewModel
|
||||
import me.zobrist.tichucounter.ui.about.AboutView
|
||||
@@ -74,6 +77,9 @@ class MainActivity : AppCompatActivity() {
|
||||
@Inject
|
||||
lateinit var settingsAdapter: SettingsAdapter
|
||||
|
||||
@Inject
|
||||
lateinit var repository: GameRepository
|
||||
|
||||
@Inject
|
||||
lateinit var reviewService: ReviewService
|
||||
|
||||
@@ -82,6 +88,7 @@ class MainActivity : AppCompatActivity() {
|
||||
private val settingsViewModel: SettingsViewModel by viewModels()
|
||||
private val mainViewModel: MainViewModel by viewModels()
|
||||
|
||||
private var newGame: Game? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -107,8 +114,15 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
mainViewModel.onNewGame = {
|
||||
reviewService.request()
|
||||
lifecycleScope.launch {
|
||||
repository.getNewGameStarted().collect{
|
||||
if(newGame == null)
|
||||
{
|
||||
newGame = it
|
||||
return@collect
|
||||
}
|
||||
reviewService.request()
|
||||
}
|
||||
}
|
||||
|
||||
setContent {
|
||||
@@ -252,7 +266,7 @@ class MainActivity : AppCompatActivity() {
|
||||
expanded = false
|
||||
it?.let {
|
||||
when (it) {
|
||||
newGameTranslated -> mainViewModel.newGame()
|
||||
newGameTranslated -> lifecycleScope.launch { repository.newGame() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package me.zobrist.tichucounter.repository
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.filter
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.take
|
||||
@@ -13,6 +14,7 @@ import me.zobrist.tichucounter.data.GameWithScores
|
||||
import me.zobrist.tichucounter.data.RoundDao
|
||||
import me.zobrist.tichucounter.data.entity.Game
|
||||
import me.zobrist.tichucounter.data.entity.Round
|
||||
import me.zobrist.tichucounter.domain.KeepScreenOn
|
||||
import java.util.Date
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -23,6 +25,8 @@ class GameRepository @Inject constructor(
|
||||
|
||||
private var activeGame: Game = Game(true, "TeamA", "TeamB", Date(), Date())
|
||||
|
||||
private val newGameFlow = MutableStateFlow(Game())
|
||||
|
||||
init {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
gameDao.getActiveAsFlow().collect {
|
||||
@@ -39,6 +43,7 @@ class GameRepository @Inject constructor(
|
||||
withContext(Dispatchers.IO) {
|
||||
val id = gameDao.insert(Game(true, activeGame.nameA, activeGame.nameB, Date(), Date()))
|
||||
setActive(id)
|
||||
newGameFlow.value= gameDao.getGameById(id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,4 +139,9 @@ class GameRepository @Inject constructor(
|
||||
fun getDistinctTeamNames(): Flow<List<String>> {
|
||||
return gameDao.getDistinctTeamNames()
|
||||
}
|
||||
|
||||
fun getNewGameStarted(): Flow<Game>
|
||||
{
|
||||
return newGameFlow
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,9 @@ import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.launch
|
||||
import me.zobrist.tichucounter.data.entity.Game
|
||||
import me.zobrist.tichucounter.data.entity.Round
|
||||
import me.zobrist.tichucounter.repository.GameRepository
|
||||
import javax.inject.Inject
|
||||
@@ -21,8 +23,6 @@ class MainViewModel @Inject constructor(
|
||||
private var redoRounds = mutableStateListOf<Round>()
|
||||
private var expectedRoundCount = 0
|
||||
|
||||
var onNewGame: (() -> Unit)? = null
|
||||
|
||||
var isUndoActionActive by mutableStateOf(false)
|
||||
|
||||
val isRedoActionActive: Boolean
|
||||
@@ -31,6 +31,8 @@ class MainViewModel @Inject constructor(
|
||||
var activeGameHasRounds by mutableStateOf(false)
|
||||
private set
|
||||
|
||||
private var newGame: Game? = null
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
|
||||
@@ -47,6 +49,17 @@ class MainViewModel @Inject constructor(
|
||||
expectedRoundCount = it.rounds.count()
|
||||
}
|
||||
}
|
||||
|
||||
viewModelScope.launch {
|
||||
gameRepository.getNewGameStarted().collect {
|
||||
if(newGame == null)
|
||||
{
|
||||
newGame = it
|
||||
return@collect
|
||||
}
|
||||
redoRounds.clear()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun undoLastRound() {
|
||||
@@ -71,12 +84,4 @@ class MainViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun newGame() {
|
||||
viewModelScope.launch {
|
||||
redoRounds.clear()
|
||||
gameRepository.newGame()
|
||||
}
|
||||
onNewGame?.let { it() }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user