< 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:115
Uncovered lines:20
Coverable lines:135
Total lines:310
Line coverage:85.1% (115 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.064084.62%
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(
 1646        Object.Instantiate(Resources.Load<BuilderProjectsPanelView>(VIEW_PREFAB_PATH))) { }
 47
 1648    internal BuilderProjectsPanelController(IBuilderProjectsPanelView view)
 49    {
 1650        this.view = view;
 1651        view.OnClosePressed += OnClose;
 1652    }
 53
 54    public void Dispose()
 55    {
 1656        StopFetchInterval();
 57
 1658        DataStore.i.HUDs.builderProjectsPanelVisible.OnChange -= OnVisibilityChanged;
 1659        DataStore.i.builderInWorld.unpublishSceneResult.OnChange -= OnSceneUnpublished;
 1660        view.OnClosePressed -= OnClose;
 61
 1662        unpublishPopupController?.Dispose();
 63
 1664        fetchLandPromise?.Dispose();
 65
 1666        leftMenuSettingsViewHandler?.Dispose();
 1667        sectionsHandler?.Dispose();
 1668        sceneContextMenuHandler?.Dispose();
 1669        leftMenuHandler?.Dispose();
 70
 1671        sectionsController?.Dispose();
 1672        scenesViewController?.Dispose();
 73
 1674        view.Dispose();
 1675    }
 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    {
 1689        if (isInitialized)
 090            return;
 91
 1692        isInitialized = true;
 93
 1694        this.sectionsController = sectionsController;
 1695        this.scenesViewController = scenesViewController;
 1696        this.landsController = landController;
 97
 1698        this.theGraph = theGraph;
 1699        this.catalyst = catalyst;
 100
 16101        this.unpublishPopupController = new UnpublishPopupController(view.GetUnpublishPopup());
 102
 103        // set listeners for sections, setup searchbar for section, handle request for opening a new section
 16104        sectionsHandler = new SectionsHandler(sectionsController, scenesViewController, landsController, view.GetSearchB
 105        // handle if main panel or settings panel should be shown in current section
 16106        leftMenuHandler = new LeftMenuHandler(view, sectionsController);
 107        // handle project scene info on the left menu panel
 16108        leftMenuSettingsViewHandler = new LeftMenuSettingsViewHandler(view.GetSettingsViewReferences(), scenesViewContro
 109        // handle scene's context menu options
 16110        sceneContextMenuHandler = new SceneContextMenuHandler(view.GetSceneCardViewContextMenu(), sectionsController, sc
 111
 16112        SetView();
 113
 16114        sectionsController.OnRequestOpenUrl += OpenUrl;
 16115        sectionsController.OnRequestGoToCoords += GoToCoords;
 16116        sectionsController.OnRequestEditSceneAtCoords += OnGoToEditScene;
 16117        scenesViewController.OnJumpInPressed += GoToCoords;
 16118        scenesViewController.OnRequestOpenUrl += OpenUrl;
 16119        scenesViewController.OnEditorPressed += OnGoToEditScene;
 120
 16121        DataStore.i.HUDs.builderProjectsPanelVisible.OnChange += OnVisibilityChanged;
 16122        DataStore.i.builderInWorld.unpublishSceneResult.OnChange += OnSceneUnpublished;
 16123    }
 124
 16125    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            if (DataStore.i.builderInWorld.landsWithAccess != null)
 4137                PanelOpenEvent(DataStore.i.builderInWorld.landsWithAccess.Get());
 138            else
 0139                sendPlayerOpenPanelEvent = true;
 140
 4141            FetchLandsAndScenes();
 4142            StartFetchInterval();
 4143            sectionsController.OpenSection(SectionId.SCENES_DEPLOYED);
 4144        }
 145        else
 146        {
 3147            StopFetchInterval();
 148        }
 3149    }
 150
 151    private void OnClose()
 152    {
 1153        SetVisibility(false);
 154
 1155        LandWithAccess[] lands = landsController.GetLands();
 1156        if (lands != null)
 157        {
 1158            Vector2Int totalLands = GetAmountOfLandsOwnedAndOperator(lands);
 1159            BIWAnalytics.PlayerClosesPanel(totalLands.x, totalLands.y);
 160        }
 1161    }
 162
 163    private void PanelOpenEvent(LandWithAccess[] lands)
 164    {
 4165        Vector2Int totalLands = GetAmountOfLandsOwnedAndOperator(lands);
 4166        BIWAnalytics.PlayerOpenPanel(totalLands.x, totalLands.y);
 4167    }
 168
 169    /// <summary>
 170    /// This counts the amount of lands that the user own and the amount of lands that the user operate
 171    /// </summary>
 172    /// <param name="lands"></param>
 173    /// <returns>Vector2: X = amount of owned lands, Y = amount of operator lands</returns>
 174    private Vector2Int GetAmountOfLandsOwnedAndOperator(LandWithAccess[] lands)
 175    {
 5176        int ownedLandsCount = 0;
 5177        int operatorLandsCount = 0;
 18178        foreach (var land in lands)
 179        {
 4180            if (land.role == LandRole.OWNER)
 4181                ownedLandsCount++;
 182            else
 0183                operatorLandsCount++;
 184        }
 5185        return new Vector2Int(ownedLandsCount, operatorLandsCount);
 186    }
 187
 188    private void SetView()
 189    {
 16190        scenesViewController.AddListener((IDeployedSceneListener) view);
 16191        scenesViewController.AddListener((IProjectSceneListener) view);
 16192    }
 193
 194    private void FetchLandsAndScenes(float landCacheTime = CACHE_TIME_LAND, float scenesCacheTime = CACHE_TIME_SCENES)
 195    {
 4196        if (isFetching)
 0197            return;
 198
 4199        isFetching = true;
 200
 4201        var address = UserProfile.GetOwnUserProfile().ethAddress;
 4202        var network = KernelConfig.i.Get().network;
 203
 204#if UNITY_EDITOR
 205        // NOTE: to be able to test in editor without getting a profile we hardcode an address here
 4206        if (string.IsNullOrEmpty(address))
 207        {
 0208            address = TESTING_ETH_ADDRESS;
 0209            network = TESTING_TLD;
 0210            DataStore.i.playerRealm.Set(new CurrentRealmModel()
 211            {
 212                domain = $"https://peer-lb.decentraland.{TESTING_TLD}",
 213                contentServerUrl = $"https://peer-lb.decentraland.{TESTING_TLD}/content",
 214            });
 215        }
 216#endif
 217
 4218        sectionsController.SetFetchingDataStart();
 219
 4220        fetchLandPromise = DeployedScenesFetcher.FetchLandsFromOwner(catalyst, theGraph, address, network, landCacheTime
 4221        fetchLandPromise
 222            .Then(LandsFetched)
 223            .Catch(LandsFetchedError);
 4224    }
 225
 226    internal void LandsFetchedError(string error)
 227    {
 1228        isFetching = false;
 1229        sectionsController.SetFetchingDataEnd();
 1230        landsController.SetLands(new LandWithAccess[] { });
 1231        scenesViewController.SetScenes(new ISceneData[] { });
 1232        Debug.LogError(error);
 1233    }
 234
 235    internal void LandsFetched(LandWithAccess[] lands)
 236    {
 1237        DataStore.i.builderInWorld.landsWithAccess.Set(lands.ToArray(), true);
 1238        sectionsController.SetFetchingDataEnd();
 1239        isFetching = false;
 240
 241        try
 242        {
 2243            var scenes = lands.Where(land => land.scenes != null && land.scenes.Count > 0)
 2244                              .Select(land => land.scenes.Where(scene => !scene.isEmpty).Select(scene => (ISceneData)new
 0245                              .Aggregate((i, j) => i.Concat(j))
 246                              .ToArray();
 247
 0248            if (sendPlayerOpenPanelEvent)PanelOpenEvent(lands);
 0249            landsController.SetLands(lands);
 0250            scenesViewController.SetScenes(scenes);
 0251        }
 1252        catch (Exception e)
 253        {
 1254            landsController.SetLands(lands);
 1255            scenesViewController.SetScenes(new ISceneData[] { });
 1256        }
 1257    }
 258
 259    internal void GoToCoords(Vector2Int coords)
 260    {
 1261        WebInterface.GoTo(coords.x, coords.y);
 1262        SetVisibility(false);
 1263        OnJumpInOrEdit?.Invoke();
 1264    }
 265
 0266    private void OpenUrl(string url) { WebInterface.OpenURL(url); }
 267
 268    internal void OnGoToEditScene(Vector2Int coords)
 269    {
 1270        bool isGoingToTeleport = BIWTeleportAndEdit.TeleportAndEdit(coords);
 1271        if (isGoingToTeleport)
 272        {
 1273            SetVisibility(false);
 274        }
 1275        OnJumpInOrEdit?.Invoke();
 1276    }
 277
 278    private void StartFetchInterval()
 279    {
 4280        if (fetchDataInterval != null)
 281        {
 0282            StopFetchInterval();
 283        }
 284
 4285        fetchDataInterval = CoroutineStarter.Start(RefreshDataInterval());
 4286    }
 287
 288    private void StopFetchInterval()
 289    {
 19290        CoroutineStarter.Stop(fetchDataInterval);
 19291        fetchDataInterval = null;
 19292    }
 293
 294    IEnumerator RefreshDataInterval()
 295    {
 0296        while (true)
 297        {
 4298            yield return WaitForSecondsCache.Get(REFRESH_INTERVAL);
 0299            FetchLandsAndScenes();
 300        }
 301    }
 302
 303    private void OnSceneUnpublished(PublishSceneResultPayload current, PublishSceneResultPayload previous)
 304    {
 0305        if (current.ok)
 306        {
 0307            FetchLandsAndScenes(CACHE_TIME_LAND, 0);
 308        }
 0309    }
 310}