< Summary

Class:DCL.Huds.QuestsPanel.QuestsPanelEntry
Assembly:QuestsPanelHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/QuestsPanelHUD/QuestsPanelEntry.cs
Covered lines:46
Uncovered lines:47
Coverable lines:93
Total lines:202
Line coverage:49.4% (46 of 93)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
QuestsPanelEntry()0%110100%
Awake()0%110100%
OnEnable()0%660100%
Populate(...)0%10100100%
OnPinToggleValueChanged(...)0%30500%
OnPinnedQuests(...)0%330100%
OnUnpinnedQuest(...)0%12300%
SetThumbnail(...)0%12.584018.75%
OnThumbnailReady(...)0%2100%
SetRewards(...)0%110100%
OnDestroy()0%2.092071.43%
Unparent()0%2.062075%
SelfDestroy()0%2.062075%

File(s)

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

#LineLine coverage
 1using DCL.Helpers;
 2using DCL.Interface;
 3using System;
 4using System.Linq;
 5using TMPro;
 6using UnityEngine;
 7using UnityEngine.UI;
 8
 9namespace DCL.Huds.QuestsPanel
 10{
 11    public class QuestsPanelEntry : MonoBehaviour
 12    {
 113        private static readonly int LOADED_ANIM_TRIGGER = Animator.StringToHash("Loaded");
 14        public event Action<string> OnReadMoreClicked;
 15
 16        [SerializeField] internal TextMeshProUGUI questName;
 17        [SerializeField] internal TextMeshProUGUI description;
 18        [SerializeField] internal Button readMoreButton;
 19        [SerializeField] internal Toggle pinQuestToggle;
 20        [SerializeField] internal Image progressInTitle;
 21        [SerializeField] internal RectTransform completedProgressInTitle;
 22        [SerializeField] internal RectTransform completedMarkInTitle;
 23        [SerializeField] internal RawImage thumbnailImage;
 24        [SerializeField] internal Button jumpInButton;
 25        [SerializeField] internal Animator animator;
 26        [SerializeField] internal GameObject rewardsPanel;
 27        [SerializeField] internal TextMeshProUGUI rewardsAmount;
 28
 29        private AssetPromise_Texture thumbnailPromise;
 30
 31        private QuestModel quest;
 32        private string currentThumbnail;
 33
 34        internal Action readMoreDelegate;
 035        private static BaseCollection<string> pinnedQuests => DataStore.i.Quests.pinnedQuests;
 36
 37        private Action jumpInDelegate;
 738        public Vector3 readMorePosition => readMoreButton.transform.position;
 39
 40        private bool isDestroyed = false;
 41
 42        private void Awake()
 43        {
 1644            jumpInButton.onClick.AddListener(() => { jumpInDelegate?.Invoke(); });
 1645            readMoreButton.onClick.AddListener(() => readMoreDelegate?.Invoke());
 1646            pinQuestToggle.onValueChanged.AddListener(OnPinToggleValueChanged);
 1647            pinnedQuests.OnAdded += OnPinnedQuests;
 1648            pinnedQuests.OnRemoved += OnUnpinnedQuest;
 1649        }
 50
 51        private void OnEnable()
 52        {
 1653            if (quest == null || string.IsNullOrEmpty(currentThumbnail) || (thumbnailPromise != null && thumbnailPromise
 1654                animator?.SetTrigger(LOADED_ANIM_TRIGGER);
 1655        }
 56
 57        public void Populate(QuestModel newQuest)
 58        {
 1759            quest = newQuest;
 60
 5061            QuestTask incompletedTask = quest.sections.FirstOrDefault(x => x.progress < 1)?.tasks.FirstOrDefault(x => x.
 1762            jumpInButton.gameObject.SetActive(incompletedTask != null && !string.IsNullOrEmpty(incompletedTask?.coordina
 1763            jumpInDelegate = () =>
 64            {
 065                if (incompletedTask == null)
 066                    return;
 67
 068                QuestsUIAnalytics.SendJumpInPressed(quest.id, incompletedTask.id, incompletedTask.coordinates, QuestsUIA
 069                WebInterface.SendChatMessage(new ChatMessage
 70                {
 71                    messageType = ChatMessage.Type.NONE,
 72                    recipient = string.Empty,
 73                    body = $"/goto {incompletedTask.coordinates}",
 74                });
 075            };
 76
 1777            readMoreDelegate = () => OnReadMoreClicked?.Invoke(quest.id);
 1778            questName.text = quest.name;
 1779            description.text = quest.description;
 1780            SetThumbnail(quest.thumbnail_entry);
 1781            pinQuestToggle.SetIsOnWithoutNotify(pinnedQuests.Contains(quest.id));
 82
 1783            pinQuestToggle.gameObject.SetActive(!quest.isCompleted);
 1784            progressInTitle.transform.localScale = new Vector3(quest.progress, 1, 1);
 1785            completedProgressInTitle.gameObject.SetActive(quest.isCompleted);
 1786            completedMarkInTitle.gameObject.SetActive(quest.isCompleted);
 87
 1788            SetRewards(quest.rewards?.Length ?? 0);
 1789        }
 90
 91        private void OnPinToggleValueChanged(bool isOn)
 92        {
 093            if (quest == null)
 094                return;
 95
 096            if (!quest.canBePinned)
 97            {
 098                pinnedQuests.Remove(quest.id);
 099                pinQuestToggle.SetIsOnWithoutNotify(false);
 0100                return;
 101            }
 102
 0103            if (isOn)
 104            {
 0105                if (!pinnedQuests.Contains(quest.id))
 0106                    pinnedQuests.Add(quest.id);
 0107            }
 108            else
 109            {
 0110                pinnedQuests.Remove(quest.id);
 111            }
 112
 0113            QuestsUIAnalytics.SendQuestPinChanged(quest.id, isOn, QuestsUIAnalytics.UIContext.QuestsLog);
 0114        }
 115
 116        private void OnPinnedQuests(string questId)
 117        {
 16118            if (quest != null && quest.id == questId)
 12119                pinQuestToggle.SetIsOnWithoutNotify(true);
 16120        }
 121
 122        private void OnUnpinnedQuest(string questId)
 123        {
 0124            if (quest != null && quest.id == questId)
 0125                pinQuestToggle.SetIsOnWithoutNotify(false);
 0126        }
 127
 128        internal void SetThumbnail(string thumbnailURL)
 129        {
 17130            if (thumbnailURL == currentThumbnail)
 131            {
 17132                animator.SetTrigger(LOADED_ANIM_TRIGGER);
 17133                return;
 134            }
 135
 0136            currentThumbnail = thumbnailURL;
 0137            if (thumbnailPromise != null)
 138            {
 0139                thumbnailPromise.ClearEvents();
 0140                AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise);
 141            }
 142
 0143            if (string.IsNullOrEmpty(currentThumbnail))
 144            {
 0145                thumbnailImage.gameObject.SetActive(false);
 0146                animator.SetTrigger(LOADED_ANIM_TRIGGER);
 0147                return;
 148            }
 149
 0150            thumbnailPromise = new AssetPromise_Texture(currentThumbnail);
 0151            thumbnailPromise.OnSuccessEvent += OnThumbnailReady;
 0152            thumbnailPromise.OnFailEvent += x =>
 153            {
 0154                thumbnailImage.gameObject.SetActive(false);
 0155                animator.SetTrigger(LOADED_ANIM_TRIGGER);
 0156                Debug.LogError($"Error downloading quest panel entry thumbnail: {currentThumbnail}");
 0157            };
 158
 0159            AssetPromiseKeeper_Texture.i.Keep(thumbnailPromise);
 0160        }
 161
 162        private void OnThumbnailReady(Asset_Texture assetTexture)
 163        {
 0164            thumbnailImage.gameObject.SetActive(true);
 0165            thumbnailImage.texture = assetTexture.texture;
 0166            animator.SetTrigger(LOADED_ANIM_TRIGGER);
 0167        }
 168
 169        private void SetRewards(int amount)
 170        {
 17171            rewardsPanel.SetActive(amount > 0);
 17172            rewardsAmount.text = amount.ToString();
 17173        }
 174
 175        private void OnDestroy()
 176        {
 16177            if (thumbnailPromise != null)
 178            {
 0179                thumbnailPromise.ClearEvents();
 0180                AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise);
 181            }
 182
 16183            pinnedQuests.OnAdded -= OnUnpinnedQuest;
 16184            pinnedQuests.OnRemoved -= OnPinnedQuests;
 16185            isDestroyed = true;
 16186        }
 187
 188        public void Unparent()
 189        {
 1190            if (isDestroyed)
 0191                return;
 1192            transform.parent = null;
 1193        }
 194
 195        public void SelfDestroy()
 196        {
 1197            if (isDestroyed)
 0198                return;
 1199            Destroy(gameObject);
 1200        }
 201    }
 202}