< Summary

Class:RealmSelectorComponentView
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/MainMenu/RealmViewer/RealmSelectorComponentView.cs
Covered lines:95
Uncovered lines:18
Coverable lines:113
Total lines:296
Line coverage:84% (95 of 113)
Covered branches:0
Total branches:0
Covered methods:15
Total methods:15
Method coverage:100% (15 of 15)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
RealmSelectorComponentView()0%110100%
Awake()0%770100%
Configure(...)0%110100%
RefreshControl()0%2.032080%
Show(...)0%110100%
Hide(...)0%110100%
Dispose()0%660100%
SetCurrentRealm(...)0%3.13077.78%
SetAvailableRealms(...)0%440100%
ApplySorting(...)0%129066.67%
RefreshSortingArrows()0%13130100%
RefreshRowColours()0%440100%
CloseModal()0%110100%
OnCloseActionTriggered(...)0%110100%
ConfigureRealmsPool()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/MainMenu/RealmViewer/RealmSelectorComponentView.cs

#LineLine coverage
 1using DCL;
 2using DCL.Helpers;
 3using DCL.Interface;
 4using System.Collections.Generic;
 5using System.Linq;
 6using DCL.Social.Friends;
 7using TMPro;
 8using UIComponents.Scripts.Components.Text;
 9using UnityEngine;
 10using UnityEngine.EventSystems;
 11using UnityEngine.UI;
 12
 13public enum RealmsSorting
 14{
 15    BY_NAME,
 16    BY_NUMBER_OF_PLAYERS
 17}
 18
 19public enum RealmsSortingDirection
 20{
 21    ASC,
 22    DESC
 23}
 24
 25public interface IRealmSelectorComponentView
 26{
 27    /// <summary>
 28    /// Set the current realm label.
 29    /// </summary>
 30    /// <param name="realm">Current realm.</param>
 31    void SetCurrentRealm(string realm);
 32
 33    /// <summary>
 34    /// Set the available realms component with a list of realms.
 35    /// </summary>
 36    /// <param name="realms">List of realms (model) to be loaded.</param>
 37    void SetAvailableRealms(List<RealmRowComponentModel> realms);
 38}
 39
 40public class RealmSelectorComponentView : BaseComponentView, IRealmSelectorComponentView, IComponentModelConfig<RealmSel
 41{
 42    private const string REALM_DOCS_LINK = "https://docs.decentraland.org/player/general/meet-with-friends/";
 43    internal const string REALMS_POOL_NAME = "RealmSelector_RealmRowsPool";
 44    internal const int REALMS_POOL_PREWARM = 20;
 45
 46    [Header("Assets References")]
 47    [SerializeField] internal RealmRowComponentView realmRowPrefab;
 48
 49    [Header("Prefab References")]
 50    [SerializeField] internal TMP_Text currentRealmText;
 51    [SerializeField] internal ButtonComponentView sortByNameButton;
 52    [SerializeField] internal Image sortByNameArrowUp;
 53    [SerializeField] internal Image sortByNameArrowDown;
 54    [SerializeField] internal ButtonComponentView sortByNumberOfPlayersButton;
 55    [SerializeField] internal Image sortByNumberOfPlayersArrowUp;
 56    [SerializeField] internal Image sortByNumberOfPlayersArrowDown;
 57    [SerializeField] internal GridContainerComponentView availableRealms;
 58    [SerializeField] internal Button modalBackgroundButton;
 59    [SerializeField] internal ButtonComponentView closeCardButton;
 60    [SerializeField] internal InputAction_Trigger closeAction;
 61    [SerializeField] internal TMPTextHyperLink realmsInformationLink;
 62    [SerializeField] internal RectTransform rootTransform;
 63
 64    [Header("Configuration")]
 65    [SerializeField] internal RealmSelectorComponentModel model;
 66    [SerializeField] internal Color colorForEvenRows;
 67    [SerializeField] internal Color colorForOddRows;
 68    [SerializeField] internal Color colorForFocusedRows;
 69    [SerializeField] internal Color colorForActiveSortingArrow;
 70    [SerializeField] internal Color colorForUnactiveSortingArrow;
 71    [SerializeField] internal Color[] friendColors = null;
 72
 73    internal Pool realmsPool;
 1374    internal RealmsSorting currentSorting = RealmsSorting.BY_NUMBER_OF_PLAYERS;
 1375    internal RealmsSortingDirection currentSortingDirection = RealmsSortingDirection.DESC;
 76    internal RealmTrackerController friendsTrackerController;
 77
 78    public override void Awake()
 79    {
 1280        base.Awake();
 81
 1282        friendsTrackerController = new RealmTrackerController(Environment.i.serviceLocator.Get<IFriendsController>(), fr
 83
 1284        if (sortByNameButton != null)
 1285            sortByNameButton.onClick.AddListener(() =>
 86            {
 087                ApplySorting(
 88                    RealmsSorting.BY_NAME,
 89                    currentSorting != RealmsSorting.BY_NAME || currentSortingDirection != RealmsSortingDirection.ASC ? R
 90
 091                EventSystem.current?.SetSelectedGameObject(null);
 092            });
 93
 1294        if (sortByNumberOfPlayersButton != null)
 1295            sortByNumberOfPlayersButton.onClick.AddListener(() =>
 96            {
 097                ApplySorting(
 98                    RealmsSorting.BY_NUMBER_OF_PLAYERS,
 99                    currentSorting != RealmsSorting.BY_NUMBER_OF_PLAYERS || currentSortingDirection != RealmsSortingDire
 100
 0101                EventSystem.current?.SetSelectedGameObject(null);
 0102            });
 103
 12104        if (closeCardButton != null)
 12105            closeCardButton.onClick.AddListener(CloseModal);
 106
 12107        if (closeAction != null)
 12108            closeAction.OnTriggered += OnCloseActionTriggered;
 109
 12110        if (modalBackgroundButton != null)
 12111            modalBackgroundButton.onClick.AddListener(CloseModal);
 112
 12113        realmsInformationLink.HyperLinkClicked += () => WebInterface.OpenURL(REALM_DOCS_LINK);
 114
 12115        ConfigureRealmsPool();
 12116        RefreshSortingArrows();
 12117    }
 118
 119    public void Configure(RealmSelectorComponentModel newModel)
 120    {
 1121        model = newModel;
 1122        RefreshControl();
 1123    }
 124
 125    public override void RefreshControl()
 126    {
 1127        if (model == null)
 0128            return;
 129
 1130        SetCurrentRealm(model.currentRealmName);
 1131        availableRealms.RefreshControl();
 1132    }
 133
 134    public override void Show(bool instant = false)
 135    {
 2136        base.Show(instant);
 137
 2138        DataStore.i.exploreV2.isSomeModalOpen.Set(true);
 2139    }
 140
 141    public override void Hide(bool instant = false)
 142    {
 32143        base.Hide(instant);
 144
 32145        DataStore.i.exploreV2.isSomeModalOpen.Set(false);
 32146    }
 147
 148    public override void Dispose()
 149    {
 47150        base.Dispose();
 151
 47152        if (sortByNameButton != null)
 47153            sortByNameButton.onClick.RemoveAllListeners();
 154
 47155        if (sortByNameButton != null)
 47156            sortByNumberOfPlayersButton.onClick.RemoveAllListeners();
 157
 47158        if (closeCardButton != null)
 47159            closeCardButton.onClick.RemoveAllListeners();
 160
 47161        if (closeAction != null)
 47162            closeAction.OnTriggered -= OnCloseActionTriggered;
 163
 47164        if (modalBackgroundButton != null)
 47165            modalBackgroundButton.onClick.RemoveAllListeners();
 47166    }
 167
 168    public void SetCurrentRealm(string realm)
 169    {
 2170        model.currentRealmName = realm;
 171
 172        // Set the current realm in the modal header
 2173        if (currentRealmText != null)
 2174            currentRealmText.text = $"You are in <b>{realm.ToUpper()}</b>";
 175
 176        // Search the current realm in the available ones and set it as connected
 2177        var instantiatedRealms = availableRealms.GetItems();
 4178        foreach (RealmRowComponentView realmRow in instantiatedRealms)
 179        {
 0180            realmRow.SetAsConnected(realmRow.model.name == realm);
 181        }
 2182    }
 183
 184    public void SetAvailableRealms(List<RealmRowComponentModel> realms)
 185    {
 1186        friendsTrackerController?.RemoveAllHandlers();
 187
 1188        availableRealms.ExtractItems();
 1189        realmsPool.ReleaseAll();
 190
 1191        List<BaseComponentView> realmsToAdd = new List<BaseComponentView>();
 6192        foreach (RealmRowComponentModel realm in realms)
 193        {
 2194            RealmRowComponentView newRealmRow = realmsPool.Get().gameObject.GetComponent<RealmRowComponentView>();
 2195            newRealmRow.Configure(realm);
 2196            newRealmRow.SetOnHoverColor(colorForFocusedRows);
 2197            newRealmRow.onWarpInClick.RemoveAllListeners();
 2198            newRealmRow.onWarpInClick.AddListener(() =>
 199            {
 0200                WebInterface.SendChatMessage(new ChatMessage
 201                {
 202                    messageType = ChatMessage.Type.NONE,
 203                    recipient = string.Empty,
 204                    body = $"/changerealm {realm.name}"
 205                });
 0206            });
 2207            realmsToAdd.Add(newRealmRow);
 208
 2209            friendsTrackerController?.AddHandler(newRealmRow.friendsHandler);
 210        }
 211
 1212        availableRealms.SetItems(realmsToAdd);
 1213        RefreshRowColours();
 214
 1215        ApplySorting(currentSorting, currentSortingDirection);
 216
 1217        rootTransform.ForceUpdateLayout();
 1218    }
 219
 220    internal void ApplySorting(RealmsSorting sortBy, RealmsSortingDirection sortingDirection)
 221    {
 1222        List<BaseComponentView> realmsToSort = availableRealms.ExtractItems();
 223
 224        switch (sortBy)
 225        {
 226            case RealmsSorting.BY_NAME:
 0227                if (sortingDirection == RealmsSortingDirection.ASC)
 0228                    realmsToSort = realmsToSort
 0229                        .OrderBy(d => ((RealmRowComponentView)d).model.name)
 230                        .ToList();
 231                else
 0232                    realmsToSort = realmsToSort
 0233                        .OrderByDescending(d => ((RealmRowComponentView)d).model.name)
 234                        .ToList();
 0235                break;
 236            case RealmsSorting.BY_NUMBER_OF_PLAYERS:
 1237                if (sortingDirection == RealmsSortingDirection.ASC)
 0238                    realmsToSort = realmsToSort
 0239                        .OrderBy(d => ((RealmRowComponentView)d).model.players)
 240                        .ToList();
 241                else
 1242                    realmsToSort = realmsToSort
 2243                        .OrderByDescending(d => ((RealmRowComponentView)d).model.players)
 244                        .ToList();
 245                break;
 246        }
 247
 1248        availableRealms.SetItems(realmsToSort);
 249
 1250        currentSorting = sortBy;
 1251        currentSortingDirection = sortingDirection;
 252
 1253        RefreshSortingArrows();
 1254        RefreshRowColours();
 255
 1256        rootTransform.ForceUpdateLayout();
 1257    }
 258
 259    internal void RefreshSortingArrows()
 260    {
 13261        sortByNameArrowUp.color = currentSorting == RealmsSorting.BY_NAME && currentSortingDirection == RealmsSortingDir
 13262        sortByNameArrowDown.color = currentSorting == RealmsSorting.BY_NAME && currentSortingDirection == RealmsSortingD
 13263        sortByNumberOfPlayersArrowUp.color = currentSorting == RealmsSorting.BY_NUMBER_OF_PLAYERS && currentSortingDirec
 13264        sortByNumberOfPlayersArrowDown.color = currentSorting == RealmsSorting.BY_NUMBER_OF_PLAYERS && currentSortingDir
 13265    }
 266
 267    internal void RefreshRowColours()
 268    {
 2269        var instantiatedRealms = availableRealms.GetItems();
 2270        bool isAnOddRow = true;
 12271        foreach (RealmRowComponentView realmRow in instantiatedRealms)
 272        {
 4273            realmRow.SetRowColor(isAnOddRow ? colorForOddRows : colorForEvenRows);
 4274            isAnOddRow = !isAnOddRow;
 275        }
 2276    }
 277
 4278    internal void CloseModal() { Hide(); }
 279
 2280    internal void OnCloseActionTriggered(DCLAction_Trigger action) { CloseModal(); }
 281
 282    internal void ConfigureRealmsPool()
 283    {
 13284        realmsPool = PoolManager.i.GetPool(REALMS_POOL_NAME);
 13285        if (realmsPool == null)
 286        {
 1287            realmsPool = PoolManager.i.AddPool(
 288                REALMS_POOL_NAME,
 289                GameObject.Instantiate(realmRowPrefab).gameObject,
 290                maxPrewarmCount: REALMS_POOL_PREWARM,
 291                isPersistent: true);
 292
 1293            realmsPool.ForcePrewarm();
 294        }
 13295    }
 296}