Skip to content

Commit

Permalink
Merge pull request #484 from code-payments/feat/load-user-identity-if…
Browse files Browse the repository at this point in the history
…-revealed

feat(conversation): load user identity if already known/revealed
  • Loading branch information
bmc08gt authored Jul 19, 2024
2 parents 0b80800 + 9b90dca commit e35a536
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ data class ChatMessageConversationScreen(
)
Column {
Text(
text = user.username,
text = user.username.orEmpty(),
style = CodeTheme.typography.screenTitle
)
state.lastSeen?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.getcode.model.chat.ChatType
import com.getcode.model.chat.Reference
import com.getcode.model.uuid
import com.getcode.network.ConversationController
import com.getcode.network.TipController
import com.getcode.network.repository.FeatureRepository
import com.getcode.solana.keys.PublicKey
import com.getcode.ui.components.chat.utils.ChatItem
Expand Down Expand Up @@ -51,9 +52,8 @@ import javax.inject.Inject
@HiltViewModel
class ConversationViewModel @Inject constructor(
private val conversationController: ConversationController,
currencyUtils: CurrencyUtils,
resources: ResourceHelper,
features: FeatureRepository,
tipController: TipController,
) : BaseViewModel2<ConversationViewModel.State, ConversationViewModel.Event>(
initialState = State.Default,
updateStateForEvent = updateStateForEvent
Expand All @@ -71,8 +71,7 @@ class ConversationViewModel @Inject constructor(
val pointers: Map<UUID, MessageStatus>,
) {
data class User(
val username: String,
val publicKey: PublicKey,
val username: String?,
val imageUrl: String?,
)

Expand All @@ -98,9 +97,8 @@ class ConversationViewModel @Inject constructor(
Event

data class OnUserRevealed(
val username: String,
val publicKey: PublicKey,
val imageUrl: String?,
val username: String? = null,
val imageUrl: String? = null,
) : Event

data class OnTipsChatCashChanged(val module: Feature) : Event
Expand Down Expand Up @@ -222,6 +220,18 @@ class ConversationViewModel @Inject constructor(
.onEach { delay(300) }
.onEach { conversationController.revealIdentity(it) }
.launchIn(viewModelScope)

stateFlow
.mapNotNull { it.user }
.distinctUntilChanged()
.filter { it.imageUrl == null }
.mapNotNull { it.username }
.map { username -> runCatching { tipController.fetch(username) } }
.map { it.getOrNull() }
.filterNotNull()
.map { it.imageUrl }
.onEach { dispatchEvent(Event.OnUserRevealed(imageUrl = it)) }
.launchIn(viewModelScope)
}

val messages: Flow<PagingData<ChatItem>> = stateFlow
Expand Down Expand Up @@ -277,7 +287,13 @@ class ConversationViewModel @Inject constructor(
conversationId = conversation.id,
title = conversation.title,
identityRevealed = conversation.hasRevealedIdentity,
pointers = event.conversationWithPointers.pointers
pointers = event.conversationWithPointers.pointers,
user = conversation.user?.let {
State.User(
username = it,
imageUrl = conversation.userImage
)
}
)
}

Expand Down Expand Up @@ -316,8 +332,7 @@ class ConversationViewModel @Inject constructor(
is Event.OnUserRevealed -> { state ->
state.copy(
user = State.User(
username = event.username,
publicKey = event.publicKey,
username = event.username ?: state.user?.username,
imageUrl = event.imageUrl,
)
)
Expand Down

0 comments on commit e35a536

Please sign in to comment.