< Summary

Class:DCL.NavmapToastView
Assembly:Navmap
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NavMap/NavmapToastView.cs
Covered lines:66
Uncovered lines:71
Coverable lines:137
Total lines:324
Line coverage:48.1% (66 of 137)
Covered branches:0
Total branches:0
Covered methods:11
Total methods:27
Method coverage:40.7% (11 of 27)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
NavmapToastView()0%110100%
NavmapToastView()0%110100%
Open(...)0%12300%
Awake()0%330100%
OpenShareContextMenu()0%2100%
OnOpenTwitter()0%6200%
OnLinkCopied()0%6200%
Start()0%110100%
OnDestroy()0%4.254075%
Populate(...)0%18.8715074.19%
PositionToast(...)0%10.0810090.91%
SetPlaceId(...)0%2100%
SetVoteButtons(...)0%2100%
ChangeVote(...)0%42600%
Close()0%220100%
OnGotoClick()0%6200%
DisplayThumbnail(...)0%110100%
SetFavoriteLoading(...)0%2100%
SetCurrentFavoriteStatus(...)0%2100%
SetIsAPlace(...)0%42600%
SetInfoButtonEnabled(...)0%12300%
SetPlayerCount(...)0%2100%
SetUserVisits(...)0%2100%
SetUserRating(...)0%12300%
RebuildLayouts()0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NavMap/NavmapToastView.cs

#LineLine coverage
 1using DCL.Helpers;
 2using DCL.Interface;
 3using System;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.Serialization;
 7using UnityEngine.UI;
 8
 9namespace DCL
 10{
 11    public class NavmapToastView : MonoBehaviour
 12    {
 113        private static readonly int triggerLoadingComplete = Animator.StringToHash("LoadingComplete");
 14
 15        public event Action<string, bool> OnFavoriteToggleClicked;
 16        public event Action<int, int> OnGoto;
 17        public event Action OnInfoClick;
 18        public event Action<string, bool?> OnVoteChanged;
 19        public event Action<Vector2Int> OnPressedLinkCopy;
 20        public event Action<Vector2Int, string> OnPressedTwitterButton;
 21
 22        [SerializeField] internal TextMeshProUGUI sceneTitleText;
 23        [SerializeField] internal TextMeshProUGUI sceneOwnerText;
 24        [SerializeField] internal TextMeshProUGUI sceneLocationText;
 25        [SerializeField] internal TextMeshProUGUI playerCountText;
 26        [SerializeField] internal TextMeshProUGUI userVisitsText;
 27        [SerializeField] internal TextMeshProUGUI userRatingText;
 28        [SerializeField] internal RectTransform numberOfUsersContainer;
 29        [SerializeField] internal RectTransform userVisitsAndRatingContainer;
 30        [SerializeField] internal RectTransform toastContainer;
 31        [SerializeField] internal GameObject scenePreviewContainer;
 32        [SerializeField] internal RawImageFillParent scenePreviewImage;
 33        [SerializeField] internal Sprite scenePreviewFailImage;
 34        [SerializeField] internal Animator toastAnimator;
 35        [SerializeField] internal Image infoButtonImage;
 36        [SerializeField] internal Image favoriteButtonImage;
 37        [SerializeField] internal Sprite normalFavorite;
 38        [SerializeField] internal Sprite blockedFavorite;
 39        [SerializeField] internal Color normalImageColor;
 40        [SerializeField] internal Color disabledImageColor;
 41
 42        [SerializeField] internal Button goToButton;
 43        [SerializeField] internal Button infoButton;
 44        [SerializeField] internal Button shareButton;
 45        [SerializeField] internal FavoriteButtonComponentView favoriteToggle;
 46        [SerializeField] internal GameObject favoriteLoading;
 47        [SerializeField] internal ButtonComponentView upvoteButton;
 48        [SerializeField] internal ButtonComponentView downvoteButton;
 49        [SerializeField] internal GameObject upvoteOff;
 50        [SerializeField] internal GameObject upvoteOn;
 51        [SerializeField] internal GameObject downvoteOff;
 52        [SerializeField] internal GameObject downvoteOn;
 53        [SerializeField] internal PlaceCopyContextualMenu placeCopyContextualMenu;
 54
 55        [field: SerializeField]
 56        [Tooltip("Distance in units")]
 457        internal float distanceToCloseView { get; private set; } = 500;
 58
 59        Vector2Int location;
 60        RectTransform rectTransform;
 61        MinimapMetadata minimapMetadata;
 62        private MinimapMetadata.MinimapSceneInfo sceneInfo;
 63
 64        AssetPromise_Texture texturePromise;
 65        string currentImageUrl;
 66        private bool placeIsUpvote;
 67        private bool placeIsDownvote;
 68        private string placeId;
 69
 70        public void Open(Vector2Int parcel, Vector2 worldPosition)
 71        {
 072            if (gameObject.activeInHierarchy)
 073                Close();
 74
 075            var sceneInfo = minimapMetadata.GetSceneInfo(parcel.x, parcel.y);
 076            if (sceneInfo == null)
 077                WebInterface.RequestScenesInfoAroundParcel(parcel, 15);
 78
 079            Populate(parcel, worldPosition, sceneInfo);
 080        }
 81
 82        private void Awake()
 83        {
 184            favoriteToggle.OnFavoriteChange += (uuid, isFavorite) => OnFavoriteToggleClicked?.Invoke(uuid, isFavorite);
 185            infoButton.onClick.RemoveAllListeners();
 186            infoButton.onClick.AddListener(()=>OnInfoClick?.Invoke());
 187            minimapMetadata = MinimapMetadata.GetMetadata();
 188            rectTransform = transform as RectTransform;
 189            if(upvoteButton != null)
 190                upvoteButton.onClick.AddListener(() => ChangeVote(true));
 91
 192            if(downvoteButton != null)
 193                downvoteButton.onClick.AddListener(() => ChangeVote(false));
 94
 195            shareButton.onClick.RemoveAllListeners();
 196            shareButton.onClick.AddListener(OpenShareContextMenu);
 97
 198            placeCopyContextualMenu.OnPlaceLinkCopied += OnLinkCopied;
 199            placeCopyContextualMenu.OnTwitter += OnOpenTwitter;
 100
 1101            placeCopyContextualMenu.Hide(true);
 1102        }
 103
 104        private void OpenShareContextMenu()
 105        {
 0106            placeCopyContextualMenu.Show();
 0107        }
 108
 109        private void OnOpenTwitter()
 110        {
 0111            OnPressedTwitterButton?.Invoke(location, sceneInfo.name);
 0112        }
 113
 114        private void OnLinkCopied()
 115        {
 0116            OnPressedLinkCopy?.Invoke(location);
 0117        }
 118
 119        private void Start()
 120        {
 1121            gameObject.SetActive(false);
 1122            goToButton.onClick.AddListener(OnGotoClick);
 1123        }
 124
 125        private void OnDestroy()
 126        {
 1127            if (texturePromise != null)
 128            {
 0129                AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 0130                texturePromise = null;
 131            }
 1132            if(upvoteButton != null)
 1133                upvoteButton.onClick.RemoveAllListeners();
 134
 1135            if(downvoteButton != null)
 1136                downvoteButton.onClick.RemoveAllListeners();
 1137        }
 138
 139        public void Populate(Vector2Int coordinates, Vector2 worldPosition, MinimapMetadata.MinimapSceneInfo sceneInfo)
 140        {
 1141            if (!gameObject.activeSelf)
 1142                AudioScriptableObjects.dialogOpen.Play(true);
 143
 1144            this.sceneInfo = sceneInfo;
 1145            bool sceneInfoExists = sceneInfo != null;
 146
 1147            gameObject.SetActive(true);
 1148            scenePreviewImage.gameObject.SetActive(false);
 1149            location = coordinates;
 150
 1151            PositionToast(worldPosition);
 152
 1153            sceneLocationText.text = $"{coordinates.x}, {coordinates.y}";
 154
 1155            sceneOwnerText.transform.parent.gameObject.SetActive(sceneInfoExists && !string.IsNullOrEmpty(sceneInfo.owne
 1156            sceneTitleText.text = "Untitled Scene";
 157
 1158            bool useDefaultThumbnail =
 159                !sceneInfoExists || string.IsNullOrEmpty(sceneInfo.previewImageUrl);
 160
 1161            if (useDefaultThumbnail)
 162            {
 1163                DisplayThumbnail(scenePreviewFailImage.texture);
 1164                currentImageUrl = "";
 165            }
 166
 1167            if (sceneInfoExists)
 168            {
 1169                sceneTitleText.text = sceneInfo.name;
 1170                sceneOwnerText.text = $"Created by: {sceneInfo.owner}";
 171
 1172                if (currentImageUrl == sceneInfo.previewImageUrl)
 173                {
 0174                    DisplayThumbnail(texturePromise?.asset?.texture);
 0175                    return;
 176                }
 177
 1178                if (texturePromise != null)
 179                {
 0180                    AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 0181                    texturePromise = null;
 182                }
 183
 1184                if (!string.IsNullOrEmpty(sceneInfo.previewImageUrl))
 185                {
 0186                    texturePromise = new AssetPromise_Texture(sceneInfo.previewImageUrl, storeTexAsNonReadable: false);
 0187                    texturePromise.OnSuccessEvent += (textureAsset) => { DisplayThumbnail(textureAsset.texture); };
 0188                    texturePromise.OnFailEvent += (textureAsset, error) => { DisplayThumbnail(scenePreviewFailImage.text
 0189                    AssetPromiseKeeper_Texture.i.Keep(texturePromise);
 190                }
 191
 1192                currentImageUrl = sceneInfoExists ? sceneInfo.previewImageUrl : "";
 193            }
 1194        }
 195
 196        private void PositionToast(Vector2 worldPosition)
 197        {
 1198            if (toastContainer == null || rectTransform == null)
 0199                return;
 200
 1201            toastContainer.position = worldPosition;
 202
 1203            bool useBottom = toastContainer.localPosition.y > 0;
 204
 1205            bool shouldOffsetHorizontally = Mathf.Abs(toastContainer.localPosition.x) > rectTransform.rect.width / 4;
 1206            bool useLeft = false;
 207
 1208            if (shouldOffsetHorizontally)
 1209                useLeft = toastContainer.localPosition.x > 0;
 210
 211            // By setting the pivot accordingly BEFORE we position the toast, we can have it always visible in an easier
 1212            toastContainer.pivot = new Vector2(shouldOffsetHorizontally ? (useLeft ? 1 : 0) : 0.5f, useBottom ? 1 : 0);
 1213            toastContainer.position = worldPosition;
 1214        }
 215
 216        public void SetPlaceId(string UUID)
 217        {
 0218            placeId = UUID;
 0219        }
 220
 221        public void SetVoteButtons(bool isUpvoted, bool isDownvoted)
 222        {
 0223            placeIsUpvote = isUpvoted;
 0224            placeIsDownvote = isDownvoted;
 0225            upvoteOn.SetActive(isUpvoted);
 0226            upvoteOff.SetActive(!isUpvoted);
 0227            downvoteOn.SetActive(isDownvoted);
 0228            downvoteOff.SetActive(!isDownvoted);
 0229        }
 230
 231        private void ChangeVote(bool upvote)
 232        {
 0233            if (upvote)
 234            {
 0235                OnVoteChanged?.Invoke(placeId, placeIsUpvote ? (bool?)null : true);
 0236                placeIsUpvote = !placeIsUpvote;
 0237                placeIsDownvote = false;
 238            }
 239            else
 240            {
 0241                OnVoteChanged?.Invoke(placeId, placeIsDownvote ? (bool?)null : false);
 0242                placeIsUpvote = false;
 0243                placeIsDownvote = !placeIsDownvote;
 244            }
 0245            SetVoteButtons(placeIsUpvote, placeIsDownvote);
 0246        }
 247
 248        public void Close()
 249        {
 1250            if (gameObject.activeSelf)
 1251                AudioScriptableObjects.dialogClose.Play(true);
 252
 1253            gameObject.SetActive(false);
 1254        }
 255
 256        private void OnGotoClick()
 257        {
 0258            OnGoto?.Invoke(location.x, location.y);
 0259            Close();
 0260        }
 261
 262        private void DisplayThumbnail(Texture2D texture)
 263        {
 1264            scenePreviewImage.texture = texture;
 1265            toastAnimator.SetTrigger(triggerLoadingComplete);
 1266        }
 267
 268        public void SetFavoriteLoading(bool isLoading)
 269        {
 0270            favoriteLoading.SetActive(isLoading);
 0271        }
 272
 273        public void SetCurrentFavoriteStatus(string uuid, bool isFavorite)
 274        {
 0275            favoriteToggle.Configure(new FavoriteButtonComponentModel()
 276            {
 277                placeUUID = uuid,
 278                isFavorite = isFavorite
 279            });
 0280        }
 281
 282        public void SetIsAPlace(bool isAPlace)
 283        {
 0284            favoriteToggle.SetInteractable(isAPlace);
 0285            favoriteButtonImage.sprite = isAPlace ? normalFavorite : blockedFavorite;
 0286            favoriteButtonImage.color = isAPlace ? normalImageColor : disabledImageColor;
 0287            SetInfoButtonEnabled(isAPlace);
 288
 0289            if(!isAPlace)
 0290                favoriteLoading.SetActive(false);
 0291        }
 292
 293        public void SetInfoButtonEnabled(bool isActive)
 294        {
 0295            infoButton.interactable = isActive;
 0296            infoButtonImage.color = isActive ? normalImageColor : disabledImageColor;
 0297        }
 298
 299        public void SetPlayerCount(int players)
 300        {
 0301            numberOfUsersContainer.gameObject.SetActive(players > 0);
 0302            playerCountText.text = players.ToString();
 0303        }
 304
 305        public void SetUserVisits(int userVisitsCount)
 306        {
 0307            userVisitsText.text = userVisitsCount.ToString();
 0308        }
 309
 310        public void SetUserRating(float? userRatingCount)
 311        {
 0312            userRatingText.text = userRatingCount != null ? $"{userRatingCount.Value * 100:0}%" : "-%";
 0313        }
 314
 315        public void RebuildLayouts()
 316        {
 0317            if (numberOfUsersContainer != null)
 0318                Utils.ForceRebuildLayoutImmediate(numberOfUsersContainer);
 319
 0320            if (userVisitsAndRatingContainer != null)
 0321                Utils.ForceRebuildLayoutImmediate(userVisitsAndRatingContainer);
 0322        }
 323    }
 324}