< 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:79
Uncovered lines:24
Coverable lines:103
Total lines:234
Line coverage:76.6% (79 of 103)
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%3.333066.67%
OnHide()0%110100%
OnSetLands(...)0%8.018095.24%
OnSearchResult(...)0%9.839078.26%
PoolView(...)0%110100%
GetPooledView()0%220100%
SetEmptyOrLoading()0%6.984042.86%
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.Configuration;
 5using DCL.Helpers;
 6using DCL.Interface;
 7using UnityEngine;
 8using Object = UnityEngine.Object;
 9
 10namespace DCL.Builder
 11{
 12    internal class SectionLandController : SectionBase, ILandsListener, ISectionOpenURLRequester, ISectionGotoCoordsRequ
 13    {
 14        public const string VIEW_PREFAB_PATH = "BuilderProjectsPanelMenuSections/SectionLandsView";
 15
 16        private const string BUILDER_LAND_URL_FORMAT = "https://builder.decentraland.org/land/{0}";
 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
 66        protected override void OnShow()
 67        {
 268            view.SetActive(true);
 269            if (landElementViews.Count == 0)
 70            {
 271                SetEmptyOrLoading();
 272            }
 73            else
 74            {
 075                OnNotEmptyContent?.Invoke();
 76            }
 077        }
 78
 279        protected override void OnHide() { view.SetActive(false); }
 80
 81        void ILandsListener.OnSetLands(LandWithAccess[] lands)
 82        {
 583            if (lands == null || lands.Length == 0)
 84            {
 185                SetEmptyOrLoading();
 86            }
 87
 588            if (lands == null)
 089                return;
 90
 591            List<LandElementView> toRemove = landElementViews.Values
 792                                                             .Where(landElementView => lands.All(land => land.id != land
 93                                                             .ToList();
 94
 1495            for (int i = 0; i < toRemove.Count; i++)
 96            {
 297                landElementViews.Remove(toRemove[i].GetId());
 298                PoolView(toRemove[i]);
 99            }
 100
 20101            for (int i = 0; i < lands.Length; i++)
 102            {
 5103                if (!landElementViews.TryGetValue(lands[i].id, out LandElementView landElementView))
 104                {
 3105                    landElementView = GetPooledView();
 3106                    landElementViews.Add(lands[i].id, landElementView);
 107                }
 108
 5109                bool isEstate = lands[i].type == LandType.ESTATE;
 5110                landElementView.Setup(lands[i]);
 5111                landElementView.SetThumbnail(GetLandThumbnailUrl(lands[i], isEstate));
 112            }
 113
 10114            landSearchHandler.SetSearchableList(landElementViews.Values.Select(scene => scene.searchInfo).ToList());
 5115        }
 116
 117        private void OnSearchResult(List<ISearchInfo> searchInfoLands)
 118        {
 5119            if (landElementViews == null)
 0120                return;
 121
 5122            using (var iterator = landElementViews.GetEnumerator())
 123            {
 10124                while (iterator.MoveNext())
 125                {
 5126                    iterator.Current.Value.SetParent(view.GetLandElementsContainer());
 5127                    iterator.Current.Value.gameObject.SetActive(false);
 128                }
 5129            }
 130
 20131            for (int i = 0; i < searchInfoLands.Count; i++)
 132            {
 5133                if (!landElementViews.TryGetValue(searchInfoLands[i].id, out LandElementView landView))
 134                    continue;
 135
 5136                landView.gameObject.SetActive(true);
 5137                landView.transform.SetSiblingIndex(i);
 138            }
 139
 5140            if (landElementViews.Count == 0)
 141            {
 1142                SetEmptyOrLoading();
 1143            }
 4144            else if (searchInfoLands.Count == 0)
 145            {
 0146                view.SetNoSearchResult();
 0147                OnNotEmptyContent?.Invoke();
 0148            }
 149            else
 150            {
 4151                view.SetFilled();
 4152                OnNotEmptyContent?.Invoke();
 153            }
 0154        }
 155
 156        private void PoolView(LandElementView landView)
 157        {
 5158            landView.OnSettingsPressed -= OnSettingsPressed;
 5159            landView.OnJumpInPressed -= OnJumpInPressed;
 5160            landView.OnOpenInDappPressed -= OnOpenInDappPressed;
 5161            landView.OnEditorPressed -= OnEditPressed;
 162
 5163            landView.SetActive(false);
 5164            landElementViewsPool.Enqueue(landView);
 5165        }
 166
 167        private LandElementView GetPooledView()
 168        {
 169            LandElementView landView;
 170
 3171            if (landElementViewsPool.Count > 0)
 172            {
 2173                landView = landElementViewsPool.Dequeue();
 2174            }
 175            else
 176            {
 1177                landView = Object.Instantiate(view.GetLandElementeBaseView(), view.GetLandElementsContainer());
 178            }
 179
 3180            landView.OnSettingsPressed += OnSettingsPressed;
 3181            landView.OnJumpInPressed += OnJumpInPressed;
 3182            landView.OnOpenInDappPressed += OnOpenInDappPressed;
 3183            landView.OnEditorPressed += OnEditPressed;
 184
 3185            return landView;
 186        }
 187
 188        private void SetEmptyOrLoading()
 189        {
 4190            if (isLoading)
 191            {
 0192                view.SetLoading();
 0193                OnNotEmptyContent?.Invoke();
 0194            }
 195            else
 196            {
 4197                view.SetEmpty();
 4198                OnEmptyContent?.Invoke();
 199            }
 0200        }
 201
 202        private string GetLandThumbnailUrl(LandWithAccess land, bool isEstate)
 203        {
 5204            if (land == null)
 0205                return null;
 206
 207            const int width = 100;
 208            const int height = 100;
 209            const int sizeFactorParcel = 15;
 210            const int sizeFactorEstate = 35;
 211
 5212            if (!isEstate)
 213            {
 5214                return MapUtils.GetMarketPlaceThumbnailUrl(new[] { land.baseCoords }, width, height, sizeFactorParcel);
 215            }
 216
 0217            return MapUtils.GetMarketPlaceThumbnailUrl(land.parcels, width, height, sizeFactorEstate);
 218        }
 219
 220        private void OnSettingsPressed(string landId)
 221        {
 222            //NOTE: for MVP we are redirecting user to Builder's page
 0223            WebInterface.OpenURL(string.Format(BUILDER_LAND_URL_FORMAT, landId));
 0224        }
 225
 0226        private void OnOpenInDappPressed(string landId) { OnRequestOpenUrl?.Invoke(string.Format(BUILDER_LAND_URL_FORMAT
 227
 0228        private void OnJumpInPressed(Vector2Int coords) { OnRequestGoToCoords?.Invoke(coords); }
 229
 0230        private void OnOpenMarketplacePressed() { OnRequestOpenUrl?.Invoke(BIWSettings.MARKETPLACE_URL); }
 231
 0232        private void OnEditPressed(Vector2Int coords) { OnRequestEditSceneAtCoords?.Invoke(coords); }
 233    }
 234}