< Summary

Class:DCL.Builder.SectionLandController
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/SectionController/MenuSections/SectionLandController.cs
Covered lines:76
Uncovered lines:18
Coverable lines:94
Total lines:219
Line coverage:80.8% (76 of 94)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SectionLandController(...)0%110100%
SectionLandController()0%2100%
SetViewContainer(...)0%2100%
Dispose()0%330100%
OnShow()0%110100%
OnHide()0%110100%
OnSetLands(...)0%8.018095.24%
OnSearchResult(...)0%7.147085.71%
PoolView(...)0%110100%
GetPooledView()0%220100%
SetEmptyOrLoading()0%2.262060%
GetLandThumbnailUrl(...)0%3.583060%
OnSettingsPressed(...)0%2100%
OnOpenInDappPressed(...)0%6200%
OnJumpInPressed(...)0%6200%
OnOpenMarketplacePressed()0%6200%
OnEditPressed(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/SectionController/MenuSections/SectionLandController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using DCL.Helpers;
 5using DCL.Interface;
 6using UnityEngine;
 7using Object = UnityEngine.Object;
 8
 9namespace DCL.Builder
 10{
 11    internal class SectionLandController : SectionBase, ILandsListener, ISectionOpenURLRequester, ISectionGotoCoordsRequ
 12    {
 13        public const string VIEW_PREFAB_PATH = "BuilderProjectsPanelMenuSections/SectionLandsView";
 14
 15        private const string BUILDER_LAND_URL_FORMAT = "https://builder.decentraland.org/land/{0}";
 16        private const string MARKETPLACE_URL = "https://market.decentraland.org/";
 17
 18        public event Action<string> OnRequestOpenUrl;
 19        public event Action<Vector2Int> OnRequestGoToCoords;
 20        public event Action<Vector2Int> OnRequestEditSceneAtCoords;
 21
 022        public override ISectionSearchHandler searchHandler => landSearchHandler;
 23
 24        private readonly SectionLandView view;
 25
 326        private readonly ISectionSearchHandler landSearchHandler = new SectionSearchHandler();
 327        private readonly Dictionary<string, LandElementView> landElementViews = new Dictionary<string, LandElementView>(
 328        private readonly Queue<LandElementView> landElementViewsPool = new Queue<LandElementView>();
 29
 030        public SectionLandController() : this(
 31            Object.Instantiate(Resources.Load<SectionLandView>(VIEW_PREFAB_PATH))
 032        ) { }
 33
 334        public SectionLandController(SectionLandView view)
 35        {
 336            this.view = view;
 337            PoolView(view.GetLandElementeBaseView());
 38
 339            view.OnOpenMarketplaceRequested += OnOpenMarketplacePressed;
 340            landSearchHandler.OnResult += OnSearchResult;
 341        }
 42
 043        public override void SetViewContainer(Transform viewContainer) { view.SetParent(viewContainer); }
 44
 45        public override void Dispose()
 46        {
 347            view.OnOpenMarketplaceRequested -= OnOpenMarketplacePressed;
 348            landSearchHandler.OnResult -= OnSearchResult;
 49
 350            using (var iterator = landElementViews.GetEnumerator())
 51            {
 452                while (iterator.MoveNext())
 53                {
 154                    iterator.Dispose();
 55                }
 356            }
 57
 658            while (landElementViewsPool.Count > 0)
 59            {
 360                landElementViewsPool.Dequeue().Dispose();
 61            }
 62
 363            view.Dispose();
 364        }
 65
 466        protected override void OnShow() { view.SetActive(true); }
 67
 268        protected override void OnHide() { view.SetActive(false); }
 69
 70        void ILandsListener.OnSetLands(LandWithAccess[] lands)
 71        {
 572            if (lands == null || lands.Length == 0)
 73            {
 174                SetEmptyOrLoading();
 75            }
 76
 577            if (lands == null)
 078                return;
 79
 580            List<LandElementView> toRemove = landElementViews.Values
 781                                                             .Where(landElementView => lands.All(land => land.id != land
 82                                                             .ToList();
 83
 1484            for (int i = 0; i < toRemove.Count; i++)
 85            {
 286                landElementViews.Remove(toRemove[i].GetId());
 287                PoolView(toRemove[i]);
 88            }
 89
 2090            for (int i = 0; i < lands.Length; i++)
 91            {
 592                if (!landElementViews.TryGetValue(lands[i].id, out LandElementView landElementView))
 93                {
 394                    landElementView = GetPooledView();
 395                    landElementViews.Add(lands[i].id, landElementView);
 96                }
 97
 598                bool isEstate = lands[i].type == LandType.ESTATE;
 599                landElementView.Setup(lands[i]);
 5100                landElementView.SetThumbnail(GetLandThumbnailUrl(lands[i], isEstate));
 101            }
 102
 10103            landSearchHandler.SetSearchableList(landElementViews.Values.Select(scene => scene.searchInfo).ToList());
 5104        }
 105
 106        private void OnSearchResult(List<ISearchInfo> searchInfoLands)
 107        {
 5108            if (landElementViews == null)
 0109                return;
 110
 5111            using (var iterator = landElementViews.GetEnumerator())
 112            {
 10113                while (iterator.MoveNext())
 114                {
 5115                    iterator.Current.Value.SetParent(view.GetLandElementsContainer());
 5116                    iterator.Current.Value.gameObject.SetActive(false);
 117                }
 5118            }
 119
 20120            for (int i = 0; i < searchInfoLands.Count; i++)
 121            {
 5122                if (!landElementViews.TryGetValue(searchInfoLands[i].id, out LandElementView landView))
 123                    continue;
 124
 5125                landView.gameObject.SetActive(true);
 5126                landView.transform.SetSiblingIndex(i);
 127            }
 128
 5129            if (landElementViews.Count == 0)
 130            {
 1131                SetEmptyOrLoading();
 1132            }
 4133            else if (searchInfoLands.Count == 0)
 134            {
 0135                view.SetNoSearchResult();
 0136            }
 137            else
 138            {
 4139                view.SetFilled();
 140            }
 4141        }
 142
 143        private void PoolView(LandElementView landView)
 144        {
 5145            landView.OnSettingsPressed -= OnSettingsPressed;
 5146            landView.OnJumpInPressed -= OnJumpInPressed;
 5147            landView.OnOpenInDappPressed -= OnOpenInDappPressed;
 5148            landView.OnEditorPressed -= OnEditPressed;
 149
 5150            landView.SetActive(false);
 5151            landElementViewsPool.Enqueue(landView);
 5152        }
 153
 154        private LandElementView GetPooledView()
 155        {
 156            LandElementView landView;
 157
 3158            if (landElementViewsPool.Count > 0)
 159            {
 2160                landView = landElementViewsPool.Dequeue();
 2161            }
 162            else
 163            {
 1164                landView = Object.Instantiate(view.GetLandElementeBaseView(), view.GetLandElementsContainer());
 165            }
 166
 3167            landView.OnSettingsPressed += OnSettingsPressed;
 3168            landView.OnJumpInPressed += OnJumpInPressed;
 3169            landView.OnOpenInDappPressed += OnOpenInDappPressed;
 3170            landView.OnEditorPressed += OnEditPressed;
 171
 3172            return landView;
 173        }
 174
 175        private void SetEmptyOrLoading()
 176        {
 2177            if (isLoading)
 178            {
 0179                view.SetLoading();
 0180            }
 181            else
 182            {
 2183                view.SetEmpty();
 184            }
 2185        }
 186
 187        private string GetLandThumbnailUrl(LandWithAccess land, bool isEstate)
 188        {
 5189            if (land == null)
 0190                return null;
 191
 192            const int width = 100;
 193            const int height = 100;
 194            const int sizeFactorParcel = 15;
 195            const int sizeFactorEstate = 35;
 196
 5197            if (!isEstate)
 198            {
 5199                return MapUtils.GetMarketPlaceThumbnailUrl(new[] { land.baseCoords }, width, height, sizeFactorParcel);
 200            }
 201
 0202            return MapUtils.GetMarketPlaceThumbnailUrl(land.parcels, width, height, sizeFactorEstate);
 203        }
 204
 205        private void OnSettingsPressed(string landId)
 206        {
 207            //NOTE: for MVP we are redirecting user to Builder's page
 0208            WebInterface.OpenURL(string.Format(BUILDER_LAND_URL_FORMAT, landId));
 0209        }
 210
 0211        private void OnOpenInDappPressed(string landId) { OnRequestOpenUrl?.Invoke(string.Format(BUILDER_LAND_URL_FORMAT
 212
 0213        private void OnJumpInPressed(Vector2Int coords) { OnRequestGoToCoords?.Invoke(coords); }
 214
 0215        private void OnOpenMarketplacePressed() { OnRequestOpenUrl?.Invoke(MARKETPLACE_URL); }
 216
 0217        private void OnEditPressed(Vector2Int coords) { OnRequestEditSceneAtCoords?.Invoke(coords); }
 218    }
 219}