| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ShellNotificationsHost.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Windows.Input; |
| | | 9 | | using MyNet.UI.Commands; |
| | | 10 | | using MyNet.UI.ViewModels.Shell.Notifications; |
| | | 11 | | |
| | | 12 | | namespace MyNet.UI.ViewModels.Shell.Drawers; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Coordinates notifications drawer state and toggle command availability for the shell host. |
| | | 16 | | /// </summary> |
| | | 17 | | public sealed class ShellNotificationsHost : IDisposable |
| | | 18 | | { |
| | | 19 | | private readonly IShellNotificationsDrawerHost _drawer; |
| | | 20 | | private readonly ShellNotificationsViewModel _notifications; |
| | | 21 | | private readonly ShellNotificationsDrawerCoordinator _coordinator; |
| | | 22 | | private readonly Func<bool> _canUseShellChrome; |
| | | 23 | | private readonly Action? _onDrawerOpening; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes a new instance of the <see cref="ShellNotificationsHost"/> class. |
| | | 27 | | /// </summary> |
| | | 28 | | public ShellNotificationsHost( |
| | | 29 | | IShellNotificationsDrawerHost drawer, |
| | | 30 | | ShellNotificationsViewModel notifications, |
| | | 31 | | ShellNotificationsDrawerCoordinator coordinator, |
| | | 32 | | Func<bool> canUseShellChrome, |
| | | 33 | | Action? onDrawerOpening = null) |
| | | 34 | | { |
| | 24 | 35 | | _drawer = drawer ?? throw new ArgumentNullException(nameof(drawer)); |
| | 24 | 36 | | _notifications = notifications ?? throw new ArgumentNullException(nameof(notifications)); |
| | 24 | 37 | | _coordinator = coordinator ?? throw new ArgumentNullException(nameof(coordinator)); |
| | 24 | 38 | | _canUseShellChrome = canUseShellChrome ?? throw new ArgumentNullException(nameof(canUseShellChrome)); |
| | 24 | 39 | | _onDrawerOpening = onDrawerOpening; |
| | | 40 | | |
| | 24 | 41 | | _coordinator.Bind(drawer, notifications); |
| | 24 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Creates the toggle notifications command. |
| | | 46 | | /// </summary> |
| | | 47 | | public ICommand CreateToggleCommand(ICommandFactory commands) |
| | 24 | 48 | | => commands.Create(() => SetDrawer(ShellDrawerAction.Toggle), CanToggle); |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Applies an action to the notifications drawer. |
| | | 52 | | /// </summary> |
| | | 53 | | public void SetDrawer(ShellDrawerAction action) |
| | | 54 | | { |
| | 9 | 55 | | if (action.WillOpen(_drawer.IsNotificationsOpen)) |
| | 6 | 56 | | _onDrawerOpening?.Invoke(); |
| | | 57 | | |
| | 9 | 58 | | _drawer.IsNotificationsOpen = action.ApplyToOpenState(_drawer.IsNotificationsOpen); |
| | 9 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Closes the notifications drawer. |
| | | 63 | | /// </summary> |
| | 12 | 64 | | public void Close() => _drawer.IsNotificationsOpen = false; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Returns whether the toggle command can execute. |
| | | 68 | | /// </summary> |
| | 0 | 69 | | public bool CanToggle() => _canUseShellChrome() && _notifications.HasNotifications; |
| | | 70 | | |
| | | 71 | | /// <inheritdoc /> |
| | 3 | 72 | | public void Dispose() => _coordinator.Unbind(); |
| | | 73 | | } |
| | | 74 | | |