diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index 464826d7..4cac18b2 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -186,7 +186,6 @@ Next No chat message relays were found for this user. No messages. Go to a profile and send them a message. - No one selected yet No one to add yet. No projects exists in this group. No proposal has been abandoned. @@ -321,7 +320,6 @@ You agreed. You are about to create a NOSTR profile. You are signing. - You can still carry on and invite people later. You declined. You have not backed up your recovery phrase You only live once. Lose this phone and the profile goes with it, along with anything it holds. @@ -519,4 +517,8 @@ More approvals Nothing is signed for the group without a quorum of you, and the group outlives any one of you. Creating it starts a key ceremony every member takes part in. A group needs at least %1$s people, you included. Go back and pick more. + Pick at least %1$s people + Pick %1$s more + Not enough people for a group yet. + A group needs at least two other people, and everyone in it is added when it's made. To talk with one person, start a chat with them instead. diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/SelectChatRoomMembersScreen.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/SelectChatRoomMembersScreen.kt index ec9249ec..883e5da0 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/SelectChatRoomMembersScreen.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/SelectChatRoomMembersScreen.kt @@ -44,10 +44,12 @@ import mobi.giza.compose.ui.theme.spacing import mobi.giza.compose.resources.Res import org.jetbrains.compose.resources.stringResource import mobi.giza.compose.resources.next -import mobi.giza.compose.resources.no_one_selected_yet import mobi.giza.compose.resources.no_one_to_add_yet +import mobi.giza.compose.resources.not_enough_people_for_a_group_yet import mobi.giza.compose.resources.search_for_people_and_chat_with_them_first -import mobi.giza.compose.resources.you_can_still_carry_on_and_invite_people +import mobi.giza.compose.resources.a_group_needs_at_least_two_other_people +import mobi.giza.compose.resources.pick_at_least_more +import mobi.giza.compose.resources.pick_more import mobi.giza.compose.resources.add_people_to import mobi.giza.compose.resources.next_with import mobi.giza.compose.resources.selected @@ -64,7 +66,7 @@ import mobi.giza.compose.ui.theme.ConformancePreviews * * The list is whatever profiles are already in the local store — no directory * lookup happens here. Creating the group waits for the next step, where the - * user says how it should be run. + * user says how many of them have to agree. */ @OptIn(ExperimentalMaterial3ExpressiveApi::class, ExperimentalMaterial3Api::class) @Composable @@ -124,6 +126,7 @@ fun SelectChatRoomMembersScreen( BottomAppBar( floatingActionButton = { ExtendedFab( + enabled = selectChatRoomMembersViewModel.canContinue(), label = if (selectedCount > 0) { stringResource(Res.string.next_with, selectedCount) } else { @@ -133,7 +136,7 @@ fun SelectChatRoomMembersScreen( Icon(Icons.AutoMirrored.Filled.NavigateNext, contentDescription = "Next") }, onClick = { - selectChatRoomMembersViewModel.selectChatRoomType( + selectChatRoomMembersViewModel.selectQuorum( onNavigateToRoute = onNavigateToRoute ) } @@ -142,10 +145,11 @@ fun SelectChatRoomMembersScreen( actions = { Text( modifier = Modifier.padding(start = MaterialTheme.spacing.space200), - text = if (selectedCount > 0) { - stringResource(Res.string.selected, selectedCount) - } else { - stringResource(Res.string.no_one_selected_yet) + // What is still missing, until nothing is. + text = when { + selectedCount == 0 -> stringResource(Res.string.pick_at_least_more, selectChatRoomMembersViewModel.minimumOthers) + !selectChatRoomMembersViewModel.canContinue() -> stringResource(Res.string.pick_more, selectChatRoomMembersViewModel.stillToPick()) + else -> stringResource(Res.string.selected, selectedCount) }, style = MaterialTheme.typography.labelLarge ) @@ -156,7 +160,7 @@ fun SelectChatRoomMembersScreen( Column( modifier = Modifier.padding(innerPadding).readableContent().fillMaxSize() ) { - if (selectChatRoomMembersUIState.profiles.isEmpty()) { + if (!selectChatRoomMembersViewModel.canMakeAGroup()) { Column( modifier = Modifier.weight(1f).fillMaxWidth().padding(MaterialTheme.spacing.space250), horizontalAlignment = Alignment.CenterHorizontally, @@ -167,7 +171,11 @@ fun SelectChatRoomMembersScreen( ) Text( - text = stringResource(Res.string.no_one_to_add_yet), + text = if (selectChatRoomMembersUIState.profiles.isEmpty()) { + stringResource(Res.string.no_one_to_add_yet) + } else { + stringResource(Res.string.not_enough_people_for_a_group_yet) + }, style = MaterialTheme.typography.bodyLarge, textAlign = TextAlign.Center ) @@ -178,8 +186,11 @@ fun SelectChatRoomMembersScreen( textAlign = TextAlign.Center ) + // There is no later invite: a group's members are the + // ones it is made with. So say what it takes, rather + // than offer a list that can never get past the button. Text( - text = stringResource(Res.string.you_can_still_carry_on_and_invite_people), + text = stringResource(Res.string.a_group_needs_at_least_two_other_people), style = MaterialTheme.typography.bodySmall, textAlign = TextAlign.Center ) diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/SelectChatRoomMembersViewModel.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/SelectChatRoomMembersViewModel.kt index ea277e3f..f2b538a5 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/SelectChatRoomMembersViewModel.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/SelectChatRoomMembersViewModel.kt @@ -12,6 +12,7 @@ import androidx.lifecycle.viewmodel.viewModelFactory import mobi.giza.compose.database.model.NegentropySynchronizeRequest import mobi.giza.compose.database.model.Profile import mobi.giza.compose.database.model.types.SynchronizationFilter +import mobi.giza.compose.managers.Quorum import mobi.giza.compose.nostr.Relays import mobi.giza.compose.repository.NostrRepository import mobi.giza.compose.ui.composable.navigation.routes.Route @@ -30,6 +31,11 @@ import kotlinx.coroutines.launch * Only profiles already in the local store are offered — this screen is a * picker, not a directory search. Nothing is persisted here; the picked keys * ride on to [SelectChatRoomTypeViewModel], which mints the group. + * + * It holds the floor every group is made on, [Quorum.MINIMUM_GROUP_SIZE]: the + * creator plus [minimumOthers]. A smaller group cannot carry a quorum, and two + * people who want to talk have a direct message. See + * docs/remove-convenient-groups.md, D2. */ class SelectChatRoomMembersViewModel( val activeUserPublicKey: HexKey, @@ -47,6 +53,27 @@ class SelectChatRoomMembersViewModel( /** Public keys the user has ticked, in the order they were ticked. */ val selectedPublicKeys = mutableStateListOf() + /** + * Fewest people to pick. The creator counts toward the floor, as a subgroup's + * coordinator does: asking for three *others* would quietly build a group of four. + */ + val minimumOthers: Int = Quorum.MINIMUM_GROUP_SIZE - 1 + + /** Whether enough people are picked for a group to be made. */ + fun canContinue(): Boolean = selectedPublicKeys.size >= minimumOthers + + /** How many more people have to be picked before the group can be made. */ + fun stillToPick(): Int = (minimumOthers - selectedPublicKeys.size).coerceAtLeast(0) + + /** + * Whether the store holds enough people for a group at all. When it does not, + * the screen says so up front rather than offering a list that can never get + * past a disabled button. + */ + fun canMakeAGroup(): Boolean = + (selectChatRoomMembersUIState as? SelectChatRoomMembersUIState.Loaded) + ?.profiles?.size?.let { it >= minimumOthers } == true + fun initiate() { viewModelScope.launch(Dispatchers.IO) { val profiles = nostrRepository.searchableProfiles( @@ -57,9 +84,10 @@ class SelectChatRoomMembersViewModel( profiles = profiles ) - // Ask for everybody's key package up front: by the time the user has finished - // ticking names and chosen how the group is run, the relay round trip is - // usually done, so creating the group doesn't have to sit and wait for it. + // Ask for everybody's key package up front. The NIP-17 room the group starts + // in needs none, but the admin room `MarmotGroupCreation` makes once the key + // ceremony completes does -- and by then the relay round trip is usually + // long done, so that step doesn't have to sit and wait for it. scheduleKeyPackageEventSynchronization(profiles) } } @@ -72,10 +100,14 @@ class SelectChatRoomMembersViewModel( fun isSelected(publicKey: HexKey): Boolean = selectedPublicKeys.contains(publicKey) - fun selectChatRoomType( + fun selectQuorum( onNavigateToRoute: (Route) -> Unit ) { - logger.d("selectChatRoomType with ${selectedPublicKeys.size} member(s)") + // Belt and braces alongside the disabled button: the next screen refuses a + // group this small too, but it should never be asked. + if (!canContinue()) return + + logger.d("selectQuorum with ${selectedPublicKeys.size} member(s)") onNavigateToRoute.invoke( SelectChatRoomTypeRoute( diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/SelectChatRoomMembersViewModelJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/SelectChatRoomMembersViewModelJvmTest.kt new file mode 100644 index 00000000..a02219f3 --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/SelectChatRoomMembersViewModelJvmTest.kt @@ -0,0 +1,110 @@ +package mobi.giza.compose.ui.view.model + +import com.vitorpamplona.quartz.nip01Core.core.HexKey +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.setMain +import kotlinx.coroutines.withTimeout +import mobi.giza.compose.database.model.NegentropySynchronizeRequest +import mobi.giza.compose.database.model.Profile +import mobi.giza.compose.repository.NostrRepository +import mobi.giza.compose.ui.composable.navigation.routes.Route +import mobi.giza.compose.ui.composable.navigation.routes.SelectChatRoomTypeRoute +import mobi.giza.compose.ui.view.state.SelectChatRoomMembersUIState +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertIs +import kotlin.test.assertTrue + +/** + * The member picker holds the floor every group is made on: two others, the + * creator being the third. docs/remove-convenient-groups.md, Phase 3. + */ +@OptIn(ExperimentalCoroutinesApi::class) +class SelectChatRoomMembersViewModelJvmTest { + @BeforeTest + fun setUp() { + Dispatchers.setMain(UnconfinedTestDispatcher()) + } + + @AfterTest + fun tearDown() { + Dispatchers.resetMain() + } + + private val ada = "a".repeat(64) + private val bob = "b".repeat(64) + private val cleo = "c".repeat(64) + + private fun profile(publicKey: HexKey) = Profile( + publicKey = publicKey, + displayName = publicKey.take(4), + nostrEventId = "event-$publicKey", + ) + + /** Hands out everyone it holds except whoever is asked to be left out. */ + private class Store(private val everyone: List) : NostrRepository by NostrRepository.NO_OP_NOSTR_REPOSITORY { + override suspend fun searchableProfiles(excludingPublicKeys: Array): List = + everyone.filterNot { it.publicKey in excludingPublicKeys } + + override suspend fun queueNegentropySynchronizeRequest(negentropySynchronizeRequests: List) = Unit + } + + private fun viewModel(vararg stored: HexKey) = SelectChatRoomMembersViewModel( + activeUserPublicKey = ada, + name = "#checkers", + description = null, + initialSelectChatRoomMembersUIState = SelectChatRoomMembersUIState.Loading, + nostrRepository = Store(stored.map(::profile)), + ) + + private suspend fun SelectChatRoomMembersViewModel.awaitLoaded() = withTimeout(5_000) { + while (selectChatRoomMembersUIState !is SelectChatRoomMembersUIState.Loaded) delay(10) + } + + @Test + fun `the picker does not continue with fewer than two others`(): Unit = runBlocking { + val viewModel = viewModel(bob, cleo) + val routes = mutableListOf() + + viewModel.selectQuorum(routes::add) + viewModel.toggleMember(bob) + assertFalse(viewModel.canContinue()) + viewModel.selectQuorum(routes::add) + + assertEquals(emptyList(), routes) + assertEquals(1, viewModel.stillToPick()) + } + + @Test + fun `two others is enough`(): Unit = runBlocking { + val viewModel = viewModel(bob, cleo) + val routes = mutableListOf() + + viewModel.toggleMember(bob) + viewModel.toggleMember(cleo) + assertTrue(viewModel.canContinue()) + viewModel.selectQuorum(routes::add) + + val route = assertIs(routes.single()) + assertEquals(listOf(bob, cleo), route.memberPublicKeys) + } + + @Test + fun `the creator is never offered, so never counted`(): Unit = runBlocking { + val viewModel = viewModel(ada, bob) + viewModel.initiate() + viewModel.awaitLoaded() + + val loaded = viewModel.selectChatRoomMembersUIState as SelectChatRoomMembersUIState.Loaded + assertEquals(listOf(bob), loaded.profiles.map { it.publicKey }) + assertFalse(viewModel.canMakeAGroup(), "one other person is not enough for a group") + } +}