diff --git a/shared/src/commonMain/kotlin/su/reya/coop/nostr/Signer.kt b/shared/src/commonMain/kotlin/su/reya/coop/nostr/Signer.kt index 4872ac7..c3119fe 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/nostr/Signer.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/nostr/Signer.kt @@ -29,16 +29,14 @@ class UniversalSigner(initialSigner: AsyncNostrSigner) : AsyncNostrSigner { /** * Switch to a new signer. */ - suspend fun switch(newSigner: AsyncNostrSigner) = mutex.withLock { - val pubkey = try { - withTimeoutOrNull(20.seconds) { - newSigner.getPublicKeyAsync() - } - } catch (e: Exception) { - throw IllegalStateException("Failed to get public key from signer", e) + suspend fun switch(newSigner: AsyncNostrSigner) { + val pubkey = withTimeoutOrNull(20.seconds) { newSigner.getPublicKeyAsync() } + ?: throw IllegalStateException("Failed to get public key from signer") + + mutex.withLock { + signer = newSigner + _publicKeyFlow.value = pubkey } - signer = newSigner - _publicKeyFlow.value = pubkey } override suspend fun getPublicKeyAsync(): PublicKey? { diff --git a/shared/src/commonMain/kotlin/su/reya/coop/viewmodel/ChatViewModel.kt b/shared/src/commonMain/kotlin/su/reya/coop/viewmodel/ChatViewModel.kt index dda430a..b780371 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/viewmodel/ChatViewModel.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/viewmodel/ChatViewModel.kt @@ -1,6 +1,7 @@ package su.reya.coop.viewmodel import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.CancellationException import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted @@ -145,6 +146,8 @@ class ChatViewModel( rooms.forEach { room -> newMap[room.id] = room } currentState.copy(rooms = newMap) } + } catch (e: CancellationException) { + throw e } catch (e: Exception) { showError("Error: ${e.message}") }