< 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:24
Uncovered lines:18
Coverable lines:42
Total lines:126
Line coverage:57.1% (24 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%2.062075%
UpdateCoroutine()0%12.415033.33%
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>
 1134        public FetchScenesHandler(float initialIntevalTime, float foregroundIntervalTime, float backgroundIntervalTime)
 35        {
 1136            this.initialIntevalTime = initialIntevalTime;
 1137            this.backgroundIntervalTime = backgroundIntervalTime;
 1138            this.foregroundIntervalTime = foregroundIntervalTime;
 1139            this.updateInterval = initialIntevalTime;
 1140            this.isFirstFetch = true;
 1141        }
 42
 43        /// <summary>
 44        /// Initialize fetch intervals
 45        /// </summary>
 46        public void Init()
 47        {
 1048            if (updateCoroutine != null)
 049                return;
 50
 1051            updateCoroutine = CoroutineStarter.Start(UpdateCoroutine());
 1052        }
 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        {
 1360            updateMode = mode;
 1361            if (isFirstFetch)
 1162                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        {
 1277            CoroutineStarter.Stop(updateCoroutine);
 78
 1279            if (HotScenesController.i)
 080                HotScenesController.i.OnHotSceneListFinishUpdating -= OnHotSceneListFinishUpdating;
 1281        }
 82
 83        private IEnumerator UpdateCoroutine()
 84        {
 085            while (true)
 86            {
 1087                float time = Time.realtimeSinceStartup;
 88
 1289                while (Time.realtimeSinceStartup - time < updateInterval)
 90                {
 1291                    yield return null;
 92                }
 93
 094                if (HotScenesController.i.timeSinceLastUpdate > updateInterval)
 95                {
 096                    HotScenesController.i.OnHotSceneListFinishUpdating += OnHotSceneListFinishUpdating;
 097                    WebInterface.FetchHotScenes();
 098                }
 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}