| | 1 | | using System; |
| | 2 | | using System.Threading; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using UnityEngine; |
| | 5 | | using Object = UnityEngine.Object; |
| | 6 | |
|
| | 7 | | namespace MainScripts.DCL.Controllers.LoadingFlow |
| | 8 | | { |
| | 9 | | public class LoadingFlowController : IDisposable |
| | 10 | | { |
| | 11 | | private const float GENERAL_TIMEOUT_IN_SECONDS = 100; |
| | 12 | | private const int WEB_SOCKET_TIMEOUT = 15; |
| | 13 | |
|
| | 14 | | private readonly ILoadingFlowView view; |
| | 15 | | private readonly BaseVariable<bool> loadingHudVisible; |
| | 16 | | private readonly RendererState rendererState; |
| | 17 | | private readonly BaseVariable<bool> websocketCommunicationEstablished; |
| | 18 | | private float timerStart; |
| | 19 | |
|
| | 20 | | private CancellationTokenSource disposeToken; |
| | 21 | |
|
| 0 | 22 | | public LoadingFlowController( |
| | 23 | | BaseVariable<bool> loadingHudVisible, |
| | 24 | | RendererState rendererState, |
| | 25 | | BaseVariable<bool> websocketCommunicationEstablished) |
| | 26 | | { |
| 0 | 27 | | this.loadingHudVisible = loadingHudVisible; |
| 0 | 28 | | this.rendererState = rendererState; |
| 0 | 29 | | this.websocketCommunicationEstablished = websocketCommunicationEstablished; |
| | 30 | |
|
| 0 | 31 | | view = CreateView(); |
| 0 | 32 | | view.Hide(); |
| | 33 | |
|
| 0 | 34 | | this.loadingHudVisible.OnChange += OnLoadingHudVisibleChanged; |
| 0 | 35 | | this.rendererState.OnChange += OnRendererStateChange; |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | private ILoadingFlowView CreateView() |
| | 39 | | { |
| 0 | 40 | | return Object.Instantiate(Resources.Load<LoadingFlowView>("LoadingFlow")); |
| | 41 | | } |
| | 42 | |
|
| | 43 | | private void OnLoadingHudVisibleChanged(bool current, bool previous) |
| | 44 | | { |
| 0 | 45 | | if (current) |
| 0 | 46 | | StartWatching(); |
| | 47 | | else |
| | 48 | | { |
| 0 | 49 | | view.Hide(); |
| 0 | 50 | | StopWatching(); |
| | 51 | | } |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | private void StartWatching() |
| | 55 | | { |
| 0 | 56 | | timerStart = Time.unscaledTime; |
| 0 | 57 | | disposeToken = new CancellationTokenSource(); |
| 0 | 58 | | ListenForTimeout(); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void StopWatching() => |
| 0 | 62 | | DisposeCancellationToken(); |
| | 63 | |
|
| | 64 | | private bool IsTimeToShowTimeout() |
| | 65 | | { |
| 0 | 66 | | var elapsedLoadingTime = Time.unscaledTime - timerStart; |
| 0 | 67 | | return elapsedLoadingTime > GENERAL_TIMEOUT_IN_SECONDS |
| | 68 | | || !websocketCommunicationEstablished.Get() && elapsedLoadingTime > WEB_SOCKET_TIMEOUT; |
| | 69 | | } |
| | 70 | |
|
| | 71 | | private void OnRendererStateChange(bool current, bool previous) |
| | 72 | | { |
| 0 | 73 | | if (current) |
| | 74 | | { |
| 0 | 75 | | view.Hide(); |
| 0 | 76 | | StopWatching(); |
| | 77 | | } |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | public void Dispose() |
| | 81 | | { |
| 0 | 82 | | loadingHudVisible.OnChange -= OnLoadingHudVisibleChanged; |
| 0 | 83 | | rendererState.OnChange -= OnRendererStateChange; |
| 0 | 84 | | DisposeCancellationToken(); |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | private async UniTask ListenForTimeout() |
| | 88 | | { |
| 0 | 89 | | while (true) |
| | 90 | | { |
| 0 | 91 | | if (IsTimeToShowTimeout()) |
| 0 | 92 | | ShowTimeout(); |
| 0 | 93 | | await UniTask.Yield(cancellationToken: disposeToken.Token); |
| | 94 | | } |
| | 95 | | } |
| | 96 | |
|
| | 97 | | private void ShowTimeout() |
| | 98 | | { |
| 0 | 99 | | view.Show(); |
| 0 | 100 | | StopWatching(); |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | private void DisposeCancellationToken() |
| | 104 | | { |
| 0 | 105 | | if (disposeToken == null) return; |
| | 106 | |
|
| 0 | 107 | | disposeToken.Cancel(); |
| 0 | 108 | | disposeToken.Dispose(); |
| 0 | 109 | | disposeToken = null; |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | } |
| | 113 | | } |