Skip to content

Commit 1cbb6b4

Browse files
authored
Merge pull request #263 from fmasa/fix-create-char
Invalidate Careers in selectbox when changed by GM
2 parents 470fcaf + a41a9b1 commit 1cbb6b4

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

common/src/commonMain/kotlin/cz/frantisekmasa/wfrp_master/common/characterCreation/CharacterCreationScreen.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import androidx.compose.material.icons.Icons
3030
import androidx.compose.material.icons.rounded.ArrowBackIos
3131
import androidx.compose.material.icons.rounded.ArrowForwardIos
3232
import androidx.compose.runtime.Composable
33-
import androidx.compose.runtime.LaunchedEffect
3433
import androidx.compose.runtime.getValue
3534
import androidx.compose.runtime.mutableStateOf
3635
import androidx.compose.runtime.remember
@@ -44,11 +43,11 @@ import androidx.compose.ui.unit.dp
4443
import cafe.adriel.voyager.core.screen.Screen
4544
import cz.frantisekmasa.wfrp_master.common.Str
4645
import cz.frantisekmasa.wfrp_master.common.character.CharacterDetailScreen
47-
import cz.frantisekmasa.wfrp_master.common.compendium.domain.Career
4846
import cz.frantisekmasa.wfrp_master.common.core.auth.UserId
4947
import cz.frantisekmasa.wfrp_master.common.core.domain.character.CharacterType
5048
import cz.frantisekmasa.wfrp_master.common.core.domain.party.PartyId
5149
import cz.frantisekmasa.wfrp_master.common.core.ui.buttons.BackButton
50+
import cz.frantisekmasa.wfrp_master.common.core.ui.flow.collectWithLifecycle
5251
import cz.frantisekmasa.wfrp_master.common.core.ui.forms.FormData
5352
import cz.frantisekmasa.wfrp_master.common.core.ui.navigation.LocalNavigationTransaction
5453
import cz.frantisekmasa.wfrp_master.common.core.ui.primitives.FullScreenProgress
@@ -58,7 +57,6 @@ import cz.frantisekmasa.wfrp_master.common.core.ui.scaffolding.SubheadBar
5857
import dev.icerock.moko.resources.compose.stringResource
5958
import kotlinx.coroutines.Dispatchers
6059
import kotlinx.coroutines.launch
61-
import kotlinx.coroutines.withContext
6260

6361
private enum class FormState {
6462
EDITED_BY_USER,
@@ -90,15 +88,7 @@ private fun Screen.MainContainer(partyId: PartyId, type: CharacterType, userId:
9088
val screenModel: CharacterCreationScreenModel = rememberScreenModel(arg = partyId)
9189
val coroutineScope = rememberCoroutineScope()
9290

93-
val (careers, setCareers) = rememberSaveable {
94-
mutableStateOf<List<Career>?>(null)
95-
}
96-
97-
LaunchedEffect(Unit) {
98-
withContext(Dispatchers.IO) {
99-
setCareers(screenModel.getCareers())
100-
}
101-
}
91+
val careers = screenModel.careers.collectWithLifecycle(null).value
10292

10393
if (careers == null) {
10494
FullScreenProgress()

common/src/commonMain/kotlin/cz/frantisekmasa/wfrp_master/common/characterCreation/CharacterCreationScreenModel.kt

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,29 @@ import cz.frantisekmasa.wfrp_master.common.core.domain.party.PartyId
1414
import cz.frantisekmasa.wfrp_master.common.core.domain.party.PartyRepository
1515
import cz.frantisekmasa.wfrp_master.common.core.logging.Reporter
1616
import cz.frantisekmasa.wfrp_master.common.core.ui.forms.SelectedCareer
17+
import cz.frantisekmasa.wfrp_master.common.core.utils.right
1718
import io.github.aakira.napier.Napier
1819
import kotlinx.coroutines.Dispatchers
19-
import kotlinx.coroutines.flow.first
20+
import kotlinx.coroutines.flow.Flow
21+
import kotlinx.coroutines.flow.combine
2022
import kotlinx.coroutines.withContext
2123

2224
class CharacterCreationScreenModel(
2325
private val partyId: PartyId,
2426
private val characters: CharacterRepository,
25-
private val careerCompendium: Compendium<Career>,
27+
careerCompendium: Compendium<Career>,
2628
private val userProvider: UserProvider,
27-
private val parties: PartyRepository,
29+
parties: PartyRepository,
2830
) : ScreenModel {
29-
30-
suspend fun getCareers(): List<Career> {
31-
val careers = careerCompendium.liveForParty(partyId).first()
32-
val party = parties.get(partyId)
33-
34-
if (userProvider.userId == party.gameMasterId) {
35-
return careers
31+
val careers: Flow<List<Career>> = careerCompendium.liveForParty(partyId)
32+
.combine(parties.getLive(partyId).right()) { careers, party ->
33+
if (userProvider.userId == party.gameMasterId) {
34+
careers
35+
} else {
36+
careers.filter { it.isVisibleToPlayers }
37+
}
3638
}
3739

38-
return careers.filter { it.isVisibleToPlayers }
39-
}
40-
4140
suspend fun createCharacter(
4241
userId: UserId?,
4342
type: CharacterType,

0 commit comments

Comments
 (0)