< Summary

Class:DCL.Backpack.AvatarSlotComponentView
Assembly:BackpackEditorHUDV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BackpackEditorHUDV2/AvatarSlot/AvatarSlotComponentView.cs
Covered lines:81
Uncovered lines:45
Coverable lines:126
Total lines:261
Line coverage:64.2% (81 of 126)
Covered branches:0
Total branches:0
Covered methods:16
Total methods:24
Method coverage:66.6% (16 of 24)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarSlotComponentView()0%110100%
Awake()0%110100%
InitializeTooltipPositions()0%110100%
ResetSlot()0%110100%
GetHideList()0%2100%
RefreshControl()0%2.012088.89%
SetHideList(...)0%110100%
SetForceRender(...)0%110100%
SetIsHidden(...)0%440100%
SetCategory(...)0%110100%
SetUnEquipAllowed(...)0%2100%
SetNftImage(...)0%2.152066.67%
SetRarity(...)0%110100%
SetWearableId(...)0%110100%
OnFocus()0%5.395075%
OnLoseFocus()0%110100%
OnSlotClick()0%6200%
UnSelect(...)0%20400%
OnPointerClickOnDifferentSlot()0%2100%
ScaleUpAnimation(...)0%110100%
ScaleDownAndResetAnimation(...)0%2100%
SetHideIconVisible(...)0%220100%
Select(...)0%20400%
ShakeAnimation()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BackpackEditorHUDV2/AvatarSlot/AvatarSlotComponentView.cs

#LineLine coverage
 1using System;
 2using TMPro;
 3using UIComponents.Scripts.Components;
 4using UnityEngine;
 5using DG.Tweening;
 6using System.Collections.Generic;
 7using System.Linq;
 8using UnityEngine.UI;
 9
 10namespace DCL.Backpack
 11{
 12    public class AvatarSlotComponentView : BaseComponentView<AvatarSlotComponentModel>, IAvatarSlotComponentView
 13    {
 14        private const float ANIMATION_TIME = 0.2f;
 15        private const float SHAKE_ANIMATION_TIME = 0.75f;
 16
 17        [SerializeField] internal NftTypeColorSupportingSO typeColorSupporting;
 18        [SerializeField] internal NftTypePreviewCameraFocusConfig previewCameraFocus;
 19        [SerializeField] internal NftTypeIconSO typeIcons;
 20        [SerializeField] internal RectTransform nftContainer;
 21        [SerializeField] internal NftRarityBackgroundSO rarityBackgrounds;
 22        [SerializeField] internal Image typeImage;
 23        [SerializeField] internal ImageComponentView nftImage;
 24        [SerializeField] internal Image backgroundRarityImage;
 25        [SerializeField] internal Image focusedImage;
 26        [SerializeField] internal Image selectedImage;
 27        [SerializeField] private GameObject emptySlot;
 28        [SerializeField] internal GameObject hiddenSlot;
 29        [SerializeField] internal RectTransform tooltipContainer;
 30        [SerializeField] internal TMP_Text tooltipCategoryText;
 31        [SerializeField] internal TMP_Text tooltipHiddenText;
 32        [SerializeField] internal Button button;
 33        [SerializeField] internal Button unequipButton;
 34        [SerializeField] internal Button overriddenHide;
 35        [SerializeField] internal Button normalHide;
 36
 37        public event Action<AvatarSlotComponentModel, bool> OnSelectAvatarSlot;
 38        public event Action<string> OnUnEquip;
 39        public event Action<string> OnFocusHiddenBy;
 40        public event Action<string, bool> OnHideUnhidePressed;
 41
 42        private bool isSelected = false;
 43
 944        private readonly HashSet<string> hiddenByList = new HashSet<string>();
 45        private Vector2 nftContainerDefaultPosition;
 46
 47        public override void Awake()
 48        {
 849            base.Awake();
 50
 851            overriddenHide.onClick.RemoveAllListeners();
 852            overriddenHide.onClick.AddListener(()=>
 53            {
 054                AudioScriptableObjects.hide.Play(true);
 055                OnHideUnhidePressed?.Invoke(model.category, false);
 056                SetForceRender(false);
 057            });
 858            normalHide.onClick.RemoveAllListeners();
 859            normalHide.onClick.AddListener(()=>
 60            {
 061                AudioScriptableObjects.show.Play(true);
 062                OnHideUnhidePressed?.Invoke(model.category, true);
 063                SetForceRender(true);
 064            });
 865            button.onClick.RemoveAllListeners();
 866            button.onClick.AddListener(OnSlotClick);
 867            unequipButton.onClick.RemoveAllListeners();
 868            unequipButton.onClick.AddListener(()=>
 69            {
 070                OnUnEquip?.Invoke(model.wearableId);
 071                unequipButton.gameObject.SetActive(false);
 072            });
 873            ResetSlot();
 874            InitializeTooltipPositions();
 875        }
 76
 77        private void InitializeTooltipPositions()
 78        {
 879            tooltipContainer.gameObject.SetActive(true);
 880            tooltipContainer.gameObject.SetActive(false);
 881            nftContainerDefaultPosition = nftContainer.anchoredPosition;
 882        }
 83
 84        public void ResetSlot()
 85        {
 886            SetRarity(null);
 887            SetNftImage("");
 888            RefreshControl();
 889            SetWearableId("");
 890            SetHideList(Array.Empty<string>());
 891            SetForceRender(false);
 892        }
 93
 94        public string[] GetHideList() =>
 095            model.hidesList;
 96
 97        public override void RefreshControl()
 98        {
 1399            if (model == null)
 0100                return;
 101
 13102            SetCategory(model.category);
 13103            SetRarity(model.rarity);
 13104            SetIsHidden(model.isHidden, model.hiddenBy);
 13105            SetNftImage(model.imageUri);
 13106            SetWearableId(model.wearableId);
 13107            SetHideList(model.hidesList);
 13108        }
 109
 110        public void SetHideList(string[] hideList) =>
 21111            model.hidesList = hideList;
 112
 113        public void SetForceRender(bool isOverridden)
 114        {
 8115            overriddenHide.gameObject.SetActive(isOverridden);
 8116            normalHide.gameObject.SetActive(!isOverridden);
 8117        }
 118
 119        public void SetIsHidden(bool isHidden, string hiddenBy)
 120        {
 18121            model.isHidden = isHidden;
 122
 18123            if (isHidden)
 4124                hiddenByList.Add(hiddenBy);
 125            else
 14126                hiddenByList.Remove(hiddenBy);
 127
 24128            List<string> sortedList = hiddenByList.OrderBy(x => WearableItem.CATEGORIES_PRIORITY.IndexOf(x)).ToList();
 129
 18130            if (sortedList.Count > 0)
 131            {
 5132                SetHideIconVisible(true);
 5133                tooltipHiddenText.gameObject.SetActive(!string.IsNullOrEmpty(model.wearableId));
 5134                tooltipHiddenText.text = $"Hidden by {WearableItem.CATEGORIES_READABLE_MAPPING[sortedList[0]]}";
 5135                model.hiddenBy = sortedList[0];
 136            }
 137            else
 138            {
 13139                SetHideIconVisible(false);
 13140                tooltipHiddenText.gameObject.SetActive(false);
 13141                model.hiddenBy = "";
 142            }
 13143        }
 144
 145        public void SetCategory(string category)
 146        {
 14147            model.category = category;
 14148            model.allowsColorChange = typeColorSupporting.IsColorSupportedByType(category);
 14149            model.previewCameraFocus = previewCameraFocus.GetPreviewCameraFocus(category);
 14150            typeImage.sprite = typeIcons.GetTypeImage(category);
 14151            WearableItem.CATEGORIES_READABLE_MAPPING.TryGetValue(category, out string readableCategory);
 14152            tooltipCategoryText.text = readableCategory;
 14153        }
 154
 155        public void SetUnEquipAllowed(bool allowUnEquip) =>
 0156            model.unEquipAllowed = allowUnEquip;
 157
 158        public void SetNftImage(string imageUri)
 159        {
 21160            model.imageUri = imageUri;
 21161            if (string.IsNullOrEmpty(imageUri))
 162            {
 21163                nftImage.SetImage(Texture2D.grayTexture);
 21164                nftImage.SetLoadingIndicatorVisible(false);
 21165                emptySlot.SetActive(true);
 21166                return;
 167            }
 168
 0169            emptySlot.SetActive(false);
 0170            nftImage.SetImage(imageUri);
 0171        }
 172
 173        public void SetRarity(string rarity)
 174        {
 22175            model.rarity = rarity;
 22176            backgroundRarityImage.sprite = rarityBackgrounds.GetRarityImage(rarity);
 22177        }
 178
 179        public void SetWearableId(string wearableId) =>
 21180            model.wearableId = wearableId;
 181
 182        public override void OnFocus()
 183        {
 1184            focusedImage.enabled = true;
 1185            tooltipContainer.gameObject.SetActive(true);
 186
 1187            if (model.unEquipAllowed && !string.IsNullOrEmpty(model.imageUri))
 0188                unequipButton.gameObject.SetActive(true);
 189
 1190            if(model.isHidden)
 0191                OnFocusHiddenBy?.Invoke(model.hiddenBy);
 192
 1193            ScaleUpAnimation(focusedImage.transform);
 1194        }
 195
 196        public override void OnLoseFocus()
 197        {
 9198            focusedImage.enabled = false;
 9199            tooltipContainer.gameObject.SetActive(false);
 9200            unequipButton.gameObject.SetActive(false);
 9201        }
 202
 203        public void OnSlotClick()
 204        {
 0205            if (!isSelected)
 0206                Select(true);
 207            else
 0208                UnSelect(true);
 0209        }
 210
 211        public void UnSelect(bool notify)
 212        {
 0213            if (!isSelected) return;
 0214            isSelected = false;
 215
 0216            ScaleDownAndResetAnimation(selectedImage);
 217
 0218            if (notify)
 0219                OnSelectAvatarSlot?.Invoke(model, isSelected);
 0220        }
 221
 222        public void OnPointerClickOnDifferentSlot()
 223        {
 0224            isSelected = false;
 0225            selectedImage.enabled = false;
 0226        }
 227
 228        private void ScaleUpAnimation(Transform targetTransform)
 229        {
 1230            targetTransform.transform.localScale = new Vector3(0, 0, 0);
 1231            targetTransform.transform.DOScale(1, ANIMATION_TIME).SetEase(Ease.OutBack);
 1232        }
 233
 234        private void ScaleDownAndResetAnimation(Image targetImage)
 235        {
 0236            targetImage.transform.DOScale(0, ANIMATION_TIME).SetEase(Ease.OutBack).OnComplete(() =>
 237            {
 0238                targetImage.enabled = false;
 0239                targetImage.transform.localScale = new Vector3(1, 1, 1);
 0240            });
 0241        }
 242
 243        public void SetHideIconVisible(bool isVisible) =>
 18244            hiddenSlot.SetActive(isVisible && !string.IsNullOrEmpty(model.wearableId));
 245
 246        public void Select(bool notify)
 247        {
 0248            if (isSelected) return;
 249
 0250            isSelected = true;
 0251            selectedImage.enabled = true;
 0252            ScaleUpAnimation(selectedImage.transform);
 253
 0254            if (notify)
 0255                OnSelectAvatarSlot?.Invoke(model, isSelected);
 0256        }
 257
 258        public void ShakeAnimation() =>
 0259            nftContainer.DOShakePosition(SHAKE_ANIMATION_TIME, 4).OnComplete(() => nftContainer.anchoredPosition = nftCo
 260    }
 261}