Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 4f57aa06 authored by Rafael Tonholo's avatar Rafael Tonholo
Browse files

feat(notification): trigger in-app notification using `InAppNotificationNotifier` and event bus

parent 171eb482
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ import androidx.compose.ui.tooling.preview.PreviewLightDark
import app.k9mail.core.ui.compose.common.koin.koinPreview
import app.k9mail.core.ui.compose.designsystem.PreviewWithThemesLightDark
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.flowOf
import net.thunderbird.core.common.resources.StringsResourceManager
import net.thunderbird.core.outcome.Outcome
@@ -16,6 +17,8 @@ import net.thunderbird.feature.mail.account.api.BaseAccount
import net.thunderbird.feature.notification.api.command.NotificationCommand.Failure
import net.thunderbird.feature.notification.api.command.NotificationCommand.Success
import net.thunderbird.feature.notification.api.content.Notification
import net.thunderbird.feature.notification.api.receiver.InAppNotificationEvent
import net.thunderbird.feature.notification.api.receiver.InAppNotificationReceiver
import net.thunderbird.feature.notification.api.sender.NotificationSender

@PreviewLightDark
@@ -47,6 +50,10 @@ private fun SecretDebugSettingsScreenPreview() {
                    ): Flow<Outcome<Success<Notification>, Failure<Notification>>> =
                        error("not implemented")
                },
                notificationReceiver = object : InAppNotificationReceiver {
                    override val events: SharedFlow<InAppNotificationEvent>
                        get() = error("not implemented")
                },
            )
        }
    } WithContent {
+1 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ val featureDebugSettingsModule = module {
            stringsResourceManager = get(),
            accountManager = get(),
            notificationSender = get(),
            notificationReceiver = get(),
        )
    }
}
+15 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.thunderbird.core.common.resources.StringsResourceManager
@@ -27,12 +28,14 @@ import net.thunderbird.feature.notification.api.content.MailNotification
import net.thunderbird.feature.notification.api.content.Notification
import net.thunderbird.feature.notification.api.content.PushServiceNotification
import net.thunderbird.feature.notification.api.content.SystemNotification
import net.thunderbird.feature.notification.api.receiver.InAppNotificationReceiver
import net.thunderbird.feature.notification.api.sender.NotificationSender

internal class DebugNotificationSectionViewModel(
    private val stringsResourceManager: StringsResourceManager,
    private val accountManager: AccountManager<BaseAccount>,
    private val notificationSender: NotificationSender,
    private val notificationReceiver: InAppNotificationReceiver,
    private val mainDispatcher: CoroutineDispatcher = Dispatchers.Main,
    ioDispatcher: CoroutineDispatcher = Dispatchers.IO,
) : BaseViewModel<State, Event, Effect>(initialState = State()), DebugNotificationSectionContract.ViewModel {
@@ -76,6 +79,18 @@ internal class DebugNotificationSectionViewModel(
                }
            }
        }

        viewModelScope.launch {
            notificationReceiver
                .events
                .collectLatest { event ->
                    updateState { state ->
                        state.copy(
                            notificationStatusLog = state.notificationStatusLog + " In-app notification event: $event",
                        )
                    }
                }
        }
    }

    override fun event(event: Event) {
+1 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ import org.jetbrains.kotlin.gradle.internal.config.LanguageFeature

plugins {
    id(ThunderbirdPlugins.Library.kmpCompose)
    alias(libs.plugins.dev.mokkery)
}

kotlin {
+19 −0
Original line number Diff line number Diff line
package net.thunderbird.feature.notification.api.receiver

import kotlinx.coroutines.flow.SharedFlow
import net.thunderbird.feature.notification.api.content.InAppNotification

/**
 * Interface for receiving in-app notification events.
 *
 * This interface provides a [SharedFlow] of [InAppNotificationEvent]s that can be observed
 * by UI components or other parts of the application to react to in-app notifications.
 */
interface InAppNotificationReceiver {
    val events: SharedFlow<InAppNotificationEvent>
}

sealed interface InAppNotificationEvent {
    data class Show(val notification: InAppNotification) : InAppNotificationEvent
    data class Dismiss(val notification: InAppNotification) : InAppNotificationEvent
}
Loading