< Summary

Class:DCL.FetchScenesHandler
Assembly:GlobalUsersPositionMarker
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/GlobalUsersPositionMarker/Handlers/FetchScenesHandler.cs
Covered lines:15
Uncovered lines:27
Coverable lines:42
Total lines:130
Line coverage:35.7% (15 of 42)
Covered branches:0
Total branches:0
Covered methods:2
Total methods:7
Method coverage:28.5% (2 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
FetchScenesHandler(...)0%110100%
Init()0%6200%
SetUpdateMode(...)0%440100%
Dispose()0%6200%
UpdateCoroutine()0%42600%
OnHotSceneListFinishUpdating()0%2100%
OnHotScenesFetched(...)0%30500%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/GlobalUsersPositionMarker/Handlers/FetchScenesHandler.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using DCL.Interface;
 5using MainScripts.DCL.Controllers.HotScenes;
 6using System.Collections;
 7using UpdateMode = DCL.MapGlobalUsersPositionMarkerController.UpdateMode;
 8
 9namespace DCL
 10{
 11    /// <summary>
 12    /// Handle the fetch of hot scenes at intervals.
 13    /// Interval time may change accordingly if set to update in background or foregraound
 14    /// </summary>
 15    [Obsolete ("Use IHotScenesFetcher instead")]
 16    internal class FetchScenesHandler : IDisposable
 17    {
 18        public event Action<List<IHotScenesController.HotSceneInfo>> OnScenesFetched;
 19
 20        float initialIntevalTime;
 21        float backgroundIntervalTime;
 22        float foregroundIntervalTime;
 23
 24        Coroutine updateCoroutine;
 25        UpdateMode updateMode;
 26
 27        internal bool isFirstFetch;
 28        internal float updateInterval;
 29
 30        /// <summary>
 31        /// Constructor
 32        /// </summary>
 33        /// <param name="initialIntevalTime">seconds for interval until the first populated scenes are fetched</param>
 34        /// <param name="foregroundIntervalTime">seconds for interval when update mode is in FOREGROUND</param>
 35        /// <param name="backgroundIntervalTime">seconds for interval when update mode is in BACKGROUND</param>
 136        public FetchScenesHandler(float initialIntevalTime, float foregroundIntervalTime, float backgroundIntervalTime)
 37        {
 138            this.initialIntevalTime = initialIntevalTime;
 139            this.backgroundIntervalTime = backgroundIntervalTime;
 140            this.foregroundIntervalTime = foregroundIntervalTime;
 141            this.updateInterval = initialIntevalTime;
 142            this.isFirstFetch = true;
 143        }
 44
 45        /// <summary>
 46        /// Initialize fetch intervals
 47        /// </summary>
 48        public void Init()
 49        {
 050            if (updateCoroutine != null)
 051                return;
 52
 053            updateCoroutine = CoroutineStarter.Start(UpdateCoroutine());
 054        }
 55
 56        /// <summary>
 57        /// Set update mode. Scene's fetch intervals will smaller when updating in FOREGROUND than when updating in BACK
 58        /// </summary>
 59        /// <param name="mode">update mode</param>
 60        public void SetUpdateMode(UpdateMode mode)
 61        {
 362            updateMode = mode;
 363            if (isFirstFetch)
 164                return;
 65
 266            switch (updateMode)
 67            {
 68                case UpdateMode.BACKGROUND:
 169                    updateInterval = backgroundIntervalTime;
 170                    break;
 71                case UpdateMode.FOREGROUND:
 172                    updateInterval = foregroundIntervalTime;
 73                    break;
 74            }
 175        }
 76
 77        public void Dispose()
 78        {
 079            CoroutineStarter.Stop(updateCoroutine);
 80
 081            if (HotScenesController.i)
 082                HotScenesController.i.OnHotSceneListFinishUpdating -= OnHotSceneListFinishUpdating;
 083        }
 84
 85        private IEnumerator UpdateCoroutine()
 86        {
 087            while (true)
 88            {
 089                float time = Time.realtimeSinceStartup;
 90
 091                while (Time.realtimeSinceStartup - time < updateInterval)
 92                {
 093                    yield return null;
 94                }
 95
 096                if(HotScenesController.i == null) yield break;
 97
 098                if (HotScenesController.i.timeSinceLastUpdate > updateInterval)
 99                {
 0100                    HotScenesController.i.OnHotSceneListFinishUpdating += OnHotSceneListFinishUpdating;
 0101                    WebInterface.FetchHotScenes();
 102                }
 103                else
 104                {
 0105                    OnHotSceneListFinishUpdating();
 106                }
 107            }
 108        }
 109
 0110        private void OnHotSceneListFinishUpdating() { OnHotScenesFetched(HotScenesController.i.hotScenesList); }
 111
 112        private void OnHotScenesFetched(List<IHotScenesController.HotSceneInfo> scenes)
 113        {
 0114            HotScenesController.i.OnHotSceneListFinishUpdating -= OnHotSceneListFinishUpdating;
 115
 0116            bool fetchSuccess = scenes.Count > 0 && scenes[0].usersTotalCount > 0;
 117
 0118            if (!fetchSuccess)
 0119                return;
 120
 0121            if (isFirstFetch)
 122            {
 0123                isFirstFetch = false;
 0124                SetUpdateMode(updateMode);
 125            }
 126
 0127            OnScenesFetched?.Invoke(scenes);
 0128        }
 129    }
 130}