Skip to content

Commit 9b90dca

Browse files
committed
feat(conversation): load user identity if already known/revealed
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent 211033f commit 9b90dca

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

app/src/main/java/com/getcode/navigation/screens/ChatScreens.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ data class ChatMessageConversationScreen(
224224
)
225225
Column {
226226
Text(
227-
text = user.username,
227+
text = user.username.orEmpty(),
228228
style = CodeTheme.typography.screenTitle
229229
)
230230
state.lastSeen?.let {

app/src/main/java/com/getcode/view/main/chat/conversation/ConversationViewModel.kt

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.getcode.model.chat.ChatType
1919
import com.getcode.model.chat.Reference
2020
import com.getcode.model.uuid
2121
import com.getcode.network.ConversationController
22+
import com.getcode.network.TipController
2223
import com.getcode.network.repository.FeatureRepository
2324
import com.getcode.solana.keys.PublicKey
2425
import com.getcode.ui.components.chat.utils.ChatItem
@@ -51,9 +52,8 @@ import javax.inject.Inject
5152
@HiltViewModel
5253
class ConversationViewModel @Inject constructor(
5354
private val conversationController: ConversationController,
54-
currencyUtils: CurrencyUtils,
55-
resources: ResourceHelper,
5655
features: FeatureRepository,
56+
tipController: TipController,
5757
) : BaseViewModel2<ConversationViewModel.State, ConversationViewModel.Event>(
5858
initialState = State.Default,
5959
updateStateForEvent = updateStateForEvent
@@ -71,8 +71,7 @@ class ConversationViewModel @Inject constructor(
7171
val pointers: Map<UUID, MessageStatus>,
7272
) {
7373
data class User(
74-
val username: String,
75-
val publicKey: PublicKey,
74+
val username: String?,
7675
val imageUrl: String?,
7776
)
7877

@@ -98,9 +97,8 @@ class ConversationViewModel @Inject constructor(
9897
Event
9998

10099
data class OnUserRevealed(
101-
val username: String,
102-
val publicKey: PublicKey,
103-
val imageUrl: String?,
100+
val username: String? = null,
101+
val imageUrl: String? = null,
104102
) : Event
105103

106104
data class OnTipsChatCashChanged(val module: Feature) : Event
@@ -222,6 +220,18 @@ class ConversationViewModel @Inject constructor(
222220
.onEach { delay(300) }
223221
.onEach { conversationController.revealIdentity(it) }
224222
.launchIn(viewModelScope)
223+
224+
stateFlow
225+
.mapNotNull { it.user }
226+
.distinctUntilChanged()
227+
.filter { it.imageUrl == null }
228+
.mapNotNull { it.username }
229+
.map { username -> runCatching { tipController.fetch(username) } }
230+
.map { it.getOrNull() }
231+
.filterNotNull()
232+
.map { it.imageUrl }
233+
.onEach { dispatchEvent(Event.OnUserRevealed(imageUrl = it)) }
234+
.launchIn(viewModelScope)
225235
}
226236

227237
val messages: Flow<PagingData<ChatItem>> = stateFlow
@@ -277,7 +287,13 @@ class ConversationViewModel @Inject constructor(
277287
conversationId = conversation.id,
278288
title = conversation.title,
279289
identityRevealed = conversation.hasRevealedIdentity,
280-
pointers = event.conversationWithPointers.pointers
290+
pointers = event.conversationWithPointers.pointers,
291+
user = conversation.user?.let {
292+
State.User(
293+
username = it,
294+
imageUrl = conversation.userImage
295+
)
296+
}
281297
)
282298
}
283299

@@ -316,8 +332,7 @@ class ConversationViewModel @Inject constructor(
316332
is Event.OnUserRevealed -> { state ->
317333
state.copy(
318334
user = State.User(
319-
username = event.username,
320-
publicKey = event.publicKey,
335+
username = event.username ?: state.user?.username,
321336
imageUrl = event.imageUrl,
322337
)
323338
)

0 commit comments

Comments
 (0)