< 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:47
Uncovered lines:46
Coverable lines:93
Total lines:204
Line coverage:50.5% (47 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;
 8135        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        {
 2053            if (quest == null || string.IsNullOrEmpty(currentThumbnail) || (thumbnailPromise != null && thumbnailPromise
 2054                animator?.SetTrigger(LOADED_ANIM_TRIGGER);
 2055        }
 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                });
 75
 076                DataStore.i.HUDs.questsPanelVisible.Set(false);
 077            };
 78
 1779            readMoreDelegate = () => OnReadMoreClicked?.Invoke(quest.id);
 1780            questName.text = quest.name;
 1781            description.text = quest.description;
 1782            SetThumbnail(quest.thumbnail_entry);
 1783            pinQuestToggle.SetIsOnWithoutNotify(pinnedQuests.Contains(quest.id));
 84
 1785            pinQuestToggle.gameObject.SetActive(!quest.isCompleted);
 1786            progressInTitle.transform.localScale = new Vector3(quest.progress, 1, 1);
 1787            completedProgressInTitle.gameObject.SetActive(quest.isCompleted);
 1788            completedMarkInTitle.gameObject.SetActive(quest.isCompleted);
 89
 1790            SetRewards(quest.rewards?.Length ?? 0);
 1791        }
 92
 93        private void OnPinToggleValueChanged(bool isOn)
 94        {
 095            if (quest == null)
 096                return;
 97
 098            if (!quest.canBePinned)
 99            {
 0100                pinnedQuests.Remove(quest.id);
 0101                pinQuestToggle.SetIsOnWithoutNotify(false);
 0102                return;
 103            }
 104
 0105            if (isOn)
 106            {
 0107                if (!pinnedQuests.Contains(quest.id))
 0108                    pinnedQuests.Add(quest.id);
 109            }
 110            else
 111            {
 0112                pinnedQuests.Remove(quest.id);
 113            }
 114
 0115            QuestsUIAnalytics.SendQuestPinChanged(quest.id, isOn, QuestsUIAnalytics.UIContext.QuestsLog);
 0116        }
 117
 118        private void OnPinnedQuests(string questId)
 119        {
 16120            if (quest != null && quest.id == questId)
 12121                pinQuestToggle.SetIsOnWithoutNotify(true);
 16122        }
 123
 124        private void OnUnpinnedQuest(string questId)
 125        {
 0126            if (quest != null && quest.id == questId)
 0127                pinQuestToggle.SetIsOnWithoutNotify(false);
 0128        }
 129
 130        internal void SetThumbnail(string thumbnailURL)
 131        {
 17132            if (thumbnailURL == currentThumbnail)
 133            {
 17134                animator.SetTrigger(LOADED_ANIM_TRIGGER);
 17135                return;
 136            }
 137
 0138            currentThumbnail = thumbnailURL;
 0139            if (thumbnailPromise != null)
 140            {
 0141                thumbnailPromise.ClearEvents();
 0142                AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise);
 143            }
 144
 0145            if (string.IsNullOrEmpty(currentThumbnail))
 146            {
 0147                thumbnailImage.gameObject.SetActive(false);
 0148                animator.SetTrigger(LOADED_ANIM_TRIGGER);
 0149                return;
 150            }
 151
 0152            thumbnailPromise = new AssetPromise_Texture(currentThumbnail);
 0153            thumbnailPromise.OnSuccessEvent += OnThumbnailReady;
 0154            thumbnailPromise.OnFailEvent += (x, error) =>
 155            {
 0156                thumbnailImage.gameObject.SetActive(false);
 0157                animator.SetTrigger(LOADED_ANIM_TRIGGER);
 0158                Debug.LogError($"Error downloading quest panel entry thumbnail: {currentThumbnail}, Exception: {error}")
 0159            };
 160
 0161            AssetPromiseKeeper_Texture.i.Keep(thumbnailPromise);
 0162        }
 163
 164        private void OnThumbnailReady(Asset_Texture assetTexture)
 165        {
 0166            thumbnailImage.gameObject.SetActive(true);
 0167            thumbnailImage.texture = assetTexture.texture;
 0168            animator.SetTrigger(LOADED_ANIM_TRIGGER);
 0169        }
 170
 171        private void SetRewards(int amount)
 172        {
 17173            rewardsPanel.SetActive(amount > 0);
 17174            rewardsAmount.text = amount.ToString();
 17175        }
 176
 177        private void OnDestroy()
 178        {
 16179            if (thumbnailPromise != null)
 180            {
 0181                thumbnailPromise.ClearEvents();
 0182                AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise);
 183            }
 184
 16185            pinnedQuests.OnAdded -= OnUnpinnedQuest;
 16186            pinnedQuests.OnRemoved -= OnPinnedQuests;
 16187            isDestroyed = true;
 16188        }
 189
 190        public void Unparent()
 191        {
 1192            if (isDestroyed)
 0193                return;
 1194            transform.parent = null;
 1195        }
 196
 197        public void SelfDestroy()
 198        {
 1199            if (isDestroyed)
 0200                return;
 1201            Destroy(gameObject);
 1202        }
 203    }
 204}