< 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:164
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;
 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
 10716    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    {
 426        if (actionButton != null)
 427            actionButton.onClick.AddListener(Dismiss);
 28
 429        AudioScriptableObjects.notification.Play(true);
 430    }
 31
 32    private void OnDisable()
 33    {
 434        if (actionButton != null)
 435            actionButton.onClick.RemoveAllListeners();
 36
 437        StopTimer();
 38
 439        if (showHideAnimator != null)
 440            showHideAnimator.OnWillFinishHide -= DismissInternal;
 441    }
 42
 43    private void OnDestroy()
 44    {
 445        if (showHideAnimator != null)
 446            showHideAnimator.OnWillFinishHide -= DismissInternal;
 47
 448        StopTimer();
 449    }
 50
 51    public void Show(Model model)
 52    {
 653        gameObject.SetActive(true);
 54
 655        if (showHideAnimator == null)
 656            showHideAnimator = GetComponent<ShowHideAnimator>();
 57
 658        if (showHideAnimator != null)
 59        {
 660            showHideAnimator.OnWillFinishHide -= DismissInternal;
 661            showHideAnimator.Show();
 62        }
 63
 664        this.model = model;
 65
 666        if (!string.IsNullOrEmpty(this.model.message))
 67        {
 568            messageLabel.text = this.model.message;
 69        }
 70
 671        if (!string.IsNullOrEmpty(this.model.buttonMessage))
 72        {
 073            actionButtonLabel.text = this.model.buttonMessage;
 74        }
 75
 676        if (this.model.timer > 0)
 77        {
 378            StopTimer();
 379            timerCoroutine = CoroutineStarter.Start(TimerCoroutine(this.model.timer));
 80        }
 81
 682        if (!string.IsNullOrEmpty(this.model.scene))
 83        {
 084            string sceneID = CommonScriptableObjects.sceneID ?? string.Empty;
 085            CurrentSceneUpdated(sceneID, string.Empty);
 86        }
 87
 688        if (actionButton != null)
 89        {
 490            if (this.model.callback != null)
 91            {
 092                actionButton.onClick.AddListener(this.model.callback.Invoke);
 93            }
 94
 495            if (!string.IsNullOrEmpty(this.model.externalCallbackID))
 96            {
 097                actionButton.onClick.AddListener(() =>
 98                {
 99                    // TODO: send message to kernel with callbackID
 0100                });
 101            }
 102        }
 6103    }
 104
 105    private IEnumerator TimerCoroutine(float timer)
 106    {
 3107        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    {
 1123        StopTimer();
 124
 1125        if (!instant && showHideAnimator != null)
 126        {
 1127            showHideAnimator.OnWillFinishHide -= DismissInternal;
 1128            showHideAnimator.OnWillFinishHide += DismissInternal;
 1129            showHideAnimator.Hide();
 1130        }
 131        else
 132        {
 0133            DismissInternal();
 134        }
 135
 1136        if (this != null)
 137        {
 1138            OnNotificationDismissed?.Invoke(this);
 139        }
 1140    }
 141
 142    private void DismissInternal(ShowHideAnimator animator = null)
 143    {
 1144        if (this == null)
 0145            return;
 146
 1147        if (showHideAnimator != null)
 1148            showHideAnimator.OnWillFinishHide -= DismissInternal;
 149
 1150        if (model.destroyOnFinish)
 1151            Destroy(gameObject);
 152        else
 0153            gameObject.SetActive(false);
 0154    }
 155
 156    private void StopTimer()
 157    {
 12158        if (timerCoroutine != null)
 159        {
 1160            CoroutineStarter.Stop(timerCoroutine);
 1161            timerCoroutine = null;
 162        }
 12163    }
 164}