@@ -19,6 +19,7 @@ import com.getcode.model.chat.ChatType
19
19
import com.getcode.model.chat.Reference
20
20
import com.getcode.model.uuid
21
21
import com.getcode.network.ConversationController
22
+ import com.getcode.network.TipController
22
23
import com.getcode.network.repository.FeatureRepository
23
24
import com.getcode.solana.keys.PublicKey
24
25
import com.getcode.ui.components.chat.utils.ChatItem
@@ -51,9 +52,8 @@ import javax.inject.Inject
51
52
@HiltViewModel
52
53
class ConversationViewModel @Inject constructor(
53
54
private val conversationController : ConversationController ,
54
- currencyUtils : CurrencyUtils ,
55
- resources : ResourceHelper ,
56
55
features : FeatureRepository ,
56
+ tipController : TipController ,
57
57
) : BaseViewModel2<ConversationViewModel.State, ConversationViewModel.Event>(
58
58
initialState = State .Default ,
59
59
updateStateForEvent = updateStateForEvent
@@ -71,8 +71,7 @@ class ConversationViewModel @Inject constructor(
71
71
val pointers : Map <UUID , MessageStatus >,
72
72
) {
73
73
data class User (
74
- val username : String ,
75
- val publicKey : PublicKey ,
74
+ val username : String? ,
76
75
val imageUrl : String? ,
77
76
)
78
77
@@ -98,9 +97,8 @@ class ConversationViewModel @Inject constructor(
98
97
Event
99
98
100
99
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 ,
104
102
) : Event
105
103
106
104
data class OnTipsChatCashChanged (val module : Feature ) : Event
@@ -222,6 +220,18 @@ class ConversationViewModel @Inject constructor(
222
220
.onEach { delay(300 ) }
223
221
.onEach { conversationController.revealIdentity(it) }
224
222
.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)
225
235
}
226
236
227
237
val messages: Flow <PagingData <ChatItem >> = stateFlow
@@ -277,7 +287,13 @@ class ConversationViewModel @Inject constructor(
277
287
conversationId = conversation.id,
278
288
title = conversation.title,
279
289
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
+ }
281
297
)
282
298
}
283
299
@@ -316,8 +332,7 @@ class ConversationViewModel @Inject constructor(
316
332
is Event .OnUserRevealed -> { state ->
317
333
state.copy(
318
334
user = State .User (
319
- username = event.username,
320
- publicKey = event.publicKey,
335
+ username = event.username ? : state.user?.username,
321
336
imageUrl = event.imageUrl,
322
337
)
323
338
)
0 commit comments