| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Threading; |
| | 5 | | using UnityEngine; |
| | 6 | | using Random = UnityEngine.Random; |
| | 7 | |
|
| | 8 | | namespace 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 | |
|
| 0 | 92 | | public LoadingTip(string text, string spriteURL) |
| | 93 | | { |
| 0 | 94 | | this.text = text; |
| 0 | 95 | | sprite = Resources.Load<Sprite>(spriteURL); |
| 0 | 96 | | } |
| | 97 | |
|
| 0 | 98 | | public LoadingTip(string text, Sprite sprite) |
| | 99 | | { |
| 0 | 100 | | this.text = text; |
| 0 | 101 | | this.sprite = sprite; |
| 0 | 102 | | } |
| | 103 | | } |
| | 104 | | } |