< 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:29
Uncovered lines:13
Coverable lines:42
Total lines:126
Line coverage:69% (29 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
FetchScenesHandler(...)0%110100%
Init()0%2.062075%
SetUpdateMode(...)0%440100%
Dispose()0%220100%
UpdateCoroutine()0%5.275077.78%
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 System.Collections;
 6using UpdateMode = DCL.MapGlobalUsersPositionMarkerController.UpdateMode;
 7
 8namespace DCL
 9{
 10    /// <summary>
 11    /// Handle the fetch of hot scenes at intervals.
 12    /// Interval time may change accordingly if set to update in background or foregraound
 13    /// </summary>
 14    internal class FetchScenesHandler : IDisposable
 15    {
 16        public event Action<List<HotScenesController.HotSceneInfo>> OnScenesFetched;
 17
 18        float initialIntevalTime;
 19        float backgroundIntervalTime;
 20        float foregroundIntervalTime;
 21
 22        Coroutine updateCoroutine;
 23        UpdateMode updateMode;
 24
 25        internal bool isFirstFetch;
 26        internal float updateInterval;
 27
 28        /// <summary>
 29        /// Constructor
 30        /// </summary>
 31        /// <param name="initialIntevalTime">seconds for interval until the first populated scenes are fetched</param>
 32        /// <param name="foregroundIntervalTime">seconds for interval when update mode is in FOREGROUND</param>
 33        /// <param name="backgroundIntervalTime">seconds for interval when update mode is in BACKGROUND</param>
 11234        public FetchScenesHandler(float initialIntevalTime, float foregroundIntervalTime, float backgroundIntervalTime)
 35        {
 11236            this.initialIntevalTime = initialIntevalTime;
 11237            this.backgroundIntervalTime = backgroundIntervalTime;
 11238            this.foregroundIntervalTime = foregroundIntervalTime;
 11239            this.updateInterval = initialIntevalTime;
 11240            this.isFirstFetch = true;
 11241        }
 42
 43        /// <summary>
 44        /// Initialize fetch intervals
 45        /// </summary>
 46        public void Init()
 47        {
 11148            if (updateCoroutine != null)
 049                return;
 50
 11151            updateCoroutine = CoroutineStarter.Start(UpdateCoroutine());
 11152        }
 53
 54        /// <summary>
 55        /// Set update mode. Scene's fetch intervals will smaller when updating in FOREGROUND than when updating in BACK
 56        /// </summary>
 57        /// <param name="mode">update mode</param>
 58        public void SetUpdateMode(UpdateMode mode)
 59        {
 11560            updateMode = mode;
 11561            if (isFirstFetch)
 11362                return;
 63
 264            switch (updateMode)
 65            {
 66                case UpdateMode.BACKGROUND:
 167                    updateInterval = backgroundIntervalTime;
 168                    break;
 69                case UpdateMode.FOREGROUND:
 170                    updateInterval = foregroundIntervalTime;
 71                    break;
 72            }
 173        }
 74
 75        public void Dispose()
 76        {
 73777            CoroutineStarter.Stop(updateCoroutine);
 78
 73779            if (HotScenesController.i)
 73280                HotScenesController.i.OnHotSceneListFinishUpdating -= OnHotSceneListFinishUpdating;
 73781        }
 82
 83        private IEnumerator UpdateCoroutine()
 84        {
 085            while (true)
 86            {
 11287                float time = Time.realtimeSinceStartup;
 88
 182689                while (Time.realtimeSinceStartup - time < updateInterval)
 90                {
 182591                    yield return null;
 92                }
 93
 194                if (HotScenesController.i.timeSinceLastUpdate > updateInterval)
 95                {
 196                    HotScenesController.i.OnHotSceneListFinishUpdating += OnHotSceneListFinishUpdating;
 197                    WebInterface.FetchHotScenes();
 198                }
 99                else
 100                {
 0101                    OnHotSceneListFinishUpdating();
 102                }
 103            }
 104        }
 105
 0106        private void OnHotSceneListFinishUpdating() { OnHotScenesFetched(HotScenesController.i.hotScenesList); }
 107
 108        private void OnHotScenesFetched(List<HotScenesController.HotSceneInfo> scenes)
 109        {
 0110            HotScenesController.i.OnHotSceneListFinishUpdating -= OnHotSceneListFinishUpdating;
 111
 0112            bool fetchSuccess = scenes.Count > 0 && scenes[0].usersTotalCount > 0;
 113
 0114            if (!fetchSuccess)
 0115                return;
 116
 0117            if (isFirstFetch)
 118            {
 0119                isFirstFetch = false;
 0120                SetUpdateMode(updateMode);
 121            }
 122
 0123            OnScenesFetched?.Invoke(scenes);
 0124        }
 125    }
 126}