| | 1 | | using DCL.Controllers; |
| | 2 | | using System; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL.LoadingScreen |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Loading screen percentage updater. The responsibility of this class is to update the loading bar only |
| | 9 | | /// listening to the destination scene |
| | 10 | | /// </summary> |
| | 11 | | public class LoadingScreenPercentageController : IDisposable |
| | 12 | | { |
| | 13 | | private readonly LoadingScreenPercentageView loadingScreenPercentageView; |
| | 14 | | private readonly ISceneController sceneController; |
| | 15 | | private readonly DataStore_Common commonDataStore; |
| | 16 | |
|
| | 17 | | private Vector2Int currentDestination; |
| | 18 | | private IParcelScene currentSceneBeingLoaded; |
| | 19 | |
|
| 0 | 20 | | public LoadingScreenPercentageController(ISceneController sceneController, LoadingScreenPercentageView loadingSc |
| | 21 | | { |
| 0 | 22 | | this.loadingScreenPercentageView = loadingScreenPercentageView; |
| 0 | 23 | | this.sceneController = sceneController; |
| 0 | 24 | | this.commonDataStore = commonDataStore; |
| | 25 | |
|
| 0 | 26 | | loadingScreenPercentageView.SetSceneLoadingMessage(); |
| 0 | 27 | | loadingScreenPercentageView.SetLoadingPercentage(0); |
| | 28 | |
|
| 0 | 29 | | sceneController.OnNewSceneAdded += SceneController_OnNewSceneAdded; |
| 0 | 30 | | commonDataStore.isSignUpFlow.OnChange += StartedSignUpFlow; |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | public void Dispose() |
| | 34 | | { |
| 0 | 35 | | sceneController.OnNewSceneAdded -= SceneController_OnNewSceneAdded; |
| 0 | 36 | | commonDataStore.isSignUpFlow.OnChange -= StartedSignUpFlow; |
| | 37 | |
|
| 0 | 38 | | if (currentSceneBeingLoaded != null) |
| 0 | 39 | | currentSceneBeingLoaded.OnLoadingStateUpdated -= StatusUpdate; |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | private void SceneController_OnNewSceneAdded(IParcelScene scene) |
| | 43 | | { |
| | 44 | | //We will only update the percentage of the current destination scene. It may be the only one we care about |
| 0 | 45 | | if (scene != null && |
| | 46 | | Environment.i.world.state.GetSceneNumberByCoords(currentDestination).Equals(scene.sceneData.sceneNumber) |
| | 47 | | { |
| 0 | 48 | | currentSceneBeingLoaded = scene; |
| 0 | 49 | | currentSceneBeingLoaded.OnLoadingStateUpdated += StatusUpdate; |
| | 50 | | } |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | private void StartedSignUpFlow(bool current, bool previous) |
| | 54 | | { |
| 0 | 55 | | if (current) |
| 0 | 56 | | StatusUpdate(100); |
| | 57 | | else |
| 0 | 58 | | StatusUpdate(0); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void StatusUpdate(float percentage) |
| | 62 | | { |
| 0 | 63 | | loadingScreenPercentageView.SetLoadingPercentage((int)percentage); |
| | 64 | |
|
| 0 | 65 | | if (currentSceneBeingLoaded != null && percentage >= 100) |
| | 66 | | { |
| 0 | 67 | | currentSceneBeingLoaded.OnLoadingStateUpdated -= StatusUpdate; |
| 0 | 68 | | currentSceneBeingLoaded = null; |
| | 69 | | } |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | public void StartLoading(Vector2Int newDestination) |
| | 73 | | { |
| 0 | 74 | | loadingScreenPercentageView.gameObject.SetActive(true); |
| 0 | 75 | | currentDestination = newDestination; |
| 0 | 76 | | loadingScreenPercentageView.SetSceneLoadingMessage(); |
| 0 | 77 | | loadingScreenPercentageView.SetLoadingPercentage(0); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | public void SetAvatarLoadingMessage() => |
| 0 | 81 | | loadingScreenPercentageView.SetPlayerLoadingMessage(); |
| | 82 | | } |
| | 83 | | } |