< Summary

Class:DCL.Builder.PublishProgressView
Assembly:BuilderPublisher
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Publisher/ProjectPublishHUD/Scripts/PublishProgressView.cs
Covered lines:0
Uncovered lines:97
Coverable lines:97
Total lines:301
Line coverage:0% (0 of 97)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
RefreshControl()0%2100%
Awake()0%2100%
Dispose()0%6200%
ShowCurrentStatus()0%30500%
SetPublishInfo(...)0%2100%
ProjectPublished()0%2100%
GetConfirmSubtitleText(...)0%2100%
ShowConfirmPopUp()0%2100%
ConfirmDeployment()0%2100%
ShowProgressView()0%6200%
ShowProjectPublishSucces()0%20400%
ShowPublishError()0%2100%
ChangeStatus(...)0%2100%
Back()0%6200%
Close()0%6200%
ConfirmPublish()0%6200%
PublishStarted()0%2100%
Hide()0%6200%
PublishError(...)0%2100%
SetPercentage(...)0%2100%
FakePublishProgress()0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Publisher/ProjectPublishHUD/Scripts/PublishProgressView.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8namespace DCL.Builder
 9{
 10    public interface IPublishProgressView
 11    {
 12        /// <summary>
 13        /// This represent the current state of the publishing view
 14        /// </summary>
 15        enum PublishStatus
 16        {
 17            IDLE = 0,
 18            CONFIRM = 1,
 19            PUBLISHING = 2,
 20            ERROR = 3,
 21            SUCCESS = 4
 22        }
 23
 24        /// <summary>
 25        /// This action will be called when the confirm button is pressed
 26        /// </summary>
 27        event Action OnPublishConfirmButtonPressed;
 28
 29        /// <summary>
 30        /// This action will be called when the view is closed
 31        /// </summary>
 32        event Action OnViewClosed;
 33
 34        /// <summary>
 35        /// Whe should go back to the previous view
 36        /// </summary>
 37        event Action OnBackPressed;
 38
 39        /// <summary>
 40        /// Show the confirm publish pop up
 41        /// </summary>
 42        void ConfirmDeployment();
 43
 44        /// <summary>
 45        /// Call this function when the publish start
 46        /// </summary>
 47        void PublishStarted();
 48
 49        /// <summary>
 50        /// Hide the view
 51        /// </summary>
 52        void Hide();
 53
 54        /// <summary>
 55        /// Shows the errors passes in a pop up
 56        /// </summary>
 57        /// <param name="message"></param>
 58        void PublishError(string message);
 59
 60        /// <summary>
 61        /// Shows that the project has been successfully published
 62        /// </summary>
 63        void ProjectPublished();
 64
 65        /// <summary>
 66        /// Sets the project to display
 67        /// </summary>
 68        /// <param name="publishedProject"></param>
 69        void SetPublishInfo(IBuilderScene publishedProject, PublishInfo info);
 70
 71        /// <summary>
 72        /// Dispose the view
 73        /// </summary>
 74        void Dispose();
 75    }
 76
 77    public class PublishProgressView : BaseComponentView, IPublishProgressView
 78    {
 79        // Main titles
 80        private const string CONFIRM_MAIN_TEXT = "Are you sure you want to publish your project?";
 81        private const string ERROR_MAIN_TEXT = "The publication of your project has failed!";
 82        private const string SUCCESS_MAIN_TEXT = "Project successfully published!";
 83        private const string PUBLISHING_MAIN_TEXT = "Publishing";
 84
 85        // Sub titles
 86        private const string CONFIRM_SUB_TITLE_TEXT =  @"{0} will be deployed in {1},{2}";
 87        private const string PUBLISHING_SUB_TITLE_TEXT =  @"{0} is being deployed in {1},{2}";
 88        private const string SUCCESS_SUB_TITLE_TEXT =  @"{0} is now a live place in [landName]{1},{2}";
 89
 90        public event Action OnPublishConfirmButtonPressed;
 91        public event Action OnViewClosed;
 92        public event Action OnBackPressed;
 93
 94        [Header("General references")]
 95        [SerializeField] internal TMP_Text mainTitleTextView;
 96        [SerializeField] internal TMP_Text subTitleTextView;
 97        [SerializeField] internal RawImage screenshotImage;
 98        [SerializeField] internal ModalComponentView modal;
 99
 100        [Header("Confirm Popup references")]
 101        [SerializeField] internal GameObject confirmGameObject;
 102        [SerializeField] internal Button noButton;
 103        [SerializeField] internal Button yesButton;
 104
 105        [Header("Publish Progress references")]
 106        [SerializeField] internal GameObject progressGameObject;
 107        [SerializeField] internal LoadingBar loadingBar;
 108        [Header("Error references")]
 109        [SerializeField] internal TMP_Text errorTextView;
 110        [SerializeField] internal GameObject errorGameObject;
 111        [SerializeField] internal Button cancelButton;
 112        [SerializeField] internal Button retryButton;
 113
 114        [Header("Success references")]
 115        [SerializeField] internal Button okButton;
 116        [SerializeField] internal GameObject successGameObject;
 117        private IPublishProgressView.PublishStatus currentStatus;
 118        private float currentProgress = 0;
 119        private Coroutine fakeProgressCoroutine;
 120        private IBuilderScene currentScene;
 121        private PublishInfo currentInfo;
 122
 0123        public override void RefreshControl() { }
 124
 125        public override void Awake()
 126        {
 0127            base.Awake();
 0128            cancelButton.onClick.AddListener(Close);
 0129            noButton.onClick.AddListener(Back);
 0130            okButton.onClick.AddListener(Close);
 131
 0132            yesButton.onClick.AddListener(ConfirmPublish);
 0133            retryButton.onClick.AddListener(ConfirmPublish);
 134
 0135            gameObject.SetActive(false);
 0136            modal.OnCloseAction += Close;
 0137        }
 138
 139        public override void Dispose()
 140        {
 0141            base.Dispose();
 0142            cancelButton.onClick.RemoveAllListeners();
 0143            noButton.onClick.RemoveAllListeners();
 0144            yesButton.onClick.RemoveAllListeners();
 0145            okButton.onClick.RemoveAllListeners();
 0146            retryButton.onClick.RemoveAllListeners();
 147
 0148            if (fakeProgressCoroutine != null)
 0149                StopCoroutine(fakeProgressCoroutine);
 150
 0151            modal.OnCloseAction -= Close;
 0152        }
 153
 154        private void ShowCurrentStatus()
 155        {
 0156            successGameObject.SetActive(false);
 0157            errorGameObject.SetActive(false);
 0158            confirmGameObject.SetActive(false);
 0159            progressGameObject.SetActive(false);
 0160            modal.CanBeCancelled(true);
 161
 0162            switch (currentStatus)
 163            {
 164                case IPublishProgressView.PublishStatus.CONFIRM:
 0165                    ShowConfirmPopUp();
 0166                    break;
 167                case IPublishProgressView.PublishStatus.PUBLISHING:
 0168                    ShowProgressView();
 0169                    break;
 170                case IPublishProgressView.PublishStatus.ERROR:
 0171                    ShowPublishError();
 0172                    break;
 173                case IPublishProgressView.PublishStatus.SUCCESS:
 0174                    ShowProjectPublishSucces();
 175                    break;
 176            }
 0177        }
 178
 179        public void SetPublishInfo(IBuilderScene publishedProject, PublishInfo info)
 180        {
 0181            screenshotImage.texture = publishedProject.sceneScreenshotTexture;
 0182            currentScene = publishedProject;
 0183            currentInfo = info;
 0184        }
 185
 0186        public void ProjectPublished() { ChangeStatus(IPublishProgressView.PublishStatus.SUCCESS); }
 187
 188        internal string GetConfirmSubtitleText(string baseText)
 189        {
 0190            string title = currentScene.manifest.project.title;
 0191            string posX = currentInfo.coordsToPublish.x.ToString();
 0192            string posY = currentInfo.coordsToPublish.y.ToString();
 193
 0194            return string.Format(baseText, title, posX, posY);
 195        }
 196
 197        public void ShowConfirmPopUp()
 198        {
 0199            confirmGameObject.SetActive(true);
 0200            mainTitleTextView.text = CONFIRM_MAIN_TEXT;
 0201            subTitleTextView.text = GetConfirmSubtitleText(CONFIRM_SUB_TITLE_TEXT);
 0202            gameObject.SetActive(true);
 0203            modal.Show();
 0204        }
 205
 0206        public void ConfirmDeployment() { ChangeStatus(IPublishProgressView.PublishStatus.CONFIRM); }
 207
 208        public void ShowProgressView()
 209        {
 0210            mainTitleTextView.text = PUBLISHING_MAIN_TEXT;
 0211            subTitleTextView.text = GetConfirmSubtitleText(PUBLISHING_SUB_TITLE_TEXT);
 212
 0213            currentProgress = 0;
 0214            modal.CanBeCancelled(false);
 0215            progressGameObject.SetActive(true);
 0216            if (fakeProgressCoroutine != null)
 0217                StopCoroutine(fakeProgressCoroutine);
 0218            fakeProgressCoroutine = StartCoroutine(FakePublishProgress());
 0219            AudioScriptableObjects.enable.Play();
 0220        }
 221
 222        public void ShowProjectPublishSucces()
 223        {
 0224            mainTitleTextView.text = SUCCESS_MAIN_TEXT;
 0225            string text = GetConfirmSubtitleText(SUCCESS_SUB_TITLE_TEXT);
 226
 0227            foreach (var land in DataStore.i.builderInWorld.landsWithAccess.Get())
 228            {
 0229                if (land.baseCoords == currentInfo.coordsToPublish && !string.IsNullOrEmpty(land.name))
 0230                    text = text.Replace("[landName]", land.name + " ");
 231            }
 232
 0233            text = text.Replace("[landName]", "");
 0234            subTitleTextView.text = text;
 235
 0236            successGameObject.SetActive(true);
 0237        }
 238
 239        public void ShowPublishError()
 240        {
 0241            mainTitleTextView.text = ERROR_MAIN_TEXT;
 0242            errorGameObject.SetActive(true);
 0243        }
 244
 245        private void ChangeStatus(IPublishProgressView.PublishStatus status)
 246        {
 0247            currentStatus = status;
 0248            ShowCurrentStatus();
 0249        }
 250
 251        public void Back()
 252        {
 0253            modal.Hide();
 0254            OnBackPressed?.Invoke();
 0255        }
 256
 257        public void Close()
 258        {
 0259            modal.Hide();
 0260            OnViewClosed?.Invoke();
 0261        }
 262
 263        [ContextMenu("Start Deployment")]
 0264        public void ConfirmPublish() { OnPublishConfirmButtonPressed?.Invoke(); }
 265
 0266        public void PublishStarted() { ChangeStatus(IPublishProgressView.PublishStatus.PUBLISHING); }
 267
 268        public void Hide()
 269        {
 0270            modal.Hide();
 0271            if (fakeProgressCoroutine != null)
 0272                StopCoroutine(fakeProgressCoroutine);
 0273        }
 274
 275        public void PublishError(string message)
 276        {
 0277            errorTextView.text = message;
 0278            ChangeStatus(IPublishProgressView.PublishStatus.ERROR);
 0279        }
 280
 0281        public void SetPercentage(float newValue) { loadingBar.SetPercentage(newValue); }
 282
 283        private IEnumerator FakePublishProgress()
 284        {
 0285            while (true)
 286            {
 0287                float newPercentage = UnityEngine.Random.Range(1f, 15f);
 0288                currentProgress += newPercentage;
 289
 0290                currentProgress = Mathf.Clamp(
 291                    currentProgress,
 292                    currentProgress - newPercentage,
 293                    99f);
 294
 0295                SetPercentage(currentProgress);
 296
 0297                yield return new WaitForSeconds(UnityEngine.Random.Range(0.15f, 0.65f));
 298            }
 299        }
 300    }
 301}