@@ -17,13 +17,12 @@ import com.getcode.model.Cursor
1717import com.getcode.model.ID
1818import com.getcode.model.MessageStatus
1919import com.getcode.model.chat.ChatMember
20- import com.getcode.model.chat.ChatType
2120import com.getcode.model.chat.Identity
2221import com.getcode.model.chat.Platform
2322import com.getcode.model.chat.Title
2423import com.getcode.model.chat.isConversation
24+ import com.getcode.model.chat.isNotification
2525import com.getcode.model.chat.selfId
26- import com.getcode.model.description
2726import com.getcode.network.client.Client
2827import com.getcode.network.client.advancePointer
2928import com.getcode.network.client.fetchChats
@@ -34,39 +33,42 @@ import com.getcode.network.repository.encodeBase64
3433import com.getcode.network.source.ChatMessagePagingSource
3534import com.getcode.util.resources.ResourceHelper
3635import com.getcode.util.resources.ResourceType
37- import com.getcode.utils.ErrorUtils
3836import com.getcode.utils.TraceType
3937import com.getcode.utils.trace
4038import kotlinx.coroutines.CoroutineScope
4139import kotlinx.coroutines.Dispatchers
4240import kotlinx.coroutines.GlobalScope
4341import kotlinx.coroutines.flow.Flow
4442import kotlinx.coroutines.flow.MutableStateFlow
43+ import kotlinx.coroutines.flow.SharingStarted
4544import kotlinx.coroutines.flow.StateFlow
46- import kotlinx.coroutines.flow.asStateFlow
4745import kotlinx.coroutines.flow.filterNotNull
4846import kotlinx.coroutines.flow.map
47+ import kotlinx.coroutines.flow.stateIn
4948import kotlinx.coroutines.flow.update
50- import kotlinx.coroutines.launch
51- import okhttp3.internal.toImmutableList
5249import timber.log.Timber
5350import java.util.Locale
5451import javax.inject.Inject
5552import javax.inject.Singleton
5653
57- // TODO: See if we can merge this into [ChatController]
5854@Singleton
59- class HistoryController @Inject constructor(
55+ class ChatHistoryController @Inject constructor(
6056 private val client : Client ,
61- private val resources : ResourceHelper ,
6257 private val tipController : TipController ,
6358 private val conversationMapper : ConversationMapper ,
6459 private val conversationMessageMapper : ConversationMessageMapper ,
6560) : CoroutineScope by CoroutineScope(Dispatchers .IO ) {
6661
67- private val _chats = MutableStateFlow <List <Chat >? > (null )
62+ private val chatEntries = MutableStateFlow <List <Chat >? > (null )
63+ val notifications: StateFlow <List <Chat >? >
64+ get() = chatEntries
65+ .map { it?.filter { entry -> entry.isNotification } }
66+ .stateIn(this , SharingStarted .Eagerly , emptyList())
67+
6868 val chats: StateFlow <List <Chat >? >
69- get() = _chats .asStateFlow()
69+ get() = chatEntries
70+ .map { it?.filter { entry -> entry.isConversation } }
71+ .stateIn(this , SharingStarted .Eagerly , emptyList())
7072
7173 var loadingMessages: Boolean = false
7274
@@ -86,9 +88,9 @@ class HistoryController @Inject constructor(
8688 pagerMap[chatId] ? : ChatMessagePagingSource (
8789 client = client,
8890 owner = owner()!! ,
89- chat = _chats .value?.find { it.id == chatId },
91+ chat = chatEntries .value?.find { it.id == chatId },
9092 onMessagesFetched = { messages ->
91- val chat = _chats .value?.find { it.id == chatId } ? : return @ChatMessagePagingSource
93+ val chat = chatEntries .value?.find { it.id == chatId } ? : return @ChatMessagePagingSource
9294 updateChatWithMessages(chat, messages)
9395 }
9496 ).also {
@@ -99,14 +101,14 @@ class HistoryController @Inject constructor(
99101 fun updateChatWithMessages (chat : Chat , messages : List <ChatMessage >) {
100102 val updatedMessages = (chat.messages + messages).distinctBy { it.id }
101103 val updatedChat = chat.copy(messages = updatedMessages)
102- val chats = _chats .value?.map {
104+ val chats = chatEntries .value?.map {
103105 if (it.id == updatedChat.id) {
104106 updatedChat
105107 } else {
106108 it
107109 }
108110 }?.sortedByDescending { it.lastMessageMillis }
109- _chats .update { chats }
111+ chatEntries .update { chats }
110112 }
111113
112114 fun chatFlow (chatId : ID ) =
@@ -132,7 +134,7 @@ class HistoryController @Inject constructor(
132134 if (! update) {
133135 pagerMap.clear()
134136 chatFlows.clear()
135- _chats .value = containers
137+ chatEntries .value = containers
136138
137139 loadingMessages = true
138140 }
@@ -151,13 +153,13 @@ class HistoryController @Inject constructor(
151153 }
152154
153155 loadingMessages = false
154- _chats .value = updatedWithMessages.sortedByDescending { it.lastMessageMillis }
156+ chatEntries .value = updatedWithMessages.sortedByDescending { it.lastMessageMillis }
155157 }
156158
157159 suspend fun advanceReadPointer (chatId : ID ) {
158160 val owner = owner() ? : return
159161
160- _chats .update {
162+ chatEntries .update {
161163 it?.toMutableList()?.apply chats@{
162164 indexOfFirst { chat -> chat.id == chatId }
163165 .takeIf { index -> index >= 0 }
@@ -180,7 +182,7 @@ class HistoryController @Inject constructor(
180182 }
181183
182184 fun advanceReadPointerUpTo (chatId : ID , timestamp : Long ) {
183- _chats .update {
185+ chatEntries .update {
184186 it?.toMutableList()?.apply chats@{
185187 indexOfFirst { chat -> chat.id == chatId }
186188 .takeIf { index -> index >= 0 }
@@ -198,7 +200,7 @@ class HistoryController @Inject constructor(
198200 suspend fun setMuted (chat : Chat , muted : Boolean ): Result <Boolean > {
199201 val owner = owner() ? : return Result .failure(Throwable (" No owner detected" ))
200202
201- _chats .update {
203+ chatEntries .update {
202204 it?.toMutableList()?.apply chats@{
203205 indexOfFirst { item -> item.id == chat.id }
204206 .takeIf { index -> index >= 0 }
@@ -216,7 +218,7 @@ class HistoryController @Inject constructor(
216218 suspend fun setSubscribed (chat : Chat , subscribed : Boolean ): Result <Boolean > {
217219 val owner = owner() ? : return Result .failure(Throwable (" No owner detected" ))
218220
219- _chats .update {
221+ chatEntries .update {
220222 it?.toMutableList()?.apply chats@{
221223 indexOfFirst { item -> item.id == chat.id }
222224 .takeIf { index -> index >= 0 }
@@ -250,7 +252,6 @@ class HistoryController @Inject constructor(
250252 MessageStatus .Delivered
251253 )
252254 }
253-
254255 }
255256 .onFailure {
256257 Timber .e(t = it, " Failed to fetch messages for $encodedId ." )
0 commit comments