| | 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 | |
|
| 61 | 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 | | { |
| 4 | 53 | | gameObject.SetActive(true); |
| | 54 | |
|
| 4 | 55 | | if (showHideAnimator == null) |
| 4 | 56 | | showHideAnimator = GetComponent<ShowHideAnimator>(); |
| | 57 | |
|
| 4 | 58 | | if (showHideAnimator != null) |
| | 59 | | { |
| 4 | 60 | | showHideAnimator.OnWillFinishHide -= DismissInternal; |
| 4 | 61 | | showHideAnimator.Show(); |
| | 62 | | } |
| | 63 | |
|
| 4 | 64 | | this.model = model; |
| | 65 | |
|
| 4 | 66 | | if (!string.IsNullOrEmpty(this.model.message)) |
| | 67 | | { |
| 4 | 68 | | messageLabel.text = this.model.message; |
| | 69 | | } |
| | 70 | |
|
| 4 | 71 | | if (!string.IsNullOrEmpty(this.model.buttonMessage)) |
| | 72 | | { |
| 0 | 73 | | actionButtonLabel.text = this.model.buttonMessage; |
| | 74 | | } |
| | 75 | |
|
| 4 | 76 | | if (this.model.timer > 0) |
| | 77 | | { |
| 1 | 78 | | StopTimer(); |
| 1 | 79 | | timerCoroutine = CoroutineStarter.Start(TimerCoroutine(this.model.timer)); |
| | 80 | | } |
| | 81 | |
|
| 4 | 82 | | if (this.model.scene > 0) |
| | 83 | | { |
| 0 | 84 | | CurrentSceneUpdated(CommonScriptableObjects.sceneNumber, -1); |
| | 85 | | } |
| | 86 | |
|
| 4 | 87 | | if (actionButton != null) |
| | 88 | | { |
| 4 | 89 | | if (this.model.callback != null) |
| | 90 | | { |
| 0 | 91 | | actionButton.onClick.AddListener(this.model.callback.Invoke); |
| | 92 | | } |
| | 93 | |
|
| 4 | 94 | | if (!string.IsNullOrEmpty(this.model.externalCallbackID)) |
| | 95 | | { |
| 0 | 96 | | actionButton.onClick.AddListener(() => |
| | 97 | | { |
| | 98 | | // TODO: send message to kernel with callbackID |
| 0 | 99 | | }); |
| | 100 | | } |
| | 101 | | } |
| 4 | 102 | | } |
| | 103 | |
|
| | 104 | | private IEnumerator TimerCoroutine(float timer) |
| | 105 | | { |
| 1 | 106 | | yield return WaitForSecondsCache.Get(timer); |
| 1 | 107 | | Dismiss(); |
| 1 | 108 | | } |
| | 109 | |
|
| | 110 | | private void CurrentSceneUpdated(int currentSceneNumber, int previousSceneNumber) |
| | 111 | | { |
| 0 | 112 | | if (currentSceneNumber != model.scene) |
| | 113 | | { |
| 0 | 114 | | Dismiss(); |
| | 115 | | } |
| 0 | 116 | | } |
| | 117 | |
|
| 2 | 118 | | protected virtual void Dismiss() { Dismiss(false); } |
| | 119 | |
|
| | 120 | | public void Dismiss(bool instant) |
| | 121 | | { |
| 1 | 122 | | StopTimer(); |
| | 123 | |
|
| 1 | 124 | | if (!instant && showHideAnimator != null) |
| | 125 | | { |
| 1 | 126 | | showHideAnimator.OnWillFinishHide -= DismissInternal; |
| 1 | 127 | | showHideAnimator.OnWillFinishHide += DismissInternal; |
| 1 | 128 | | showHideAnimator.Hide(); |
| | 129 | | } |
| | 130 | | else |
| | 131 | | { |
| 0 | 132 | | DismissInternal(); |
| | 133 | | } |
| | 134 | |
|
| 1 | 135 | | if (this != null) |
| | 136 | | { |
| 1 | 137 | | OnNotificationDismissed?.Invoke(this); |
| | 138 | | } |
| 1 | 139 | | } |
| | 140 | |
|
| | 141 | | private void DismissInternal(ShowHideAnimator animator = null) |
| | 142 | | { |
| 1 | 143 | | if (this == null) |
| 0 | 144 | | return; |
| | 145 | |
|
| 1 | 146 | | if (showHideAnimator != null) |
| 1 | 147 | | showHideAnimator.OnWillFinishHide -= DismissInternal; |
| | 148 | |
|
| 1 | 149 | | if (model.destroyOnFinish) |
| 1 | 150 | | Destroy(gameObject); |
| | 151 | | else |
| 0 | 152 | | gameObject.SetActive(false); |
| 0 | 153 | | } |
| | 154 | |
|
| | 155 | | private void StopTimer() |
| | 156 | | { |
| 10 | 157 | | if (timerCoroutine != null) |
| | 158 | | { |
| 1 | 159 | | CoroutineStarter.Stop(timerCoroutine); |
| 1 | 160 | | timerCoroutine = null; |
| | 161 | | } |
| 10 | 162 | | } |
| | 163 | | } |