< Summary

Class:WelcomeHUDView
Assembly:WelcomeHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WelcomeHUD/WelcomeHUDView.cs
Covered lines:50
Uncovered lines:19
Coverable lines:69
Total lines:164
Line coverage:72.4% (50 of 69)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CreateView()0%110100%
Initialize(...)0%6.896070.83%
OnTextureRetrieved(...)0%6200%
UpdateTimer()0%20400%
SetupButtons(...)0%660100%
ClearButtons()0%220100%
CleanUp()0%110100%
CleanUpPromise()0%3.192033.33%
DisposeSelf()0%220100%
SetVisible(...)0%110100%
OnDestroy()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WelcomeHUD/WelcomeHUDView.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using DCL;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.Events;
 7using UnityEngine.UI;
 8
 9public interface IWelcomeHUDView
 10{
 11    void Initialize(UnityAction<int> OnConfirm, UnityAction OnClose, MessageOfTheDayConfig config);
 12    void DisposeSelf();
 13    void SetVisible(bool visible);
 14}
 15
 16public class WelcomeHUDView : MonoBehaviour, IWelcomeHUDView
 17{
 18    private const string PREFAB_PATH = "WelcomeHUD";
 19
 20    [SerializeField] internal RawImage backgroundImage;
 21    [SerializeField] internal TextMeshProUGUI titleText;
 22    [SerializeField] internal TextMeshProUGUI timeLeftText;
 23    [SerializeField] internal TextMeshProUGUI bodyText;
 24    [SerializeField] internal Transform buttonsParent;
 25    [SerializeField] internal Button_OnPointerDown closeButton;
 26
 27    [Space]
 28    [SerializeField] internal GameObject buttonPrefab;
 29
 30    [Space]
 31    [SerializeField] internal bool useImageNativeSize = false;
 32
 33    private AssetPromise_Texture texturePromise;
 34    private Coroutine updateTimeCoroutine;
 35    private UnityAction OnCloseButtonPressed;
 36
 37    private bool disposed = false;
 38
 739    public static WelcomeHUDView CreateView() => Instantiate(Resources.Load<GameObject>(PREFAB_PATH)).GetComponent<Welco
 40
 41    public void Initialize(UnityAction<int> OnConfirm, UnityAction OnClose, MessageOfTheDayConfig config)
 42    {
 543        if (config == null)
 44        {
 145            CleanUp();
 146            return;
 47        }
 48
 449        ClearButtons();
 450        titleText.text = config.title;
 451        bodyText.text = config.body;
 52
 453        SetupButtons(config.buttons, OnConfirm);
 454        closeButton.onPointerDown -= OnCloseButtonPressed;
 455        OnCloseButtonPressed = () => OnClose?.Invoke();
 456        closeButton.onPointerDown += OnCloseButtonPressed;
 57
 458        CleanUpPromise();
 459        if (!String.IsNullOrEmpty(config.background_banner))
 60        {
 061            texturePromise = new AssetPromise_Texture(config.background_banner);
 062            texturePromise.OnSuccessEvent += OnTextureRetrieved;
 63        }
 64
 465        AssetPromiseKeeper_Texture.i.Keep(texturePromise);
 66
 467        if (config.endUnixTimestamp > 0)
 68        {
 069            timeLeftText.gameObject.SetActive(false);
 070            if (updateTimeCoroutine == null)
 071                updateTimeCoroutine = StartCoroutine(UpdateTimer(DateTimeOffset.FromUnixTimeSeconds(config.endUnixTimest
 072        }
 73        else
 74        {
 475            timeLeftText.gameObject.SetActive(false);
 476            if (updateTimeCoroutine != null)
 077                StopCoroutine(updateTimeCoroutine);
 78        }
 479    }
 80
 81    private void OnTextureRetrieved(Asset_Texture assetTexture)
 82    {
 083        backgroundImage.texture = assetTexture.texture;
 84
 085        if (useImageNativeSize)
 086            backgroundImage.SetNativeSize();
 087    }
 88
 89    private IEnumerator UpdateTimer(DateTime localEndDate)
 90    {
 091        while (true)
 92        {
 093            TimeSpan remainingTime = DateTime.Now > localEndDate ? TimeSpan.Zero : (localEndDate - DateTime.Now);
 094            timeLeftText.text = $"Ending in: {remainingTime.Days:00}:{remainingTime.Hours:00}:{remainingTime.Minutes:00}
 095            yield return new WaitForSeconds(.5f);
 96        }
 97    }
 98
 99    private void SetupButtons(MessageOfTheDayConfig.Button[] buttons, UnityAction<int> buttonsCallback)
 100    {
 4101        if (buttons == null)
 1102            return;
 103
 14104        for (var i = 0; i < buttons.Length; i++)
 105        {
 4106            int index = i;
 4107            GameObject buttonGO = Instantiate(buttonPrefab, buttonsParent);
 108
 4109            Button_OnPointerDown button = buttonGO.GetComponentInChildren<Button_OnPointerDown>();
 4110            if (button != null)
 4111                button.onPointerDown += () => buttonsCallback?.Invoke(index);
 112
 4113            TextMeshProUGUI caption = buttonGO.GetComponentInChildren<TextMeshProUGUI>();
 4114            if (caption != null)
 4115                caption.text = buttons[i].caption;
 116
 4117            Image buttonImage = buttonGO.GetComponentInChildren<Image>();
 4118            if (buttonImage != null)
 4119                buttonImage.color = buttons[i].tint;
 120        }
 3121    }
 122
 123    private void ClearButtons()
 124    {
 32125        for (int i = buttonsParent.childCount - 1; i >= 0; i--)
 126        {
 4127            Destroy(buttonsParent.GetChild(i).gameObject);
 128        }
 12129    }
 130
 131    private void CleanUp()
 132    {
 8133        CleanUpPromise();
 8134        ClearButtons();
 8135        closeButton.onPointerDown -= OnCloseButtonPressed;
 8136    }
 137
 138    private void CleanUpPromise()
 139    {
 12140        if (texturePromise == null)
 12141            return;
 142
 0143        AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 0144        texturePromise.Cleanup();
 0145        texturePromise = null;
 0146    }
 147
 148    public void DisposeSelf()
 149    {
 4150        if (!disposed)
 151        {
 4152            disposed = true;
 4153            Destroy(gameObject);
 154        }
 4155    }
 156
 16157    public void SetVisible(bool visible) { gameObject.SetActive(visible); }
 158
 159    private void OnDestroy()
 160    {
 7161        disposed = true;
 7162        CleanUp();
 7163    }
 164}