< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LoadingScreenTipsController(...)0%2100%
LoadCustomTips(...)0%6200%
GetNextLoadingTip()0%6200%
IterateTipsAsync()0%12300%
StopTips()0%6200%
StartTips()0%6200%

File(s)

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

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using System;
 3using System.Collections.Generic;
 4using System.Threading;
 5using UnityEngine;
 6using Random = UnityEngine.Random;
 7
 8namespace DCL.LoadingScreen
 9{
 10    /// <summary>
 11    /// Loading screen tips provider. The responsibility of this class is to create the loading tips and provide them as
 12    /// </summary>
 13    public class LoadingScreenTipsController
 14    {
 015        private readonly TimeSpan SHOWING_TIME_TIPS = TimeSpan.FromSeconds(5);
 16
 17        private List<LoadingTip> currentLoadingTips;
 18        private int currentRandomIndex;
 19
 20        private readonly LoadingScreenTipsView tipsView;
 21        private CancellationTokenSource disposeCts;
 22
 023        public LoadingScreenTipsController(LoadingScreenTipsView tipsView)
 24        {
 025            this.tipsView = tipsView;
 26
 027            tipsView.gameObject.SetActive(false);
 28
 029            currentLoadingTips = tipsView.defaultTips;
 030            currentRandomIndex = Random.Range(0, currentLoadingTips.Count);
 031        }
 32
 33        //TODO: We will use this method when the WORLDs loads downloaded images and tips. This will come with the
 34        // AboutResponse var on the RealmPlugin change
 35        public void LoadCustomTips(List<Tuple<string, Sprite>> customList)
 36        {
 037            currentLoadingTips = new List<LoadingTip>();
 38
 039            foreach (Tuple<string, Sprite> tipsTuple in customList)
 040                currentLoadingTips.Add(new LoadingTip(tipsTuple.Item1, tipsTuple.Item2));
 41
 042            currentRandomIndex = Random.Range(0, currentLoadingTips.Count);
 043        }
 44
 45        public LoadingTip GetNextLoadingTip()
 46        {
 47            //We select a random value that is not the current one
 048            int randomIndexCandidate = Random.Range(0, currentLoadingTips.Count);
 49
 050            if (randomIndexCandidate.Equals(currentRandomIndex))
 051                randomIndexCandidate = (currentRandomIndex + 1) % currentLoadingTips.Count;
 52
 053            currentRandomIndex = randomIndexCandidate;
 054            return currentLoadingTips[currentRandomIndex];
 55        }
 56
 57        private async UniTask IterateTipsAsync()
 58        {
 059            while (true)
 60            {
 061                tipsView.ShowTip(GetNextLoadingTip());
 062                await UniTask.Delay(SHOWING_TIME_TIPS, cancellationToken: disposeCts.Token);
 63            }
 64        }
 65
 66        public void StopTips()
 67        {
 68            //This means that tips have been already stopped once. We only show them once, so we return and ignore this 
 069            if (disposeCts ==  null) return;
 70
 071            tipsView.gameObject.SetActive(false);
 072            disposeCts.Cancel();
 073            disposeCts.Dispose();
 074            disposeCts = null;
 075        }
 76
 77        public void StartTips()
 78        {
 079            if (disposeCts != null) return;
 80
 081            disposeCts = new CancellationTokenSource();
 082            IterateTipsAsync();
 083        }
 84    }
 85
 86    [Serializable]
 87    public class LoadingTip
 88    {
 89        public string text;
 90        public Sprite sprite;
 91
 92        public LoadingTip(string text, string spriteURL)
 93        {
 94            this.text = text;
 95            sprite = Resources.Load<Sprite>(spriteURL);
 96        }
 97
 98        public LoadingTip(string text, Sprite sprite)
 99        {
 100            this.text = text;
 101            this.sprite = sprite;
 102        }
 103    }
 104}