< Summary

Class:Notification
Assembly:NotificationHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationHUD/Notification.cs
Covered lines:55
Uncovered lines:13
Coverable lines:68
Total lines:176
Line coverage:80.8% (55 of 68)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Notification()0%110100%
OnEnable()0%220100%
OnDisable()0%330100%
OnDestroy()0%220100%
Show(...)0%14.7413078.26%
TimerCoroutine()0%330100%
CurrentSceneUpdated(...)0%6200%
Dismiss()0%110100%
Dismiss(...)0%5.025090%
DismissInternal(...)0%4.844062.5%
StopTimer()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationHUD/Notification.cs

#LineLine coverage
 1using System.Collections;
 2using TMPro;
 3using UnityEngine;
 4using UnityEngine.UI;
 5
 6public class Notification : MonoBehaviour
 7{
 8    public class Model
 9    {
 10        public NotificationFactory.Type type;
 11        public string message;
 12        public string buttonMessage;
 13        public float timer;
 14        public string scene;
 15        public System.Action callback;
 16        public string externalCallbackID;
 17
 18        public string groupID;
 19        public bool destroyOnFinish = false;
 20    }
 21
 22    [SerializeField] internal TextMeshProUGUI messageLabel;
 23
 24    [SerializeField] private Button actionButton;
 25
 26    [SerializeField] private TextMeshProUGUI actionButtonLabel;
 27
 21228    public Notification.Model model { get; private set; } = new Model();
 29
 30    public event System.Action<Notification> OnNotificationDismissed;
 31
 32    Coroutine timerCoroutine;
 33
 34    ShowHideAnimator showHideAnimator;
 35
 36    private void OnEnable()
 37    {
 438        if (actionButton != null)
 439            actionButton.onClick.AddListener(Dismiss);
 40
 441        AudioScriptableObjects.notification.Play(true);
 442    }
 43
 44    private void OnDisable()
 45    {
 446        if (actionButton != null)
 447            actionButton.onClick.RemoveAllListeners();
 48
 449        StopTimer();
 50
 451        if (showHideAnimator != null)
 452            showHideAnimator.OnWillFinishHide -= DismissInternal;
 453    }
 54
 55    private void OnDestroy()
 56    {
 457        if (showHideAnimator != null)
 458            showHideAnimator.OnWillFinishHide -= DismissInternal;
 59
 460        StopTimer();
 461    }
 62
 63    public void Show(Notification.Model model)
 64    {
 665        gameObject.SetActive(true);
 66
 667        if (showHideAnimator == null)
 668            showHideAnimator = GetComponent<ShowHideAnimator>();
 69
 670        if (showHideAnimator != null)
 71        {
 672            showHideAnimator.OnWillFinishHide -= DismissInternal;
 673            showHideAnimator.Show();
 74        }
 75
 676        this.model = model;
 77
 678        if (!string.IsNullOrEmpty(this.model.message))
 79        {
 580            messageLabel.text = this.model.message;
 81        }
 82
 683        if (!string.IsNullOrEmpty(this.model.buttonMessage))
 84        {
 085            actionButtonLabel.text = this.model.buttonMessage;
 86        }
 87
 688        if (this.model.timer > 0)
 89        {
 390            StopTimer();
 391            timerCoroutine = CoroutineStarter.Start(TimerCoroutine(this.model.timer));
 92        }
 93
 694        if (!string.IsNullOrEmpty(this.model.scene))
 95        {
 096            string sceneID = CommonScriptableObjects.sceneID ?? string.Empty;
 097            CurrentSceneUpdated(sceneID, string.Empty);
 98        }
 99
 6100        if (actionButton != null)
 101        {
 4102            if (this.model.callback != null)
 103            {
 0104                actionButton.onClick.AddListener(this.model.callback.Invoke);
 105            }
 106
 4107            if (!string.IsNullOrEmpty(this.model.externalCallbackID))
 108            {
 0109                actionButton.onClick.AddListener(() =>
 110                {
 111                    // TODO: send message to kernel with callbackID
 0112                });
 113            }
 114        }
 6115    }
 116
 117    private IEnumerator TimerCoroutine(float timer)
 118    {
 3119        yield return WaitForSecondsCache.Get(timer);
 1120        Dismiss();
 1121    }
 122
 123    private void CurrentSceneUpdated(string current, string previous)
 124    {
 0125        if (string.CompareOrdinal(current, model.scene) != 0)
 126        {
 0127            Dismiss();
 128        }
 0129    }
 130
 2131    protected virtual void Dismiss() { Dismiss(false); }
 132
 133    public void Dismiss(bool instant)
 134    {
 1135        StopTimer();
 136
 1137        if (!instant && showHideAnimator != null)
 138        {
 1139            showHideAnimator.OnWillFinishHide -= DismissInternal;
 1140            showHideAnimator.OnWillFinishHide += DismissInternal;
 1141            showHideAnimator.Hide();
 1142        }
 143        else
 144        {
 0145            DismissInternal();
 146        }
 147
 1148        if (this != null)
 149        {
 1150            OnNotificationDismissed?.Invoke(this);
 151        }
 1152    }
 153
 154    private void DismissInternal(ShowHideAnimator animator = null)
 155    {
 1156        if (this == null)
 0157            return;
 158
 1159        if (showHideAnimator != null)
 1160            showHideAnimator.OnWillFinishHide -= DismissInternal;
 161
 1162        if (model.destroyOnFinish)
 1163            Destroy(gameObject);
 164        else
 0165            gameObject.SetActive(false);
 0166    }
 167
 168    private void StopTimer()
 169    {
 12170        if (timerCoroutine != null)
 171        {
 1172            CoroutineStarter.Stop(timerCoroutine);
 1173            timerCoroutine = null;
 174        }
 12175    }
 176}