Skip to content

Commit f81ee41

Browse files
committed
chore: add naming variance between screens and entities around V1 vs. V2 Chat
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent f2fec69 commit f81ee41

18 files changed

Lines changed: 165 additions & 192 deletions

api/src/main/java/com/getcode/model/chat/Chats.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,15 @@ typealias SetSubscriptionStateRequestV1 = com.codeinc.gen.chat.v1.ChatService.Se
5252
typealias SetSubscriptionStateRequestV2 = com.codeinc.gen.chat.v2.ChatService.SetSubscriptionStateRequest
5353
typealias SetSubscriptionStateResponseV1 = com.codeinc.gen.chat.v1.ChatService.SetSubscriptionStateResponse
5454
typealias SetSubscriptionStateResponseV2 = com.codeinc.gen.chat.v2.ChatService.SetSubscriptionStateResponse
55+
56+
/**
57+
* Code reference to a V1 [Chat] that serves as a collection of messages associated
58+
* with a notification type (Tips, Cash Payments, Web Payments, etc.)
59+
*/
60+
typealias NotificationCollectionEntity = Chat
61+
62+
/**
63+
* Code reference to a V2 [Chat] that is a full end-to-end chat that suports
64+
* peer-to-peer messaging between users.
65+
*/
66+
typealias ConversationEntity = Chat

api/src/main/java/com/getcode/network/ChatHistoryController.kt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.getcode.network
22

3-
import androidx.paging.PagingConfig
43
import androidx.paging.PagingData
54
import androidx.paging.PagingSource
65
import com.getcode.db.AppDatabase
@@ -12,17 +11,16 @@ import com.getcode.mapper.ConversationMessageMapper
1211
import com.getcode.model.Cursor
1312
import com.getcode.model.ID
1413
import com.getcode.model.MessageStatus
15-
import com.getcode.model.chat.Chat
1614
import com.getcode.model.chat.ChatMember
1715
import com.getcode.model.chat.ChatMessage
16+
import com.getcode.model.chat.ConversationEntity
1817
import com.getcode.model.chat.Identity
1918
import com.getcode.model.chat.Platform
2019
import com.getcode.model.chat.Title
2120
import com.getcode.model.chat.isConversation
2221
import com.getcode.model.chat.selfId
2322
import com.getcode.network.client.Client
2423
import com.getcode.network.client.advancePointer
25-
import com.getcode.network.client.fetchChats
2624
import com.getcode.network.client.fetchMessagesFor
2725
import com.getcode.network.client.fetchV2Chats
2826
import com.getcode.network.repository.encodeBase64
@@ -45,14 +43,14 @@ import javax.inject.Singleton
4543
@Singleton
4644
class ChatHistoryController @Inject constructor(
4745
private val client: Client,
48-
private val tipController: TipController,
46+
private val twitterUserController: TwitterUserController,
4947
private val conversationMapper: ConversationMapper,
5048
private val conversationMessageMapper: ConversationMessageMapper,
5149
) : CoroutineScope by CoroutineScope(Dispatchers.IO) {
5250

53-
private val chatEntries = MutableStateFlow<List<Chat>?>(null)
51+
private val chatEntries = MutableStateFlow<List<ConversationEntity>?>(null)
5452

55-
val chats: StateFlow<List<Chat>?>
53+
val chats: StateFlow<List<ConversationEntity>?>
5654
get() = chatEntries
5755
.map { it?.filter { entry -> entry.isConversation } }
5856
.stateIn(this, SharingStarted.Eagerly, emptyList())
@@ -69,7 +67,7 @@ class ChatHistoryController @Inject constructor(
6967
chatFlows.clear()
7068
}
7169

72-
fun updateChatWithMessages(chat: Chat, messages: List<ChatMessage>) {
70+
fun updateChatWithMessages(chat: ConversationEntity, messages: List<ChatMessage>) {
7371
val updatedMessages = (chat.messages + messages).distinctBy { it.id }
7472
val updatedChat = chat.copy(messages = updatedMessages)
7573
val chats = chatEntries.value?.map {
@@ -90,10 +88,10 @@ class ChatHistoryController @Inject constructor(
9088

9189
private fun owner(): KeyPair? = SessionManager.getKeyPair()
9290

93-
suspend fun fetchChats(update: Boolean = false) {
91+
suspend fun fetch(update: Boolean = false) {
9492
if (loadingMessages) return
9593

96-
val updatedWithMessages = mutableListOf<Chat>()
94+
val updatedWithMessages = mutableListOf<ConversationEntity>()
9795
val containers = fetchChatsWithoutMessages()
9896
trace(message = "Fetched ${containers.count()} chats", type = TraceType.Silent)
9997

@@ -122,12 +120,12 @@ class ChatHistoryController @Inject constructor(
122120
chatEntries.value = updatedWithMessages.sortedByDescending { it.lastMessageMillis }
123121
}
124122

125-
fun addChat(chat: Chat) {
123+
fun addChat(chat: ConversationEntity) {
126124
chatEntries.value = (chatEntries.value.orEmpty() + chat)
127125
.sortedByDescending { it.lastMessageMillis }
128126
}
129127

130-
fun findChat(predicate: (Chat) -> Boolean): Chat? {
128+
fun findChat(predicate: (ConversationEntity) -> Boolean): ConversationEntity? {
131129
return chatEntries.value?.firstOrNull(predicate)
132130
}
133131

@@ -144,7 +142,7 @@ class ChatHistoryController @Inject constructor(
144142
}
145143
}
146144

147-
private suspend fun fetchLatestMessageForChat(chat: Chat): Result<ChatMessage?> {
145+
private suspend fun fetchLatestMessageForChat(chat: ConversationEntity): Result<ChatMessage?> {
148146
val encodedId = chat.id.toByteArray().encodeBase64()
149147
Timber.d("fetching last message for $encodedId")
150148
val owner = owner() ?: return Result.success(null)
@@ -169,7 +167,7 @@ class ChatHistoryController @Inject constructor(
169167
}.map { it.getOrNull(0) }
170168
}
171169

172-
private suspend fun fetchChatsWithoutMessages(): List<Chat> {
170+
private suspend fun fetchChatsWithoutMessages(): List<ConversationEntity> {
173171
val owner = owner() ?: return emptyList()
174172
val result = client.fetchV2Chats(owner)
175173
.map { chats ->
@@ -201,14 +199,14 @@ class ChatHistoryController @Inject constructor(
201199
return result.getOrNull().orEmpty()
202200
}
203201

204-
private suspend fun fetchMemberImages(chat: Chat): List<ChatMember> {
202+
private suspend fun fetchMemberImages(chat: ConversationEntity): List<ChatMember> {
205203
return chat.members
206204
.map { member ->
207205
if (member.isSelf) return@map member
208206
if (member.identity == null) return@map member
209207
if (member.identity.imageUrl != null) return@map member
210208
val metadata = runCatching {
211-
tipController.fetch(member.identity.username)
209+
twitterUserController.fetchUser(member.identity.username)
212210
}.getOrNull() ?: return@map member
213211

214212
member.copy(

api/src/main/java/com/getcode/network/ConversationListController.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import javax.inject.Inject
88
class ConversationListController @Inject constructor(
99
private val historyController: ChatHistoryController,
1010
) {
11-
val isLoadingChats: Boolean
11+
val isLoading: Boolean
1212
get() = historyController.loadingMessages
1313

1414
fun observeConversations() = historyController.chats
1515

16-
suspend fun fetchChats() = historyController.fetchChats(true)
16+
suspend fun fetchChats() = historyController.fetch(true)
1717
}
1818

1919
class ChatPagingSource(

0 commit comments

Comments
 (0)