feat: the member picker holds the three-person floor (remove-convenient-groups, Phase 3)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c2010abe44
commit
7fc1233bbc
4 files changed
+173
-18
No files matched your search
@@ -186,7 +186,6 @@
|
||||
<string name="next">Next</string>
|
||||
<string name="no_chat_message_relays_were_found_for_this">No chat message relays were found for this user.</string>
|
||||
<string name="no_messages_go_to_a_profile_and_send_them_a">No messages. Go to a profile and send them a message.</string>
|
||||
<string name="no_one_selected_yet">No one selected yet</string>
|
||||
<string name="no_one_to_add_yet">No one to add yet.</string>
|
||||
<string name="no_projects_exists_in_this_group">No projects exists in this group.</string>
|
||||
<string name="no_proposal_has_been_abandoned">No proposal has been abandoned.</string>
|
||||
@@ -321,7 +320,6 @@
|
||||
<string name="you_agreed">You agreed.</string>
|
||||
<string name="you_are_about_to_create_a_nostr_profile">You are about to create a NOSTR profile.</string>
|
||||
<string name="you_are_signing">You are signing.</string>
|
||||
<string name="you_can_still_carry_on_and_invite_people">You can still carry on and invite people later.</string>
|
||||
<string name="you_declined">You declined.</string>
|
||||
<string name="you_have_not_backed_up_your_recovery_phrase">You have not backed up your recovery phrase</string>
|
||||
<string name="you_only_live_once_lose_this_phone_and_the">You only live once. Lose this phone and the profile goes with it, along with anything it holds.</string>
|
||||
@@ -519,4 +517,8 @@
|
||||
<string name="more_approvals">More approvals</string>
|
||||
<string name="creating_the_group_starts_a_key_ceremony">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.</string>
|
||||
<string name="a_group_needs_at_least_people">A group needs at least %1$s people, you included. Go back and pick more.</string>
|
||||
<string name="pick_at_least_more">Pick at least %1$s people</string>
|
||||
<string name="pick_more">Pick %1$s more</string>
|
||||
<string name="not_enough_people_for_a_group_yet">Not enough people for a group yet.</string>
|
||||
<string name="a_group_needs_at_least_two_other_people">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.</string>
|
||||
</resources>
|
||||
+22
-11
@@ -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
|
||||
)
|
||||
|
||||
+37
-5
@@ -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<HexKey>()
|
||||
|
||||
/**
|
||||
* 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(
|
||||
|
||||
+110
@@ -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<Profile>) : NostrRepository by NostrRepository.NO_OP_NOSTR_REPOSITORY {
|
||||
override suspend fun searchableProfiles(excludingPublicKeys: Array<HexKey>): List<Profile> =
|
||||
everyone.filterNot { it.publicKey in excludingPublicKeys }
|
||||
|
||||
override suspend fun queueNegentropySynchronizeRequest(negentropySynchronizeRequests: List<NegentropySynchronizeRequest>) = 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<Route>()
|
||||
|
||||
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<Route>()
|
||||
|
||||
viewModel.toggleMember(bob)
|
||||
viewModel.toggleMember(cleo)
|
||||
assertTrue(viewModel.canContinue())
|
||||
viewModel.selectQuorum(routes::add)
|
||||
|
||||
val route = assertIs<SelectChatRoomTypeRoute>(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")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user