< 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:116
Uncovered lines:19
Coverable lines:135
Total lines:314
Line coverage:85.9% (116 of 135)
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%4.014092.31%
OnClose()0%220100%
PanelOpenEvent(...)0%110100%
GetAmountOfLandsOwnedAndOperator(...)0%3.013088.89%
SetView()0%110100%
FetchLandsAndScenes(...)0%3.263069.23%
LandsFetchedError(...)0%110100%
LandsFetched(...)0%6.145064.29%
GoToCoords(...)0%220100%
OpenUrl(...)0%2100%
OnGoToEditScene(...)0%330100%
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 = "0xDc13378daFca7Fe2306368A16BCFac38c80BfCAD";
 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    internal 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
 1645    public BuilderProjectsPanelController() : this(
 46        Object.Instantiate(Resources.Load<BuilderProjectsPanelView>(VIEW_PREFAB_PATH)))
 47    {
 1648    }
 49
 1650    internal BuilderProjectsPanelController(IBuilderProjectsPanelView view)
 51    {
 1652        this.view = view;
 1653        view.OnClosePressed += OnClose;
 1654    }
 55
 56    public void Dispose()
 57    {
 1658        StopFetchInterval();
 59
 1660        DataStore.i.HUDs.builderProjectsPanelVisible.OnChange -= OnVisibilityChanged;
 1661        DataStore.i.builderInWorld.unpublishSceneResult.OnChange -= OnSceneUnpublished;
 1662        view.OnClosePressed -= OnClose;
 63
 1664        unpublishPopupController?.Dispose();
 65
 1666        fetchLandPromise?.Dispose();
 67
 1668        leftMenuSettingsViewHandler?.Dispose();
 1669        sectionsHandler?.Dispose();
 1670        sceneContextMenuHandler?.Dispose();
 1671        leftMenuHandler?.Dispose();
 72
 1673        sectionsController?.Dispose();
 1674        scenesViewController?.Dispose();
 75
 1676        view.Dispose();
 1677    }
 78
 79    public void Initialize()
 80    {
 181        Initialize(new SectionsController(view.GetSectionContainer()),
 82            new ScenesViewController(view.GetCardViewPrefab(), view.GetTransform()),
 83            new LandController(),
 84            Environment.i.platform.serviceProviders.theGraph,
 85            Environment.i.platform.serviceProviders.catalyst);
 186    }
 87
 88    internal void Initialize(ISectionsController sectionsController,
 89        IScenesViewController scenesViewController, ILandController landController, ITheGraph theGraph, ICatalyst cataly
 90    {
 1691        if (isInitialized)
 092            return;
 93
 1694        isInitialized = true;
 95
 1696        this.sectionsController = sectionsController;
 1697        this.scenesViewController = scenesViewController;
 1698        this.landsController = landController;
 99
 16100        this.theGraph = theGraph;
 16101        this.catalyst = catalyst;
 102
 16103        this.unpublishPopupController = new UnpublishPopupController(view.GetUnpublishPopup());
 104
 105        // set listeners for sections, setup searchbar for section, handle request for opening a new section
 16106        sectionsHandler = new SectionsHandler(sectionsController, scenesViewController, landsController, view.GetSearchB
 107        // handle if main panel or settings panel should be shown in current section
 16108        leftMenuHandler = new LeftMenuHandler(view, sectionsController);
 109        // handle project scene info on the left menu panel
 16110        leftMenuSettingsViewHandler = new LeftMenuSettingsViewHandler(view.GetSettingsViewReferences(), scenesViewContro
 111        // handle scene's context menu options
 16112        sceneContextMenuHandler = new SceneContextMenuHandler(view.GetSceneCardViewContextMenu(), sectionsController, sc
 113
 16114        SetView();
 115
 16116        sectionsController.OnRequestOpenUrl += OpenUrl;
 16117        sectionsController.OnRequestGoToCoords += GoToCoords;
 16118        sectionsController.OnRequestEditSceneAtCoords += OnGoToEditScene;
 16119        scenesViewController.OnJumpInPressed += GoToCoords;
 16120        scenesViewController.OnRequestOpenUrl += OpenUrl;
 16121        scenesViewController.OnEditorPressed += OnGoToEditScene;
 122
 16123        DataStore.i.HUDs.builderProjectsPanelVisible.OnChange += OnVisibilityChanged;
 16124        DataStore.i.builderInWorld.unpublishSceneResult.OnChange += OnSceneUnpublished;
 16125    }
 126
 16127    public void SetVisibility(bool visible) { DataStore.i.HUDs.builderProjectsPanelVisible.Set(visible); }
 128
 129    private void OnVisibilityChanged(bool isVisible, bool prev)
 130    {
 8131        if (isVisible == prev)
 0132            return;
 133
 8134        view.SetVisible(isVisible);
 135
 8136        if (isVisible)
 137        {
 5138            if (DataStore.i.builderInWorld.landsWithAccess.Get() != null)
 4139                PanelOpenEvent(DataStore.i.builderInWorld.landsWithAccess.Get());
 140            else
 1141                sendPlayerOpenPanelEvent = true;
 142
 5143            FetchLandsAndScenes();
 5144            StartFetchInterval();
 5145            sectionsController.OpenSection(SectionId.SCENES_DEPLOYED);
 5146        }
 147        else
 148        {
 3149            StopFetchInterval();
 150        }
 3151    }
 152
 153    private void OnClose()
 154    {
 1155        SetVisibility(false);
 156
 1157        LandWithAccess[] lands = landsController.GetLands();
 1158        if (lands != null)
 159        {
 1160            Vector2Int totalLands = GetAmountOfLandsOwnedAndOperator(lands);
 1161            BIWAnalytics.PlayerClosesPanel(totalLands.x, totalLands.y);
 162        }
 1163    }
 164
 165    private void PanelOpenEvent(LandWithAccess[] lands)
 166    {
 4167        Vector2Int totalLands = GetAmountOfLandsOwnedAndOperator(lands);
 4168        BIWAnalytics.PlayerOpenPanel(totalLands.x, totalLands.y);
 4169    }
 170
 171    /// <summary>
 172    /// This counts the amount of lands that the user own and the amount of lands that the user operate
 173    /// </summary>
 174    /// <param name="lands"></param>
 175    /// <returns>Vector2: X = amount of owned lands, Y = amount of operator lands</returns>
 176    private Vector2Int GetAmountOfLandsOwnedAndOperator(LandWithAccess[] lands)
 177    {
 5178        int ownedLandsCount = 0;
 5179        int operatorLandsCount = 0;
 18180        foreach (var land in lands)
 181        {
 4182            if (land.role == LandRole.OWNER)
 4183                ownedLandsCount++;
 184            else
 0185                operatorLandsCount++;
 186        }
 187
 5188        return new Vector2Int(ownedLandsCount, operatorLandsCount);
 189    }
 190
 191    private void SetView()
 192    {
 16193        scenesViewController.AddListener((IDeployedSceneListener) view);
 16194        scenesViewController.AddListener((IProjectSceneListener) view);
 16195    }
 196
 197    private void FetchLandsAndScenes(float landCacheTime = CACHE_TIME_LAND, float scenesCacheTime = CACHE_TIME_SCENES)
 198    {
 5199        if (isFetching)
 0200            return;
 201
 5202        isFetching = true;
 203
 5204        var address = UserProfile.GetOwnUserProfile().ethAddress;
 5205        var network = KernelConfig.i.Get().network;
 206
 207#if UNITY_EDITOR
 208        // NOTE: to be able to test in editor without getting a profile we hardcode an address here
 5209        if (string.IsNullOrEmpty(address))
 210        {
 0211            address = TESTING_ETH_ADDRESS;
 0212            network = TESTING_TLD;
 0213            DataStore.i.playerRealm.Set(new CurrentRealmModel()
 214            {
 215                domain = $"https://peer-lb.decentraland.{TESTING_TLD}",
 216                contentServerUrl = $"https://peer-lb.decentraland.{TESTING_TLD}/content",
 217            });
 218        }
 219#endif
 220
 5221        sectionsController.SetFetchingDataStart();
 222
 5223        fetchLandPromise = DeployedScenesFetcher.FetchLandsFromOwner(catalyst, theGraph, address, network, landCacheTime
 5224        fetchLandPromise
 225            .Then(LandsFetched)
 226            .Catch(LandsFetchedError);
 5227    }
 228
 229    internal void LandsFetchedError(string error)
 230    {
 1231        isFetching = false;
 1232        sectionsController.SetFetchingDataEnd();
 1233        landsController.SetLands(new LandWithAccess[] { });
 1234        scenesViewController.SetScenes(new ISceneData[] { });
 1235        Debug.LogError(error);
 1236    }
 237
 238    internal void LandsFetched(LandWithAccess[] lands)
 239    {
 1240        DataStore.i.builderInWorld.landsWithAccess.Set(lands.ToArray(), true);
 1241        sectionsController.SetFetchingDataEnd();
 1242        isFetching = false;
 243
 244        try
 245        {
 2246            var scenes = lands.Where(land => land.scenes != null && land.scenes.Count > 0)
 2247                .Select(land => land.scenes.Where(scene => !scene.isEmpty).Select(scene => (ISceneData)new SceneData(sce
 0248                .Aggregate((i, j) => i.Concat(j))
 249                .ToArray();
 250
 0251            if (sendPlayerOpenPanelEvent) PanelOpenEvent(lands);
 0252            landsController.SetLands(lands);
 0253            scenesViewController.SetScenes(scenes);
 0254        }
 1255        catch (Exception e)
 256        {
 1257            landsController.SetLands(lands);
 1258            scenesViewController.SetScenes(new ISceneData[] { });
 1259        }
 1260    }
 261
 262    internal void GoToCoords(Vector2Int coords)
 263    {
 1264        WebInterface.GoTo(coords.x, coords.y);
 1265        SetVisibility(false);
 1266        OnJumpInOrEdit?.Invoke();
 1267    }
 268
 0269    private void OpenUrl(string url) { WebInterface.OpenURL(url); }
 270
 271    internal void OnGoToEditScene(Vector2Int coords)
 272    {
 1273        bool isGoingToTeleport = BIWTeleportAndEdit.TeleportAndEdit(coords);
 1274        if (isGoingToTeleport)
 275        {
 1276            SetVisibility(false);
 277        }
 278
 1279        OnJumpInOrEdit?.Invoke();
 1280    }
 281
 282    private void StartFetchInterval()
 283    {
 5284        if (fetchDataInterval != null)
 285        {
 0286            StopFetchInterval();
 287        }
 288
 5289        fetchDataInterval = CoroutineStarter.Start(RefreshDataInterval());
 5290    }
 291
 292    private void StopFetchInterval()
 293    {
 19294        CoroutineStarter.Stop(fetchDataInterval);
 19295        fetchDataInterval = null;
 19296    }
 297
 298    IEnumerator RefreshDataInterval()
 299    {
 0300        while (true)
 301        {
 5302            yield return WaitForSecondsCache.Get(REFRESH_INTERVAL);
 0303            FetchLandsAndScenes();
 304        }
 305    }
 306
 307    private void OnSceneUnpublished(PublishSceneResultPayload current, PublishSceneResultPayload previous)
 308    {
 0309        if (current.ok)
 310        {
 0311            FetchLandsAndScenes(CACHE_TIME_LAND, 0);
 312        }
 0313    }
 314}