< Summary

Class:WelcomeHUDController
Assembly:WelcomeHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WelcomeHUD/WelcomeHUDController.cs
Covered lines:44
Uncovered lines:20
Coverable lines:64
Total lines:166
Line coverage:68.7% (44 of 64)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CreateView()0%110100%
WelcomeHUDController()0%110100%
Initialize(...)0%2.262060%
Close()0%110100%
OnConfirmPressed(...)0%880100%
OnClosePressed()0%2100%
Dispose()0%220100%
SetVisibility(...)0%220100%
SendAction(...)0%2.062075%
StartPopupRoutine()0%110100%
StopPopupRoutine()0%220100%
ShowPopupDelayed()0%37.1411040%
ResetPopupDelayed()0%6200%
TutorialActive_OnChange(...)0%6200%
EmailPromptActive_OnChange(...)0%6200%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections;
 3using DCL.Helpers;
 4using DCL.Interface;
 5using UnityEngine;
 6
 7/// <summary>
 8/// Model with the configuration for a Message of the Day
 9/// </summary>
 10[Serializable]
 11public class MessageOfTheDayConfig
 12{
 13    /// <summary>
 14    /// Model with the configuration for each button in the Message of the Day
 15    /// </summary>
 16    [Serializable]
 17    public class Button
 18    {
 19        public string caption;
 20        public string action; //global chat action to perform
 21        public Color tint;
 22    }
 23
 24    public string background_banner;
 25    public int endUnixTimestamp;
 26    public string title;
 27    public string body;
 28    public Button[] buttons;
 29}
 30
 31public class WelcomeHUDController : IHUD
 32{
 33    const float POPUP_DELAY = 2;
 34
 35    internal IWelcomeHUDView view;
 36    private MessageOfTheDayConfig config = null;
 37    private Coroutine showPopupDelayedRoutine;
 38    private bool isPopupRoutineRunning = false;
 39
 440    internal virtual IWelcomeHUDView CreateView() => WelcomeHUDView.CreateView();
 41
 442    public WelcomeHUDController()
 43    {
 444        view = CreateView();
 445        view.SetVisible(false);
 46
 447        CommonScriptableObjects.tutorialActive.OnChange -= TutorialActive_OnChange;
 448        CommonScriptableObjects.tutorialActive.OnChange += TutorialActive_OnChange;
 49
 450        CommonScriptableObjects.emailPromptActive.OnChange -= EmailPromptActive_OnChange;
 451        CommonScriptableObjects.emailPromptActive.OnChange += EmailPromptActive_OnChange;
 452    }
 53
 54    public void Initialize(MessageOfTheDayConfig config)
 55    {
 356        this.config = config;
 57
 358        if (!isPopupRoutineRunning)
 359            StartPopupRoutine();
 60        else
 061            ResetPopupDelayed();
 062    }
 63
 664    internal void Close() { SetVisibility(false); }
 65
 66    internal virtual void OnConfirmPressed(int buttonIndex)
 67    {
 368        Close();
 69
 370        if (config?.buttons != null && buttonIndex >= 0 && buttonIndex < config?.buttons.Length)
 71        {
 172            string action = config.buttons[buttonIndex].action;
 173            SendAction(action);
 74        }
 375    }
 76
 077    void OnClosePressed() { Close(); }
 78
 79    public void Dispose()
 80    {
 481        StopPopupRoutine();
 82
 483        CommonScriptableObjects.tutorialActive.OnChange -= TutorialActive_OnChange;
 484        CommonScriptableObjects.emailPromptActive.OnChange -= EmailPromptActive_OnChange;
 85
 486        view?.DisposeSelf();
 487    }
 88
 89    public void SetVisibility(bool visible)
 90    {
 491        view.SetVisible(visible);
 492        if (visible)
 93        {
 194            Utils.UnlockCursor();
 195            AudioScriptableObjects.dialogOpen.Play(true);
 196        }
 97        else
 98        {
 399            AudioScriptableObjects.dialogClose.Play(true);
 100        }
 101
 4102        CommonScriptableObjects.motdActive.Set(visible);
 4103    }
 104
 105    internal virtual void SendAction(string action)
 106    {
 1107        if (string.IsNullOrEmpty(action))
 0108            return;
 109
 1110        WebInterface.SendChatMessage(new ChatMessage
 111        {
 112            messageType = ChatMessage.Type.NONE,
 113            recipient = string.Empty,
 114            body = action,
 115        });
 1116    }
 117
 6118    void StartPopupRoutine() { showPopupDelayedRoutine = CoroutineStarter.Start(ShowPopupDelayed(POPUP_DELAY)); }
 119
 120    void StopPopupRoutine()
 121    {
 4122        if (showPopupDelayedRoutine != null)
 123        {
 3124            CoroutineStarter.Stop(showPopupDelayedRoutine);
 3125            showPopupDelayedRoutine = null;
 126        }
 127
 4128        isPopupRoutineRunning = false;
 4129    }
 130
 131    private IEnumerator ShowPopupDelayed(float seconds)
 132    {
 3133        isPopupRoutineRunning = true;
 3134        view.Initialize(OnConfirmPressed, OnClosePressed, config);
 135
 6136        yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get());
 2137        yield return new WaitUntil(() => !CommonScriptableObjects.tutorialActive.Get());
 0138        yield return new WaitUntil(() => !CommonScriptableObjects.emailPromptActive.Get());
 0139        yield return WaitForSecondsCache.Get(seconds);
 0140        yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get());
 141
 0142        SetVisibility(true);
 0143        isPopupRoutineRunning = false;
 0144    }
 145
 146    void ResetPopupDelayed()
 147    {
 0148        if (isPopupRoutineRunning)
 149        {
 0150            StopPopupRoutine();
 0151            StartPopupRoutine();
 152        }
 0153    }
 154
 155    private void TutorialActive_OnChange(bool current, bool previous)
 156    {
 0157        if (current)
 0158            ResetPopupDelayed();
 0159    }
 160
 161    private void EmailPromptActive_OnChange(bool current, bool previous)
 162    {
 0163        if (current)
 0164            ResetPopupDelayed();
 0165    }
 166}