< Summary

Class:DCL.LoadingScreen.LoadingScreenController
Assembly:DCL.LoadingScreen
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/LoadingScreen/Scripts/LoadingScreenController.cs
Covered lines:0
Uncovered lines:60
Coverable lines:60
Total lines:167
Line coverage:0% (0 of 60)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LoadingScreenController(...)0%2100%
Dispose()0%2100%
FadeInFinished(...)0%2100%
ReadyScene(...)0%12300%
PlayerLoaded(...)0%6200%
OnSignupFlow(...)0%6200%
TeleportRequested(...)0%12300%
IsNewRealm()0%12300%
IsSceneLoaded(...)0%2100%
CheckSceneTimeout(...)0%30500%
FadeOutView()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/LoadingScreen/Scripts/LoadingScreenController.cs

#LineLine coverage
 1using DCL.Helpers;
 2using System;
 3using UnityEngine;
 4using DCL.NotificationModel;
 5using Type = DCL.NotificationModel.Type;
 6
 7namespace DCL.LoadingScreen
 8{
 9    /// <summary>
 10    /// Controls the state of the loading screen. It's responsibility is to update the view depending on the SceneContro
 11    /// Creates and provides the controllers associated to the LoadingScreen: TipsController and PercentageController
 12    /// </summary>
 13    public class LoadingScreenController : IDisposable
 14    {
 15        private readonly ILoadingScreenView view;
 16        private readonly ISceneController sceneController;
 17        private readonly DataStore_Player playerDataStore;
 18        private readonly DataStore_Common commonDataStore;
 19        private readonly DataStore_LoadingScreen loadingScreenDataStore;
 20        private readonly DataStore_Realm realmDataStore;
 21        private readonly IWorldState worldState;
 22        private readonly NotificationsController notificationsController;
 23
 24        private Vector2Int currentDestination;
 25        private string currentRealm;
 26        private bool currentRealmIsWorld;
 27        private readonly LoadingScreenTipsController tipsController;
 28        private readonly LoadingScreenPercentageController percentageController;
 29
 030        public LoadingScreenController(ILoadingScreenView view, ISceneController sceneController, IWorldState worldState
 31            DataStore_Player playerDataStore, DataStore_Common commonDataStore, DataStore_LoadingScreen loadingScreenDat
 32        {
 033            this.view = view;
 034            this.sceneController = sceneController;
 035            this.playerDataStore = playerDataStore;
 036            this.commonDataStore = commonDataStore;
 037            this.worldState = worldState;
 038            this.loadingScreenDataStore = loadingScreenDataStore;
 039            this.realmDataStore = realmDataStore;
 040            this.notificationsController = notificationsController;
 41
 042            tipsController = new LoadingScreenTipsController(view.GetTipsView());
 043            percentageController = new LoadingScreenPercentageController(sceneController, view.GetPercentageView(), comm
 44
 045            this.playerDataStore.lastTeleportPosition.OnChange += TeleportRequested;
 046            this.commonDataStore.isSignUpFlow.OnChange += OnSignupFlow;
 047            this.sceneController.OnReadyScene += ReadyScene;
 048            view.OnFadeInFinish += FadeInFinished;
 049        }
 50
 51        public void Dispose()
 52        {
 053            view.Dispose();
 054            percentageController.Dispose();
 055            playerDataStore.lastTeleportPosition.OnChange -= TeleportRequested;
 056            commonDataStore.isSignUpFlow.OnChange -= OnSignupFlow;
 057            sceneController.OnReadyScene -= ReadyScene;
 058            view.OnFadeInFinish -= FadeInFinished;
 059        }
 60
 61        private void FadeInFinished(ShowHideAnimator obj)
 62        {
 063            loadingScreenDataStore.decoupledLoadingHUD.visible.Set(true);
 064        }
 65
 66        private void ReadyScene(int obj)
 67        {
 68            //We have to check that the latest scene loaded is the one from our current destination
 069            if (worldState.GetSceneNumberByCoords(currentDestination).Equals(obj))
 70            {
 71                //We have to check if the player is loaded
 072                if(commonDataStore.isPlayerRendererLoaded.Get())
 073                    FadeOutView();
 74                else
 75                {
 076                    percentageController.SetAvatarLoadingMessage();
 077                    commonDataStore.isPlayerRendererLoaded.OnChange += PlayerLoaded;
 78                }
 79            }
 080        }
 81
 82        //We have to add one more check not to show the loadingScreen unless the player is loaded
 83        private void PlayerLoaded(bool loaded, bool _)
 84        {
 085            if(loaded)
 086                FadeOutView();
 87
 088            commonDataStore.isPlayerRendererLoaded.OnChange -= PlayerLoaded;
 089        }
 90
 91        private void OnSignupFlow(bool current, bool previous)
 92        {
 093            if (current)
 094                FadeOutView();
 95            else
 096                view.FadeIn(false, false);
 097        }
 98
 99        private void TeleportRequested(Vector3 current, Vector3 previous)
 100        {
 0101            Vector2Int currentDestinationCandidate = Utils.WorldToGridPosition(current);
 102
 0103            if (IsNewRealm() || IsSceneLoaded(currentDestinationCandidate))
 104            {
 0105                currentDestination = currentDestinationCandidate;
 106
 107                //On a teleport, to copy previos behaviour, we disable tips entirely and show the teleporting screen
 108                //This is probably going to change with the integration of WORLDS loading screen
 109                //Temporarily removing tips until V2
 110                //tipsController.StopTips();
 0111                percentageController.StartLoading(currentDestination);
 112
 0113                view.FadeIn(false, true);
 114            }
 115            else
 116            {
 117                //We are going to check if the scene has timeout using the POSITION_SETTLED event.
 0118                CheckSceneTimeout(currentDestinationCandidate);
 119            }
 0120        }
 121
 122        //The realm gets changed before the scenes starts to unload. So, if we try to teleport to a world scene in which
 123        //we wont see the loading screen. Same happens when leaving a world. Thats why we need to keep track of the late
 124        private bool IsNewRealm()
 125        {
 126            bool realmChangeRequiresLoadingScreen;
 127
 0128            if (commonDataStore.isWorld.Get())
 0129                realmChangeRequiresLoadingScreen = string.IsNullOrEmpty(currentRealm) || !currentRealm.Equals(realmDataS
 130            else
 0131                realmChangeRequiresLoadingScreen = currentRealmIsWorld;
 132
 0133            currentRealm = realmDataStore.playerRealmAboutConfiguration.Get().RealmName;
 0134            currentRealmIsWorld = commonDataStore.isWorld.Get();
 0135            return realmChangeRequiresLoadingScreen;
 136        }
 137
 138        //If the destination scene is not loaded, we show the teleport screen. THis is called in the POSITION_UNSETTLED
 139        //On the other hand, the POSITION_SETTLED event is called; but since the scene will already be loaded, the loadi
 140        private bool IsSceneLoaded(Vector2Int currentDestinationCandidate) =>
 0141             worldState.GetSceneNumberByCoords(currentDestinationCandidate).Equals(-1);
 142
 143        private void CheckSceneTimeout(Vector2Int currentDestinationCandidate)
 144        {
 145            //If we are settling on the destination position, but loading is not complete, this means that kernel is cal
 146            //For now, we hide the loading screen and add a notification
 0147            if (currentDestinationCandidate.Equals(currentDestination) &&
 148                worldState.GetScene(worldState.GetSceneNumberByCoords(currentDestination))?.loadingProgress < 100)
 149            {
 0150                notificationsController.ShowNotification(new Model
 151                {
 152                    message = "Loading scene timeout",
 153                    type = Type.GENERIC,
 154                    timer = 10f,
 155                    destroyOnFinish = true
 156                });
 0157                FadeOutView();
 158            }
 0159        }
 160
 161        private void FadeOutView()
 162        {
 0163            view.FadeOut();
 0164            loadingScreenDataStore.decoupledLoadingHUD.visible.Set(false);
 0165        }
 166    }
 167}