< 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:149
Uncovered lines:25
Coverable lines:174
Total lines:453
Line coverage:85.6% (149 of 174)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlaceCardComponentView()0%110100%
Awake()0%660100%
Configure(...)0%220100%
RefreshControl()0%5.935066.67%
OnFocus()0%330100%
OnLoseFocus()0%330100%
Show(...)0%110100%
Hide(...)0%110100%
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%110100%
SetLoadingIndicatorVisible(...)0%110100%
OnPlaceImageLoaded(...)0%12300%
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;
 2using DCL.Helpers;
 3using System.Collections.Generic;
 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<PlaceCardCompone
 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
 89    [Header("Assets References")]
 90    [SerializeField] internal FriendHeadForPlaceCardComponentView friendHeadPrefab;
 91
 92    [Header("Prefab References")]
 93    [SerializeField] internal ImageComponentView placeImage;
 94    [SerializeField] internal TMP_Text placeNameOnIdleText;
 95    [SerializeField] internal TMP_Text placeNameOnFocusText;
 96    [SerializeField] internal TMP_Text placeDescText;
 97    [SerializeField] internal TMP_Text placeAuthorText;
 98    [SerializeField] internal TMP_Text numberOfUsersText;
 99    [SerializeField] internal TMP_Text coordsText;
 100    [SerializeField] internal Button modalBackgroundButton;
 101    [SerializeField] internal ButtonComponentView closeCardButton;
 102    [SerializeField] internal InputAction_Trigger closeAction;
 103    [SerializeField] internal ButtonComponentView infoButton;
 104    [SerializeField] internal ButtonComponentView jumpinButton;
 105    [SerializeField] internal GridContainerComponentView friendsGrid;
 106    [SerializeField] internal GameObject imageContainer;
 107    [SerializeField] internal GameObject placeInfoContainer;
 108    [SerializeField] internal GameObject loadingSpinner;
 109    [SerializeField] internal GameObject cardSelectionFrame;
 110    [SerializeField] internal VerticalLayoutGroup contentVerticalLayout;
 111    [SerializeField] internal VerticalLayoutGroup infoVerticalLayout;
 112    [SerializeField] internal PlaceCardAnimatorBase cardAnimator;
 113
 114    [Header("Configuration")]
 115    [SerializeField] internal Sprite defaultPicture;
 116    [SerializeField] internal PlaceCardComponentModel model;
 117
 827118    public FriendsHandler friendsHandler { get; set; }
 167119    internal MapInfoHandler mapInfoHandler { get; set; }
 120
 438121    internal readonly Dictionary<string, BaseComponentView> currentFriendHeads = new ();
 122
 82123    public Button.ButtonClickedEvent onJumpInClick => jumpinButton != null ? jumpinButton.onClick : new Button.ButtonCli
 82124    public Button.ButtonClickedEvent onInfoClick => infoButton != null ? infoButton.onClick : new Button.ButtonClickedEv
 125
 126    private bool thumbnailFromMarketPlaceRequested;
 127
 128    public override void Awake()
 129    {
 417130        base.Awake();
 131
 417132        if (placeImage != null)
 417133            placeImage.OnLoaded += OnPlaceImageLoaded;
 134
 417135        if (cardSelectionFrame != null)
 191136            cardSelectionFrame.SetActive(false);
 137
 417138        if (closeCardButton != null)
 46139            closeCardButton.onClick.AddListener(CloseModal);
 140
 417141        if (closeAction != null)
 46142            closeAction.OnTriggered += OnCloseActionTriggered;
 143
 417144        if (modalBackgroundButton != null)
 46145            modalBackgroundButton.onClick.AddListener(CloseModal);
 146
 417147        CleanFriendHeadsItems();
 417148    }
 149
 150    public void Configure(PlaceCardComponentModel newModel)
 151    {
 42152        model = newModel;
 153
 42154        InitializeFriendsTracker();
 155
 42156        if (mapInfoHandler != null)
 42157            mapInfoHandler.SetMinimapSceneInfo(model.hotSceneInfo);
 158
 42159        RefreshControl();
 42160    }
 161
 162    public override void RefreshControl()
 163    {
 42164        thumbnailFromMarketPlaceRequested = false;
 165
 42166        if (model == null)
 0167            return;
 168
 42169        SetParcels(model.parcels);
 170
 42171        if (model.placePictureSprite != null)
 42172            SetPlacePicture(model.placePictureSprite);
 0173        else if (model.placePictureTexture != null)
 0174            SetPlacePicture(model.placePictureTexture);
 0175        else if (!string.IsNullOrEmpty(model.placePictureUri))
 0176            SetPlacePicture(model.placePictureUri);
 177        else
 0178            OnPlaceImageLoaded(null);
 179
 42180        SetPlaceName(model.placeName);
 42181        SetPlaceDescription(model.placeDescription);
 42182        SetPlaceAuthor(model.placeAuthor);
 42183        SetNumberOfUsers(model.numberOfUsers);
 42184        SetCoords(model.coords);
 185
 42186        RebuildCardLayouts();
 42187    }
 188
 189    public override void OnFocus()
 190    {
 1191        base.OnFocus();
 192
 1193        if (cardSelectionFrame != null)
 1194            cardSelectionFrame.SetActive(true);
 195
 1196        cardAnimator?.Focus();
 1197    }
 198
 199    public override void OnLoseFocus()
 200    {
 506201        base.OnLoseFocus();
 202
 506203        if (cardSelectionFrame != null)
 264204            cardSelectionFrame.SetActive(false);
 205
 506206        cardAnimator?.Idle();
 460207    }
 208
 209    public override void Show(bool instant = false)
 210    {
 4211        base.Show(instant);
 212
 4213        DataStore.i.exploreV2.isSomeModalOpen.Set(true);
 4214    }
 215
 216    public override void Hide(bool instant = false)
 217    {
 142218        base.Hide(instant);
 219
 142220        DataStore.i.exploreV2.isSomeModalOpen.Set(false);
 142221    }
 222
 223    public override void Dispose()
 224    {
 673225        base.Dispose();
 226
 673227        if (placeImage != null)
 228        {
 673229            placeImage.OnLoaded -= OnPlaceImageLoaded;
 673230            placeImage.Dispose();
 231        }
 232
 673233        if (closeCardButton != null)
 281234            closeCardButton.onClick.RemoveAllListeners();
 235
 673236        if (closeAction != null)
 281237            closeAction.OnTriggered -= OnCloseActionTriggered;
 238
 673239        if (modalBackgroundButton != null)
 281240            modalBackgroundButton.onClick.RemoveAllListeners();
 241
 673242        if (friendsHandler != null)
 243        {
 26244            friendsHandler.OnFriendAddedEvent -= OnFriendAdded;
 26245            friendsHandler.OnFriendRemovedEvent -= OnFriendRemoved;
 246        }
 247
 673248        if (friendsGrid != null)
 391249            friendsGrid.Dispose();
 673250    }
 251
 252    public void SetPlacePicture(Sprite sprite)
 253    {
 43254        if (sprite == null && defaultPicture != null)
 0255            sprite = defaultPicture;
 256
 43257        model.placePictureSprite = sprite;
 258
 43259        if (placeImage == null)
 0260            return;
 261
 43262        placeImage.SetImage(sprite);
 43263    }
 264
 265    public void SetPlacePicture(Texture2D texture)
 266    {
 1267        if (texture == null && defaultPicture != null)
 268        {
 0269            SetPlacePicture(defaultPicture);
 0270            return;
 271        }
 272
 1273        model.placePictureTexture = texture;
 274
 1275        if (!Application.isPlaying)
 0276            return;
 277
 1278        if (placeImage == null)
 0279            return;
 280
 1281        placeImage.SetImage(texture);
 1282    }
 283
 284    public void SetPlacePicture(string uri)
 285    {
 1286        if (string.IsNullOrEmpty(uri) && defaultPicture != null)
 287        {
 0288            SetPlacePicture(defaultPicture);
 0289            return;
 290        }
 291
 1292        model.placePictureUri = uri;
 293
 1294        if (!Application.isPlaying)
 0295            return;
 296
 1297        if (placeImage == null)
 0298            return;
 299
 1300        placeImage.SetImage(uri);
 1301    }
 302
 303    public void SetPlaceName(string newText)
 304    {
 43305        model.placeName = newText;
 306
 43307        if (placeNameOnIdleText != null)
 43308            placeNameOnIdleText.text = newText;
 309
 43310        if (placeNameOnFocusText != null)
 38311            placeNameOnFocusText.text = newText;
 43312    }
 313
 314    public void SetPlaceDescription(string newText)
 315    {
 43316        model.placeDescription = newText;
 317
 43318        if (placeDescText == null)
 37319            return;
 320
 6321        placeDescText.text = newText;
 6322    }
 323
 324    public void SetPlaceAuthor(string newText)
 325    {
 43326        model.placeAuthor = newText;
 327
 43328        if (placeAuthorText == null)
 39329            return;
 330
 4331        placeAuthorText.text = newText;
 4332    }
 333
 334    public void SetNumberOfUsers(int newNumberOfUsers)
 335    {
 43336        model.numberOfUsers = newNumberOfUsers;
 337
 43338        if (numberOfUsersText == null)
 3339            return;
 340
 40341        numberOfUsersText.text = newNumberOfUsers.ToString();
 40342    }
 343
 344    public void SetCoords(Vector2Int newCoords)
 345    {
 43346        model.coords = newCoords;
 347
 43348        if (coordsText == null)
 39349            return;
 350
 4351        coordsText.text = $"{newCoords.x},{newCoords.y}";
 4352    }
 353
 86354    public void SetParcels(Vector2Int[] parcels) { model.parcels = parcels; }
 355
 356    public void SetLoadingIndicatorVisible(bool isVisible)
 357    {
 2358        imageContainer.SetActive(!isVisible);
 2359        placeInfoContainer.SetActive(!isVisible);
 2360        loadingSpinner.SetActive(isVisible);
 2361    }
 362
 363    internal void OnPlaceImageLoaded(Sprite sprite)
 364    {
 0365        if (sprite != null)
 0366            return;
 367
 0368        if (!thumbnailFromMarketPlaceRequested)
 369        {
 0370            thumbnailFromMarketPlaceRequested = true;
 0371            SetPlacePicture(MapUtils.GetMarketPlaceThumbnailUrl(model.parcels, THMBL_MARKETPLACE_WIDTH, THMBL_MARKETPLAC
 372        }
 373        else
 374        {
 0375            SetPlacePicture(sprite: null);
 376        }
 0377    }
 378
 379    internal void InitializeFriendsTracker()
 380    {
 43381        CleanFriendHeadsItems();
 382
 43383        if (mapInfoHandler == null)
 19384            mapInfoHandler = new MapInfoHandler();
 385
 43386        if (friendsHandler == null)
 387        {
 19388            friendsHandler = new FriendsHandler(mapInfoHandler);
 19389            friendsHandler.OnFriendAddedEvent += OnFriendAdded;
 19390            friendsHandler.OnFriendRemovedEvent += OnFriendRemoved;
 391        }
 43392    }
 393
 394    internal void OnFriendAdded(UserProfile profile, Color backgroundColor)
 395    {
 3396        if (currentFriendHeads.ContainsKey(profile.userId))
 0397            return;
 398
 3399        BaseComponentView newFriend = InstantiateAndConfigureFriendHead(
 400            new FriendHeadForPlaceCardComponentModel
 401            {
 402                userProfile = profile,
 403                backgroundColor = backgroundColor
 404            },
 405            friendHeadPrefab);
 406
 3407        if (friendsGrid != null)
 3408            friendsGrid.AddItemWithResize(newFriend);
 409
 3410        currentFriendHeads.Add(profile.userId, newFriend);
 3411    }
 412
 413    internal void OnFriendRemoved(UserProfile profile)
 414    {
 1415        if (!currentFriendHeads.ContainsKey(profile.userId))
 0416            return;
 417
 1418        if (friendsGrid != null)
 1419            friendsGrid.RemoveItem(currentFriendHeads[profile.userId]);
 420
 1421        currentFriendHeads.Remove(profile.userId);
 1422    }
 423
 424    internal void CleanFriendHeadsItems()
 425    {
 461426        if (friendsGrid != null)
 427        {
 412428            friendsGrid.RemoveItems();
 412429            currentFriendHeads.Clear();
 430        }
 461431    }
 432
 433    internal BaseComponentView InstantiateAndConfigureFriendHead(FriendHeadForPlaceCardComponentModel friendInfo, Friend
 434    {
 4435        FriendHeadForPlaceCardComponentView friendHeadGO = GameObject.Instantiate(prefabToUse);
 4436        friendHeadGO.Configure(friendInfo);
 437
 4438        return friendHeadGO;
 439    }
 440
 441    internal void RebuildCardLayouts()
 442    {
 42443        if (contentVerticalLayout != null)
 3444            Utils.ForceRebuildLayoutImmediate(contentVerticalLayout.transform as RectTransform);
 445
 42446        if (infoVerticalLayout != null)
 3447            Utils.ForceRebuildLayoutImmediate(infoVerticalLayout.transform as RectTransform);
 42448    }
 449
 4450    internal void CloseModal() { Hide(); }
 451
 2452    internal void OnCloseActionTriggered(DCLAction_Trigger action) { CloseModal(); }
 453}