< 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:51
Uncovered lines:23
Coverable lines:74
Total lines:177
Line coverage:68.9% (51 of 74)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
NavmapToastView()0%110100%
Awake()0%110100%
OnDestroy()0%2.262060%
Populate(...)0%16.2113073.33%
OnMapMetadataInfoUpdated(...)0%19.475016.67%
PositionToast(...)0%10.0810090.91%
OnCloseClick()0%220100%
OnGotoClick()0%6200%
DisplayThumbnail(...)0%110100%

File(s)

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

#LineLine coverage
 1using DCL.Interface;
 2using System.Collections;
 3using TMPro;
 4using UnityEngine;
 5using UnityEngine.Networking;
 6using UnityEngine.UI;
 7
 8namespace DCL
 9{
 10    public class NavmapToastView : MonoBehaviour
 11    {
 112        private static readonly int triggerLoadingComplete = Animator.StringToHash("LoadingComplete");
 13
 14        [SerializeField] internal TextMeshProUGUI sceneTitleText;
 15        [SerializeField] internal TextMeshProUGUI sceneOwnerText;
 16        [SerializeField] internal TextMeshProUGUI sceneLocationText;
 17        [SerializeField] internal RectTransform toastContainer;
 18        [SerializeField] internal GameObject scenePreviewContainer;
 19        [SerializeField] internal RawImageFillParent scenePreviewImage;
 20        [SerializeField] internal Sprite scenePreviewFailImage;
 21        [SerializeField] internal Animator toastAnimator;
 22
 23        [SerializeField] internal Button goToButton;
 24        Vector2Int location;
 25        RectTransform rectTransform;
 26        MinimapMetadata minimapMetadata;
 27
 28        AssetPromise_Texture texturePromise = null;
 29
 30        public System.Action OnGotoClicked;
 31
 532        public bool isOpen { get { return gameObject.activeInHierarchy; } }
 33
 34        private void Awake()
 35        {
 10236            minimapMetadata = MinimapMetadata.GetMetadata();
 10237            rectTransform = transform as RectTransform;
 38
 10239            goToButton.onClick.AddListener(OnGotoClick);
 40
 10241            minimapMetadata.OnSceneInfoUpdated += OnMapMetadataInfoUpdated;
 10242        }
 43
 44        private void OnDestroy()
 45        {
 10146            minimapMetadata.OnSceneInfoUpdated -= OnMapMetadataInfoUpdated;
 10147            if (texturePromise != null)
 48            {
 049                AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 050                texturePromise = null;
 51            }
 10152        }
 53
 54        public void Populate(Vector2Int coordinates, MinimapMetadata.MinimapSceneInfo sceneInfo)
 55        {
 256            if (!gameObject.activeSelf)
 257                AudioScriptableObjects.dialogOpen.Play(true);
 58
 259            bool sceneInfoExists = sceneInfo != null;
 60
 261            gameObject.SetActive(true);
 262            scenePreviewImage.gameObject.SetActive(false);
 263            location = coordinates;
 64
 265            PositionToast(coordinates);
 66
 267            sceneLocationText.text = $"{coordinates.x}, {coordinates.y}";
 68
 269            sceneOwnerText.transform.parent.gameObject.SetActive(sceneInfoExists && !string.IsNullOrEmpty(sceneInfo.owne
 270            sceneTitleText.text = "Untitled Scene";
 71
 272            bool useDefaultThumbnail =
 73                !sceneInfoExists || (sceneInfoExists && string.IsNullOrEmpty(sceneInfo.previewImageUrl));
 74
 275            if (useDefaultThumbnail)
 76            {
 277                DisplayThumbnail(scenePreviewFailImage.texture);
 278                currentImageUrl = "";
 79            }
 80
 281            if (sceneInfoExists)
 82            {
 283                sceneTitleText.text = sceneInfo.name;
 284                sceneOwnerText.text = $"Created by: {sceneInfo.owner}";
 85
 286                if (currentImageUrl == sceneInfo.previewImageUrl)
 87                {
 088                    DisplayThumbnail(texturePromise.asset.texture);
 089                    return;
 90                }
 91
 292                if (texturePromise != null)
 93                {
 094                    AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 095                    texturePromise = null;
 96                }
 97
 98
 299                if (!string.IsNullOrEmpty(sceneInfo.previewImageUrl))
 100                {
 0101                    texturePromise = new AssetPromise_Texture(sceneInfo.previewImageUrl, storeTexAsNonReadable: false);
 0102                    texturePromise.OnSuccessEvent += (textureAsset) => { DisplayThumbnail(textureAsset.texture); };
 0103                    texturePromise.OnFailEvent += (textureAsset) => { DisplayThumbnail(scenePreviewFailImage.texture); }
 0104                    AssetPromiseKeeper_Texture.i.Keep(texturePromise);
 105                }
 106
 2107                currentImageUrl = sceneInfoExists ? sceneInfo.previewImageUrl : "";
 108            }
 2109        }
 110
 111        public void OnMapMetadataInfoUpdated(MinimapMetadata.MinimapSceneInfo sceneInfo)
 112        {
 5113            if (!isOpen)
 5114                return;
 115
 0116            bool updatedCurrentLocationInfo = false;
 0117            foreach (Vector2Int parcel in sceneInfo.parcels)
 118            {
 0119                if (parcel == location)
 120                {
 0121                    updatedCurrentLocationInfo = true;
 0122                    break;
 123                }
 124            }
 125
 0126            if (updatedCurrentLocationInfo)
 0127                Populate(location, sceneInfo);
 0128        }
 129
 130        void PositionToast(Vector2Int coordinates)
 131        {
 2132            if (toastContainer == null || rectTransform == null)
 0133                return;
 134
 135            // position the toast over the parcel parcelHighlightImage so that we can easily check with LOCAL pos info w
 2136            toastContainer.position = MapRenderer.i.parcelHighlightImage.transform.position;
 137
 2138            bool useBottom = toastContainer.localPosition.y > 0;
 139
 2140            bool shouldOffsetHorizontally = Mathf.Abs(toastContainer.localPosition.x) > rectTransform.rect.width / 4;
 2141            bool useLeft = false;
 142
 2143            if (shouldOffsetHorizontally)
 2144                useLeft = toastContainer.localPosition.x > 0;
 145
 146            // By setting the pivot accordingly BEFORE we position the toast, we can have it always visible in an easier
 2147            toastContainer.pivot = new Vector2(shouldOffsetHorizontally ? (useLeft ? 1 : 0) : 0.5f, useBottom ? 1 : 0);
 2148            toastContainer.position = MapRenderer.i.parcelHighlightImage.transform.position;
 149
 2150        }
 151
 152        public void OnCloseClick()
 153        {
 1154            if (gameObject.activeSelf)
 1155                AudioScriptableObjects.dialogClose.Play(true);
 156
 1157            gameObject.SetActive(false);
 1158        }
 159
 160        private void OnGotoClick()
 161        {
 0162            OnGotoClicked?.Invoke();
 163
 0164            WebInterface.GoTo(location.x, location.y);
 165
 0166            OnCloseClick();
 0167        }
 168
 169        string currentImageUrl;
 170
 171        private void DisplayThumbnail(Texture2D texture)
 172        {
 2173            scenePreviewImage.texture = texture;
 2174            toastAnimator.SetTrigger(triggerLoadingComplete);
 2175        }
 176    }
 177}