< 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:92
Uncovered lines:18
Coverable lines:110
Total lines:285
Line coverage:83.6% (92 of 110)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
RealmSelectorComponentView()0%110100%
Awake()0%660100%
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%12.699064.29%
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.Interface;
 3using System.Collections.Generic;
 4using System.Linq;
 5using DCL.Social.Friends;
 6using TMPro;
 7using UnityEngine;
 8using UnityEngine.EventSystems;
 9using UnityEngine.UI;
 10
 11public enum RealmsSorting
 12{
 13    BY_NAME,
 14    BY_NUMBER_OF_PLAYERS
 15}
 16
 17public enum RealmsSortingDirection
 18{
 19    ASC,
 20    DESC
 21}
 22
 23public interface IRealmSelectorComponentView
 24{
 25    /// <summary>
 26    /// Set the current realm label.
 27    /// </summary>
 28    /// <param name="realm">Current realm.</param>
 29    void SetCurrentRealm(string realm);
 30
 31    /// <summary>
 32    /// Set the available realms component with a list of realms.
 33    /// </summary>
 34    /// <param name="realms">List of realms (model) to be loaded.</param>
 35    void SetAvailableRealms(List<RealmRowComponentModel> realms);
 36}
 37
 38public class RealmSelectorComponentView : BaseComponentView, IRealmSelectorComponentView, IComponentModelConfig<RealmSel
 39{
 40    internal const string REALMS_POOL_NAME = "RealmSelector_RealmRowsPool";
 41    internal const int REALMS_POOL_PREWARM = 20;
 42
 43    [Header("Assets References")]
 44    [SerializeField] internal RealmRowComponentView realmRowPrefab;
 45
 46    [Header("Prefab References")]
 47    [SerializeField] internal TMP_Text currentRealmText;
 48    [SerializeField] internal ButtonComponentView sortByNameButton;
 49    [SerializeField] internal Image sortByNameArrowUp;
 50    [SerializeField] internal Image sortByNameArrowDown;
 51    [SerializeField] internal ButtonComponentView sortByNumberOfPlayersButton;
 52    [SerializeField] internal Image sortByNumberOfPlayersArrowUp;
 53    [SerializeField] internal Image sortByNumberOfPlayersArrowDown;
 54    [SerializeField] internal GridContainerComponentView availableRealms;
 55    [SerializeField] internal Button modalBackgroundButton;
 56    [SerializeField] internal ButtonComponentView closeCardButton;
 57    [SerializeField] internal InputAction_Trigger closeAction;
 58
 59    [Header("Configuration")]
 60    [SerializeField] internal RealmSelectorComponentModel model;
 61    [SerializeField] internal Color colorForEvenRows;
 62    [SerializeField] internal Color colorForOddRows;
 63    [SerializeField] internal Color colorForFocusedRows;
 64    [SerializeField] internal Color colorForActiveSortingArrow;
 65    [SerializeField] internal Color colorForUnactiveSortingArrow;
 66    [SerializeField] internal Color[] friendColors = null;
 67
 68    internal Pool realmsPool;
 1369    internal RealmsSorting currentSorting = RealmsSorting.BY_NUMBER_OF_PLAYERS;
 1370    internal RealmsSortingDirection currentSortingDirection = RealmsSortingDirection.DESC;
 71    internal RealmTrackerController friendsTrackerController;
 72
 73    public override void Awake()
 74    {
 1275        base.Awake();
 76
 1277        friendsTrackerController = new RealmTrackerController(FriendsController.i, friendColors);
 78
 1279        if (sortByNameButton != null)
 1280            sortByNameButton.onClick.AddListener(() =>
 81            {
 082                ApplySorting(
 83                    RealmsSorting.BY_NAME,
 84                    currentSorting != RealmsSorting.BY_NAME || currentSortingDirection != RealmsSortingDirection.ASC ? R
 85
 086                EventSystem.current?.SetSelectedGameObject(null);
 087            });
 88
 1289        if (sortByNumberOfPlayersButton != null)
 1290            sortByNumberOfPlayersButton.onClick.AddListener(() =>
 91            {
 092                ApplySorting(
 93                    RealmsSorting.BY_NUMBER_OF_PLAYERS,
 94                    currentSorting != RealmsSorting.BY_NUMBER_OF_PLAYERS || currentSortingDirection != RealmsSortingDire
 95
 096                EventSystem.current?.SetSelectedGameObject(null);
 097            });
 98
 1299        if (closeCardButton != null)
 12100            closeCardButton.onClick.AddListener(CloseModal);
 101
 12102        if (closeAction != null)
 12103            closeAction.OnTriggered += OnCloseActionTriggered;
 104
 12105        if (modalBackgroundButton != null)
 12106            modalBackgroundButton.onClick.AddListener(CloseModal);
 107
 12108        ConfigureRealmsPool();
 12109        RefreshSortingArrows();
 12110    }
 111
 112    public void Configure(RealmSelectorComponentModel newModel)
 113    {
 1114        model = newModel;
 1115        RefreshControl();
 1116    }
 117
 118    public override void RefreshControl()
 119    {
 1120        if (model == null)
 0121            return;
 122
 1123        SetCurrentRealm(model.currentRealmName);
 1124        availableRealms.RefreshControl();
 1125    }
 126
 127    public override void Show(bool instant = false)
 128    {
 2129        base.Show(instant);
 130
 2131        DataStore.i.exploreV2.isSomeModalOpen.Set(true);
 2132    }
 133
 134    public override void Hide(bool instant = false)
 135    {
 32136        base.Hide(instant);
 137
 32138        DataStore.i.exploreV2.isSomeModalOpen.Set(false);
 32139    }
 140
 141    public override void Dispose()
 142    {
 47143        base.Dispose();
 144
 47145        if (sortByNameButton != null)
 47146            sortByNameButton.onClick.RemoveAllListeners();
 147
 47148        if (sortByNameButton != null)
 47149            sortByNumberOfPlayersButton.onClick.RemoveAllListeners();
 150
 47151        if (closeCardButton != null)
 47152            closeCardButton.onClick.RemoveAllListeners();
 153
 47154        if (closeAction != null)
 47155            closeAction.OnTriggered -= OnCloseActionTriggered;
 156
 47157        if (modalBackgroundButton != null)
 47158            modalBackgroundButton.onClick.RemoveAllListeners();
 47159    }
 160
 161    public void SetCurrentRealm(string realm)
 162    {
 2163        model.currentRealmName = realm;
 164
 165        // Set the current realm in the modal header
 2166        if (currentRealmText != null)
 2167            currentRealmText.text = $"You are in <b>{realm.ToUpper()}</b>";
 168
 169        // Search the current realm in the available ones and set it as connected
 2170        var instantiatedRealms = availableRealms.GetItems();
 4171        foreach (RealmRowComponentView realmRow in instantiatedRealms)
 172        {
 0173            realmRow.SetAsConnected(realmRow.model.name == realm);
 174        }
 2175    }
 176
 177    public void SetAvailableRealms(List<RealmRowComponentModel> realms)
 178    {
 1179        friendsTrackerController?.RemoveAllHandlers();
 180
 1181        availableRealms.ExtractItems();
 1182        realmsPool.ReleaseAll();
 183
 1184        List<BaseComponentView> realmsToAdd = new List<BaseComponentView>();
 6185        foreach (RealmRowComponentModel realm in realms)
 186        {
 2187            RealmRowComponentView newRealmRow = realmsPool.Get().gameObject.GetComponent<RealmRowComponentView>();
 2188            newRealmRow.Configure(realm);
 2189            newRealmRow.SetOnHoverColor(colorForFocusedRows);
 2190            newRealmRow.onWarpInClick.RemoveAllListeners();
 2191            newRealmRow.onWarpInClick.AddListener(() =>
 192            {
 0193                WebInterface.SendChatMessage(new ChatMessage
 194                {
 195                    messageType = ChatMessage.Type.NONE,
 196                    recipient = string.Empty,
 197                    body = $"/changerealm {realm.name}"
 198                });
 0199            });
 2200            realmsToAdd.Add(newRealmRow);
 201
 2202            friendsTrackerController?.AddHandler(newRealmRow.friendsHandler);
 203        }
 204
 1205        availableRealms.SetItems(realmsToAdd);
 1206        RefreshRowColours();
 207
 1208        ApplySorting(currentSorting, currentSortingDirection);
 1209    }
 210
 211    internal void ApplySorting(RealmsSorting sortBy, RealmsSortingDirection sortingDirection)
 212    {
 1213        List<BaseComponentView> realmsToSort = availableRealms.ExtractItems();
 214
 215        switch (sortBy)
 216        {
 217            case RealmsSorting.BY_NAME:
 0218                if (sortingDirection == RealmsSortingDirection.ASC)
 0219                    realmsToSort = realmsToSort
 0220                        .OrderBy(d => ((RealmRowComponentView)d).model.name)
 221                        .ToList();
 222                else
 0223                    realmsToSort = realmsToSort
 0224                        .OrderByDescending(d => ((RealmRowComponentView)d).model.name)
 225                        .ToList();
 0226                break;
 227            case RealmsSorting.BY_NUMBER_OF_PLAYERS:
 1228                if (sortingDirection == RealmsSortingDirection.ASC)
 0229                    realmsToSort = realmsToSort
 0230                        .OrderBy(d => ((RealmRowComponentView)d).model.players)
 231                        .ToList();
 232                else
 1233                    realmsToSort = realmsToSort
 2234                        .OrderByDescending(d => ((RealmRowComponentView)d).model.players)
 235                        .ToList();
 236                break;
 237        }
 238
 1239        availableRealms.SetItems(realmsToSort);
 240
 1241        currentSorting = sortBy;
 1242        currentSortingDirection = sortingDirection;
 243
 1244        RefreshSortingArrows();
 1245        RefreshRowColours();
 1246    }
 247
 248    internal void RefreshSortingArrows()
 249    {
 13250        sortByNameArrowUp.color = currentSorting == RealmsSorting.BY_NAME && currentSortingDirection == RealmsSortingDir
 13251        sortByNameArrowDown.color = currentSorting == RealmsSorting.BY_NAME && currentSortingDirection == RealmsSortingD
 13252        sortByNumberOfPlayersArrowUp.color = currentSorting == RealmsSorting.BY_NUMBER_OF_PLAYERS && currentSortingDirec
 13253        sortByNumberOfPlayersArrowDown.color = currentSorting == RealmsSorting.BY_NUMBER_OF_PLAYERS && currentSortingDir
 13254    }
 255
 256    internal void RefreshRowColours()
 257    {
 2258        var instantiatedRealms = availableRealms.GetItems();
 2259        bool isAnOddRow = true;
 12260        foreach (RealmRowComponentView realmRow in instantiatedRealms)
 261        {
 4262            realmRow.SetRowColor(isAnOddRow ? colorForOddRows : colorForEvenRows);
 4263            isAnOddRow = !isAnOddRow;
 264        }
 2265    }
 266
 4267    internal void CloseModal() { Hide(); }
 268
 2269    internal void OnCloseActionTriggered(DCLAction_Trigger action) { CloseModal(); }
 270
 271    internal void ConfigureRealmsPool()
 272    {
 13273        realmsPool = PoolManager.i.GetPool(REALMS_POOL_NAME);
 13274        if (realmsPool == null)
 275        {
 1276            realmsPool = PoolManager.i.AddPool(
 277                REALMS_POOL_NAME,
 278                GameObject.Instantiate(realmRowPrefab).gameObject,
 279                maxPrewarmCount: REALMS_POOL_PREWARM,
 280                isPersistent: true);
 281
 1282            realmsPool.ForcePrewarm();
 283        }
 13284    }
 285}