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