< Summary

Class:DCL.Huds.QuestsPanel.QuestsPanelPopup
Assembly:QuestsPanelHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/QuestsPanelHUD/QuestsPanelPopup.cs
Covered lines:50
Uncovered lines:42
Coverable lines:92
Total lines:203
Line coverage:54.3% (50 of 92)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
QuestsPanelPopup()0%110100%
Awake()0%110100%
Populate(...)0%550100%
OnPinToggleValueChanged(...)0%30500%
OnPinnedQuests(...)0%12300%
OnUnpinnedQuest(...)0%12300%
SetThumbnail(...)0%5.323036.36%
OnThumbnailReady(...)0%2100%
CreateSection()0%110100%
PrepareSections(...)0%9.065045.45%
Show()0%110100%
Close()0%110100%
Update()0%12300%
SetRewards(...)0%110100%
OnDestroy()0%3.213071.43%
SetPositionToReadMoreButton(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/QuestsPanelHUD/QuestsPanelPopup.cs

#LineLine coverage
 1using DCL.Helpers;
 2using System.Collections.Generic;
 3using System.Linq;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8namespace DCL.Huds.QuestsPanel
 9{
 10    public class QuestsPanelPopup : MonoBehaviour
 11    {
 12        private const int ARROW_OFFSET = 220;
 13
 14        [SerializeField] internal TextMeshProUGUI questName;
 15        [SerializeField] internal TextMeshProUGUI description;
 16        [SerializeField] internal RectTransform sectionsContainer;
 17        [SerializeField] internal GameObject sectionPrefab;
 18        [SerializeField] internal Button closeButton;
 19        [SerializeField] internal Toggle pinQuestToggle;
 20        [SerializeField] internal RawImage thumbnailImage;
 21        [SerializeField] internal Button closePopupAreaButton;
 22        [SerializeField] private DynamicScrollSensitivity dynamicScrollSensitivity;
 23        [SerializeField] internal GameObject rewardsPanel;
 24        [SerializeField] internal TextMeshProUGUI rewardsAmount;
 25
 26        private AssetPromise_Texture thumbnailPromise;
 27        private bool forceRebuildLayout = false;
 28
 29        internal QuestModel quest;
 2030        internal readonly List<QuestsPanelSection> sections = new List<QuestsPanelSection>();
 4231        private static BaseCollection<string> pinnedQuests => DataStore.i.Quests.pinnedQuests;
 32        private RectTransform rectTransform;
 33
 34        private void Awake()
 35        {
 736            rectTransform = GetComponent<RectTransform>();
 37
 738            closeButton.onClick.AddListener(Close);
 39
 740            pinQuestToggle.onValueChanged.AddListener(OnPinToggleValueChanged);
 741            pinnedQuests.OnAdded += OnPinnedQuests;
 742            pinnedQuests.OnRemoved += OnUnpinnedQuest;
 43
 744            closePopupAreaButton.onClick.AddListener(Close);
 745        }
 46
 47        public void Populate(QuestModel newQuest)
 48        {
 749            quest = newQuest;
 2150            QuestSection[] availableSections = quest.sections.Where(x => x.tasks.Any(y => y.status != QuestsLiterals.Sta
 751            PrepareSections(availableSections.Length);
 52
 753            questName.text = quest.name;
 754            description.text = quest.description;
 755            SetThumbnail(quest.thumbnail_banner);
 2856            for (int i = 0; i < availableSections.Length; i++)
 57            {
 758                sections[i].Populate(quest, availableSections[i]);
 59            }
 60
 761            pinQuestToggle.SetIsOnWithoutNotify(pinnedQuests.Contains(quest.id));
 762            pinQuestToggle.gameObject.SetActive(!quest.isCompleted);
 763            forceRebuildLayout = true;
 64
 765            SetRewards(quest.rewards?.Length ?? 0);
 766        }
 67
 68        private void OnPinToggleValueChanged(bool isOn)
 69        {
 070            if (quest == null)
 071                return;
 72
 073            if (isOn)
 74            {
 075                if (!pinnedQuests.Contains(quest.id))
 076                    pinnedQuests.Add(quest.id);
 077            }
 78            else
 79            {
 080                if (pinnedQuests.Contains(quest.id))
 081                    pinnedQuests.Remove(quest.id);
 82            }
 83
 084            QuestsUIAnalytics.SendQuestPinChanged(quest.id, isOn, QuestsUIAnalytics.UIContext.QuestDetails);
 085        }
 86
 87        private void OnPinnedQuests(string questId)
 88        {
 089            if (quest != null && quest.id == questId)
 090                pinQuestToggle.SetIsOnWithoutNotify(true);
 091        }
 92
 93        private void OnUnpinnedQuest(string questId)
 94        {
 095            if (quest != null && quest.id == questId)
 096                pinQuestToggle.SetIsOnWithoutNotify(false);
 097        }
 98
 99        internal void SetThumbnail(string thumbnailURL)
 100        {
 7101            if (thumbnailPromise != null)
 102            {
 0103                thumbnailPromise.ClearEvents();
 0104                AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise);
 105            }
 106
 7107            if (string.IsNullOrEmpty(thumbnailURL))
 108            {
 7109                thumbnailImage.gameObject.SetActive(false);
 7110                return;
 111            }
 112
 0113            thumbnailPromise = new AssetPromise_Texture(thumbnailURL);
 0114            thumbnailPromise.OnSuccessEvent += OnThumbnailReady;
 0115            thumbnailPromise.OnFailEvent += (x, error) =>
 116            {
 0117                thumbnailImage.gameObject.SetActive(false);
 0118                Debug.Log($"Error downloading quest panel popup thumbnail: {thumbnailURL}, Exception: {error}");
 0119            };
 120
 0121            AssetPromiseKeeper_Texture.i.Keep(thumbnailPromise);
 0122        }
 123
 124        private void OnThumbnailReady(Asset_Texture assetTexture)
 125        {
 0126            thumbnailImage.gameObject.SetActive(true);
 0127            thumbnailImage.texture = assetTexture.texture;
 0128        }
 129
 14130        internal void CreateSection() { sections.Add(Instantiate(sectionPrefab, sectionsContainer).GetComponent<QuestsPa
 131
 132        internal void PrepareSections(int sectionsAmount)
 133        {
 7134            if (sections.Count == sectionsAmount)
 0135                return;
 136
 7137            if (sections.Count < sectionsAmount)
 138            {
 14139                while (sections.Count < sectionsAmount)
 7140                    CreateSection();
 7141            }
 142            else
 143            {
 0144                while (sections.Count > sectionsAmount)
 145                {
 0146                    var section = sections.Last();
 0147                    sections.RemoveAt(sections.Count - 1);
 0148                    Destroy(section.gameObject);
 149                }
 150            }
 0151        }
 152
 153        public void Show()
 154        {
 7155            gameObject.SetActive(true);
 7156            AudioScriptableObjects.dialogOpen.Play();
 7157        }
 158
 159        public void Close()
 160        {
 21161            gameObject.SetActive(false);
 21162            AudioScriptableObjects.dialogClose.Play();
 21163        }
 164
 165        private void Update()
 166        {
 0167            if (forceRebuildLayout)
 168            {
 0169                forceRebuildLayout = false;
 0170                rectTransform.ForceUpdateLayout();
 0171                dynamicScrollSensitivity?.RecalculateSensitivity();
 172            }
 0173        }
 174
 175        private void SetRewards(int amount)
 176        {
 7177            rewardsPanel.SetActive(amount > 0);
 7178            rewardsAmount.text = amount.ToString();
 7179        }
 180
 181        private void OnDestroy()
 182        {
 7183            if (thumbnailPromise != null)
 184            {
 0185                thumbnailPromise.ClearEvents();
 0186                AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise);
 187            }
 188
 7189            if (pinnedQuests != null)
 190            {
 7191                pinnedQuests.OnAdded -= OnPinnedQuests;
 7192                pinnedQuests.OnRemoved -= OnUnpinnedQuest;
 193            }
 7194        }
 195
 196        public void SetPositionToReadMoreButton(Vector3 position)
 197        {
 7198            Vector3 pos = transform.position;
 7199            pos.y = Mathf.Max(0, position.y - ARROW_OFFSET);
 7200            transform.position = pos;
 7201        }
 202    }
 203}