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

Unverified Commit e60d7058 authored by Rafael Tonholo's avatar Rafael Tonholo
Browse files

feat(notification): add notification system foundation

parent e597d012
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation(projects.core.common)
            implementation(projects.core.outcome)
        }
    }
}
+24 −0
Original line number Diff line number Diff line
package net.thunderbird.feature.notification.command

import net.thunderbird.core.outcome.Outcome
import net.thunderbird.feature.notification.command.NotificationCommand.CommandOutcome.Failure
import net.thunderbird.feature.notification.command.NotificationCommand.CommandOutcome.Success
import net.thunderbird.feature.notification.content.InAppNotification
import net.thunderbird.feature.notification.receiver.NotificationNotifier

/**
 * A command that handles in-app notifications.
 *
 * This class is responsible for executing the logic associated with displaying an in-app notification.
 *
 * @param notification The [InAppNotification] to be handled.
 * @param notifier The [NotificationNotifier] responsible for actually displaying the notification.
 */
internal class InAppNotificationCommand(
    notification: InAppNotification,
    notifier: NotificationNotifier<InAppNotification>,
) : NotificationCommand<InAppNotification>(notification, notifier) {
    override fun execute(): Outcome<Success<InAppNotification>, Failure<InAppNotification>> {
        TODO("Implementation on GitHub Issue #9245")
    }
}
+55 −0
Original line number Diff line number Diff line
package net.thunderbird.feature.notification.command

import net.thunderbird.core.outcome.Outcome
import net.thunderbird.feature.notification.command.NotificationCommand.CommandOutcome.Failure
import net.thunderbird.feature.notification.command.NotificationCommand.CommandOutcome.Success
import net.thunderbird.feature.notification.content.Notification
import net.thunderbird.feature.notification.receiver.NotificationNotifier

/**
 * Represents a command that can be executed on a notification.
 *
 * This class is the base for all notification commands. It defines the basic structure
 * of a command and the possible outcomes of its execution.
 *
 * @param TNotification The type of notification this command operates on.
 * @property notification The notification instance this command will act upon.
 * @property notifier The notifier responsible for handling the notification.
 */
abstract class NotificationCommand<TNotification : Notification>(
    protected val notification: TNotification,
    protected val notifier: NotificationNotifier<TNotification>,
) {
    /**
     * Executes the command.
     * @return The result of the execution.
     */
    internal abstract fun execute(): Outcome<Success<TNotification>, Failure<TNotification>>

    /**
     * Represents the outcome of a command's execution.
     */
    sealed interface CommandOutcome {
        /**
         * Represents a successful command execution.
         *
         * @param TNotification The type of notification associated with the command.
         * @property command The command that was executed successfully.
         */
        data class Success<out TNotification : Notification>(
            val command: NotificationCommand<out TNotification>,
        ) : CommandOutcome

        /**
         * Represents a failed command execution.
         *
         * @param TNotification The type of notification associated with the command.
         * @property command The command that failed.
         * @property throwable The exception that caused the failure.
         */
        data class Failure<out TNotification : Notification>(
            val command: NotificationCommand<out TNotification>,
            val throwable: Throwable,
        ) : CommandOutcome
    }
}
+47 −0
Original line number Diff line number Diff line
package net.thunderbird.feature.notification.command

import net.thunderbird.feature.notification.content.InAppNotification
import net.thunderbird.feature.notification.content.Notification
import net.thunderbird.feature.notification.content.SystemNotification
import net.thunderbird.feature.notification.receiver.InAppNotificationNotifier
import net.thunderbird.feature.notification.receiver.SystemNotificationNotifier

/**
 * A factory for creating a set of notification commands based on a given notification.
 */
internal class NotificationCommandFactory(
    private val systemNotificationNotifier: SystemNotificationNotifier,
    private val inAppNotificationNotifier: InAppNotificationNotifier,
) {
    /**
     * Creates a set of [NotificationCommand]s for the given [notification].
     *
     * The commands are returned in a [LinkedHashSet] to preserve the order in which they should be executed.
     *
     * @param notification The notification for which to create commands.
     * @return A set of notification commands.
     */
    fun create(notification: Notification): LinkedHashSet<NotificationCommand<out Notification>> {
        val commands = linkedSetOf<NotificationCommand<out Notification>>()

        if (notification is SystemNotification) {
            commands.add(
                SystemNotificationCommand(
                    notification = notification,
                    notifier = systemNotificationNotifier,
                ),
            )
        }

        if (notification is InAppNotification) {
            commands.add(
                InAppNotificationCommand(
                    notification = notification,
                    notifier = inAppNotificationNotifier,
                ),
            )
        }

        return commands
    }
}
+23 −0
Original line number Diff line number Diff line
package net.thunderbird.feature.notification.command

import net.thunderbird.core.outcome.Outcome
import net.thunderbird.feature.notification.command.NotificationCommand.CommandOutcome.Failure
import net.thunderbird.feature.notification.command.NotificationCommand.CommandOutcome.Success
import net.thunderbird.feature.notification.content.InAppNotification
import net.thunderbird.feature.notification.content.SystemNotification
import net.thunderbird.feature.notification.receiver.NotificationNotifier

/**
 * Command for displaying system notifications.
 *
 * @param notification The system notification to display.
 * @param notifier The notifier responsible for displaying the notification.
 */
internal class SystemNotificationCommand(
    notification: SystemNotification,
    notifier: NotificationNotifier<SystemNotification>,
) : NotificationCommand<SystemNotification>(notification, notifier) {
    override fun execute(): Outcome<Success<SystemNotification>, Failure<SystemNotification>> {
        TODO("Implementation on GitHub Issue #9245")
    }
}
Loading