feat: the Shares section follows the group while it is open (Phase 3)

The room detail screen read the shares summary once, and again only
after one of its own actions, so a key state signed in the background
left Rotate shares disabled until the screen was reopened.
ChatRepository.observeSharesInputs emits when the room's key state,
signing sessions or refreshes change; the view model reads the section
again once they settle, and nothing else on the screen.

ChatRoomDetailSharesJvmTest 2/2, m3Audit, compileDebugKotlinAndroid.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso NgakoandClaude Opus 5.5 committed 2026-11-20 00:47:00 +02:00
1 parent d358a0db38
commit 98567629ba
4 files changed
+153

No files matched your search

@@ -25,6 +25,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import mobi.giza.compose.database.GENESIS_AT
import mobi.giza.compose.database.GizaDatabase
import mobi.giza.compose.database.model.ChatMessage
@@ -702,6 +703,12 @@ class DatabaseChatRepository(
)
}.getOrNull()
override fun observeSharesInputs(chatRoomId: String): Flow<Unit> = combine(
database.groupKeyStateDao().observeByChatRoomId(chatRoomId),
database.frostSigningSessionDao().observeSessionsForChatRoom(chatRoomId),
database.shareRefreshDao().observeSessionsForRoom(chatRoomId),
) { _, _, _ -> }
private fun failureOf(t: Throwable?): String? = t?.let { it.message ?: it::class.simpleName ?: "Unknown error" }
private fun privateKeyOrNull(): Pair<HexKey, ByteArray>? {
@@ -7,6 +7,7 @@ import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerSync
import com.vitorpamplona.quartz.nip17Dm.messages.ChatMessageEvent
import com.vitorpamplona.quartz.nip17Dm.settings.DmRelayListEvent
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flow
import mobi.giza.compose.database.model.ChatRoom
import mobi.giza.compose.database.model.GiftWrapPayload
@@ -284,6 +285,14 @@ interface ChatRepository {
/** The room's key as the Shares section shows it, or null when the room has none. */
suspend fun sharesSummary(localChatRoom: LocalChatRoom): SharesSummary?
/**
* Emits whenever something [sharesSummary] reads for [chatRoomId] may have
* changed -- the key state the group signed, a signing session, a refresh --
* so the section follows the group without being reopened
* (docs/rotation-waits-for-key-state.md, Phase 3).
*/
fun observeSharesInputs(chatRoomId: String): Flow<Unit>
/** Proposes rotating every share of the room's key. Null on success, or why not. */
suspend fun rotateShares(localChatRoom: LocalChatRoom): String?
@@ -499,6 +508,8 @@ interface ChatRepository {
override suspend fun sharesSummary(localChatRoom: LocalChatRoom): SharesSummary? = null
override fun observeSharesInputs(chatRoomId: String): Flow<Unit> = emptyFlow()
override suspend fun rotateShares(localChatRoom: LocalChatRoom): String? = null
override suspend fun approveShares(localChatRoom: LocalChatRoom, approval: SharesSummary.PendingApproval): String? = null
@@ -16,7 +16,10 @@ import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.IO
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.launch
import mobi.giza.compose.database.model.intermdiate.LocalChatRoom
import mobi.giza.compose.database.model.types.MarmotReindexReport
@@ -110,6 +113,30 @@ class ChatRoomDetailViewModel(
shares = chatRepository.sharesSummary(localChatRoom),
)
}
localChatRoom?.let { observeShares(it.chatRoom.id) }
}
}
private var sharesObserver: Job? = null
/**
* Keeps the Shares section following the group: a key state signed, a
* proposal declined, a refresh moved on by another device, all while this
* screen is open (docs/rotation-waits-for-key-state.md, Phase 3). Only the
* section is read again -- the rest of the screen has its own paths.
*/
@OptIn(FlowPreview::class)
private fun observeShares(roomId: String) {
if (sharesObserver != null) return
sharesObserver = viewModelScope.launch(Dispatchers.IO) {
chatRepository.observeSharesInputs(roomId)
// A signing session writes a row per message; read once it settles.
.debounce(SHARES_SETTLE_MILLIS)
.collect {
val loaded = chatRoomDetailUIState as? ChatRoomDetailUIState.Loaded ?: return@collect
val shares = chatRepository.sharesSummary(loaded.localChatRoom)
(chatRoomDetailUIState as? ChatRoomDetailUIState.Loaded)?.let { chatRoomDetailUIState = it.copy(shares = shares) }
}
}
}
@@ -286,6 +313,9 @@ class ChatRoomDetailViewModel(
companion object {
private const val TAG = "ChatRoomDetailViewModel"
/** How long the Shares section's inputs have to be still before it is read again. */
internal const val SHARES_SETTLE_MILLIS = 250L
fun factory(
activeUserPublicKey: HexKey,
chatRoomId: String,
@@ -0,0 +1,105 @@
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.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
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.ChatRoom
import mobi.giza.compose.database.model.intermdiate.LocalChatRoom
import mobi.giza.compose.repository.ChatRepository
import mobi.giza.compose.repository.NostrRepository
import mobi.giza.compose.ui.view.state.ChatRoomDetailUIState
import mobi.giza.compose.ui.view.state.SharesSummary
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
/**
* The room detail screen's Shares section follows the group while it is open
* (docs/rotation-waits-for-key-state.md, Phase 3): the rotate button the last
* rotation held comes back when the group signs, without the screen being left
* and opened again.
*/
@OptIn(ExperimentalCoroutinesApi::class)
class ChatRoomDetailSharesJvmTest {
@BeforeTest
fun setUp() {
Dispatchers.setMain(UnconfinedTestDispatcher())
}
@AfterTest
fun tearDown() {
Dispatchers.resetMain()
}
private val chatRoomId = "c0ffee00".repeat(8)
private val me = "a".repeat(64)
private val localChatRoom = LocalChatRoom(
chatRoom = ChatRoom(id = chatRoomId, userPublicKey = me, subject = "Admins", description = null, mlsGroupState = null)
)
private fun summary(rotation: SharesSummary.Rotation) =
SharesSummary(generation = 1, members = emptyList(), pending = emptyList(), rotation = rotation, canAskToBeRepaired = false)
private class Repository(val room: LocalChatRoom, var shares: SharesSummary) :
ChatRepository by ChatRepository.NO_OP_CHAT_REPOSITORY {
val inputs = MutableSharedFlow<Unit>(replay = 1)
var reads = 0
override suspend fun getChatRoomByIdentifier(id: HexKey): LocalChatRoom? = room.takeIf { id == room.chatRoom.id }
override suspend fun sharesSummary(localChatRoom: LocalChatRoom): SharesSummary { reads++; return shares }
override fun observeSharesInputs(chatRoomId: String): Flow<Unit> = inputs
}
private fun viewModel(repository: Repository) = ChatRoomDetailViewModel(
chatRoomId = chatRoomId,
activeUserPublicKey = me,
relayHint = null,
initialChatRoomDetailUIState = ChatRoomDetailUIState.Loading,
nostrRepository = NostrRepository.NO_OP_NOSTR_REPOSITORY,
chatRepository = repository,
)
private suspend fun ChatRoomDetailViewModel.awaitRotation(expected: SharesSummary.Rotation) = withTimeout(5_000) {
while ((chatRoomDetailUIState as? ChatRoomDetailUIState.Loaded)?.shares?.rotation != expected) delay(10)
}
@Test
fun `the rotate button comes back when the group signs, with the screen open`() = runBlocking<Unit> {
val held = SharesSummary.Rotation.Held(SharesSummary.Rotation.Reason.AWAITING_SIGNATURES)
val repository = Repository(localChatRoom, summary(held))
val viewModel = viewModel(repository)
viewModel.initiateChatRoomDetail()
viewModel.awaitRotation(held)
// The group's signature lands in the database while the screen is open.
repository.shares = summary(SharesSummary.Rotation.Available)
repository.inputs.emit(Unit)
viewModel.awaitRotation(SharesSummary.Rotation.Available)
}
@Test
fun `a burst of signing messages is read once it settles`() = runBlocking<Unit> {
val repository = Repository(localChatRoom, summary(SharesSummary.Rotation.Available))
val viewModel = viewModel(repository)
viewModel.initiateChatRoomDetail()
viewModel.awaitRotation(SharesSummary.Rotation.Available)
delay(ChatRoomDetailViewModel.SHARES_SETTLE_MILLIS * 2)
val before = repository.reads
repeat(20) { repository.inputs.emit(Unit) }
delay(ChatRoomDetailViewModel.SHARES_SETTLE_MILLIS * 3)
val reads = repository.reads - before
assertTrue(reads in 1..2, "20 changes in a burst read the section $reads times")
assertEquals(SharesSummary.Rotation.Available, (viewModel.chatRoomDetailUIState as ChatRoomDetailUIState.Loaded).shares?.rotation)
}
}