< 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:12
Uncovered lines:62
Coverable lines:74
Total lines:177
Line coverage:16.2% (12 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%1821300%
OnMapMetadataInfoUpdated(...)0%19.475016.67%
PositionToast(...)0%1101000%
OnCloseClick()0%6200%
OnGotoClick()0%6200%
DisplayThumbnail(...)0%2100%

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
 132        public bool isOpen { get { return gameObject.activeInHierarchy; } }
 33
 34        private void Awake()
 35        {
 5236            minimapMetadata = MinimapMetadata.GetMetadata();
 5237            rectTransform = transform as RectTransform;
 38
 5239            goToButton.onClick.AddListener(OnGotoClick);
 40
 5241            minimapMetadata.OnSceneInfoUpdated += OnMapMetadataInfoUpdated;
 5242        }
 43
 44        private void OnDestroy()
 45        {
 5246            minimapMetadata.OnSceneInfoUpdated -= OnMapMetadataInfoUpdated;
 5247            if (texturePromise != null)
 48            {
 049                AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 050                texturePromise = null;
 51            }
 5252        }
 53
 54        public void Populate(Vector2Int coordinates, MinimapMetadata.MinimapSceneInfo sceneInfo)
 55        {
 056            if (!gameObject.activeSelf)
 057                AudioScriptableObjects.dialogOpen.Play(true);
 58
 059            bool sceneInfoExists = sceneInfo != null;
 60
 061            gameObject.SetActive(true);
 062            scenePreviewImage.gameObject.SetActive(false);
 063            location = coordinates;
 64
 065            PositionToast(coordinates);
 66
 067            sceneLocationText.text = $"{coordinates.x}, {coordinates.y}";
 68
 069            sceneOwnerText.transform.parent.gameObject.SetActive(sceneInfoExists && !string.IsNullOrEmpty(sceneInfo.owne
 070            sceneTitleText.text = "Untitled Scene";
 71
 072            bool useDefaultThumbnail =
 73                !sceneInfoExists || (sceneInfoExists && string.IsNullOrEmpty(sceneInfo.previewImageUrl));
 74
 075            if (useDefaultThumbnail)
 76            {
 077                DisplayThumbnail(scenePreviewFailImage.texture);
 078                currentImageUrl = "";
 79            }
 80
 081            if (sceneInfoExists)
 82            {
 083                sceneTitleText.text = sceneInfo.name;
 084                sceneOwnerText.text = $"Created by: {sceneInfo.owner}";
 85
 086                if (currentImageUrl == sceneInfo.previewImageUrl)
 87                {
 088                    DisplayThumbnail(texturePromise.asset.texture);
 089                    return;
 90                }
 91
 092                if (texturePromise != null)
 93                {
 094                    AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 095                    texturePromise = null;
 96                }
 97
 98
 099                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, error) => { DisplayThumbnail(scenePreviewFailImage.text
 0104                    AssetPromiseKeeper_Texture.i.Keep(texturePromise);
 105                }
 106
 0107                currentImageUrl = sceneInfoExists ? sceneInfo.previewImageUrl : "";
 108            }
 0109        }
 110
 111        public void OnMapMetadataInfoUpdated(MinimapMetadata.MinimapSceneInfo sceneInfo)
 112        {
 1113            if (!isOpen)
 1114                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        {
 0132            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
 0136            toastContainer.position = MapRenderer.i.parcelHighlightImage.transform.position;
 137
 0138            bool useBottom = toastContainer.localPosition.y > 0;
 139
 0140            bool shouldOffsetHorizontally = Mathf.Abs(toastContainer.localPosition.x) > rectTransform.rect.width / 4;
 0141            bool useLeft = false;
 142
 0143            if (shouldOffsetHorizontally)
 0144                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
 0147            toastContainer.pivot = new Vector2(shouldOffsetHorizontally ? (useLeft ? 1 : 0) : 0.5f, useBottom ? 1 : 0);
 0148            toastContainer.position = MapRenderer.i.parcelHighlightImage.transform.position;
 149
 0150        }
 151
 152        public void OnCloseClick()
 153        {
 0154            if (gameObject.activeSelf)
 0155                AudioScriptableObjects.dialogClose.Play(true);
 156
 0157            gameObject.SetActive(false);
 0158        }
 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        {
 0173            scenePreviewImage.texture = texture;
 0174            toastAnimator.SetTrigger(triggerLoadingComplete);
 0175        }
 176    }
 177}