Skip to content

Commit 5ab8497

Browse files
committed
fix(notifications): in-app notification events getting lost by not having listeners
1 parent 34ca35e commit 5ab8497

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

feature/notification/api/src/androidMain/kotlin/net/thunderbird/feature/notification/api/ui/InAppNotificationHost.kt

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import androidx.compose.runtime.LaunchedEffect
77
import androidx.compose.runtime.collectAsState
88
import androidx.compose.runtime.getValue
99
import androidx.compose.ui.Modifier
10-
import androidx.lifecycle.compose.collectAsStateWithLifecycle
10+
import androidx.lifecycle.compose.LifecycleStartEffect
11+
import androidx.lifecycle.coroutineScope
1112
import app.k9mail.core.ui.compose.designsystem.template.Scaffold
1213
import kotlinx.collections.immutable.ImmutableSet
14+
import kotlinx.coroutines.flow.filter
15+
import kotlinx.coroutines.launch
1316
import net.thunderbird.feature.notification.api.receiver.InAppNotificationEvent
1417
import net.thunderbird.feature.notification.api.receiver.InAppNotificationReceiver
1518
import net.thunderbird.feature.notification.api.ui.action.NotificationAction
@@ -94,20 +97,22 @@ fun InAppNotificationHost(
9497
eventFilter: (InAppNotificationEvent) -> Boolean = { true },
9598
content: @Composable (PaddingValues) -> Unit,
9699
) {
97-
val inAppNotificationEvents by koinInject<InAppNotificationReceiver>()
98-
.events
99-
.collectAsStateWithLifecycle(initialValue = null)
100-
100+
val receiver = koinInject<InAppNotificationReceiver>()
101101
val state by hostStateHolder.currentInAppNotificationHostState.collectAsState()
102102

103-
LaunchedEffect(inAppNotificationEvents, eventFilter) {
104-
val event = inAppNotificationEvents
105-
if (event != null && eventFilter(event)) {
106-
when (event) {
107-
is InAppNotificationEvent.Dismiss -> Unit // TODO(#9626): Handle dismiss
108-
is InAppNotificationEvent.Show -> hostStateHolder.showInAppNotification(event.notification)
109-
}
103+
LifecycleStartEffect(receiver, eventFilter) {
104+
val job = lifecycle.coroutineScope.launch {
105+
receiver
106+
.events
107+
.filter(eventFilter)
108+
.collect { event ->
109+
when (event) {
110+
is InAppNotificationEvent.Dismiss -> hostStateHolder.dismiss(event.notification)
111+
is InAppNotificationEvent.Show -> hostStateHolder.showInAppNotification(event.notification)
112+
}
113+
}
110114
}
115+
onStopOrDispose { job.cancel() }
111116
}
112117

113118
LaunchedEffect(state.snackbarVisual, onSnackbarNotificationEvent) {

feature/notification/impl/src/commonMain/kotlin/net/thunderbird/feature/notification/impl/receiver/InAppNotificationEventBus.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal interface InAppNotificationEventBus : InAppNotificationReceiver {
2222
}
2323

2424
internal fun InAppNotificationEventBus(): InAppNotificationEventBus = object : InAppNotificationEventBus {
25-
private val _events = MutableSharedFlow<InAppNotificationEvent>(replay = 1)
25+
private val _events = MutableSharedFlow<InAppNotificationEvent>(replay = 32)
2626
override val events: SharedFlow<InAppNotificationEvent> = _events.asSharedFlow()
2727

2828
override suspend fun publish(event: InAppNotificationEvent) {

0 commit comments

Comments
 (0)