diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletUIState.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletUIState.kt index b74ea910..b6b227b2 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletUIState.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletUIState.kt @@ -12,6 +12,7 @@ import com.machankura.bennu.compose.wallet.GroupWalletState import com.machankura.bennu.compose.wallet.GroupSpend import com.machankura.bennu.compose.database.model.GroupTransaction import com.machankura.bennu.compose.database.model.types.FrostSigningStage +import com.vitorpamplona.quartz.nip01Core.core.HexKey sealed interface GroupWalletUIState { /** @@ -107,6 +108,45 @@ data class GuardedWalletRow( } } +/** + * One member's wallets as an ancestor's list gathers them, so a group can see + * at a glance that one person has several, guarded by different groups below + * it. `docs/member-wallet-guarding-group.md`, D6. + */ +data class PersonGuardedWallets( + val ownerPublicKey: HexKey, + /** The member's profile name on this device, or null. */ + val ownerName: String?, + /** Nearest first, then by the guarding group's name: wallets from one branch sit together. */ + val rows: List, +) { + /** How many groups below this one directly guard these wallets. */ + val guardianCount: Int get() = rows.map { it.wallet.directGuardianChatRoomId.lowercase() }.distinct().size +} + +/** + * The member wallets among these rows, by person, people by name and then key. + * Subgroups' wallets are left out: a row there is a room, not a person. + */ +fun List.byPerson(): List = + mapNotNull { row -> (row.wallet.subject as? GuardianManager.GuardedSubject.Member)?.let { it to row } } + .groupBy { (member, _) -> member.ownerPublicKey.lowercase() } + .map { (_, owned) -> + val member = owned.first().first + PersonGuardedWallets( + ownerPublicKey = member.ownerPublicKey, + ownerName = owned.firstNotNullOfOrNull { (it, _) -> it.ownerName }, + rows = owned.map { it.second }.sortedWith( + compareBy( + { it.wallet.position }, + { (it.wallet.subject as GuardianManager.GuardedSubject.Member).guardianName?.lowercase() ?: it.wallet.directGuardianChatRoomId }, + { (it.wallet.subject as GuardianManager.GuardedSubject.Member).label?.lowercase() }, + ) + ), + ) + } + .sortedWith(compareBy({ it.ownerName?.lowercase() ?: it.ownerPublicKey }, { it.ownerPublicKey })) + /** Where one of the room's spends has got to, as one fact a member reads. */ enum class SpendStatus { WAITING_FOR_SIGNATURES, diff --git a/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/ui/view/state/GuardedWalletRowTest.kt b/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/ui/view/state/GuardedWalletRowTest.kt index 08f8f12b..5be93861 100644 --- a/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/ui/view/state/GuardedWalletRowTest.kt +++ b/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/ui/view/state/GuardedWalletRowTest.kt @@ -3,6 +3,8 @@ package com.machankura.bennu.compose.ui.view.state import com.machankura.bennu.compose.managers.GroupWalletDerivation import com.machankura.bennu.compose.managers.GuardianDerivation import com.machankura.bennu.compose.managers.GuardianManager +import com.machankura.bennu.compose.managers.LineageExample +import com.machankura.bennu.compose.managers.LineageExample.heldBy import com.machankura.bennu.compose.nostr.wallet.GroupWalletStatementEvent import com.machankura.bennu.compose.wallet.GroupWalletState import com.vitorpamplona.quartz.nip01Core.core.Event @@ -79,4 +81,43 @@ class GuardedWalletRowTest { assertNull(row.arriving) assertNull(row.withinReach) } + + // ---- By person: docs/member-wallet-guarding-group.md, Phase 4 --------- + + private val r = LineageExample.r + private val savingsAtR = GuardedWalletRow(heldBy(r, LineageExample.savings, LineageExample.savingsGuardians, label = "Savings", guardianName = "#M"), walletState = null) + private val rentAtR = GuardedWalletRow(heldBy(r, LineageExample.rent, LineageExample.rentGuardians, label = "Rent", guardianName = "#N"), walletState = null) + + @Test + fun `a member's two wallets under two groups are one person, the nearer first`() { + val people = listOf(savingsAtR, rentAtR).byPerson() + + val alice = people.single() + assertEquals(LineageExample.alice, alice.ownerPublicKey) + assertEquals("alice", alice.ownerName) + assertEquals(2, alice.guardianCount) + assertEquals(listOf("Rent", "Savings"), alice.rows.map { (it.wallet.subject as GuardianManager.GuardedSubject.Member).label }) + } + + @Test + fun `two wallets under one group count one guardian`() { + // A second wallet N guards for her: the guardians name the same rooms. + val holiday = GuardedWalletRow(heldBy(r, "9".repeat(64), LineageExample.rentGuardians, label = "Holiday"), walletState = null) + val alice = listOf(rentAtR, holiday).byPerson().single() + assertEquals(1, alice.guardianCount) + } + + @Test + fun `a subgroup's wallet is not a person`() { + assertEquals(emptyList(), listOf(GuardedWalletRow(wallet, walletState = null)).byPerson()) + } + + @Test + fun `people sort by name, and by key where there is none`() { + val bob = GuardedWalletRow(heldBy(r, "8".repeat(64), LineageExample.rentGuardians, ownerPublicKey = "b".repeat(64), ownerName = "bob"), walletState = null) + val nameless = GuardedWalletRow(heldBy(r, "6".repeat(64), LineageExample.rentGuardians, ownerPublicKey = "0".repeat(64), ownerName = null), walletState = null) + + val people = listOf(bob, nameless, savingsAtR).byPerson() + assertEquals(listOf("0".repeat(64), LineageExample.alice, "b".repeat(64)), people.map { it.ownerPublicKey }) + } }