| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL.Builder |
| | 7 | | { |
| | 8 | | public interface IPublishProjectController |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Start the publish flow for a project |
| | 12 | | /// </summary> |
| | 13 | | /// <param name="builderScene"></param> |
| | 14 | | void StartPublishFlow(IBuilderScene builderScene); |
| | 15 | | } |
| | 16 | |
|
| | 17 | | public class PublishProjectController : IPublishProjectController |
| | 18 | | { |
| | 19 | | private const string DETAIL_PREFAB_PATH = "PublishDetailView"; |
| | 20 | | private const string PROGRESS_PREFAB_PATH = "PublishProgressView"; |
| | 21 | | private const string SUCCESS_PREFAB_PATH = "PublishSuccessView"; |
| | 22 | |
|
| | 23 | | private const string GENERIC_DEPLOY_ERROR = "Error deploying the scene"; |
| | 24 | |
|
| | 25 | | public enum ProjectPublishState |
| | 26 | | { |
| | 27 | | IDLE = 0, |
| | 28 | | PUBLISH_CONFIRM = 1, |
| | 29 | | PUBLISH_IN_PROGRESS = 2, |
| | 30 | | PUBLISH_END = 3 |
| | 31 | | } |
| | 32 | |
|
| | 33 | | internal IPublishProjectDetailView detailView; |
| | 34 | | internal IPublishProjectProgressView progressView; |
| | 35 | | internal IPublishProjectSuccesView succesView; |
| | 36 | |
|
| | 37 | | internal ProjectPublishState projectPublishState = ProjectPublishState.IDLE; |
| | 38 | | internal BuilderScene sceneToPublish; |
| | 39 | |
|
| 0 | 40 | | public PublishProjectController() |
| | 41 | | { |
| 0 | 42 | | detailView = Object.Instantiate(Resources.Load<PublishProjectDetailView>(DETAIL_PREFAB_PATH)); |
| 0 | 43 | | progressView = Object.Instantiate(Resources.Load<PublishProjectProgressView>(PROGRESS_PREFAB_PATH)); |
| 0 | 44 | | succesView = Object.Instantiate(Resources.Load<PublishProjectSuccesView>(SUCCESS_PREFAB_PATH)); |
| 0 | 45 | | Initialize(); |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | public void Initialize() |
| | 49 | | { |
| 0 | 50 | | detailView.OnCancel += ViewClosed; |
| 0 | 51 | | detailView.OnPublishButtonPressed += PublishButtonPressedButtonPressed; |
| | 52 | |
|
| 0 | 53 | | progressView.OnViewClosed += ViewClosed; |
| 0 | 54 | | progressView.OnPublishConfirmButtonPressed += StartPublish; |
| | 55 | |
|
| 0 | 56 | | succesView.OnViewClose += ViewClosed; |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | public void Dipose() |
| | 60 | | { |
| 0 | 61 | | detailView.OnCancel -= ViewClosed; |
| 0 | 62 | | detailView.OnPublishButtonPressed -= PublishButtonPressedButtonPressed; |
| | 63 | |
|
| 0 | 64 | | progressView.OnViewClosed -= ViewClosed; |
| 0 | 65 | | progressView.OnPublishConfirmButtonPressed -= StartPublish; |
| | 66 | |
|
| 0 | 67 | | succesView.OnViewClose -= ViewClosed; |
| | 68 | |
|
| 0 | 69 | | detailView.Dispose(); |
| 0 | 70 | | progressView.Dispose(); |
| 0 | 71 | | succesView.Dispose(); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | public void StartPublishFlow(IBuilderScene builderScene) |
| | 75 | | { |
| 0 | 76 | | if (projectPublishState != ProjectPublishState.IDLE) |
| 0 | 77 | | return; |
| | 78 | |
|
| | 79 | | //Note: in a future PR we will use IBuilderScene directly |
| 0 | 80 | | sceneToPublish = (BuilderScene) builderScene; |
| 0 | 81 | | projectPublishState = ProjectPublishState.PUBLISH_CONFIRM; |
| | 82 | |
|
| 0 | 83 | | detailView.SetProjectToPublish(sceneToPublish); |
| 0 | 84 | | detailView.Show(); |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | private void ViewClosed() |
| | 88 | | { |
| 0 | 89 | | projectPublishState = ProjectPublishState.IDLE; |
| | 90 | |
|
| 0 | 91 | | detailView.Hide(); |
| 0 | 92 | | progressView.Hide(); |
| 0 | 93 | | succesView.Hide(); |
| 0 | 94 | | } |
| | 95 | |
|
| 0 | 96 | | private void PublishButtonPressedButtonPressed() { progressView.ShowConfirmPopUp(); } |
| | 97 | |
|
| | 98 | | private void StartPublish() |
| | 99 | | { |
| 0 | 100 | | projectPublishState = ProjectPublishState.PUBLISH_IN_PROGRESS; |
| | 101 | |
|
| 0 | 102 | | progressView.PublishStarted(); |
| | 103 | |
|
| 0 | 104 | | Promise<bool> deploymentPromise = DeployScene(sceneToPublish); |
| 0 | 105 | | deploymentPromise.Then(success => |
| | 106 | | { |
| 0 | 107 | | if (success) |
| 0 | 108 | | DeploySuccess(); |
| | 109 | | else |
| 0 | 110 | | DeployError(GENERIC_DEPLOY_ERROR); |
| 0 | 111 | | }); |
| 0 | 112 | | deploymentPromise.Catch(DeployError); |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | private void DeploySuccess() |
| | 116 | | { |
| 0 | 117 | | projectPublishState = ProjectPublishState.PUBLISH_END; |
| 0 | 118 | | progressView.Hide(); |
| 0 | 119 | | succesView.ProjectPublished(sceneToPublish); |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | private void DeployError(string error) |
| | 123 | | { |
| 0 | 124 | | projectPublishState = ProjectPublishState.PUBLISH_END; |
| 0 | 125 | | progressView.PublishError(error); |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | Promise<bool> DeployScene(BuilderScene scene) |
| | 129 | | { |
| 0 | 130 | | Promise<bool> scenePromise = new Promise<bool>(); |
| | 131 | |
|
| | 132 | | //TODO: We need to implement the deployment functionality, for now we just mocked a delay to test the functi |
| 0 | 133 | | CoroutineStarter.Start(MockedDelay(scenePromise)); |
| 0 | 134 | | return scenePromise; |
| | 135 | | } |
| | 136 | |
|
| | 137 | | // This will be deleted in the future, this just simulate a deploy after 3 seconds to test functionality |
| | 138 | | IEnumerator MockedDelay(Promise<bool> promise) |
| | 139 | | { |
| 0 | 140 | | yield return new WaitForSeconds(3f); |
| 0 | 141 | | promise.Resolve(true); |
| 0 | 142 | | } |
| | 143 | | } |
| | 144 | | } |