< Summary

Class:RealmRowComponentView
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/MainMenu/RealmViewer/RealmRowComponentView.cs
Covered lines:81
Uncovered lines:11
Coverable lines:92
Total lines:247
Line coverage:88% (81 of 92)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
RealmRowComponentView()0%110100%
Awake()0%110100%
Configure(...)0%220100%
RefreshControl()0%2.012087.5%
OnFocus()0%2100%
OnLoseFocus()0%110100%
Dispose()0%330100%
SetName(...)0%2.032080%
SetNumberOfPlayers(...)0%2.032080%
SetAsConnected(...)0%2.022083.33%
SetRowColor(...)0%2.022083.33%
SetOnHoverColor(...)0%110100%
InitializeFriendsTracker()0%330100%
OnFriendAdded(...)0%4.184077.78%
OnFriendRemoved(...)0%3.043083.33%
CleanFriendHeadsItems()0%220100%
InstantiateAndConfigureFriendHead(...)0%110100%

File(s)

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

#LineLine coverage
 1using System.Collections.Generic;
 2using TMPro;
 3using UnityEngine;
 4using UnityEngine.UI;
 5
 6public interface IRealmRowComponentView
 7{
 8    RealmHandler friendsHandler { get; set; }
 9
 10    /// <summary>
 11    /// Event that will be triggered when the Warp In button is clicked.
 12    /// </summary>
 13    Button.ButtonClickedEvent onWarpInClick { get; }
 14
 15    /// <summary>
 16    /// Set the name label.
 17    /// </summary>
 18    /// <param name="name">A string.</param>
 19    void SetName(string name);
 20
 21    /// <summary>
 22    /// Set the number of players label.
 23    /// </summary>
 24    /// <param name="numberOfPlayers">Number of players.</param>
 25    void SetNumberOfPlayers(int numberOfPlayers);
 26
 27    /// <summary>
 28    /// Show/hide the connected mark.
 29    /// </summary>
 30    /// <param name="isConnected">True for showing the connected mark.</param>
 31    void SetAsConnected(bool isConnected);
 32
 33    /// <summary>
 34    /// Set the background color of the row.
 35    /// </summary>
 36    /// <param name="color">Color to apply.</param>
 37    void SetRowColor(Color color);
 38
 39    /// <summary>
 40    /// Set the background color of the row when it is hovered.
 41    /// </summary>
 42    /// <param name="color">Color to apply.</param>
 43    void SetOnHoverColor(Color color);
 44}
 45
 46public class RealmRowComponentView : BaseComponentView, IRealmRowComponentView, IComponentModelConfig<RealmRowComponentM
 47{
 48    [Header("Assets References")]
 49    [SerializeField] internal FriendHeadForPlaceCardComponentView friendHeadPrefab;
 50
 51    [Header("Prefab References")]
 52    [SerializeField] internal TMP_Text nameText;
 53    [SerializeField] internal TMP_Text playersText;
 54    [SerializeField] internal ButtonComponentView warpInButton;
 55    [SerializeField] internal GameObject connectedMark;
 56    [SerializeField] internal Image backgroundImage;
 57    [SerializeField] internal GridContainerComponentView friendsGrid;
 58
 59    [Header("Configuration")]
 1560    [SerializeField] internal int maxFriendsToShow = 6;
 61    [SerializeField] internal RealmRowComponentModel model;
 62
 63    internal Color originalBackgroundColor;
 64    internal Color onHoverColor;
 65
 5766    public RealmHandler friendsHandler { get; set; }
 2067    internal RealmInfoHandler realmInfoHandler { get; set; }
 68
 469    public Button.ButtonClickedEvent onWarpInClick => warpInButton?.onClick;
 70
 1571    internal Dictionary<string, BaseComponentView> currentFriendHeads = new Dictionary<string, BaseComponentView>();
 72
 73    public override void Awake()
 74    {
 1475        base.Awake();
 76
 1477        CleanFriendHeadsItems();
 78
 1479        originalBackgroundColor = backgroundImage.color;
 1480        onHoverColor = backgroundImage.color;
 1481    }
 82
 83    public void Configure(RealmRowComponentModel newModel)
 84    {
 385        model = newModel;
 86
 387        InitializeFriendsTracker();
 88
 389        if (realmInfoHandler != null)
 390            realmInfoHandler.SetRealmInfo(model.name);
 91
 392        RefreshControl();
 393    }
 94
 95    public override void RefreshControl()
 96    {
 397        if (model == null)
 098            return;
 99
 3100        SetName(model.name);
 3101        SetNumberOfPlayers(model.players);
 3102        SetAsConnected(model.isConnected);
 3103        SetRowColor(model.backgroundColor);
 3104        SetOnHoverColor(model.onHoverColor);
 3105    }
 106
 107    public override void OnFocus()
 108    {
 0109        base.OnFocus();
 110
 0111        backgroundImage.color = onHoverColor;
 0112    }
 113
 114    public override void OnLoseFocus()
 115    {
 18116        base.OnLoseFocus();
 117
 18118        backgroundImage.color = originalBackgroundColor;
 18119    }
 120
 121    public override void Dispose()
 122    {
 25123        base.Dispose();
 124
 25125        if (friendsHandler != null)
 126        {
 6127            friendsHandler.OnFriendAddedEvent -= OnFriendAdded;
 6128            friendsHandler.OnFriendRemovedEvent -= OnFriendRemoved;
 129        }
 130
 25131        if (friendsGrid != null)
 24132            friendsGrid.Dispose();
 25133    }
 134
 135    public void SetName(string name)
 136    {
 4137        model.name = name;
 138
 4139        if (nameText == null)
 0140            return;
 141
 4142        nameText.text = name.ToUpper();
 4143    }
 144
 145    public void SetNumberOfPlayers(int numberOfPlayers)
 146    {
 4147        model.players = numberOfPlayers;
 148
 4149        if (playersText == null)
 0150            return;
 151
 4152        playersText.text = ExploreV2CommonUtils.FormatNumberToString(numberOfPlayers);
 4153    }
 154
 155    public void SetAsConnected(bool isConnected)
 156    {
 4157        model.isConnected = isConnected;
 158
 4159        if (connectedMark == null)
 0160            return;
 161
 4162        connectedMark.SetActive(isConnected);
 4163        warpInButton.gameObject.SetActive(!isConnected);
 4164    }
 165
 166    public void SetRowColor(Color color)
 167    {
 8168        model.backgroundColor = color;
 169
 8170        if (backgroundImage == null)
 0171            return;
 172
 8173        backgroundImage.color = color;
 8174        originalBackgroundColor = color;
 8175    }
 176
 177    public void SetOnHoverColor(Color color)
 178    {
 6179        model.onHoverColor = color;
 6180        onHoverColor = color;
 6181    }
 182
 183    internal void InitializeFriendsTracker()
 184    {
 4185        CleanFriendHeadsItems();
 186
 4187        if (realmInfoHandler == null)
 4188            realmInfoHandler = new RealmInfoHandler();
 189
 4190        if (friendsHandler == null)
 191        {
 4192            friendsHandler = new RealmHandler(realmInfoHandler);
 4193            friendsHandler.OnFriendAddedEvent += OnFriendAdded;
 4194            friendsHandler.OnFriendRemovedEvent += OnFriendRemoved;
 195        }
 4196    }
 197
 198    internal void OnFriendAdded(UserProfile profile, Color backgroundColor)
 199    {
 3200        if (currentFriendHeads.Count == maxFriendsToShow)
 0201            return;
 202
 3203        if (currentFriendHeads.ContainsKey(profile.userId))
 0204            return;
 205
 3206        BaseComponentView newFriend = InstantiateAndConfigureFriendHead(
 207            new FriendHeadForPlaceCardComponentModel
 208            {
 209                userProfile = profile,
 210                backgroundColor = backgroundColor
 211            },
 212            friendHeadPrefab);
 213
 3214        if (friendsGrid != null)
 3215            friendsGrid.AddItemWithResize(newFriend);
 216
 3217        currentFriendHeads.Add(profile.userId, newFriend);
 3218    }
 219
 220    internal void OnFriendRemoved(UserProfile profile)
 221    {
 1222        if (!currentFriendHeads.ContainsKey(profile.userId))
 0223            return;
 224
 1225        if (friendsGrid != null)
 1226            friendsGrid.RemoveItem(currentFriendHeads[profile.userId]);
 227
 1228        currentFriendHeads.Remove(profile.userId);
 1229    }
 230
 231    internal void CleanFriendHeadsItems()
 232    {
 19233        if (friendsGrid != null)
 234        {
 19235            friendsGrid.RemoveItems();
 19236            currentFriendHeads.Clear();
 237        }
 19238    }
 239
 240    internal BaseComponentView InstantiateAndConfigureFriendHead(FriendHeadForPlaceCardComponentModel friendInfo, Friend
 241    {
 4242        FriendHeadForPlaceCardComponentView friendHeadGO = GameObject.Instantiate(prefabToUse);
 4243        friendHeadGO.Configure(friendInfo);
 244
 4245        return friendHeadGO;
 246    }
 247}