Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing the option to use multiple LifetimeSupervisors #120

Open
wants to merge 2 commits into
base: master-v2
Choose a base branch
from

Conversation

alexandrejh
Copy link

Feature: Allowing multiple LifetimeSupervisors with different behaviors
I need to use two different LifetimeSupervisor, one with CountBasedLifetimeSupervisor and other with TimeAndCountBasedLifetimeSupervisor. If I create 2 separated Notifiers the notifications can be displayed over the notifications that are already opened.
For this I create a way to set multiple LifetimeSupervisors to same Notifiers and same DisplaySupervisor.
To work correctly the LifetimeSupervisors need to be cached, to pass the same instance.
Example of how to use:

private static Notifier GenerateNotifier(double notificationLifeTime, Corner notificationPosition)
{
    //here I check if the LifetimeSupervisor to use is based on time
    //if it is null I create new instance
    if (notificationLifeTime > 0 && _lifetimeTimeAndCount == null)
    {
        _lifetimeTimeAndCount = new TimeAndCountBasedLifetimeSupervisor(
            notificationLifetime: TimeSpan.FromSeconds(notificationLifeTime),
            maximumNotificationCount: MaximumNotificationCount.FromCount(5));
    }
    
    //here I check if the LifetimeSupervisor to use is based only on count
    //if it is null I create new instance
    if (notificationLifeTime == 0 && _lifetimeCount == null)
    {
        _lifetimeCount = new CountBasedLifetimeSupervisor(maximumNotificationCount: MaximumNotificationCount.FromCount(5));
    }

    //get the correct LifetimeSupervisor to use
    INotificationsLifetimeSupervisor lifetimeSupervisor = notificationLifeTime > 0 ? (INotificationsLifetimeSupervisor)_lifetimeTimeAndCount : (INotificationsLifetimeSupervisor)_lifetimeCount;

    if (_notifier == null)
    {
        _notifier = new Notifier(cfg =>
        {
            cfg.PositionProvider = new WindowPositionProvider(
                parentWindow: Application.Current.MainWindow,
                corner: notificationPosition,
                offsetX: 10,
                offsetY: 49);

            cfg.LifetimeSupervisor = lifetimeSupervisor;

            cfg.Dispatcher = Application.Current.Dispatcher;
        });
    }

    //update the current LifetimeSupervisor in the notifier
    _notifier.UpdateLifetimeSupervisor(lifetimeSupervisor);

    return _notifier;
}

In this case I'm not checking if the notificationLifeTime is different, but if you need you can cache into a dictionary with notificationLifeTime as the key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant