| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DefaultToastFactory.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.UI.Commands; |
| | | 9 | | using MyNet.UI.Notifications.Models; |
| | | 10 | | using MyNet.UI.Toasting.Models; |
| | | 11 | | using MyNet.UI.Toasting.Settings; |
| | | 12 | | |
| | | 13 | | namespace MyNet.UI.Toasting; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Creates default toast instances from notifications. |
| | | 17 | | /// </summary> |
| | | 18 | | public sealed class DefaultToastFactory : IToastFactory |
| | | 19 | | { |
| | | 20 | | private readonly ToastManagerOptions _options; |
| | | 21 | | private readonly ICommandFactory _commandFactory; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="DefaultToastFactory"/> class. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="options">Global toast defaults used when resolving settings.</param> |
| | | 27 | | /// <param name="commandFactory">Optional command factory for toast interaction commands.</param> |
| | | 28 | | public DefaultToastFactory(ToastManagerOptions? options = null, ICommandFactory? commandFactory = null) |
| | | 29 | | { |
| | 21 | 30 | | _options = options ?? new ToastManagerOptions(); |
| | 21 | 31 | | _commandFactory = commandFactory.GetOrDefault(); |
| | 21 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public IToast Create(INotification notification) |
| | | 36 | | { |
| | 18 | 37 | | var settings = ToastSettingsMerger.Merge(notification, _options); |
| | | 38 | | |
| | 18 | 39 | | var closeCommand = notification is IClosableNotification { IsClosable: true } closable |
| | 18 | 40 | | ? _commandFactory.Create((Action)(() => closable.RequestClose())) |
| | 18 | 41 | | : null; |
| | | 42 | | |
| | 18 | 43 | | var clickCommand = notification is ActionNotification { Action: not null } actionNotification |
| | 18 | 44 | | ? _commandFactory.Create(() => actionNotification.Action(actionNotification)) |
| | 18 | 45 | | : null; |
| | | 46 | | |
| | 18 | 47 | | return new Toast(notification, settings, clickCommand: clickCommand, closeCommand: closeCommand); |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |