< Summary

Class:Notification
Assembly:NotificationHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationHUD/Notification.cs
Covered lines:57
Uncovered lines:9
Coverable lines:66
Total lines:163
Line coverage:86.3% (57 of 66)
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%11.7311081.82%
TimerCoroutine()0%330100%
CurrentSceneUpdated(...)0%6200%
Dismiss()0%110100%
Dismiss(...)0%550100%
DismissInternal(...)0%4.034087.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;
 2using System.Collections;
 3using TMPro;
 4using UnityEngine;
 5using UnityEngine.UI;
 6using DCL.NotificationModel;
 7
 8public class Notification : MonoBehaviour, INotification
 9{
 10    [SerializeField] internal TextMeshProUGUI messageLabel;
 11
 12    [SerializeField] private Button actionButton;
 13
 14    [SerializeField] private TextMeshProUGUI actionButtonLabel;
 15
 20216    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    {
 5226        if (actionButton != null)
 5227            actionButton.onClick.AddListener(Dismiss);
 28
 5229        AudioScriptableObjects.notification.Play(true);
 5230    }
 31
 32    private void OnDisable()
 33    {
 5234        if (actionButton != null)
 5235            actionButton.onClick.RemoveAllListeners();
 36
 5237        StopTimer();
 38
 5239        if (showHideAnimator != null)
 440            showHideAnimator.OnWillFinishHide -= DismissInternal;
 5241    }
 42
 43    private void OnDestroy()
 44    {
 5245        if (showHideAnimator != null)
 446            showHideAnimator.OnWillFinishHide -= DismissInternal;
 47
 5248        StopTimer();
 5249    }
 50
 51    public void Show(Model model)
 52    {
 453        gameObject.SetActive(true);
 54
 455        if (showHideAnimator == null)
 456            showHideAnimator = GetComponent<ShowHideAnimator>();
 57
 458        if (showHideAnimator != null)
 59        {
 460            showHideAnimator.OnWillFinishHide -= DismissInternal;
 461            showHideAnimator.Show();
 62        }
 63
 464        this.model = model;
 65
 466        if (!string.IsNullOrEmpty(this.model.message))
 67        {
 468            messageLabel.text = this.model.message;
 69        }
 70
 471        if (!string.IsNullOrEmpty(this.model.buttonMessage))
 72        {
 073            actionButtonLabel.text = this.model.buttonMessage;
 74        }
 75
 476        if (this.model.timer > 0)
 77        {
 178            StopTimer();
 179            timerCoroutine = CoroutineStarter.Start(TimerCoroutine(this.model.timer));
 80        }
 81
 482        if (this.model.scene > 0)
 83        {
 084            CurrentSceneUpdated(CommonScriptableObjects.sceneNumber, -1);
 85        }
 86
 487        if (actionButton != null)
 88        {
 489            if (this.model.callback != null)
 90            {
 091                actionButton.onClick.AddListener(this.model.callback.Invoke);
 92            }
 93
 494            if (!string.IsNullOrEmpty(this.model.externalCallbackID))
 95            {
 096                actionButton.onClick.AddListener(() =>
 97                {
 98                    // TODO: send message to kernel with callbackID
 099                });
 100            }
 101        }
 4102    }
 103
 104    private IEnumerator TimerCoroutine(float timer)
 105    {
 1106        yield return WaitForSecondsCache.Get(timer);
 1107        Dismiss();
 1108    }
 109
 110    private void CurrentSceneUpdated(int currentSceneNumber, int previousSceneNumber)
 111    {
 0112        if (currentSceneNumber != model.scene)
 113        {
 0114            Dismiss();
 115        }
 0116    }
 117
 2118    protected virtual void Dismiss() { Dismiss(false); }
 119
 120    public void Dismiss(bool instant)
 121    {
 2122        StopTimer();
 123
 2124        if (!instant && showHideAnimator != null)
 125        {
 1126            showHideAnimator.OnWillFinishHide -= DismissInternal;
 1127            showHideAnimator.OnWillFinishHide += DismissInternal;
 1128            showHideAnimator.Hide();
 129        }
 130        else
 131        {
 1132            DismissInternal();
 133        }
 134
 2135        if (this != null)
 136        {
 2137            OnNotificationDismissed?.Invoke(this);
 138        }
 1139    }
 140
 141    private void DismissInternal(ShowHideAnimator animator = null)
 142    {
 2143        if (this == null)
 0144            return;
 145
 2146        if (showHideAnimator != null)
 1147            showHideAnimator.OnWillFinishHide -= DismissInternal;
 148
 2149        if (model.destroyOnFinish)
 1150            Destroy(gameObject);
 151        else
 1152            gameObject.SetActive(false);
 1153    }
 154
 155    private void StopTimer()
 156    {
 107157        if (timerCoroutine != null)
 158        {
 1159            CoroutineStarter.Stop(timerCoroutine);
 1160            timerCoroutine = null;
 161        }
 107162    }
 163}