| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Providers; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | | using Object = UnityEngine.Object; |
| | 9 | |
|
| | 10 | | namespace MainScripts.DCL.Controllers.HUD.Preloading |
| | 11 | | { |
| | 12 | | public class PreloadingController : IDisposable |
| | 13 | | { |
| | 14 | | private readonly DataStoreRef<DataStore_LoadingScreen> loadingScreenRef; |
| | 15 | | private readonly CancellationTokenSource cancellationTokenSource; |
| | 16 | |
|
| | 17 | | private GameObject view; |
| | 18 | | private bool isDisposed; |
| | 19 | |
|
| 0 | 20 | | private BaseVariable<bool> isSignUpFlow => DataStore.i.common.isSignUpFlow; |
| | 21 | |
|
| 0 | 22 | | public PreloadingController(IAddressableResourceProvider addressableResourceProvider) |
| | 23 | | { |
| 0 | 24 | | cancellationTokenSource = new CancellationTokenSource(); |
| 0 | 25 | | GetView(cancellationTokenSource.Token).Forget(); |
| | 26 | |
|
| 0 | 27 | | loadingScreenRef.Ref.decoupledLoadingHUD.visible.OnChange += OnDecoupledLoadingScreenVisibilityChange; |
| 0 | 28 | | isSignUpFlow.OnChange += SignUpFlowChanged; |
| | 29 | |
|
| | 30 | | async UniTask GetView(CancellationToken cancellationToken) => |
| 0 | 31 | | view = (await addressableResourceProvider.Instantiate<Transform>("Preloading", cancellationToken: cancel |
| | 32 | | .gameObject; |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | public void Dispose() |
| | 36 | | { |
| 0 | 37 | | if (isDisposed) return; |
| 0 | 38 | | isDisposed = true; |
| | 39 | |
|
| 0 | 40 | | cancellationTokenSource.Cancel(); |
| 0 | 41 | | cancellationTokenSource.Dispose(); |
| | 42 | |
|
| 0 | 43 | | WaitForViewsToFadeOut().Forget(); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | private void OnDecoupledLoadingScreenVisibilityChange(bool current, bool _) |
| | 47 | | { |
| 0 | 48 | | if (current) |
| 0 | 49 | | Dispose(); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | private async UniTask WaitForViewsToFadeOut() |
| | 53 | | { |
| | 54 | | //This wait will be removed when we merge both loading screen into a single decoupled loading screen |
| 0 | 55 | | await UniTask.Delay(TimeSpan.FromSeconds(2), ignoreTimeScale: false); |
| | 56 | |
|
| 0 | 57 | | loadingScreenRef.Ref.decoupledLoadingHUD.visible.OnChange -= OnDecoupledLoadingScreenVisibilityChange; |
| 0 | 58 | | isSignUpFlow.OnChange -= SignUpFlowChanged; |
| | 59 | |
|
| 0 | 60 | | if(view != null) |
| 0 | 61 | | Object.Destroy(view.gameObject); |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | private void SignUpFlowChanged(bool current, bool previous) |
| | 65 | | { |
| 0 | 66 | | if (current) |
| 0 | 67 | | Dispose(); |
| 0 | 68 | | } |
| | 69 | | } |
| | 70 | | } |