Skip to content

Commit a577df3

Browse files
authored
chore(chat): only notify of message received when not actively chatting (#901)
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 8eb4422 commit a577df3

4 files changed

Lines changed: 16 additions & 1 deletion

File tree

apps/flipcash/shared/chat/src/main/kotlin/com/flipcash/shared/chat/ChatCoordinator.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ class ChatCoordinator @Inject constructor(
8282
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
8383
private val cluster = MutableStateFlow<AccountCluster?>(null)
8484
private val _state = MutableStateFlow(ChatState())
85-
8685
private var syncJob: Job? = null
8786
private var eventStreamCollectJob: Job? = null
8887
private var eventStreamRetryJob: Job? = null
@@ -264,6 +263,14 @@ class ChatCoordinator @Inject constructor(
264263
return messagingController.advancePointer(chatId, PointerType.READ, messageId)
265264
}
266265

266+
fun setActiveChatId(chatId: ChatId?) {
267+
_state.update { it.copy(activeChat = chatId) }
268+
}
269+
270+
fun isActiveChat(chatId: ChatId): Boolean {
271+
return _state.value.activeChat == chatId
272+
}
273+
267274
fun dismissNotifications(chatId: ChatId) {
268275
notificationManager.cancel(chatId.hashCode())
269276
}

apps/flipcash/shared/chat/src/main/kotlin/com/flipcash/shared/chat/ChatState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ data class ChatState(
99
val feed: List<ChatMetadata> = emptyList(),
1010
val typingIndicators: Map<ChatId, Set<ActiveTypist>> = emptyMap(),
1111
val feedSyncState: FeedSyncState = FeedSyncState.Idle,
12+
val activeChat: ChatId? = null,
1213
)
1314

1415
data class ChatSummary(

apps/flipcash/shared/notifications/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ android {
99

1010
dependencies {
1111
implementation(project(":apps:flipcash:shared:authentication"))
12+
implementation(project(":apps:flipcash:shared:chat"))
1213
implementation(project(":apps:flipcash:shared:contacts"))
1314
implementation(project(":apps:flipcash:shared:persistence:sources"))
1415
implementation(project(":apps:flipcash:shared:phone"))

apps/flipcash/shared/notifications/src/main/kotlin/com/flipcash/app/notifications/NotificationService.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.flipcash.app.auth.AuthManager
2424
import com.flipcash.app.contacts.ContactCoordinator
2525
import com.flipcash.app.contacts.ContactResolver
2626
import com.flipcash.app.core.util.Linkify
27+
import com.flipcash.shared.chat.ChatCoordinator
2728
import com.flipcash.app.tokens.TokenCoordinator
2829
import com.flipcash.services.controllers.PushController
2930
import com.flipcash.services.models.NavigationTrigger
@@ -74,6 +75,9 @@ class NotificationService : FirebaseMessagingService(),
7475
@Inject
7576
lateinit var contactResolver: ContactResolver
7677

78+
@Inject
79+
lateinit var chatCoordinator: ChatCoordinator
80+
7781
override fun onNewToken(token: String) {
7882
super.onNewToken(token)
7983
authenticateIfNeeded {
@@ -142,6 +146,8 @@ class NotificationService : FirebaseMessagingService(),
142146
notificationManager.createNotificationChannel(channel)
143147

144148
val chatId = (payload?.navigation as? NavigationTrigger.Chat)?.chatId
149+
if (chatId != null && chatCoordinator.isActiveChat(chatId)) return
150+
145151
val groupKey = payload?.groupKey?.takeIf { it.isNotEmpty() }
146152

147153
val builder = NotificationCompat.Builder(this, channel.id)

0 commit comments

Comments
 (0)