| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | | using DCL.NotificationModel; |
| | 7 | |
|
| | 8 | | public class Notification : MonoBehaviour, INotification |
| | 9 | | { |
| | 10 | | [SerializeField] internal TextMeshProUGUI messageLabel; |
| | 11 | |
|
| | 12 | | [SerializeField] private Button actionButton; |
| | 13 | |
|
| | 14 | | [SerializeField] private TextMeshProUGUI actionButtonLabel; |
| | 15 | |
|
| 107 | 16 | | public Model model { get; private set; } = new Model(); |
| | 17 | |
|
| | 18 | | public event Action<INotification> OnNotificationDismissed; |
| | 19 | |
|
| | 20 | | Coroutine timerCoroutine; |
| | 21 | |
|
| | 22 | | ShowHideAnimator showHideAnimator; |
| | 23 | |
|
| | 24 | | private void OnEnable() |
| | 25 | | { |
| 4 | 26 | | if (actionButton != null) |
| 4 | 27 | | actionButton.onClick.AddListener(Dismiss); |
| | 28 | |
|
| 4 | 29 | | AudioScriptableObjects.notification.Play(true); |
| 4 | 30 | | } |
| | 31 | |
|
| | 32 | | private void OnDisable() |
| | 33 | | { |
| 4 | 34 | | if (actionButton != null) |
| 4 | 35 | | actionButton.onClick.RemoveAllListeners(); |
| | 36 | |
|
| 4 | 37 | | StopTimer(); |
| | 38 | |
|
| 4 | 39 | | if (showHideAnimator != null) |
| 4 | 40 | | showHideAnimator.OnWillFinishHide -= DismissInternal; |
| 4 | 41 | | } |
| | 42 | |
|
| | 43 | | private void OnDestroy() |
| | 44 | | { |
| 4 | 45 | | if (showHideAnimator != null) |
| 4 | 46 | | showHideAnimator.OnWillFinishHide -= DismissInternal; |
| | 47 | |
|
| 4 | 48 | | StopTimer(); |
| 4 | 49 | | } |
| | 50 | |
|
| | 51 | | public void Show(Model model) |
| | 52 | | { |
| 6 | 53 | | gameObject.SetActive(true); |
| | 54 | |
|
| 6 | 55 | | if (showHideAnimator == null) |
| 6 | 56 | | showHideAnimator = GetComponent<ShowHideAnimator>(); |
| | 57 | |
|
| 6 | 58 | | if (showHideAnimator != null) |
| | 59 | | { |
| 6 | 60 | | showHideAnimator.OnWillFinishHide -= DismissInternal; |
| 6 | 61 | | showHideAnimator.Show(); |
| | 62 | | } |
| | 63 | |
|
| 6 | 64 | | this.model = model; |
| | 65 | |
|
| 6 | 66 | | if (!string.IsNullOrEmpty(this.model.message)) |
| | 67 | | { |
| 5 | 68 | | messageLabel.text = this.model.message; |
| | 69 | | } |
| | 70 | |
|
| 6 | 71 | | if (!string.IsNullOrEmpty(this.model.buttonMessage)) |
| | 72 | | { |
| 0 | 73 | | actionButtonLabel.text = this.model.buttonMessage; |
| | 74 | | } |
| | 75 | |
|
| 6 | 76 | | if (this.model.timer > 0) |
| | 77 | | { |
| 3 | 78 | | StopTimer(); |
| 3 | 79 | | timerCoroutine = CoroutineStarter.Start(TimerCoroutine(this.model.timer)); |
| | 80 | | } |
| | 81 | |
|
| 6 | 82 | | if (!string.IsNullOrEmpty(this.model.scene)) |
| | 83 | | { |
| 0 | 84 | | string sceneID = CommonScriptableObjects.sceneID ?? string.Empty; |
| 0 | 85 | | CurrentSceneUpdated(sceneID, string.Empty); |
| | 86 | | } |
| | 87 | |
|
| 6 | 88 | | if (actionButton != null) |
| | 89 | | { |
| 4 | 90 | | if (this.model.callback != null) |
| | 91 | | { |
| 0 | 92 | | actionButton.onClick.AddListener(this.model.callback.Invoke); |
| | 93 | | } |
| | 94 | |
|
| 4 | 95 | | if (!string.IsNullOrEmpty(this.model.externalCallbackID)) |
| | 96 | | { |
| 0 | 97 | | actionButton.onClick.AddListener(() => |
| | 98 | | { |
| | 99 | | // TODO: send message to kernel with callbackID |
| 0 | 100 | | }); |
| | 101 | | } |
| | 102 | | } |
| 6 | 103 | | } |
| | 104 | |
|
| | 105 | | private IEnumerator TimerCoroutine(float timer) |
| | 106 | | { |
| 3 | 107 | | yield return WaitForSecondsCache.Get(timer); |
| 1 | 108 | | Dismiss(); |
| 1 | 109 | | } |
| | 110 | |
|
| | 111 | | private void CurrentSceneUpdated(string current, string previous) |
| | 112 | | { |
| 0 | 113 | | if (string.CompareOrdinal(current, model.scene) != 0) |
| | 114 | | { |
| 0 | 115 | | Dismiss(); |
| | 116 | | } |
| 0 | 117 | | } |
| | 118 | |
|
| 2 | 119 | | protected virtual void Dismiss() { Dismiss(false); } |
| | 120 | |
|
| | 121 | | public void Dismiss(bool instant) |
| | 122 | | { |
| 1 | 123 | | StopTimer(); |
| | 124 | |
|
| 1 | 125 | | if (!instant && showHideAnimator != null) |
| | 126 | | { |
| 1 | 127 | | showHideAnimator.OnWillFinishHide -= DismissInternal; |
| 1 | 128 | | showHideAnimator.OnWillFinishHide += DismissInternal; |
| 1 | 129 | | showHideAnimator.Hide(); |
| 1 | 130 | | } |
| | 131 | | else |
| | 132 | | { |
| 0 | 133 | | DismissInternal(); |
| | 134 | | } |
| | 135 | |
|
| 1 | 136 | | if (this != null) |
| | 137 | | { |
| 1 | 138 | | OnNotificationDismissed?.Invoke(this); |
| | 139 | | } |
| 1 | 140 | | } |
| | 141 | |
|
| | 142 | | private void DismissInternal(ShowHideAnimator animator = null) |
| | 143 | | { |
| 1 | 144 | | if (this == null) |
| 0 | 145 | | return; |
| | 146 | |
|
| 1 | 147 | | if (showHideAnimator != null) |
| 1 | 148 | | showHideAnimator.OnWillFinishHide -= DismissInternal; |
| | 149 | |
|
| 1 | 150 | | if (model.destroyOnFinish) |
| 1 | 151 | | Destroy(gameObject); |
| | 152 | | else |
| 0 | 153 | | gameObject.SetActive(false); |
| 0 | 154 | | } |
| | 155 | |
|
| | 156 | | private void StopTimer() |
| | 157 | | { |
| 12 | 158 | | if (timerCoroutine != null) |
| | 159 | | { |
| 1 | 160 | | CoroutineStarter.Stop(timerCoroutine); |
| 1 | 161 | | timerCoroutine = null; |
| | 162 | | } |
| 12 | 163 | | } |
| | 164 | | } |