< 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
 232        public bool isOpen { get { return gameObject.activeInHierarchy; } }
 33
 34        private void Awake()
 35        {
 5336            minimapMetadata = MinimapMetadata.GetMetadata();
 5337            rectTransform = transform as RectTransform;
 38
 5339            goToButton.onClick.AddListener(OnGotoClick);
 40
 5341            minimapMetadata.OnSceneInfoUpdated += OnMapMetadataInfoUpdated;
 5342        }
 43
 44        private void OnDestroy()
 45        {
 5346            minimapMetadata.OnSceneInfoUpdated -= OnMapMetadataInfoUpdated;
 5347            if (texturePromise != null)
 48            {
 049                AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 050                texturePromise = null;
 51            }
 5352        }
 53
 54        public void Populate(Vector2Int coordinates, MinimapMetadata.MinimapSceneInfo sceneInfo)
 55        {
 156            if (!gameObject.activeSelf)
 157                AudioScriptableObjects.dialogOpen.Play(true);
 58
 159            bool sceneInfoExists = sceneInfo != null;
 60
 161            gameObject.SetActive(true);
 162            scenePreviewImage.gameObject.SetActive(false);
 163            location = coordinates;
 64
 165            PositionToast(coordinates);
 66
 167            sceneLocationText.text = $"{coordinates.x}, {coordinates.y}";
 68
 169            sceneOwnerText.transform.parent.gameObject.SetActive(sceneInfoExists && !string.IsNullOrEmpty(sceneInfo.owne
 170            sceneTitleText.text = "Untitled Scene";
 71
 172            bool useDefaultThumbnail =
 73                !sceneInfoExists || (sceneInfoExists && string.IsNullOrEmpty(sceneInfo.previewImageUrl));
 74
 175            if (useDefaultThumbnail)
 76            {
 177                DisplayThumbnail(scenePreviewFailImage.texture);
 178                currentImageUrl = "";
 79            }
 80
 181            if (sceneInfoExists)
 82            {
 183                sceneTitleText.text = sceneInfo.name;
 184                sceneOwnerText.text = $"Created by: {sceneInfo.owner}";
 85
 186                if (currentImageUrl == sceneInfo.previewImageUrl)
 87                {
 088                    DisplayThumbnail(texturePromise.asset.texture);
 089                    return;
 90                }
 91
 192                if (texturePromise != null)
 93                {
 094                    AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 095                    texturePromise = null;
 96                }
 97
 98
 199                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
 1107                currentImageUrl = sceneInfoExists ? sceneInfo.previewImageUrl : "";
 108            }
 1109        }
 110
 111        public void OnMapMetadataInfoUpdated(MinimapMetadata.MinimapSceneInfo sceneInfo)
 112        {
 2113            if (!isOpen)
 2114                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        {
 1132            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
 1136            toastContainer.position = MapRenderer.i.parcelHighlightImage.transform.position;
 137
 1138            bool useBottom = toastContainer.localPosition.y > 0;
 139
 1140            bool shouldOffsetHorizontally = Mathf.Abs(toastContainer.localPosition.x) > rectTransform.rect.width / 4;
 1141            bool useLeft = false;
 142
 1143            if (shouldOffsetHorizontally)
 1144                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
 1147            toastContainer.pivot = new Vector2(shouldOffsetHorizontally ? (useLeft ? 1 : 0) : 0.5f, useBottom ? 1 : 0);
 1148            toastContainer.position = MapRenderer.i.parcelHighlightImage.transform.position;
 149
 1150        }
 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        {
 1173            scenePreviewImage.texture = texture;
 1174            toastAnimator.SetTrigger(triggerLoadingComplete);
 1175        }
 176    }
 177}