< Summary

Class:Notification
Assembly:NotificationHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationHUD/Notification.cs
Covered lines:58
Uncovered lines:10
Coverable lines:68
Total lines:164
Line coverage:85.2% (58 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%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
 16116    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    {
 5326        if (actionButton != null)
 5327            actionButton.onClick.AddListener(Dismiss);
 28
 5329        AudioScriptableObjects.notification.Play(true);
 5330    }
 31
 32    private void OnDisable()
 33    {
 5334        if (actionButton != null)
 5335            actionButton.onClick.RemoveAllListeners();
 36
 5337        StopTimer();
 38
 5339        if (showHideAnimator != null)
 540            showHideAnimator.OnWillFinishHide -= DismissInternal;
 5341    }
 42
 43    private void OnDestroy()
 44    {
 5345        if (showHideAnimator != null)
 546            showHideAnimator.OnWillFinishHide -= DismissInternal;
 47
 5348        StopTimer();
 5349    }
 50
 51    public void Show(Model model)
 52    {
 553        gameObject.SetActive(true);
 54
 555        if (showHideAnimator == null)
 556            showHideAnimator = GetComponent<ShowHideAnimator>();
 57
 558        if (showHideAnimator != null)
 59        {
 560            showHideAnimator.OnWillFinishHide -= DismissInternal;
 561            showHideAnimator.Show();
 62        }
 63
 564        this.model = model;
 65
 566        if (!string.IsNullOrEmpty(this.model.message))
 67        {
 468            messageLabel.text = this.model.message;
 69        }
 70
 571        if (!string.IsNullOrEmpty(this.model.buttonMessage))
 72        {
 073            actionButtonLabel.text = this.model.buttonMessage;
 74        }
 75
 576        if (this.model.timer > 0)
 77        {
 278            StopTimer();
 279            timerCoroutine = CoroutineStarter.Start(TimerCoroutine(this.model.timer));
 80        }
 81
 582        if (!string.IsNullOrEmpty(this.model.scene))
 83        {
 084            string sceneID = CommonScriptableObjects.sceneID ?? string.Empty;
 085            CurrentSceneUpdated(sceneID, string.Empty);
 86        }
 87
 588        if (actionButton != null)
 89        {
 590            if (this.model.callback != null)
 91            {
 092                actionButton.onClick.AddListener(this.model.callback.Invoke);
 93            }
 94
 595            if (!string.IsNullOrEmpty(this.model.externalCallbackID))
 96            {
 097                actionButton.onClick.AddListener(() =>
 98                {
 99                    // TODO: send message to kernel with callbackID
 0100                });
 101            }
 102        }
 5103    }
 104
 105    private IEnumerator TimerCoroutine(float timer)
 106    {
 2107        yield return WaitForSecondsCache.Get(timer);
 1108        Dismiss();
 1109    }
 110
 111    private void CurrentSceneUpdated(string current, string previous)
 112    {
 0113        if (string.CompareOrdinal(current, model.scene) != 0)
 114        {
 0115            Dismiss();
 116        }
 0117    }
 118
 2119    protected virtual void Dismiss() { Dismiss(false); }
 120
 121    public void Dismiss(bool instant)
 122    {
 3123        StopTimer();
 124
 3125        if (!instant && showHideAnimator != null)
 126        {
 1127            showHideAnimator.OnWillFinishHide -= DismissInternal;
 1128            showHideAnimator.OnWillFinishHide += DismissInternal;
 1129            showHideAnimator.Hide();
 1130        }
 131        else
 132        {
 2133            DismissInternal();
 134        }
 135
 3136        if (this != null)
 137        {
 3138            OnNotificationDismissed?.Invoke(this);
 139        }
 2140    }
 141
 142    private void DismissInternal(ShowHideAnimator animator = null)
 143    {
 3144        if (this == null)
 0145            return;
 146
 3147        if (showHideAnimator != null)
 2148            showHideAnimator.OnWillFinishHide -= DismissInternal;
 149
 3150        if (model.destroyOnFinish)
 2151            Destroy(gameObject);
 152        else
 1153            gameObject.SetActive(false);
 1154    }
 155
 156    private void StopTimer()
 157    {
 111158        if (timerCoroutine != null)
 159        {
 2160            CoroutineStarter.Stop(timerCoroutine);
 2161            timerCoroutine = null;
 162        }
 111163    }
 164}