< Summary

Class:PlacesSubSectionComponentView
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/PlacesSubSection/PlacesSubSectionMenu/PlacesSubSectionComponentView.cs
Covered lines:80
Uncovered lines:43
Coverable lines:123
Total lines:295
Line coverage:65% (80 of 123)
Covered branches:0
Total branches:0
Covered methods:20
Total methods:34
Method coverage:58.8% (20 of 34)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlacesSubSectionComponentView()0%110100%
SetAllAsLoading()0%2100%
SetShowMoreButtonActive(...)0%2100%
SetPOICoords(...)0%2100%
Awake()0%110100%
Start()0%22090%
LoadSortByDropdown()0%110100%
SortByDropdownValueChanged(...)0%12300%
OnEnable()0%3.473062.5%
Dispose()0%440100%
ConfigurePools()0%110100%
RefreshControl()0%2100%
SetPlaces(...)0%220100%
SetPlacesAsLoading(...)0%220100%
AddPlaces(...)0%110100%
AddPlacesAsync()0%18.0612065.22%
SetActive(...)0%330100%
SetShowMorePlacesButtonActive(...)0%110100%
ShowPlaceModal(...)0%110100%
HidePlaceModal()0%220100%
RestartScrollViewPosition()0%2100%
SetSortDropdownValue(...)0%110100%
DeselectButtons()0%6200%
SetPlaceCategories(...)0%6200%
SetResultCounter(...)0%2100%
OnCategoryButtonClicked(...)0%20400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/PlacesSubSection/PlacesSubSectionMenu/PlacesSubSectionComponentView.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using DCL.Tasks;
 4using System;
 5using System.Collections.Generic;
 6using System.Threading;
 7using UnityEngine;
 8using UnityEngine.UI;
 9using MainScripts.DCL.Controllers.HotScenes;
 10using TMPro;
 11using UnityEngine.EventSystems;
 12
 13public class PlacesSubSectionComponentView : BaseComponentView, IPlacesSubSectionComponentView
 14{
 15    internal const string PLACE_CARDS_POOL_NAME = "Places_PlaceCardsPool";
 16    private const int PLACE_CARDS_POOL_PREWARM = 20;
 17    private const string MOST_ACTIVE_FILTER_ID = "most_active";
 18    private const string MOST_ACTIVE_FILTER_TEXT = "Most active";
 19    private const string BEST_FILTER_ID = "like_rate";
 20    private const string BEST_FILTER_TEXT = "Best";
 21
 5722    private readonly CancellationTokenSource disposeCts = new ();
 5723    private CancellationTokenSource setPlacesCts = new ();
 5724    private readonly List<PlaceCategoryButton> placeCategoryButtons = new ();
 25
 26    private List<string> poiCoords;
 27
 28    [Header("Assets References")]
 29    [SerializeField] internal PlaceCardComponentView placeCardPrefab;
 30    [SerializeField] internal PlaceCardComponentView placeCardModalPrefab;
 31    [SerializeField] internal PlaceCategoryButton placeCategoryButtonPrefab;
 32
 33    [Header("Prefab References")]
 34    [SerializeField] internal ScrollRect scrollView;
 35    [SerializeField] internal GridContainerComponentView places;
 36    [SerializeField] internal GameObject placesLoading;
 37    [SerializeField] internal GameObject placesNoDataContainer;
 38    [SerializeField] internal Color[] friendColors = null;
 39    [SerializeField] internal GameObject showMorePlacesButtonContainer;
 40    [SerializeField] internal ButtonComponentView showMorePlacesButton;
 41    [SerializeField] internal DropdownComponentView sortByDropdown;
 42    [SerializeField] internal Transform placeCategoriesGrid;
 43    [SerializeField] internal TMP_Text totalResultsCounter;
 44
 45    [SerializeField] private Canvas canvas;
 46
 47    internal PlaceCardComponentView placeModal;
 48    internal Pool placeCardsPool;
 49    private Canvas placesCanvas;
 50
 051    public Color[] currentFriendColors => friendColors;
 52
 053    public int currentPlacesPerRow => places.currentItemsPerRow;
 54
 055    public void SetAllAsLoading() => SetPlacesAsLoading(true);
 056    public void SetShowMoreButtonActive(bool isActive) => SetShowMorePlacesButtonActive(isActive);
 57
 58    public void SetPOICoords(List<string> poiList) =>
 059        poiCoords = poiList;
 60
 061    public int CurrentTilesPerRow => currentPlacesPerRow;
 062    public int CurrentGoingTilesPerRow { get; }
 14963    public string filter { get; private set; }
 14964    public string sort { get; private set; }
 65
 66    public event Action OnReady;
 67    public event Action<PlaceCardComponentModel> OnInfoClicked;
 68    public event Action<IHotScenesController.PlaceInfo> OnJumpInClicked;
 69    public event Action<string, bool?> OnVoteChanged;
 70    public event Action<string, bool> OnFavoriteClicked;
 71    public event Action<IFriendTrackerHandler> OnFriendHandlerAdded;
 72    public event Action OnPlacesSubSectionEnable;
 73    public event Action OnFilterChanged;
 74    public event Action OnSortingChanged;
 75    public event Action OnShowMorePlacesClicked;
 76
 77    public override void Awake()
 78    {
 5479        base.Awake();
 5480        placesCanvas = places.GetComponent<Canvas>();
 5481    }
 82
 83    public void Start()
 84    {
 5385        placeModal = PlacesAndEventsCardsFactory.GetPlaceCardTemplateHiddenLazy(placeCardModalPrefab);
 86
 5387        places.RemoveItems();
 5388        LoadSortByDropdown();
 89
 5390        showMorePlacesButton.onClick.RemoveAllListeners();
 5391        showMorePlacesButton.onClick.AddListener(() => OnShowMorePlacesClicked?.Invoke());
 5392        sortByDropdown.OnOptionSelectionChanged += SortByDropdownValueChanged;
 5393        filter = "";
 5394        SetSortDropdownValue(MOST_ACTIVE_FILTER_ID, MOST_ACTIVE_FILTER_TEXT, false);
 5395        OnReady?.Invoke();
 096    }
 97
 98    private void LoadSortByDropdown()
 99    {
 53100        List<ToggleComponentModel> sortingMethodsToAdd = new List<ToggleComponentModel>
 101        {
 102            new () { id = MOST_ACTIVE_FILTER_ID, text = MOST_ACTIVE_FILTER_TEXT, isOn = true, isTextActive = true, chang
 103            new () { id = BEST_FILTER_ID, text = BEST_FILTER_TEXT, isOn = false, isTextActive = true, changeTextColorOnS
 104        };
 105
 53106        sortByDropdown.SetTitle(sortingMethodsToAdd[0].text);
 53107        sortByDropdown.SetOptions(sortingMethodsToAdd);
 53108    }
 109
 110    private void SortByDropdownValueChanged(bool isOn, string optionId, string optionName)
 111    {
 0112        if (!isOn)
 0113            return;
 114
 0115        SetSortDropdownValue(optionId, optionName, false);
 0116        OnSortingChanged?.Invoke();
 0117    }
 118
 119    public override void OnEnable()
 120    {
 192121        foreach (PlaceCategoryButton categoryBtn in placeCategoryButtons)
 0122            categoryBtn.SetStatus(false);
 123
 96124        filter = "";
 96125        SetSortDropdownValue(MOST_ACTIVE_FILTER_ID, MOST_ACTIVE_FILTER_TEXT, false);
 96126        OnPlacesSubSectionEnable?.Invoke();
 0127    }
 128
 129    public override void Dispose()
 130    {
 116131        base.Dispose();
 116132        disposeCts?.SafeCancelAndDispose();
 116133        setPlacesCts?.SafeCancelAndDispose();
 134
 116135        showMorePlacesButton.onClick.RemoveAllListeners();
 136
 116137        sortByDropdown.OnOptionSelectionChanged -= SortByDropdownValueChanged;
 138
 116139        places.Dispose();
 140
 116141        if (placeModal != null)
 142        {
 76143            placeModal.Dispose();
 76144            Destroy(placeModal.gameObject);
 145        }
 116146    }
 147
 148    public void ConfigurePools() =>
 17149        placeCardsPool = PlacesAndEventsCardsFactory.GetCardsPoolLazy(PLACE_CARDS_POOL_NAME, placeCardPrefab, PLACE_CARD
 150
 151    public override void RefreshControl() =>
 0152        places.RefreshControl();
 153
 154    public void SetPlaces(List<PlaceCardComponentModel> places)
 155    {
 4156        SetPlacesAsLoading(false);
 4157        placesNoDataContainer.SetActive(places.Count == 0);
 158
 4159        placeCardsPool.ReleaseAll();
 160
 4161        this.places.ExtractItems();
 4162        this.places.RemoveItems();
 163
 4164        setPlacesCts?.SafeCancelAndDispose();
 4165        setPlacesCts = CancellationTokenSource.CreateLinkedTokenSource(disposeCts.Token);
 4166        AddPlacesAsync(places, setPlacesCts.Token).Forget();
 4167    }
 168
 169    public void SetPlacesAsLoading(bool isVisible)
 170    {
 6171        placesCanvas.enabled = !isVisible;
 172
 6173        placesLoading.SetActive(isVisible);
 174
 6175        if (isVisible)
 1176            placesNoDataContainer.SetActive(false);
 6177    }
 178
 179    public void AddPlaces(List<PlaceCardComponentModel> places) =>
 4180        AddPlacesAsync(places, disposeCts.Token).Forget();
 181
 182    private async UniTask AddPlacesAsync(List<PlaceCardComponentModel> places, CancellationToken cancellationToken)
 183    {
 58184        foreach (PlaceCardComponentModel place in places)
 185        {
 24186            PlaceCardComponentView placeCard = PlacesAndEventsCardsFactory.CreateConfiguredPlaceCard(placeCardsPool, pla
 187
 24188            var isPoi = false;
 189
 24190            if (poiCoords != null)
 191            {
 0192                foreach (Vector2Int placeParcel in place.parcels)
 193                {
 0194                    if (!poiCoords.Contains($"{placeParcel.x},{placeParcel.y}"))
 195                        continue;
 196
 0197                    isPoi = true;
 0198                    break;
 199                }
 200            }
 201
 24202            placeCard.SetIsPOI(isPoi);
 203
 24204            OnFriendHandlerAdded?.Invoke(placeCard.friendsHandler);
 205
 24206            this.places.AddItem(placeCard);
 207
 72208            await UniTask.NextFrame(cancellationToken);
 209        }
 210
 2211        this.places.SetItemSizeForModel();
 2212        await placeCardsPool.PrewarmAsync(places.Count, cancellationToken);
 2213    }
 214
 215    public void SetActive(bool isActive)
 216    {
 124217        if (canvas.enabled == isActive)
 10218            return;
 114219        canvas.enabled = isActive;
 220
 114221        if (isActive)
 42222            OnEnable();
 223        else
 72224            OnDisable();
 72225    }
 226
 227    public void SetShowMorePlacesButtonActive(bool isActive) =>
 2228        showMorePlacesButtonContainer.gameObject.SetActive(isActive);
 229
 230    public void ShowPlaceModal(PlaceCardComponentModel placeInfo)
 231    {
 1232        placeModal.Show();
 1233        PlacesCardsConfigurator.Configure(placeModal, placeInfo, OnInfoClicked, OnJumpInClicked, OnVoteChanged, OnFavori
 1234    }
 235
 236    public void HidePlaceModal()
 237    {
 1238        if (placeModal != null)
 1239            placeModal.Hide();
 1240    }
 241
 242    public void RestartScrollViewPosition() =>
 0243        scrollView.verticalNormalizedPosition = 1;
 244
 245    private void SetSortDropdownValue(string id, string title, bool notify)
 246    {
 149247        sort = id;
 149248        sortByDropdown.SetTitle(title);
 149249        sortByDropdown.SelectOption(id, notify);
 149250    }
 251
 252    private static void DeselectButtons()
 253    {
 0254        if (EventSystem.current == null)
 0255            return;
 256
 0257        EventSystem.current.SetSelectedGameObject(null);
 0258    }
 259
 260    public void SetPlaceCategories(List<(string id, string nameToShow)> placeCategories)
 261    {
 0262        foreach (var category in placeCategories)
 263        {
 0264            var categoryButton = Instantiate(placeCategoryButtonPrefab, placeCategoriesGrid);
 0265            categoryButton.SetCategory(category.id, category.nameToShow);
 0266            categoryButton.SetStatus(false);
 0267            categoryButton.OnClick += OnCategoryButtonClicked;
 0268            placeCategoryButtons.Add(categoryButton);
 269        }
 270
 0271        placeModal.SetAllPlaceCategories(placeCategories);
 0272    }
 273
 274    public void SetResultCounter(int totalResults) =>
 0275        totalResultsCounter.text = $"Result ({totalResults})";
 276
 277    private void OnCategoryButtonClicked(string category, bool isSelected)
 278    {
 0279        foreach (PlaceCategoryButton categoryBtn in placeCategoryButtons)
 0280            categoryBtn.SetStatus(category == categoryBtn.currenCategory && isSelected);
 281
 0282        if (isSelected)
 283        {
 0284            filter = $"categories={category}";
 0285            SetSortDropdownValue(BEST_FILTER_ID, BEST_FILTER_TEXT, false);
 286        }
 287        else
 288        {
 0289            filter = "";
 0290            SetSortDropdownValue(MOST_ACTIVE_FILTER_ID, MOST_ACTIVE_FILTER_TEXT, false);
 291        }
 292
 0293        OnFilterChanged?.Invoke();
 0294    }
 295}