< Summary

Class:MainScripts.DCL.Controllers.LoadingFlow.LoadingFlowController
Assembly:LoadingFlowController
File(s):/tmp/workspace/explorer-desktop/unity-renderer-desktop/Assets/Scripts/MainScripts/DCL/Controllers/LoadingFlow/LoadingFlowController.cs
Covered lines:0
Uncovered lines:42
Coverable lines:42
Total lines:113
Line coverage:0% (0 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LoadingFlowController(...)0%2100%
CreateView()0%2100%
OnLoadingHudVisibleChanged(...)0%6200%
StartWatching()0%2100%
StopWatching()0%2100%
IsTimeToShowTimeout()0%12300%
OnRendererStateChange(...)0%6200%
Dispose()0%2100%
ListenForTimeout()0%20400%
ShowTimeout()0%2100%
DisposeCancellationToken()0%6200%

File(s)

/tmp/workspace/explorer-desktop/unity-renderer-desktop/Assets/Scripts/MainScripts/DCL/Controllers/LoadingFlow/LoadingFlowController.cs

#LineLine coverage
 1using System;
 2using System.Threading;
 3using Cysharp.Threading.Tasks;
 4using UnityEngine;
 5using Object = UnityEngine.Object;
 6
 7namespace MainScripts.DCL.Controllers.LoadingFlow
 8{
 9    public class LoadingFlowController : IDisposable
 10    {
 11        private const float GENERAL_TIMEOUT_IN_SECONDS = 100;
 12        private const int WEB_SOCKET_TIMEOUT = 15;
 13
 14        private readonly ILoadingFlowView view;
 15        private readonly BaseVariable<bool> loadingHudVisible;
 16        private readonly RendererState rendererState;
 17        private readonly BaseVariable<bool> websocketCommunicationEstablished;
 18        private float timerStart;
 19
 20        private CancellationTokenSource disposeToken;
 21
 022        public LoadingFlowController(
 23            BaseVariable<bool> loadingHudVisible,
 24            RendererState rendererState,
 25            BaseVariable<bool> websocketCommunicationEstablished)
 26        {
 027            this.loadingHudVisible = loadingHudVisible;
 028            this.rendererState = rendererState;
 029            this.websocketCommunicationEstablished = websocketCommunicationEstablished;
 30
 031            view = CreateView();
 032            view.Hide();
 33
 034            this.loadingHudVisible.OnChange += OnLoadingHudVisibleChanged;
 035            this.rendererState.OnChange += OnRendererStateChange;
 036        }
 37
 38        private ILoadingFlowView CreateView()
 39        {
 040            return Object.Instantiate(Resources.Load<LoadingFlowView>("LoadingFlow"));
 41        }
 42
 43        private void OnLoadingHudVisibleChanged(bool current, bool previous)
 44        {
 045            if (current)
 046                StartWatching();
 47            else
 48            {
 049                view.Hide();
 050                StopWatching();
 51            }
 052        }
 53
 54        private void StartWatching()
 55        {
 056            timerStart = Time.unscaledTime;
 057            disposeToken = new CancellationTokenSource();
 058            ListenForTimeout();
 059        }
 60
 61        private void StopWatching() =>
 062            DisposeCancellationToken();
 63
 64        private bool IsTimeToShowTimeout()
 65        {
 066            var elapsedLoadingTime = Time.unscaledTime - timerStart;
 067            return elapsedLoadingTime > GENERAL_TIMEOUT_IN_SECONDS
 68                   || !websocketCommunicationEstablished.Get() && elapsedLoadingTime > WEB_SOCKET_TIMEOUT;
 69        }
 70
 71        private void OnRendererStateChange(bool current, bool previous)
 72        {
 073            if (current)
 74            {
 075                view.Hide();
 076                StopWatching();
 77            }
 078        }
 79
 80        public void Dispose()
 81        {
 082            loadingHudVisible.OnChange -= OnLoadingHudVisibleChanged;
 083            rendererState.OnChange -= OnRendererStateChange;
 084            DisposeCancellationToken();
 085        }
 86
 87        private async UniTask ListenForTimeout()
 88        {
 089            while (true)
 90            {
 091                if (IsTimeToShowTimeout())
 092                    ShowTimeout();
 093                await UniTask.Yield(cancellationToken: disposeToken.Token);
 94            }
 95        }
 96
 97        private void ShowTimeout()
 98        {
 099            view.Show();
 0100            StopWatching();
 0101        }
 102
 103        private void DisposeCancellationToken()
 104        {
 0105            if (disposeToken == null) return;
 106
 0107            disposeToken.Cancel();
 0108            disposeToken.Dispose();
 0109            disposeToken = null;
 0110        }
 111
 112    }
 113}