< Summary

Class:HighlightScenesController
Assembly:ExploreHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ExploreHUD/Scripts/HighlightScenes/HighlightScenesController.cs
Covered lines:49
Uncovered lines:22
Coverable lines:71
Total lines:163
Line coverage:69% (49 of 71)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
HighlightScenesController()0%110100%
Initialize(...)0%110100%
RefreshIfNeeded()0%5.155081.82%
FetchHotScenes()0%110100%
OnFetchHotScenes()0%110100%
ProcessHotScenes()0%3.043083.33%
ProcessReceivedHotScene(...)0%5.145082.35%
AddActiveHotSceneCell(...)0%110100%
IsHotSceneCellActive(...)0%110100%
RemoveActiveHotSceneCell(...)0%6200%
OnDisable()0%2.52050%
OnDestroy()0%220100%
FirstTimeLoadingRoutine()0%30500%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ExploreHUD/Scripts/HighlightScenes/HighlightScenesController.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using UnityEngine;
 5using UnityEngine.UI;
 6using DCL.Interface;
 7
 8internal class HighlightScenesController : MonoBehaviour
 9{
 10    const float SCENES_UPDATE_INTERVAL = 60;
 11    private const float SCENE_FIRST_LOAD_DELAY = 0.1f;
 12
 13    [SerializeField] HotSceneCellView hotsceneBaseCellView;
 14    [SerializeField] ScrollRect scrollRect;
 15
 816    Dictionary<Vector2Int, HotSceneCellView> cachedHotScenes = new Dictionary<Vector2Int, HotSceneCellView>();
 817    Dictionary<Vector2Int, HotSceneCellView> activeHotSceneViews = new Dictionary<Vector2Int, HotSceneCellView>();
 18
 19    FriendTrackerController friendsController;
 20
 21    ViewPool<HotSceneCellView> hotScenesViewPool;
 22
 23    float lastTimeRefreshed = 0;
 24    private Coroutine firstLoadAnimRoutine = null;
 25
 26    public void Initialize(FriendTrackerController friendsController)
 27    {
 428        this.friendsController = friendsController;
 429        hotScenesViewPool = new ViewPool<HotSceneCellView>(hotsceneBaseCellView, 9);
 430    }
 31
 32    public void RefreshIfNeeded()
 33    {
 334        bool isFirstTimeLoad = cachedHotScenes.Count == 0;
 335        if (isFirstTimeLoad && !ExploreHUDController.isTest)
 36        {
 037            firstLoadAnimRoutine = StartCoroutine(FirstTimeLoadingRoutine());
 038        }
 339        else if (HotScenesController.i.timeSinceLastUpdate >= SCENES_UPDATE_INTERVAL)
 40        {
 241            FetchHotScenes();
 242        }
 143        else if ((Time.realtimeSinceStartup - lastTimeRefreshed) >= SCENES_UPDATE_INTERVAL)
 44        {
 145            ProcessHotScenes();
 46        }
 47
 348        scrollRect.verticalNormalizedPosition = 1;
 349    }
 50
 51    void FetchHotScenes()
 52    {
 253        WebInterface.FetchHotScenes();
 54
 255        HotScenesController.i.OnHotSceneListFinishUpdating -= OnFetchHotScenes;
 256        HotScenesController.i.OnHotSceneListFinishUpdating += OnFetchHotScenes;
 257    }
 58
 59    void OnFetchHotScenes()
 60    {
 161        HotScenesController.i.OnHotSceneListFinishUpdating -= OnFetchHotScenes;
 162        ProcessHotScenes();
 163    }
 64
 65    void ProcessHotScenes()
 66    {
 267        lastTimeRefreshed = Time.realtimeSinceStartup;
 68
 269        List<Vector2Int> cellsToHide = new List<Vector2Int>(activeHotSceneViews.Keys);
 70
 1671        for (int i = 0; i < HotScenesController.i.hotScenesList.Count; i++)
 72        {
 673            cellsToHide.Remove(HotScenesController.i.hotScenesList[i].baseCoords);
 674            ProcessReceivedHotScene(HotScenesController.i.hotScenesList[i], i);
 75        }
 76
 477        for (int i = 0; i < cellsToHide.Count; i++)
 78        {
 079            RemoveActiveHotSceneCell(cellsToHide[i]);
 80        }
 281    }
 82
 83    void ProcessReceivedHotScene(HotScenesController.HotSceneInfo hotSceneInfo, int priority)
 84    {
 685        Vector2Int baseCoords = hotSceneInfo.baseCoords;
 686        HotSceneCellView hotSceneView = null;
 87
 688        if (cachedHotScenes.ContainsKey(baseCoords))
 89        {
 090            hotSceneView = cachedHotScenes[baseCoords];
 091            if (!hotSceneView)
 092                return;
 93        }
 94        else
 95        {
 696            hotSceneView = hotScenesViewPool.GetView();
 697            hotSceneView.Initialize();
 698            cachedHotScenes.Add(baseCoords, hotSceneView);
 99        }
 100
 6101        hotSceneView.transform.SetSiblingIndex(priority);
 102
 6103        if (!hotSceneView.gameObject.activeSelf)
 104        {
 6105            hotSceneView.gameObject.SetActive(true);
 106        }
 107
 6108        if (!IsHotSceneCellActive(baseCoords))
 109        {
 6110            AddActiveHotSceneCell(baseCoords, hotSceneView);
 111        }
 112
 6113        hotSceneView.Setup(hotSceneInfo);
 6114        friendsController.AddHandler(hotSceneView.friendsHandler);
 6115    }
 116
 12117    void AddActiveHotSceneCell(Vector2Int coords, HotSceneCellView view) { activeHotSceneViews.Add(coords, view); }
 118
 6119    bool IsHotSceneCellActive(Vector2Int coords) { return activeHotSceneViews.ContainsKey(coords); }
 120
 121    void RemoveActiveHotSceneCell(Vector2Int coords)
 122    {
 0123        if (activeHotSceneViews.TryGetValue(coords, out HotSceneCellView view))
 124        {
 0125            view.gameObject.SetActive(false);
 0126            view.Clear();
 0127            friendsController.RemoveHandler(view.friendsHandler);
 128        }
 129
 0130        activeHotSceneViews.Remove(coords);
 0131    }
 132
 133    private void OnDisable()
 134    {
 4135        if (!(firstLoadAnimRoutine is null))
 136        {
 0137            StopCoroutine(firstLoadAnimRoutine);
 0138            firstLoadAnimRoutine = null;
 139        }
 4140    }
 141
 142    void OnDestroy()
 143    {
 4144        HotScenesController.i.OnHotSceneListFinishUpdating -= OnFetchHotScenes;
 4145        hotScenesViewPool?.Dispose();
 4146    }
 147
 148    IEnumerator FirstTimeLoadingRoutine()
 149    {
 0150        using (var iterator = hotScenesViewPool.GetEnumerator())
 151        {
 0152            while (iterator.MoveNext())
 153            {
 0154                if (iterator.Current is null)
 155                    continue;
 156
 0157                iterator.Current.gameObject.SetActive(true);
 0158                yield return new WaitForSeconds(SCENE_FIRST_LOAD_DELAY);
 159            }
 0160        }
 0161        FetchHotScenes();
 0162    }
 163}