< 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:150
Uncovered lines:29
Coverable lines:179
Total lines:458
Line coverage:83.7% (150 of 179)
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%
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%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;
 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
 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
 401123    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    {
 178132        base.Awake();
 133
 178134        if (placeImage != null)
 178135            placeImage.OnLoaded += OnPlaceImageLoaded;
 136
 178137        if (cardSelectionFrame != null)
 66138            cardSelectionFrame.SetActive(false);
 139
 178140        if (closeCardButton != null)
 34141            closeCardButton.onClick.AddListener(CloseModal);
 142
 178143        if (closeAction != null)
 34144            closeAction.OnTriggered += OnCloseActionTriggered;
 145
 178146        if (modalBackgroundButton != null)
 34147            modalBackgroundButton.onClick.AddListener(CloseModal);
 148
 178149        CleanFriendHeadsItems();
 178150    }
 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    {
 224204        base.OnLoseFocus();
 205
 224206        if (cardSelectionFrame != null)
 77207            cardSelectionFrame.SetActive(false);
 208
 224209        if (cardAnimator != null)
 190210            cardAnimator.SetBool(ON_FOCUS_CARD_COMPONENT_BOOL, false);
 224211    }
 212
 213    public override void Show(bool instant = false)
 214    {
 4215        base.Show(instant);
 216
 4217        DataStore.i.exploreV2.isSomeModalOpen.Set(true);
 4218    }
 219
 220    public override void Hide(bool instant = false)
 221    {
 29222        base.Hide(instant);
 223
 29224        DataStore.i.exploreV2.isSomeModalOpen.Set(false);
 29225    }
 226
 227    public override void Dispose()
 228    {
 258229        base.Dispose();
 230
 258231        if (placeImage != null)
 232        {
 258233            placeImage.OnLoaded -= OnPlaceImageLoaded;
 258234            placeImage.Dispose();
 235        }
 236
 258237        if (closeCardButton != null)
 92238            closeCardButton.onClick.RemoveAllListeners();
 239
 258240        if (closeAction != null)
 92241            closeAction.OnTriggered -= OnCloseActionTriggered;
 242
 258243        if (modalBackgroundButton != null)
 92244            modalBackgroundButton.onClick.RemoveAllListeners();
 245
 258246        if (friendsHandler != null)
 247        {
 18248            friendsHandler.OnFriendAddedEvent -= OnFriendAdded;
 18249            friendsHandler.OnFriendRemovedEvent -= OnFriendRemoved;
 250        }
 251
 258252        if (friendsGrid != null)
 165253            friendsGrid.Dispose();
 258254    }
 255
 256    public void SetPlacePicture(Sprite sprite)
 257    {
 14258        if (sprite == null && defaultPicture != null)
 0259            sprite = defaultPicture;
 260
 14261        model.placePictureSprite = sprite;
 262
 14263        if (placeImage == null)
 0264            return;
 265
 14266        placeImage.SetImage(sprite);
 14267    }
 268
 269    public void SetPlacePicture(Texture2D texture)
 270    {
 1271        if (texture == null && defaultPicture != null)
 272        {
 0273            SetPlacePicture(defaultPicture);
 0274            return;
 275        }
 276
 1277        model.placePictureTexture = texture;
 278
 1279        if (!Application.isPlaying)
 0280            return;
 281
 1282        if (placeImage == null)
 0283            return;
 284
 1285        placeImage.SetImage(texture);
 1286    }
 287
 288    public void SetPlacePicture(string uri)
 289    {
 1290        if (string.IsNullOrEmpty(uri) && defaultPicture != null)
 291        {
 0292            SetPlacePicture(defaultPicture);
 0293            return;
 294        }
 295
 1296        model.placePictureUri = uri;
 297
 1298        if (!Application.isPlaying)
 0299            return;
 300
 1301        if (placeImage == null)
 0302            return;
 303
 1304        placeImage.SetImage(uri);
 1305    }
 306
 307    public void SetPlaceName(string newText)
 308    {
 13309        model.placeName = newText;
 310
 13311        if (placeNameOnIdleText != null)
 13312            placeNameOnIdleText.text = newText;
 313
 13314        if (placeNameOnFocusText != null)
 8315            placeNameOnFocusText.text = newText;
 13316    }
 317
 318    public void SetPlaceDescription(string newText)
 319    {
 13320        model.placeDescription = newText;
 321
 13322        if (placeDescText == null)
 7323            return;
 324
 6325        placeDescText.text = newText;
 6326    }
 327
 328    public void SetPlaceAuthor(string newText)
 329    {
 13330        model.placeAuthor = newText;
 331
 13332        if (placeAuthorText == null)
 9333            return;
 334
 4335        placeAuthorText.text = newText;
 4336    }
 337
 338    public void SetNumberOfUsers(int newNumberOfUsers)
 339    {
 13340        model.numberOfUsers = newNumberOfUsers;
 341
 13342        if (numberOfUsersText == null)
 3343            return;
 344
 10345        numberOfUsersText.text = newNumberOfUsers.ToString();
 10346    }
 347
 348    public void SetCoords(Vector2Int newCoords)
 349    {
 13350        model.coords = newCoords;
 351
 13352        if (coordsText == null)
 9353            return;
 354
 4355        coordsText.text = $"{newCoords.x},{newCoords.y}";
 4356    }
 357
 0358    public void SetParcels(Vector2Int[] parcels) { model.parcels = parcels; }
 359
 360    public void SetLoadingIndicatorVisible(bool isVisible)
 361    {
 2362        imageContainer.SetActive(!isVisible);
 2363        placeInfoContainer.SetActive(!isVisible);
 2364        loadingSpinner.SetActive(isVisible);
 2365    }
 366
 367    internal void OnPlaceImageLoaded(Sprite sprite)
 368    {
 1369        if (sprite != null)
 370        {
 1371            SetPlacePicture(sprite);
 1372        }
 0373        else if (!thumbnailFromMarketPlaceRequested)
 374        {
 0375            thumbnailFromMarketPlaceRequested = true;
 0376            SetPlacePicture(MapUtils.GetMarketPlaceThumbnailUrl(model.parcels, THMBL_MARKETPLACE_WIDTH, THMBL_MARKETPLAC
 0377        }
 378        else
 379        {
 0380            SetPlacePicture(sprite: null);
 381        }
 0382    }
 383
 384    internal void InitializeFriendsTracker()
 385    {
 13386        CleanFriendHeadsItems();
 387
 13388        if (mapInfoHandler == null)
 11389            mapInfoHandler = new MapInfoHandler();
 390
 13391        if (friendsHandler == null)
 392        {
 11393            friendsHandler = new FriendsHandler(mapInfoHandler);
 11394            friendsHandler.OnFriendAddedEvent += OnFriendAdded;
 11395            friendsHandler.OnFriendRemovedEvent += OnFriendRemoved;
 396        }
 13397    }
 398
 399    internal void OnFriendAdded(UserProfile profile, Color backgroundColor)
 400    {
 3401        if (currentFriendHeads.ContainsKey(profile.userId))
 0402            return;
 403
 3404        BaseComponentView newFriend = InstantiateAndConfigureFriendHead(
 405            new FriendHeadForPlaceCardComponentModel
 406            {
 407                userProfile = profile,
 408                backgroundColor = backgroundColor
 409            },
 410            friendHeadPrefab);
 411
 3412        if (friendsGrid != null)
 3413            friendsGrid.AddItem(newFriend);
 414
 3415        currentFriendHeads.Add(profile.userId, newFriend);
 3416    }
 417
 418    internal void OnFriendRemoved(UserProfile profile)
 419    {
 1420        if (!currentFriendHeads.ContainsKey(profile.userId))
 0421            return;
 422
 1423        if (friendsGrid != null)
 1424            friendsGrid.RemoveItem(currentFriendHeads[profile.userId]);
 425
 1426        currentFriendHeads.Remove(profile.userId);
 1427    }
 428
 429    internal void CleanFriendHeadsItems()
 430    {
 192431        if (friendsGrid != null)
 432        {
 155433            friendsGrid.RemoveItems();
 155434            currentFriendHeads.Clear();
 435        }
 192436    }
 437
 438    internal BaseComponentView InstantiateAndConfigureFriendHead(FriendHeadForPlaceCardComponentModel friendInfo, Friend
 439    {
 4440        FriendHeadForPlaceCardComponentView friendHeadGO = GameObject.Instantiate(prefabToUse);
 4441        friendHeadGO.Configure(friendInfo);
 442
 4443        return friendHeadGO;
 444    }
 445
 446    internal void RebuildCardLayouts()
 447    {
 12448        if (contentVerticalLayout != null)
 3449            Utils.ForceRebuildLayoutImmediate(contentVerticalLayout.transform as RectTransform);
 450
 12451        if (infoVerticalLayout != null)
 3452            Utils.ForceRebuildLayoutImmediate(infoVerticalLayout.transform as RectTransform);
 12453    }
 454
 4455    internal void CloseModal() { Hide(); }
 456
 2457    internal void OnCloseActionTriggered(DCLAction_Trigger action) { CloseModal(); }
 458}