| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ShellNotificationsDrawerCoordinator.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 | | |
| | | 10 | | namespace MyNet.UI.ViewModels.Shell.Notifications; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Closes the notifications drawer when the panel becomes empty and refreshes toggle command availability. |
| | | 14 | | /// </summary> |
| | | 15 | | public sealed class ShellNotificationsDrawerCoordinator |
| | | 16 | | { |
| | | 17 | | private IShellNotificationsDrawerHost? _host; |
| | | 18 | | private ShellNotificationsViewModel? _notifications; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Subscribes to <paramref name="notifications"/> and drives <paramref name="host"/> drawer state. |
| | | 22 | | /// </summary> |
| | | 23 | | public void Bind(IShellNotificationsDrawerHost host, ShellNotificationsViewModel notifications) |
| | | 24 | | { |
| | 30 | 25 | | ArgumentNullException.ThrowIfNull(host); |
| | 30 | 26 | | ArgumentNullException.ThrowIfNull(notifications); |
| | | 27 | | |
| | 30 | 28 | | Unbind(); |
| | | 29 | | |
| | 30 | 30 | | _host = host; |
| | 30 | 31 | | _notifications = notifications; |
| | 30 | 32 | | _notifications.NotificationsChanged += OnNotificationsChanged; |
| | 30 | 33 | | OnNotificationsChanged(notifications, EventArgs.Empty); |
| | 30 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Removes subscriptions created by <see cref="Bind"/>. |
| | | 38 | | /// </summary> |
| | | 39 | | public void Unbind() |
| | | 40 | | { |
| | 36 | 41 | | _notifications?.NotificationsChanged -= OnNotificationsChanged; |
| | | 42 | | |
| | 36 | 43 | | _host = null; |
| | 36 | 44 | | _notifications = null; |
| | 36 | 45 | | } |
| | | 46 | | |
| | | 47 | | private void OnNotificationsChanged(object? sender, EventArgs e) |
| | | 48 | | { |
| | 30 | 49 | | if (_host is null || _notifications is null) |
| | 0 | 50 | | return; |
| | | 51 | | |
| | 30 | 52 | | if (!_notifications.HasNotifications) |
| | 30 | 53 | | _host.IsNotificationsOpen = false; |
| | | 54 | | |
| | 30 | 55 | | if (_host.ToggleNotificationsCommand is IRaiseCanExecuteChanged raise) |
| | 6 | 56 | | raise.RaiseCanExecuteChanged(); |
| | 30 | 57 | | } |
| | | 58 | | } |
| | | 59 | | |