< Summary

Class:BuilderProjectsPanelController
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/Scripts/BuilderProjectsPanelController.cs
Covered lines:87
Uncovered lines:45
Coverable lines:132
Total lines:302
Line coverage:65.9% (87 of 132)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BuilderProjectsPanelController()0%110100%
BuilderProjectsPanelController(...)0%110100%
Dispose()0%990100%
Initialize()0%110100%
Initialize(...)0%22095.65%
SetVisibility(...)0%110100%
OnVisibilityChanged(...)0%3.013090.91%
OnClose()0%220100%
PanelOpenEvent(...)0%2100%
GetAmountOfLandsOwnedAndOperator(...)0%3.793055.56%
SetView()0%110100%
FetchLandsAndScenes(...)0%33092.31%
GoToCoords(...)0%6200%
OpenUrl(...)0%2100%
OnGoToEditScene(...)0%12300%
StartFetchInterval()0%2.062075%
StopFetchInterval()0%110100%
RefreshDataInterval()0%5.673033.33%
OnSceneUnpublished(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/Scripts/BuilderProjectsPanelController.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Linq;
 4using DCL;
 5using DCL.Helpers;
 6using DCL.Interface;
 7using UnityEngine;
 8using Variables.RealmsInfo;
 9using Environment = DCL.Environment;
 10using Object = UnityEngine.Object;
 11
 12public class BuilderProjectsPanelController : IHUD
 13{
 14    private const string TESTING_ETH_ADDRESS = "0x2fa1859029A483DEFbB664bB6026D682f55e2fcD";
 15    private const string TESTING_TLD = "org";
 16    private const string VIEW_PREFAB_PATH = "BuilderProjectsPanel";
 17
 18    private const float CACHE_TIME_LAND = 5 * 60;
 19    private const float CACHE_TIME_SCENES = 1 * 60;
 20    private const float REFRESH_INTERVAL = 2 * 60;
 21
 22    internal readonly IBuilderProjectsPanelView view;
 23
 24    private ISectionsController sectionsController;
 25    private IScenesViewController scenesViewController;
 26    private ILandController landsController;
 27    private UnpublishPopupController unpublishPopupController;
 28
 29    private SectionsHandler sectionsHandler;
 30    private SceneContextMenuHandler sceneContextMenuHandler;
 31    private LeftMenuHandler leftMenuHandler;
 32    private LeftMenuSettingsViewHandler leftMenuSettingsViewHandler;
 33
 34    private ITheGraph theGraph;
 35    private ICatalyst catalyst;
 36
 37    private bool isInitialized = false;
 38    private bool isFetching = false;
 39    private bool sendPlayerOpenPanelEvent = false;
 40    private Coroutine fetchDataInterval;
 41    private Promise<LandWithAccess[]> fetchLandPromise = null;
 42
 43    public event Action OnJumpInOrEdit;
 44
 1245    public BuilderProjectsPanelController() : this(
 1246        Object.Instantiate(Resources.Load<BuilderProjectsPanelView>(VIEW_PREFAB_PATH))) { }
 47
 1248    internal BuilderProjectsPanelController(IBuilderProjectsPanelView view)
 49    {
 1250        this.view = view;
 1251        view.OnClosePressed += OnClose;
 1252    }
 53
 54    public void Dispose()
 55    {
 1256        StopFetchInterval();
 57
 1258        DataStore.i.HUDs.builderProjectsPanelVisible.OnChange -= OnVisibilityChanged;
 1259        DataStore.i.builderInWorld.unpublishSceneResult.OnChange -= OnSceneUnpublished;
 1260        view.OnClosePressed -= OnClose;
 61
 1262        unpublishPopupController?.Dispose();
 63
 1264        fetchLandPromise?.Dispose();
 65
 1266        leftMenuSettingsViewHandler?.Dispose();
 1267        sectionsHandler?.Dispose();
 1268        sceneContextMenuHandler?.Dispose();
 1269        leftMenuHandler?.Dispose();
 70
 1271        sectionsController?.Dispose();
 1272        scenesViewController?.Dispose();
 73
 1274        view.Dispose();
 1275    }
 76
 77    public void Initialize()
 78    {
 179        Initialize(new SectionsController(view.GetSectionContainer()),
 80            new ScenesViewController(view.GetCardViewPrefab(), view.GetTransform()),
 81            new LandController(),
 82            Environment.i.platform.serviceProviders.theGraph,
 83            Environment.i.platform.serviceProviders.catalyst);
 184    }
 85
 86    internal void Initialize(ISectionsController sectionsController,
 87        IScenesViewController scenesViewController, ILandController landController, ITheGraph theGraph, ICatalyst cataly
 88    {
 1289        if (isInitialized)
 090            return;
 91
 1292        isInitialized = true;
 93
 1294        this.sectionsController = sectionsController;
 1295        this.scenesViewController = scenesViewController;
 1296        this.landsController = landController;
 97
 1298        this.theGraph = theGraph;
 1299        this.catalyst = catalyst;
 100
 12101        this.unpublishPopupController = new UnpublishPopupController(view.GetUnpublishPopup());
 102
 103        // set listeners for sections, setup searchbar for section, handle request for opening a new section
 12104        sectionsHandler = new SectionsHandler(sectionsController, scenesViewController, landsController, view.GetSearchB
 105        // handle if main panel or settings panel should be shown in current section
 12106        leftMenuHandler = new LeftMenuHandler(view, sectionsController);
 107        // handle project scene info on the left menu panel
 12108        leftMenuSettingsViewHandler = new LeftMenuSettingsViewHandler(view.GetSettingsViewReferences(), scenesViewContro
 109        // handle scene's context menu options
 12110        sceneContextMenuHandler = new SceneContextMenuHandler(view.GetSceneCardViewContextMenu(), sectionsController, sc
 111
 12112        SetView();
 113
 12114        sectionsController.OnRequestOpenUrl += OpenUrl;
 12115        sectionsController.OnRequestGoToCoords += GoToCoords;
 12116        sectionsController.OnRequestEditSceneAtCoords += OnGoToEditScene;
 12117        scenesViewController.OnJumpInPressed += GoToCoords;
 12118        scenesViewController.OnRequestOpenUrl += OpenUrl;
 12119        scenesViewController.OnEditorPressed += OnGoToEditScene;
 120
 12121        DataStore.i.HUDs.builderProjectsPanelVisible.OnChange += OnVisibilityChanged;
 12122        DataStore.i.builderInWorld.unpublishSceneResult.OnChange += OnSceneUnpublished;
 12123    }
 124
 12125    public void SetVisibility(bool visible) { DataStore.i.HUDs.builderProjectsPanelVisible.Set(visible); }
 126
 127    private void OnVisibilityChanged(bool isVisible, bool prev)
 128    {
 7129        if (isVisible == prev)
 0130            return;
 131
 7132        view.SetVisible(isVisible);
 133
 7134        if (isVisible)
 135        {
 4136            sendPlayerOpenPanelEvent = true;
 137
 4138            FetchLandsAndScenes();
 4139            StartFetchInterval();
 4140            sectionsController.OpenSection(SectionId.SCENES_DEPLOYED);
 4141        }
 142        else
 143        {
 3144            StopFetchInterval();
 145        }
 3146    }
 147
 148    private void OnClose()
 149    {
 1150        SetVisibility(false);
 151
 1152        LandWithAccess[] lands = landsController.GetLands();
 1153        if (lands != null)
 154        {
 1155            Vector2Int totalLands = GetAmountOfLandsOwnedAndOperator(lands);
 1156            BIWAnalytics.PlayerClosesPanel(totalLands.x, totalLands.y);
 157        }
 1158    }
 159
 160    private void PanelOpenEvent(LandWithAccess[] lands)
 161    {
 0162        Vector2Int totalLands = GetAmountOfLandsOwnedAndOperator(lands);
 0163        BIWAnalytics.PlayerOpenPanel(totalLands.x, totalLands.y);
 0164    }
 165
 166    /// <summary>
 167    /// This counts the amount of lands that the user own and the amount of lands that the user operate
 168    /// </summary>
 169    /// <param name="lands"></param>
 170    /// <returns>Vector2: X = amount of owned lands, Y = amount of operator lands</returns>
 171    private Vector2Int GetAmountOfLandsOwnedAndOperator(LandWithAccess[] lands)
 172    {
 1173        int ownedLandsCount = 0;
 1174        int operatorLandsCount = 0;
 2175        foreach (var land in lands)
 176        {
 0177            if (land.role == LandRole.OWNER)
 0178                ownedLandsCount++;
 179            else
 0180                operatorLandsCount++;
 181        }
 1182        return new Vector2Int(ownedLandsCount, operatorLandsCount);
 183    }
 184
 185    private void SetView()
 186    {
 12187        scenesViewController.AddListener((IDeployedSceneListener) view);
 12188        scenesViewController.AddListener((IProjectSceneListener) view);
 12189    }
 190
 191    private void FetchLandsAndScenes(float landCacheTime = CACHE_TIME_LAND, float scenesCacheTime = CACHE_TIME_SCENES)
 192    {
 4193        if (isFetching)
 0194            return;
 195
 4196        isFetching = true;
 197
 4198        var address = UserProfile.GetOwnUserProfile().ethAddress;
 4199        var tld = KernelConfig.i.Get().tld;
 200
 201#if UNITY_EDITOR
 202        // NOTE: to be able to test in editor without getting a profile we hardcode an address here
 4203        if (string.IsNullOrEmpty(address))
 204        {
 4205            address = TESTING_ETH_ADDRESS;
 4206            tld = TESTING_TLD;
 4207            DataStore.i.playerRealm.Set(new CurrentRealmModel()
 208            {
 209                domain = $"https://peer.decentraland.{TESTING_TLD}",
 210                contentServerUrl = $"https://peer.decentraland.{TESTING_TLD}/content",
 211            });
 212        }
 213#endif
 214
 4215        sectionsController.SetFetchingDataStart();
 216
 4217        fetchLandPromise = DeployedScenesFetcher.FetchLandsFromOwner(catalyst, theGraph, address, tld, landCacheTime, sc
 4218        fetchLandPromise
 219            .Then(lands =>
 220            {
 0221                sectionsController.SetFetchingDataEnd();
 0222                isFetching = false;
 223
 224                try
 225                {
 0226                    var scenes = lands.Where(land => land.scenes != null && land.scenes.Count > 0)
 0227                                      .Select(land => land.scenes.Where(scene => !scene.isEmpty).Select(scene => (IScene
 0228                                      .Aggregate((i, j) => i.Concat(j))
 229                                      .ToArray();
 230
 0231                    PanelOpenEvent(lands);
 0232                    landsController.SetLands(lands);
 0233                    scenesViewController.SetScenes(scenes);
 0234                }
 0235                catch (Exception e)
 236                {
 0237                    landsController.SetLands(lands);
 0238                    scenesViewController.SetScenes(new ISceneData[] { });
 0239                }
 0240            })
 241            .Catch(error =>
 242            {
 0243                isFetching = false;
 0244                sectionsController.SetFetchingDataEnd();
 0245                landsController.SetLands(new LandWithAccess[] { });
 0246                scenesViewController.SetScenes(new ISceneData[] { });
 0247                Debug.LogError(error);
 0248            });
 4249    }
 250
 251    private void GoToCoords(Vector2Int coords)
 252    {
 0253        WebInterface.GoTo(coords.x, coords.y);
 0254        SetVisibility(false);
 0255        OnJumpInOrEdit?.Invoke();
 0256    }
 257
 0258    private void OpenUrl(string url) { WebInterface.OpenURL(url); }
 259
 260    private void OnGoToEditScene(Vector2Int coords)
 261    {
 0262        bool isGoingToTeleport = BuilderInWorldTeleportAndEdit.TeleportAndEdit(coords);
 0263        if (isGoingToTeleport)
 264        {
 0265            SetVisibility(false);
 266        }
 0267        OnJumpInOrEdit?.Invoke();
 0268    }
 269
 270    private void StartFetchInterval()
 271    {
 4272        if (fetchDataInterval != null)
 273        {
 0274            StopFetchInterval();
 275        }
 276
 4277        fetchDataInterval = CoroutineStarter.Start(RefreshDataInterval());
 4278    }
 279
 280    private void StopFetchInterval()
 281    {
 15282        CoroutineStarter.Stop(fetchDataInterval);
 15283        fetchDataInterval = null;
 15284    }
 285
 286    IEnumerator RefreshDataInterval()
 287    {
 0288        while (true)
 289        {
 4290            yield return WaitForSecondsCache.Get(REFRESH_INTERVAL);
 0291            FetchLandsAndScenes();
 292        }
 293    }
 294
 295    private void OnSceneUnpublished(PublishSceneResultPayload current, PublishSceneResultPayload previous)
 296    {
 0297        if (current.ok)
 298        {
 0299            FetchLandsAndScenes(CACHE_TIME_LAND, 0);
 300        }
 0301    }
 302}