< 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:83
Uncovered lines:11
Coverable lines:94
Total lines:250
Line coverage:88.2% (83 of 94)
Covered branches:0
Total branches:0
Covered methods:21
Total methods:22
Method coverage:95.4% (21 of 22)

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%3.033085.71%
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")]
 1660    [SerializeField] internal int maxFriendsToShow = 6;
 61    [SerializeField] internal RealmRowComponentModel model;
 62
 63    internal Color originalBackgroundColor;
 64    internal Color onHoverColor;
 65
 5966    public RealmHandler friendsHandler { get; set; }
 2067    internal RealmInfoHandler realmInfoHandler { get; set; }
 68
 469    public Button.ButtonClickedEvent onWarpInClick => warpInButton?.onClick;
 70
 1671    internal Dictionary<string, BaseComponentView> currentFriendHeads = new Dictionary<string, BaseComponentView>();
 72
 73    public override void Awake()
 74    {
 1575        base.Awake();
 76
 1577        CleanFriendHeadsItems();
 78
 1579        originalBackgroundColor = backgroundImage.color;
 1580        onHoverColor = backgroundImage.color;
 1581    }
 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    {
 19116        base.OnLoseFocus();
 117
 19118        backgroundImage.color = originalBackgroundColor;
 19119    }
 120
 121    public override void Dispose()
 122    {
 27123        base.Dispose();
 124
 27125        if (friendsHandler != null)
 126        {
 6127            friendsHandler.OnFriendAddedEvent -= OnFriendAdded;
 6128            friendsHandler.OnFriendRemovedEvent -= OnFriendRemoved;
 129        }
 130
 27131        if (friendsGrid != null)
 26132            friendsGrid.Dispose();
 27133    }
 134
 135    public void SetName(string name)
 136    {
 5137        if (string.IsNullOrEmpty(name))
 1138            name = string.Empty;
 139
 5140        model.name = name;
 141
 5142        if (nameText == null)
 0143            return;
 144
 5145        nameText.text = name.ToUpper();
 5146    }
 147
 148    public void SetNumberOfPlayers(int numberOfPlayers)
 149    {
 4150        model.players = numberOfPlayers;
 151
 4152        if (playersText == null)
 0153            return;
 154
 4155        playersText.text = ExploreV2CommonUtils.FormatNumberToString(numberOfPlayers);
 4156    }
 157
 158    public void SetAsConnected(bool isConnected)
 159    {
 4160        model.isConnected = isConnected;
 161
 4162        if (connectedMark == null)
 0163            return;
 164
 4165        connectedMark.SetActive(isConnected);
 4166        warpInButton.gameObject.SetActive(!isConnected);
 4167    }
 168
 169    public void SetRowColor(Color color)
 170    {
 8171        model.backgroundColor = color;
 172
 8173        if (backgroundImage == null)
 0174            return;
 175
 8176        backgroundImage.color = color;
 8177        originalBackgroundColor = color;
 8178    }
 179
 180    public void SetOnHoverColor(Color color)
 181    {
 6182        model.onHoverColor = color;
 6183        onHoverColor = color;
 6184    }
 185
 186    internal void InitializeFriendsTracker()
 187    {
 4188        CleanFriendHeadsItems();
 189
 4190        if (realmInfoHandler == null)
 4191            realmInfoHandler = new RealmInfoHandler();
 192
 4193        if (friendsHandler == null)
 194        {
 4195            friendsHandler = new RealmHandler(realmInfoHandler);
 4196            friendsHandler.OnFriendAddedEvent += OnFriendAdded;
 4197            friendsHandler.OnFriendRemovedEvent += OnFriendRemoved;
 198        }
 4199    }
 200
 201    internal void OnFriendAdded(UserProfile profile, Color backgroundColor)
 202    {
 3203        if (currentFriendHeads.Count == maxFriendsToShow)
 0204            return;
 205
 3206        if (currentFriendHeads.ContainsKey(profile.userId))
 0207            return;
 208
 3209        BaseComponentView newFriend = InstantiateAndConfigureFriendHead(
 210            new FriendHeadForPlaceCardComponentModel
 211            {
 212                userProfile = profile,
 213                backgroundColor = backgroundColor
 214            },
 215            friendHeadPrefab);
 216
 3217        if (friendsGrid != null)
 3218            friendsGrid.AddItemWithResize(newFriend);
 219
 3220        currentFriendHeads.Add(profile.userId, newFriend);
 3221    }
 222
 223    internal void OnFriendRemoved(UserProfile profile)
 224    {
 1225        if (!currentFriendHeads.ContainsKey(profile.userId))
 0226            return;
 227
 1228        if (friendsGrid != null)
 1229            friendsGrid.RemoveItem(currentFriendHeads[profile.userId]);
 230
 1231        currentFriendHeads.Remove(profile.userId);
 1232    }
 233
 234    internal void CleanFriendHeadsItems()
 235    {
 20236        if (friendsGrid != null)
 237        {
 20238            friendsGrid.RemoveItems();
 20239            currentFriendHeads.Clear();
 240        }
 20241    }
 242
 243    internal BaseComponentView InstantiateAndConfigureFriendHead(FriendHeadForPlaceCardComponentModel friendInfo, Friend
 244    {
 4245        FriendHeadForPlaceCardComponentView friendHeadGO = GameObject.Instantiate(prefabToUse);
 4246        friendHeadGO.Configure(friendInfo);
 247
 4248        return friendHeadGO;
 249    }
 250}