< 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:284
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 TMPro;
 6using UnityEngine;
 7using UnityEngine.EventSystems;
 8using UnityEngine.UI;
 9
 10public enum RealmsSorting
 11{
 12    BY_NAME,
 13    BY_NUMBER_OF_PLAYERS
 14}
 15
 16public enum RealmsSortingDirection
 17{
 18    ASC,
 19    DESC
 20}
 21
 22public interface IRealmSelectorComponentView
 23{
 24    /// <summary>
 25    /// Set the current realm label.
 26    /// </summary>
 27    /// <param name="realm">Current realm.</param>
 28    void SetCurrentRealm(string realm);
 29
 30    /// <summary>
 31    /// Set the available realms component with a list of realms.
 32    /// </summary>
 33    /// <param name="realms">List of realms (model) to be loaded.</param>
 34    void SetAvailableRealms(List<RealmRowComponentModel> realms);
 35}
 36
 37public class RealmSelectorComponentView : BaseComponentView, IRealmSelectorComponentView, IComponentModelConfig
 38{
 39    internal const string REALMS_POOL_NAME = "RealmSelector_RealmRowsPool";
 40    internal const int REALMS_POOL_PREWARM = 20;
 41
 42    [Header("Assets References")]
 43    [SerializeField] internal RealmRowComponentView realmRowPrefab;
 44
 45    [Header("Prefab References")]
 46    [SerializeField] internal TMP_Text currentRealmText;
 47    [SerializeField] internal ButtonComponentView sortByNameButton;
 48    [SerializeField] internal Image sortByNameArrowUp;
 49    [SerializeField] internal Image sortByNameArrowDown;
 50    [SerializeField] internal ButtonComponentView sortByNumberOfPlayersButton;
 51    [SerializeField] internal Image sortByNumberOfPlayersArrowUp;
 52    [SerializeField] internal Image sortByNumberOfPlayersArrowDown;
 53    [SerializeField] internal GridContainerComponentView availableRealms;
 54    [SerializeField] internal Button modalBackgroundButton;
 55    [SerializeField] internal ButtonComponentView closeCardButton;
 56    [SerializeField] internal InputAction_Trigger closeAction;
 57
 58    [Header("Configuration")]
 59    [SerializeField] internal RealmSelectorComponentModel model;
 60    [SerializeField] internal Color colorForEvenRows;
 61    [SerializeField] internal Color colorForOddRows;
 62    [SerializeField] internal Color colorForFocusedRows;
 63    [SerializeField] internal Color colorForActiveSortingArrow;
 64    [SerializeField] internal Color colorForUnactiveSortingArrow;
 65    [SerializeField] internal Color[] friendColors = null;
 66
 67    internal Pool realmsPool;
 1468    internal RealmsSorting currentSorting = RealmsSorting.BY_NUMBER_OF_PLAYERS;
 1469    internal RealmsSortingDirection currentSortingDirection = RealmsSortingDirection.DESC;
 70    internal RealmTrackerController friendsTrackerController;
 71
 72    public override void Awake()
 73    {
 1374        base.Awake();
 75
 1376        friendsTrackerController = new RealmTrackerController(FriendsController.i, friendColors);
 77
 1378        if (sortByNameButton != null)
 1379            sortByNameButton.onClick.AddListener(() =>
 80            {
 081                ApplySorting(
 82                    RealmsSorting.BY_NAME,
 83                    currentSorting != RealmsSorting.BY_NAME || currentSortingDirection != RealmsSortingDirection.ASC ? R
 84
 085                EventSystem.current?.SetSelectedGameObject(null);
 086            });
 87
 1388        if (sortByNumberOfPlayersButton != null)
 1389            sortByNumberOfPlayersButton.onClick.AddListener(() =>
 90            {
 091                ApplySorting(
 92                    RealmsSorting.BY_NUMBER_OF_PLAYERS,
 93                    currentSorting != RealmsSorting.BY_NUMBER_OF_PLAYERS || currentSortingDirection != RealmsSortingDire
 94
 095                EventSystem.current?.SetSelectedGameObject(null);
 096            });
 97
 1398        if (closeCardButton != null)
 1399            closeCardButton.onClick.AddListener(CloseModal);
 100
 13101        if (closeAction != null)
 13102            closeAction.OnTriggered += OnCloseActionTriggered;
 103
 13104        if (modalBackgroundButton != null)
 13105            modalBackgroundButton.onClick.AddListener(CloseModal);
 106
 13107        ConfigureRealmsPool();
 13108        RefreshSortingArrows();
 13109    }
 110
 111    public void Configure(BaseComponentModel newModel)
 112    {
 1113        model = (RealmSelectorComponentModel)newModel;
 1114        RefreshControl();
 1115    }
 116
 117    public override void RefreshControl()
 118    {
 1119        if (model == null)
 0120            return;
 121
 1122        SetCurrentRealm(model.currentRealmName);
 1123        availableRealms.RefreshControl();
 1124    }
 125
 126    public override void Show(bool instant = false)
 127    {
 2128        base.Show(instant);
 129
 2130        DataStore.i.exploreV2.isSomeModalOpen.Set(true);
 2131    }
 132
 133    public override void Hide(bool instant = false)
 134    {
 43135        base.Hide(instant);
 136
 43137        DataStore.i.exploreV2.isSomeModalOpen.Set(false);
 43138    }
 139
 140    public override void Dispose()
 141    {
 66142        base.Dispose();
 143
 66144        if (sortByNameButton != null)
 66145            sortByNameButton.onClick.RemoveAllListeners();
 146
 66147        if (sortByNameButton != null)
 66148            sortByNumberOfPlayersButton.onClick.RemoveAllListeners();
 149
 66150        if (closeCardButton != null)
 66151            closeCardButton.onClick.RemoveAllListeners();
 152
 66153        if (closeAction != null)
 66154            closeAction.OnTriggered -= OnCloseActionTriggered;
 155
 66156        if (modalBackgroundButton != null)
 66157            modalBackgroundButton.onClick.RemoveAllListeners();
 66158    }
 159
 160    public void SetCurrentRealm(string realm)
 161    {
 2162        model.currentRealmName = realm;
 163
 164        // Set the current realm in the modal header
 2165        if (currentRealmText != null)
 2166            currentRealmText.text = $"You are in <b>{realm.ToUpper()}</b>";
 167
 168        // Search the current realm in the available ones and set it as connected
 2169        var instantiatedRealms = availableRealms.GetItems();
 4170        foreach (RealmRowComponentView realmRow in instantiatedRealms)
 171        {
 0172            realmRow.SetAsConnected(realmRow.model.name == realm);
 173        }
 2174    }
 175
 176    public void SetAvailableRealms(List<RealmRowComponentModel> realms)
 177    {
 1178        friendsTrackerController?.RemoveAllHandlers();
 179
 1180        availableRealms.ExtractItems();
 1181        realmsPool.ReleaseAll();
 182
 1183        List<BaseComponentView> realmsToAdd = new List<BaseComponentView>();
 6184        foreach (RealmRowComponentModel realm in realms)
 185        {
 2186            RealmRowComponentView newRealmRow = realmsPool.Get().gameObject.GetComponent<RealmRowComponentView>();
 2187            newRealmRow.Configure(realm);
 2188            newRealmRow.SetOnHoverColor(colorForFocusedRows);
 2189            newRealmRow.onWarpInClick.RemoveAllListeners();
 2190            newRealmRow.onWarpInClick.AddListener(() =>
 191            {
 0192                WebInterface.SendChatMessage(new ChatMessage
 193                {
 194                    messageType = ChatMessage.Type.NONE,
 195                    recipient = string.Empty,
 196                    body = $"/changerealm {realm.name}"
 197                });
 0198            });
 2199            realmsToAdd.Add(newRealmRow);
 200
 2201            friendsTrackerController?.AddHandler(newRealmRow.friendsHandler);
 202        }
 203
 1204        availableRealms.SetItems(realmsToAdd);
 1205        RefreshRowColours();
 206
 1207        ApplySorting(currentSorting, currentSortingDirection);
 1208    }
 209
 210    internal void ApplySorting(RealmsSorting sortBy, RealmsSortingDirection sortingDirection)
 211    {
 1212        List<BaseComponentView> realmsToSort = availableRealms.ExtractItems();
 213
 214        switch (sortBy)
 215        {
 216            case RealmsSorting.BY_NAME:
 0217                if (sortingDirection == RealmsSortingDirection.ASC)
 0218                    realmsToSort = realmsToSort
 0219                        .OrderBy(d => ((RealmRowComponentView)d).model.name)
 220                        .ToList();
 221                else
 0222                    realmsToSort = realmsToSort
 0223                        .OrderByDescending(d => ((RealmRowComponentView)d).model.name)
 224                        .ToList();
 0225                break;
 226            case RealmsSorting.BY_NUMBER_OF_PLAYERS:
 1227                if (sortingDirection == RealmsSortingDirection.ASC)
 0228                    realmsToSort = realmsToSort
 0229                        .OrderBy(d => ((RealmRowComponentView)d).model.players)
 230                        .ToList();
 231                else
 1232                    realmsToSort = realmsToSort
 2233                        .OrderByDescending(d => ((RealmRowComponentView)d).model.players)
 234                        .ToList();
 235                break;
 236        }
 237
 1238        availableRealms.SetItems(realmsToSort);
 239
 1240        currentSorting = sortBy;
 1241        currentSortingDirection = sortingDirection;
 242
 1243        RefreshSortingArrows();
 1244        RefreshRowColours();
 1245    }
 246
 247    internal void RefreshSortingArrows()
 248    {
 14249        sortByNameArrowUp.color = currentSorting == RealmsSorting.BY_NAME && currentSortingDirection == RealmsSortingDir
 14250        sortByNameArrowDown.color = currentSorting == RealmsSorting.BY_NAME && currentSortingDirection == RealmsSortingD
 14251        sortByNumberOfPlayersArrowUp.color = currentSorting == RealmsSorting.BY_NUMBER_OF_PLAYERS && currentSortingDirec
 14252        sortByNumberOfPlayersArrowDown.color = currentSorting == RealmsSorting.BY_NUMBER_OF_PLAYERS && currentSortingDir
 14253    }
 254
 255    internal void RefreshRowColours()
 256    {
 2257        var instantiatedRealms = availableRealms.GetItems();
 2258        bool isAnOddRow = true;
 12259        foreach (RealmRowComponentView realmRow in instantiatedRealms)
 260        {
 4261            realmRow.SetRowColor(isAnOddRow ? colorForOddRows : colorForEvenRows);
 4262            isAnOddRow = !isAnOddRow;
 263        }
 2264    }
 265
 4266    internal void CloseModal() { Hide(); }
 267
 2268    internal void OnCloseActionTriggered(DCLAction_Trigger action) { CloseModal(); }
 269
 270    internal void ConfigureRealmsPool()
 271    {
 14272        realmsPool = PoolManager.i.GetPool(REALMS_POOL_NAME);
 14273        if (realmsPool == null)
 274        {
 1275            realmsPool = PoolManager.i.AddPool(
 276                REALMS_POOL_NAME,
 277                GameObject.Instantiate(realmRowPrefab).gameObject,
 278                maxPrewarmCount: REALMS_POOL_PREWARM,
 279                isPersistent: true);
 280
 1281            realmsPool.ForcePrewarm();
 282        }
 14283    }
 284}