< Summary

Class:WelcomeHUDController
Assembly:WelcomeHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WelcomeHUD/WelcomeHUDController.cs
Covered lines:40
Uncovered lines:23
Coverable lines:63
Total lines:163
Line coverage:63.4% (40 of 63)
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%6200%
ShowPopupDelayed()0%18.7411060%
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        CommonScriptableObjects.tutorialActive.OnChange -= TutorialActive_OnChange;
 482        CommonScriptableObjects.emailPromptActive.OnChange -= EmailPromptActive_OnChange;
 83
 484        view?.DisposeSelf();
 485    }
 86
 87    public void SetVisibility(bool visible)
 88    {
 489        view.SetVisible(visible);
 490        if (visible)
 91        {
 192            Utils.UnlockCursor();
 193            AudioScriptableObjects.dialogOpen.Play(true);
 194        }
 95        else
 96        {
 397            AudioScriptableObjects.dialogClose.Play(true);
 98        }
 99
 4100        CommonScriptableObjects.motdActive.Set(visible);
 4101    }
 102
 103    internal virtual void SendAction(string action)
 104    {
 1105        if (string.IsNullOrEmpty(action))
 0106            return;
 107
 1108        WebInterface.SendChatMessage(new ChatMessage
 109        {
 110            messageType = ChatMessage.Type.NONE,
 111            recipient = string.Empty,
 112            body = action,
 113        });
 1114    }
 115
 6116    void StartPopupRoutine() { showPopupDelayedRoutine = CoroutineStarter.Start(ShowPopupDelayed(POPUP_DELAY)); }
 117
 118    void StopPopupRoutine()
 119    {
 0120        if (showPopupDelayedRoutine != null)
 121        {
 0122            CoroutineStarter.Stop(showPopupDelayedRoutine);
 0123            showPopupDelayedRoutine = null;
 124        }
 0125        isPopupRoutineRunning = false;
 0126    }
 127
 128    private IEnumerator ShowPopupDelayed(float seconds)
 129    {
 3130        isPopupRoutineRunning = true;
 3131        view.Initialize(OnConfirmPressed, OnClosePressed, config);
 132
 6133        yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get());
 6134        yield return new WaitUntil(() => !CommonScriptableObjects.tutorialActive.Get());
 6135        yield return new WaitUntil(() => !CommonScriptableObjects.emailPromptActive.Get());
 3136        yield return WaitForSecondsCache.Get(seconds);
 0137        yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get());
 138
 0139        SetVisibility(true);
 0140        isPopupRoutineRunning = false;
 0141    }
 142
 143    void ResetPopupDelayed()
 144    {
 0145        if (isPopupRoutineRunning)
 146        {
 0147            StopPopupRoutine();
 0148            StartPopupRoutine();
 149        }
 0150    }
 151
 152    private void TutorialActive_OnChange(bool current, bool previous)
 153    {
 0154        if (current)
 0155            ResetPopupDelayed();
 0156    }
 157
 158    private void EmailPromptActive_OnChange(bool current, bool previous)
 159    {
 0160        if (current)
 0161            ResetPopupDelayed();
 0162    }
 163}