< Summary

Class:DCL.LoadingScreen.LoadingTip
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:8
Coverable lines:8
Total lines:104
Line coverage:0% (0 of 8)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LoadingTip(...)0%2100%
LoadingTip(...)0%2100%

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    {
 15        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
 23        public LoadingScreenTipsController(LoadingScreenTipsView tipsView)
 24        {
 25            this.tipsView = tipsView;
 26
 27            tipsView.gameObject.SetActive(false);
 28
 29            currentLoadingTips = tipsView.defaultTips;
 30            currentRandomIndex = Random.Range(0, currentLoadingTips.Count);
 31        }
 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        {
 37            currentLoadingTips = new List<LoadingTip>();
 38
 39            foreach (Tuple<string, Sprite> tipsTuple in customList)
 40                currentLoadingTips.Add(new LoadingTip(tipsTuple.Item1, tipsTuple.Item2));
 41
 42            currentRandomIndex = Random.Range(0, currentLoadingTips.Count);
 43        }
 44
 45        public LoadingTip GetNextLoadingTip()
 46        {
 47            //We select a random value that is not the current one
 48            int randomIndexCandidate = Random.Range(0, currentLoadingTips.Count);
 49
 50            if (randomIndexCandidate.Equals(currentRandomIndex))
 51                randomIndexCandidate = (currentRandomIndex + 1) % currentLoadingTips.Count;
 52
 53            currentRandomIndex = randomIndexCandidate;
 54            return currentLoadingTips[currentRandomIndex];
 55        }
 56
 57        private async UniTask IterateTipsAsync()
 58        {
 59            while (true)
 60            {
 61                tipsView.ShowTip(GetNextLoadingTip());
 62                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 
 69            if (disposeCts ==  null) return;
 70
 71            tipsView.gameObject.SetActive(false);
 72            disposeCts.Cancel();
 73            disposeCts.Dispose();
 74            disposeCts = null;
 75        }
 76
 77        public void StartTips()
 78        {
 79            if (disposeCts != null) return;
 80
 81            disposeCts = new CancellationTokenSource();
 82            IterateTipsAsync();
 83        }
 84    }
 85
 86    [Serializable]
 87    public class LoadingTip
 88    {
 89        public string text;
 90        public Sprite sprite;
 91
 092        public LoadingTip(string text, string spriteURL)
 93        {
 094            this.text = text;
 095            sprite = Resources.Load<Sprite>(spriteURL);
 096        }
 97
 098        public LoadingTip(string text, Sprite sprite)
 99        {
 0100            this.text = text;
 0101            this.sprite = sprite;
 0102        }
 103    }
 104}