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 com.google.accompanist.systemuicontroller.rememberSystemUiController
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.flow.collect
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import me.zobrist.tichucounter.data.entity.Game
|
||||||
import me.zobrist.tichucounter.domain.DrawerItem
|
import me.zobrist.tichucounter.domain.DrawerItem
|
||||||
import me.zobrist.tichucounter.domain.KeepScreenOn
|
import me.zobrist.tichucounter.domain.KeepScreenOn
|
||||||
import me.zobrist.tichucounter.domain.Language
|
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.TopBarAction
|
||||||
import me.zobrist.tichucounter.domain.TopBarState
|
import me.zobrist.tichucounter.domain.TopBarState
|
||||||
import me.zobrist.tichucounter.domain.navigate
|
import me.zobrist.tichucounter.domain.navigate
|
||||||
|
import me.zobrist.tichucounter.repository.GameRepository
|
||||||
import me.zobrist.tichucounter.ui.AppTheme
|
import me.zobrist.tichucounter.ui.AppTheme
|
||||||
import me.zobrist.tichucounter.ui.MainViewModel
|
import me.zobrist.tichucounter.ui.MainViewModel
|
||||||
import me.zobrist.tichucounter.ui.about.AboutView
|
import me.zobrist.tichucounter.ui.about.AboutView
|
||||||
@@ -74,6 +77,9 @@ class MainActivity : AppCompatActivity() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var settingsAdapter: SettingsAdapter
|
lateinit var settingsAdapter: SettingsAdapter
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var repository: GameRepository
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var reviewService: ReviewService
|
lateinit var reviewService: ReviewService
|
||||||
|
|
||||||
@@ -82,6 +88,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
private val settingsViewModel: SettingsViewModel by viewModels()
|
private val settingsViewModel: SettingsViewModel by viewModels()
|
||||||
private val mainViewModel: MainViewModel by viewModels()
|
private val mainViewModel: MainViewModel by viewModels()
|
||||||
|
|
||||||
|
private var newGame: Game? = null
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@@ -107,8 +114,15 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mainViewModel.onNewGame = {
|
lifecycleScope.launch {
|
||||||
reviewService.request()
|
repository.getNewGameStarted().collect{
|
||||||
|
if(newGame == null)
|
||||||
|
{
|
||||||
|
newGame = it
|
||||||
|
return@collect
|
||||||
|
}
|
||||||
|
reviewService.request()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
@@ -252,7 +266,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
expanded = false
|
expanded = false
|
||||||
it?.let {
|
it?.let {
|
||||||
when (it) {
|
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.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.filter
|
import kotlinx.coroutines.flow.filter
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.flow.take
|
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.RoundDao
|
||||||
import me.zobrist.tichucounter.data.entity.Game
|
import me.zobrist.tichucounter.data.entity.Game
|
||||||
import me.zobrist.tichucounter.data.entity.Round
|
import me.zobrist.tichucounter.data.entity.Round
|
||||||
|
import me.zobrist.tichucounter.domain.KeepScreenOn
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -23,6 +25,8 @@ class GameRepository @Inject constructor(
|
|||||||
|
|
||||||
private var activeGame: Game = Game(true, "TeamA", "TeamB", Date(), Date())
|
private var activeGame: Game = Game(true, "TeamA", "TeamB", Date(), Date())
|
||||||
|
|
||||||
|
private val newGameFlow = MutableStateFlow(Game())
|
||||||
|
|
||||||
init {
|
init {
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
gameDao.getActiveAsFlow().collect {
|
gameDao.getActiveAsFlow().collect {
|
||||||
@@ -39,6 +43,7 @@ class GameRepository @Inject constructor(
|
|||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
val id = gameDao.insert(Game(true, activeGame.nameA, activeGame.nameB, Date(), Date()))
|
val id = gameDao.insert(Game(true, activeGame.nameA, activeGame.nameB, Date(), Date()))
|
||||||
setActive(id)
|
setActive(id)
|
||||||
|
newGameFlow.value= gameDao.getGameById(id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,4 +139,9 @@ class GameRepository @Inject constructor(
|
|||||||
fun getDistinctTeamNames(): Flow<List<String>> {
|
fun getDistinctTeamNames(): Flow<List<String>> {
|
||||||
return gameDao.getDistinctTeamNames()
|
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.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import kotlinx.coroutines.flow.collect
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import me.zobrist.tichucounter.data.entity.Game
|
||||||
import me.zobrist.tichucounter.data.entity.Round
|
import me.zobrist.tichucounter.data.entity.Round
|
||||||
import me.zobrist.tichucounter.repository.GameRepository
|
import me.zobrist.tichucounter.repository.GameRepository
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -21,8 +23,6 @@ class MainViewModel @Inject constructor(
|
|||||||
private var redoRounds = mutableStateListOf<Round>()
|
private var redoRounds = mutableStateListOf<Round>()
|
||||||
private var expectedRoundCount = 0
|
private var expectedRoundCount = 0
|
||||||
|
|
||||||
var onNewGame: (() -> Unit)? = null
|
|
||||||
|
|
||||||
var isUndoActionActive by mutableStateOf(false)
|
var isUndoActionActive by mutableStateOf(false)
|
||||||
|
|
||||||
val isRedoActionActive: Boolean
|
val isRedoActionActive: Boolean
|
||||||
@@ -31,6 +31,8 @@ class MainViewModel @Inject constructor(
|
|||||||
var activeGameHasRounds by mutableStateOf(false)
|
var activeGameHasRounds by mutableStateOf(false)
|
||||||
private set
|
private set
|
||||||
|
|
||||||
|
private var newGame: Game? = null
|
||||||
|
|
||||||
init {
|
init {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
|
|
||||||
@@ -47,6 +49,17 @@ class MainViewModel @Inject constructor(
|
|||||||
expectedRoundCount = it.rounds.count()
|
expectedRoundCount = it.rounds.count()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
viewModelScope.launch {
|
||||||
|
gameRepository.getNewGameStarted().collect {
|
||||||
|
if(newGame == null)
|
||||||
|
{
|
||||||
|
newGame = it
|
||||||
|
return@collect
|
||||||
|
}
|
||||||
|
redoRounds.clear()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun undoLastRound() {
|
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