< 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:144
Uncovered lines:29
Coverable lines:173
Total lines:444
Line coverage:83.2% (144 of 173)
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%5.935066.67%
OnFocus()0%330100%
OnLoseFocus()0%330100%
Dispose()0%770100%
SetPlacePicture(...)0%4.374071.43%
SetPlacePicture(...)0%6.65060%
SetPlacePicture(...)0%6.65060%
SetPlaceName(...)0%330100%
SetPlaceDescription(...)0%220100%
SetPlaceAuthor(...)0%220100%
SetNumberOfUsers(...)0%220100%
SetCoords(...)0%220100%
SetParcels(...)0%2100%
SetLoadingIndicatorVisible(...)0%110100%
OnPlaceImageLoaded(...)0%5.673033.33%
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 Sprite defaultPicture;
 118    [SerializeField] internal PlaceCardComponentModel model;
 119
 0120    public FriendsHandler friendsHandler { get; set; }
 0121    internal MapInfoHandler mapInfoHandler { get; set; }
 122
 396123    internal Dictionary<string, BaseComponentView> currentFriendHeads = new Dictionary<string, BaseComponentView>();
 124
 0125    public Button.ButtonClickedEvent onJumpInClick => jumpinButton?.onClick;
 0126    public Button.ButtonClickedEvent onInfoClick => infoButton?.onClick;
 127
 128    internal bool thumbnailFromMarketPlaceRequested = false;
 129
 130    public override void Awake()
 131    {
 326132        base.Awake();
 133
 326134        if (placeImage != null)
 326135            placeImage.OnLoaded += OnPlaceImageLoaded;
 136
 326137        if (cardSelectionFrame != null)
 103138            cardSelectionFrame.SetActive(false);
 139
 326140        if (closeCardButton != null)
 34141            closeCardButton.onClick.AddListener(CloseModal);
 142
 326143        if (closeAction != null)
 34144            closeAction.OnTriggered += OnCloseActionTriggered;
 145
 326146        if (modalBackgroundButton != null)
 34147            modalBackgroundButton.onClick.AddListener(CloseModal);
 148
 326149        CleanFriendHeadsItems();
 326150    }
 151
 152    public void Configure(BaseComponentModel newModel)
 153    {
 12154        model = (PlaceCardComponentModel)newModel;
 155
 12156        InitializeFriendsTracker();
 157
 12158        if (mapInfoHandler != null)
 12159            mapInfoHandler.SetMinimapSceneInfo(model.hotSceneInfo);
 160
 12161        RefreshControl();
 12162    }
 163
 164    public override void RefreshControl()
 165    {
 12166        thumbnailFromMarketPlaceRequested = false;
 167
 12168        if (model == null)
 0169            return;
 170
 12171        SetParcels(model.parcels);
 172
 12173        if (model.placePictureSprite != null)
 12174            SetPlacePicture(model.placePictureSprite);
 0175        else if (model.placePictureTexture != null)
 0176            SetPlacePicture(model.placePictureTexture);
 0177        else if (!string.IsNullOrEmpty(model.placePictureUri))
 0178            SetPlacePicture(model.placePictureUri);
 179        else
 0180            OnPlaceImageLoaded(null);
 181
 12182        SetPlaceName(model.placeName);
 12183        SetPlaceDescription(model.placeDescription);
 12184        SetPlaceAuthor(model.placeAuthor);
 12185        SetNumberOfUsers(model.numberOfUsers);
 12186        SetCoords(model.coords);
 187
 12188        RebuildCardLayouts();
 12189    }
 190
 191    public override void OnFocus()
 192    {
 1193        base.OnFocus();
 194
 1195        if (cardSelectionFrame != null)
 1196            cardSelectionFrame.SetActive(true);
 197
 1198        if (cardAnimator != null)
 1199            cardAnimator.SetBool(ON_FOCUS_CARD_COMPONENT_BOOL, true);
 1200    }
 201
 202    public override void OnLoseFocus()
 203    {
 1204        base.OnLoseFocus();
 205
 1206        if (cardSelectionFrame != null)
 1207            cardSelectionFrame.SetActive(false);
 208
 1209        if (cardAnimator != null)
 1210            cardAnimator.SetBool(ON_FOCUS_CARD_COMPONENT_BOOL, false);
 1211    }
 212
 213    public override void Dispose()
 214    {
 406215        base.Dispose();
 216
 406217        if (placeImage != null)
 218        {
 406219            placeImage.OnLoaded -= OnPlaceImageLoaded;
 406220            placeImage.Dispose();
 221        }
 222
 406223        if (closeCardButton != null)
 92224            closeCardButton.onClick.RemoveAllListeners();
 225
 406226        if (closeAction != null)
 92227            closeAction.OnTriggered -= OnCloseActionTriggered;
 228
 406229        if (modalBackgroundButton != null)
 92230            modalBackgroundButton.onClick.RemoveAllListeners();
 231
 406232        if (friendsHandler != null)
 233        {
 18234            friendsHandler.OnFriendAddedEvent -= OnFriendAdded;
 18235            friendsHandler.OnFriendRemovedEvent -= OnFriendRemoved;
 236        }
 237
 406238        if (friendsGrid != null)
 313239            friendsGrid.Dispose();
 406240    }
 241
 242    public void SetPlacePicture(Sprite sprite)
 243    {
 14244        if (sprite == null && defaultPicture != null)
 0245            sprite = defaultPicture;
 246
 14247        model.placePictureSprite = sprite;
 248
 14249        if (placeImage == null)
 0250            return;
 251
 14252        placeImage.SetImage(sprite);
 14253    }
 254
 255    public void SetPlacePicture(Texture2D texture)
 256    {
 1257        if (texture == null && defaultPicture != null)
 258        {
 0259            SetPlacePicture(defaultPicture);
 0260            return;
 261        }
 262
 1263        model.placePictureTexture = texture;
 264
 1265        if (!Application.isPlaying)
 0266            return;
 267
 1268        if (placeImage == null)
 0269            return;
 270
 1271        placeImage.SetImage(texture);
 1272    }
 273
 274    public void SetPlacePicture(string uri)
 275    {
 1276        if (string.IsNullOrEmpty(uri) && defaultPicture != null)
 277        {
 0278            SetPlacePicture(defaultPicture);
 0279            return;
 280        }
 281
 1282        model.placePictureUri = uri;
 283
 1284        if (!Application.isPlaying)
 0285            return;
 286
 1287        if (placeImage == null)
 0288            return;
 289
 1290        placeImage.SetImage(uri);
 1291    }
 292
 293    public void SetPlaceName(string newText)
 294    {
 13295        model.placeName = newText;
 296
 13297        if (placeNameOnIdleText != null)
 13298            placeNameOnIdleText.text = newText;
 299
 13300        if (placeNameOnFocusText != null)
 8301            placeNameOnFocusText.text = newText;
 13302    }
 303
 304    public void SetPlaceDescription(string newText)
 305    {
 13306        model.placeDescription = newText;
 307
 13308        if (placeDescText == null)
 7309            return;
 310
 6311        placeDescText.text = newText;
 6312    }
 313
 314    public void SetPlaceAuthor(string newText)
 315    {
 13316        model.placeAuthor = newText;
 317
 13318        if (placeAuthorText == null)
 9319            return;
 320
 4321        placeAuthorText.text = newText;
 4322    }
 323
 324    public void SetNumberOfUsers(int newNumberOfUsers)
 325    {
 13326        model.numberOfUsers = newNumberOfUsers;
 327
 13328        if (numberOfUsersText == null)
 3329            return;
 330
 10331        numberOfUsersText.text = newNumberOfUsers.ToString();
 10332    }
 333
 334    public void SetCoords(Vector2Int newCoords)
 335    {
 13336        model.coords = newCoords;
 337
 13338        if (coordsText == null)
 9339            return;
 340
 4341        coordsText.text = $"{newCoords.x},{newCoords.y}";
 4342    }
 343
 0344    public void SetParcels(Vector2Int[] parcels) { model.parcels = parcels; }
 345
 346    public void SetLoadingIndicatorVisible(bool isVisible)
 347    {
 2348        imageContainer.SetActive(!isVisible);
 2349        placeInfoContainer.SetActive(!isVisible);
 2350        loadingSpinner.SetActive(isVisible);
 2351    }
 352
 353    internal void OnPlaceImageLoaded(Sprite sprite)
 354    {
 1355        if (sprite != null)
 356        {
 1357            SetPlacePicture(sprite);
 1358        }
 0359        else if (!thumbnailFromMarketPlaceRequested)
 360        {
 0361            thumbnailFromMarketPlaceRequested = true;
 0362            SetPlacePicture(MapUtils.GetMarketPlaceThumbnailUrl(model.parcels, THMBL_MARKETPLACE_WIDTH, THMBL_MARKETPLAC
 0363        }
 364        else
 365        {
 0366            SetPlacePicture(sprite: null);
 367        }
 0368    }
 369
 370    internal void InitializeFriendsTracker()
 371    {
 13372        CleanFriendHeadsItems();
 373
 13374        if (mapInfoHandler == null)
 11375            mapInfoHandler = new MapInfoHandler();
 376
 13377        if (friendsHandler == null)
 378        {
 11379            friendsHandler = new FriendsHandler(mapInfoHandler);
 11380            friendsHandler.OnFriendAddedEvent += OnFriendAdded;
 11381            friendsHandler.OnFriendRemovedEvent += OnFriendRemoved;
 382        }
 13383    }
 384
 385    internal void OnFriendAdded(UserProfile profile, Color backgroundColor)
 386    {
 3387        if (currentFriendHeads.ContainsKey(profile.userId))
 0388            return;
 389
 3390        BaseComponentView newFriend = InstantiateAndConfigureFriendHead(
 391            new FriendHeadForPlaceCardComponentModel
 392            {
 393                userProfile = profile,
 394                backgroundColor = backgroundColor
 395            },
 396            friendHeadPrefab);
 397
 3398        if (friendsGrid != null)
 3399            friendsGrid.AddItem(newFriend);
 400
 3401        currentFriendHeads.Add(profile.userId, newFriend);
 3402    }
 403
 404    internal void OnFriendRemoved(UserProfile profile)
 405    {
 1406        if (!currentFriendHeads.ContainsKey(profile.userId))
 0407            return;
 408
 1409        if (friendsGrid != null)
 1410            friendsGrid.RemoveItem(currentFriendHeads[profile.userId]);
 411
 1412        currentFriendHeads.Remove(profile.userId);
 1413    }
 414
 415    internal void CleanFriendHeadsItems()
 416    {
 340417        if (friendsGrid != null)
 418        {
 303419            friendsGrid.RemoveItems();
 303420            currentFriendHeads.Clear();
 421        }
 340422    }
 423
 424    internal BaseComponentView InstantiateAndConfigureFriendHead(FriendHeadForPlaceCardComponentModel friendInfo, Friend
 425    {
 4426        FriendHeadForPlaceCardComponentView friendHeadGO = GameObject.Instantiate(prefabToUse);
 4427        friendHeadGO.Configure(friendInfo);
 428
 4429        return friendHeadGO;
 430    }
 431
 432    internal void RebuildCardLayouts()
 433    {
 12434        if (contentVerticalLayout != null)
 3435            Utils.ForceRebuildLayoutImmediate(contentVerticalLayout.transform as RectTransform);
 436
 12437        if (infoVerticalLayout != null)
 3438            Utils.ForceRebuildLayoutImmediate(infoVerticalLayout.transform as RectTransform);
 12439    }
 440
 4441    internal void CloseModal() { Hide(); }
 442
 2443    internal void OnCloseActionTriggered(DCLAction_Trigger action) { CloseModal(); }
 444}