@@ -19,6 +19,7 @@ import com.getcode.model.chat.ChatType
1919import com.getcode.model.chat.Reference
2020import com.getcode.model.uuid
2121import com.getcode.network.ConversationController
22+ import com.getcode.network.TipController
2223import com.getcode.network.repository.FeatureRepository
2324import com.getcode.solana.keys.PublicKey
2425import com.getcode.ui.components.chat.utils.ChatItem
@@ -51,9 +52,8 @@ import javax.inject.Inject
5152@HiltViewModel
5253class 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