| | 1 | | using System.Collections.Generic; |
| | 2 | |
|
| | 3 | | public class NotificationHUDController : IHUD, INotificationHUDController |
| | 4 | | { |
| | 5 | | [System.Serializable] |
| | 6 | | public class Model |
| | 7 | | { |
| 6 | 8 | | public List<INotification> notifications = new(); |
| | 9 | | } |
| | 10 | |
|
| 24 | 11 | | public NotificationHUDView view { get; } |
| 10 | 12 | | public Model model { get; } |
| | 13 | |
|
| 12 | 14 | | public NotificationHUDController(NotificationHUDView view) : this(new Model(), view) { } |
| | 15 | |
|
| 6 | 16 | | public NotificationHUDController(Model model, NotificationHUDView view) |
| | 17 | | { |
| 6 | 18 | | this.model = model; |
| 6 | 19 | | this.view = view; |
| | 20 | |
|
| 6 | 21 | | view.OnNotificationDismissedEvent += OnNotificationDismissed; |
| 6 | 22 | | } |
| | 23 | |
|
| | 24 | | public void Dispose() |
| | 25 | | { |
| 6 | 26 | | if (view != null) |
| | 27 | | { |
| 6 | 28 | | UnityEngine.Object.Destroy(view.gameObject); |
| 6 | 29 | | view.OnNotificationDismissedEvent -= OnNotificationDismissed; |
| | 30 | | } |
| 6 | 31 | | } |
| | 32 | |
|
| | 33 | | public void ShowNotification(INotification notification) |
| | 34 | | { |
| 0 | 35 | | model.notifications.Add(notification); |
| 0 | 36 | | view.ShowNotification(notification, notification.model); |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | public void ShowNotification(DCL.NotificationModel.Model model) |
| | 40 | | { |
| 4 | 41 | | if (!string.IsNullOrEmpty(model.groupID)) |
| | 42 | | { |
| | 43 | | //NOTE(Brian): If more notifications of the same group are visible, hide them. |
| | 44 | | // This works when having notifications in different contexts (i.e. FriendsHUD vs global notific |
| 0 | 45 | | DismissAllNotifications(model.groupID); |
| | 46 | | } |
| | 47 | |
|
| 4 | 48 | | var notification = view.ShowNotification(model); |
| 4 | 49 | | this.model.notifications.Add(notification); |
| 4 | 50 | | } |
| | 51 | |
|
| | 52 | | public void DismissAllNotifications(string groupID) |
| | 53 | | { |
| 0 | 54 | | if (this.model.notifications.Count > 0) |
| | 55 | | { |
| | 56 | | //NOTE(Brian): Copy list to avoid modify while iterating error |
| 0 | 57 | | var notiList = new List<INotification>(this.model.notifications); |
| 0 | 58 | | int notiCount = notiList.Count; |
| | 59 | |
|
| 0 | 60 | | for (int i = 0; i < notiCount; i++) |
| | 61 | | { |
| 0 | 62 | | if (!string.IsNullOrEmpty(groupID) && groupID != notiList[i].model.groupID) |
| | 63 | | continue; |
| | 64 | |
|
| 0 | 65 | | notiList[i].Dismiss(instant: true); |
| | 66 | | } |
| | 67 | | } |
| 0 | 68 | | } |
| | 69 | |
|
| 2 | 70 | | private void OnNotificationDismissed(INotification notification) { model.notifications.Remove(notification); } |
| | 71 | |
|
| 2 | 72 | | public void SetActive(bool active) { view.SetActive(active); } |
| | 73 | |
|
| | 74 | |
|
| | 75 | |
|
| 2 | 76 | | public void SetVisibility(bool visible) { SetActive(visible); } |
| | 77 | |
|
| | 78 | | public void ShowWelcomeNotification() |
| | 79 | | { |
| 0 | 80 | | var model = WelcomeNotification.Get(); |
| 0 | 81 | | ShowNotification(model); |
| 0 | 82 | | } |
| | 83 | | } |