< Summary

Class:PlaceCardComponentView
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/PlacesSubSection/PlaceCard/PlaceCardComponentView.cs
Covered lines:139
Uncovered lines:20
Coverable lines:159
Total lines:415
Line coverage:87.4% (139 of 159)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlaceCardComponentView()0%110100%
PlaceCardComponentView()0%110100%
Awake()0%660100%
Configure(...)0%220100%
RefreshControl()0%6.15064.71%
OnFocus()0%330100%
OnLoseFocus()0%330100%
Dispose()0%770100%
SetPlacePicture(...)0%2.032080%
SetPlacePicture(...)0%3.213071.43%
SetPlacePicture(...)0%3.213071.43%
SetPlaceName(...)0%330100%
SetPlaceDescription(...)0%220100%
SetPlaceAuthor(...)0%220100%
SetNumberOfUsers(...)0%220100%
SetCoords(...)0%220100%
SetParcels(...)0%2100%
SetLoadingIndicatorVisible(...)0%110100%
OnPlaceImageLoaded(...)0%2.52050%
InitializeFriendsTracker()0%330100%
OnFriendAdded(...)0%3.033085.71%
OnFriendRemoved(...)0%3.043083.33%
CleanFriendHeadsItems()0%220100%
InstantiateAndConfigureFriendHead(...)0%110100%
RebuildCardLayouts()0%330100%
CloseModal()0%110100%
OnCloseActionTriggered(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/PlacesSubSection/PlaceCard/PlaceCardComponentView.cs

#LineLine coverage
 1using DCL.Helpers;
 2using System.Collections.Generic;
 3using System.Linq;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8public interface IPlaceCardComponentView
 9{
 10    FriendsHandler friendsHandler { get; set; }
 11
 12    /// <summary>
 13    /// Event that will be triggered when the jumpIn button is clicked.
 14    /// </summary>
 15    Button.ButtonClickedEvent onJumpInClick { get; }
 16
 17    /// <summary>
 18    /// Event that will be triggered when the info button is clicked.
 19    /// </summary>
 20    Button.ButtonClickedEvent onInfoClick { get; }
 21
 22    /// <summary>
 23    /// Set the place picture directly from a sprite.
 24    /// </summary>
 25    /// <param name="sprite">Place picture (sprite).</param>
 26    void SetPlacePicture(Sprite sprite);
 27
 28    /// <summary>
 29    /// Set the place picture from a 2D texture.
 30    /// </summary>
 31    /// <param name="texture">Place picture (texture).</param>
 32    void SetPlacePicture(Texture2D texture);
 33
 34    /// <summary>
 35    /// Set the place picture from an uri.
 36    /// </summary>
 37    /// <param name="uri">Place picture (url).</param>
 38    void SetPlacePicture(string uri);
 39
 40    /// <summary>
 41    /// Set the place name in the card.
 42    /// </summary>
 43    /// <param name="newText">New place name.</param>
 44    void SetPlaceName(string newText);
 45
 46    /// <summary>
 47    /// Set the place description in the card.
 48    /// </summary>
 49    /// <param name="newText">New place description.</param>
 50    void SetPlaceDescription(string newText);
 51
 52    /// <summary>
 53    /// Set the place organizer in the card.
 54    /// </summary>
 55    /// <param name="newText">New place organizer.</param>
 56    void SetPlaceAuthor(string newText);
 57
 58    /// <summary>
 59    /// Set the the number of users in the place.
 60    /// </summary>
 61    /// <param name="newNumberOfUsers">Number of users.</param>
 62    void SetNumberOfUsers(int newNumberOfUsers);
 63
 64    /// <summary>
 65    /// Set the place coords.
 66    /// </summary>
 67    /// <param name="newCoords">Place coords.</param>
 68    void SetCoords(Vector2Int newCoords);
 69
 70    /// <summary>
 71    /// Set the parcels contained in the place.
 72    /// </summary>
 73    /// <param name="parcels">List of parcels.</param>
 74    void SetParcels(Vector2Int[] parcels);
 75
 76    /// <summary>
 77    /// Active or deactive the loading indicator.
 78    /// </summary>
 79    /// <param name="isVisible">True for showing the loading indicator and hiding the card info.</param>
 80    void SetLoadingIndicatorVisible(bool isVisible);
 81}
 82
 83public class PlaceCardComponentView : BaseComponentView, IPlaceCardComponentView, IComponentModelConfig
 84{
 85    internal const int THMBL_MARKETPLACE_WIDTH = 196;
 86    internal const int THMBL_MARKETPLACE_HEIGHT = 143;
 87    internal const int THMBL_MARKETPLACE_SIZEFACTOR = 50;
 88
 189    internal static readonly int ON_FOCUS_CARD_COMPONENT_BOOL = Animator.StringToHash("OnFocus");
 90
 91    [Header("Assets References")]
 92    [SerializeField] internal FriendHeadForPlaceCardComponentView friendHeadPrefab;
 93
 94    [Header("Prefab References")]
 95    [SerializeField] internal ImageComponentView placeImage;
 96    [SerializeField] internal TMP_Text placeNameOnIdleText;
 97    [SerializeField] internal TMP_Text placeNameOnFocusText;
 98    [SerializeField] internal TMP_Text placeDescText;
 99    [SerializeField] internal TMP_Text placeAuthorText;
 100    [SerializeField] internal TMP_Text numberOfUsersText;
 101    [SerializeField] internal TMP_Text coordsText;
 102    [SerializeField] internal Button modalBackgroundButton;
 103    [SerializeField] internal ButtonComponentView closeCardButton;
 104    [SerializeField] internal InputAction_Trigger closeAction;
 105    [SerializeField] internal ButtonComponentView infoButton;
 106    [SerializeField] internal ButtonComponentView jumpinButton;
 107    [SerializeField] internal GridContainerComponentView friendsGrid;
 108    [SerializeField] internal GameObject imageContainer;
 109    [SerializeField] internal GameObject placeInfoContainer;
 110    [SerializeField] internal GameObject loadingSpinner;
 111    [SerializeField] internal Animator cardAnimator;
 112    [SerializeField] internal GameObject cardSelectionFrame;
 113    [SerializeField] internal VerticalLayoutGroup contentVerticalLayout;
 114    [SerializeField] internal VerticalLayoutGroup infoVerticalLayout;
 115
 116    [Header("Configuration")]
 117    [SerializeField] internal PlaceCardComponentModel model;
 118
 0119    public FriendsHandler friendsHandler { get; set; }
 0120    internal MapInfoHandler mapInfoHandler { get; set; }
 121
 96122    internal Dictionary<string, BaseComponentView> currentFriendHeads = new Dictionary<string, BaseComponentView>();
 123
 0124    public Button.ButtonClickedEvent onJumpInClick => jumpinButton?.onClick;
 0125    public Button.ButtonClickedEvent onInfoClick => infoButton?.onClick;
 126
 127    public override void Awake()
 128    {
 73129        base.Awake();
 130
 73131        if (placeImage != null)
 73132            placeImage.OnLoaded += OnPlaceImageLoaded;
 133
 73134        if (cardSelectionFrame != null)
 39135            cardSelectionFrame.SetActive(false);
 136
 73137        if (closeCardButton != null)
 34138            closeCardButton.onClick.AddListener(CloseModal);
 139
 73140        if (closeAction != null)
 34141            closeAction.OnTriggered += OnCloseActionTriggered;
 142
 73143        if (modalBackgroundButton != null)
 34144            modalBackgroundButton.onClick.AddListener(CloseModal);
 145
 73146        CleanFriendHeadsItems();
 73147    }
 148
 149    public void Configure(BaseComponentModel newModel)
 150    {
 7151        model = (PlaceCardComponentModel)newModel;
 152
 7153        InitializeFriendsTracker();
 154
 7155        if (mapInfoHandler != null)
 7156            mapInfoHandler.SetMinimapSceneInfo(model.hotSceneInfo);
 157
 7158        RefreshControl();
 7159    }
 160
 161    public override void RefreshControl()
 162    {
 7163        if (model == null)
 0164            return;
 165
 7166        SetParcels(model.parcels);
 167
 7168        if (model.placePictureSprite != null)
 7169            SetPlacePicture(model.placePictureSprite);
 0170        else if (model.placePictureTexture != null)
 0171            SetPlacePicture(model.placePictureTexture);
 0172        else if (!string.IsNullOrEmpty(model.placePictureUri))
 0173            SetPlacePicture(model.placePictureUri);
 174        else
 0175            OnPlaceImageLoaded(null);
 176
 7177        SetPlaceName(model.placeName);
 7178        SetPlaceDescription(model.placeDescription);
 7179        SetPlaceAuthor(model.placeAuthor);
 7180        SetNumberOfUsers(model.numberOfUsers);
 7181        SetCoords(model.coords);
 182
 7183        RebuildCardLayouts();
 7184    }
 185
 186    public override void OnFocus()
 187    {
 1188        base.OnFocus();
 189
 1190        if (cardSelectionFrame != null)
 1191            cardSelectionFrame.SetActive(true);
 192
 1193        if (cardAnimator != null)
 1194            cardAnimator.SetBool(ON_FOCUS_CARD_COMPONENT_BOOL, true);
 1195    }
 196
 197    public override void OnLoseFocus()
 198    {
 1199        base.OnLoseFocus();
 200
 1201        if (cardSelectionFrame != null)
 1202            cardSelectionFrame.SetActive(false);
 203
 1204        if (cardAnimator != null)
 1205            cardAnimator.SetBool(ON_FOCUS_CARD_COMPONENT_BOOL, false);
 1206    }
 207
 208    public override void Dispose()
 209    {
 129210        base.Dispose();
 211
 129212        if (placeImage != null)
 213        {
 129214            placeImage.OnLoaded -= OnPlaceImageLoaded;
 129215            placeImage.Dispose();
 216        }
 217
 129218        if (closeCardButton != null)
 68219            closeCardButton.onClick.RemoveAllListeners();
 220
 129221        if (closeAction != null)
 68222            closeAction.OnTriggered -= OnCloseActionTriggered;
 223
 129224        if (modalBackgroundButton != null)
 68225            modalBackgroundButton.onClick.RemoveAllListeners();
 226
 129227        if (friendsHandler != null)
 228        {
 10229            friendsHandler.OnFriendAddedEvent -= OnFriendAdded;
 10230            friendsHandler.OnFriendRemovedEvent -= OnFriendRemoved;
 231        }
 232
 129233        if (friendsGrid != null)
 61234            friendsGrid.Dispose();
 129235    }
 236
 237    public void SetPlacePicture(Sprite sprite)
 238    {
 9239        model.placePictureSprite = sprite;
 240
 9241        if (placeImage == null)
 0242            return;
 243
 9244        placeImage.SetImage(sprite);
 9245    }
 246
 247    public void SetPlacePicture(Texture2D texture)
 248    {
 1249        model.placePictureTexture = texture;
 250
 1251        if (!Application.isPlaying)
 0252            return;
 253
 1254        if (placeImage == null)
 0255            return;
 256
 1257        placeImage.SetImage(texture);
 1258    }
 259
 260    public void SetPlacePicture(string uri)
 261    {
 1262        model.placePictureUri = uri;
 263
 1264        if (!Application.isPlaying)
 0265            return;
 266
 1267        if (placeImage == null)
 0268            return;
 269
 1270        placeImage.SetImage(uri);
 1271    }
 272
 273    public void SetPlaceName(string newText)
 274    {
 8275        model.placeName = newText;
 276
 8277        if (placeNameOnIdleText != null)
 8278            placeNameOnIdleText.text = newText;
 279
 8280        if (placeNameOnFocusText != null)
 6281            placeNameOnFocusText.text = newText;
 8282    }
 283
 284    public void SetPlaceDescription(string newText)
 285    {
 8286        model.placeDescription = newText;
 287
 8288        if (placeDescText == null)
 5289            return;
 290
 3291        placeDescText.text = newText;
 3292    }
 293
 294    public void SetPlaceAuthor(string newText)
 295    {
 8296        model.placeAuthor = newText;
 297
 8298        if (placeAuthorText == null)
 5299            return;
 300
 3301        placeAuthorText.text = newText;
 3302    }
 303
 304    public void SetNumberOfUsers(int newNumberOfUsers)
 305    {
 8306        model.numberOfUsers = newNumberOfUsers;
 307
 8308        if (numberOfUsersText == null)
 2309            return;
 310
 6311        numberOfUsersText.text = newNumberOfUsers.ToString();
 6312    }
 313
 314    public void SetCoords(Vector2Int newCoords)
 315    {
 8316        model.coords = newCoords;
 317
 8318        if (coordsText == null)
 5319            return;
 320
 3321        coordsText.text = $"{newCoords.x},{newCoords.y}";
 3322    }
 323
 0324    public void SetParcels(Vector2Int[] parcels) { model.parcels = parcels; }
 325
 326    public void SetLoadingIndicatorVisible(bool isVisible)
 327    {
 2328        imageContainer.SetActive(!isVisible);
 2329        placeInfoContainer.SetActive(!isVisible);
 2330        loadingSpinner.SetActive(isVisible);
 2331    }
 332
 333    internal void OnPlaceImageLoaded(Sprite sprite)
 334    {
 1335        if (sprite != null)
 1336            SetPlacePicture(sprite);
 337        else
 0338            SetPlacePicture(MapUtils.GetMarketPlaceThumbnailUrl(model.parcels, THMBL_MARKETPLACE_WIDTH, THMBL_MARKETPLAC
 0339    }
 340
 341    internal void InitializeFriendsTracker()
 342    {
 8343        CleanFriendHeadsItems();
 344
 8345        if (mapInfoHandler == null)
 6346            mapInfoHandler = new MapInfoHandler();
 347
 8348        if (friendsHandler == null)
 349        {
 6350            friendsHandler = new FriendsHandler(mapInfoHandler);
 6351            friendsHandler.OnFriendAddedEvent += OnFriendAdded;
 6352            friendsHandler.OnFriendRemovedEvent += OnFriendRemoved;
 353        }
 8354    }
 355
 356    internal void OnFriendAdded(UserProfile profile, Color backgroundColor)
 357    {
 3358        if (currentFriendHeads.ContainsKey(profile.userId))
 0359            return;
 360
 3361        BaseComponentView newFriend = InstantiateAndConfigureFriendHead(
 362            new FriendHeadForPlaceCardComponentModel
 363            {
 364                userProfile = profile,
 365                backgroundColor = backgroundColor
 366            },
 367            friendHeadPrefab);
 368
 3369        if (friendsGrid != null)
 3370            friendsGrid.AddItem(newFriend);
 371
 3372        currentFriendHeads.Add(profile.userId, newFriend);
 3373    }
 374
 375    internal void OnFriendRemoved(UserProfile profile)
 376    {
 1377        if (!currentFriendHeads.ContainsKey(profile.userId))
 0378            return;
 379
 1380        if (friendsGrid != null)
 1381            friendsGrid.RemoveItem(currentFriendHeads[profile.userId]);
 382
 1383        currentFriendHeads.Remove(profile.userId);
 1384    }
 385
 386    internal void CleanFriendHeadsItems()
 387    {
 82388        if (friendsGrid != null)
 389        {
 46390            friendsGrid.RemoveItems();
 46391            currentFriendHeads.Clear();
 392        }
 82393    }
 394
 395    internal BaseComponentView InstantiateAndConfigureFriendHead(FriendHeadForPlaceCardComponentModel friendInfo, Friend
 396    {
 4397        FriendHeadForPlaceCardComponentView friendHeadGO = GameObject.Instantiate(prefabToUse);
 4398        friendHeadGO.Configure(friendInfo);
 399
 4400        return friendHeadGO;
 401    }
 402
 403    internal void RebuildCardLayouts()
 404    {
 7405        if (contentVerticalLayout != null)
 2406            Utils.ForceRebuildLayoutImmediate(contentVerticalLayout.transform as RectTransform);
 407
 7408        if (infoVerticalLayout != null)
 2409            Utils.ForceRebuildLayoutImmediate(infoVerticalLayout.transform as RectTransform);
 7410    }
 411
 4412    internal void CloseModal() { Hide(); }
 413
 2414    internal void OnCloseActionTriggered(DCLAction_Trigger action) { CloseModal(); }
 415}