< Summary

Class:DCL.Huds.QuestsTracker.QuestsTrackerHUDView
Assembly:QuestsTrackerHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/QuestsTrackerHUD/QuestsTrackerHUDView.cs
Covered lines:50
Uncovered lines:14
Coverable lines:64
Total lines:160
Line coverage:78.1% (50 of 64)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
QuestsTrackerHUDView()0%110100%
QuestsTrackerHUDView()0%110100%
Create()0%110100%
Awake()0%110100%
UpdateQuest(...)0%330100%
AddOrUpdateQuest(...)0%5.165081.25%
RemoveEntry(...)0%2.032080%
PinQuest(...)0%220100%
UnpinQuest(...)0%2.062075%
Update()0%330100%
ClearEntries()0%6200%
SetVisibility(...)0%110100%
AddReward(...)0%6200%
Dispose()0%220100%
OnDestroy()0%110100%
RemoveEntriesRoutine()0%6.846071.43%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/QuestsTrackerHUD/QuestsTrackerHUDView.cs

#LineLine coverage
 1using DCL.Helpers;
 2using System;
 3using System.Collections;
 4using System.Collections.Generic;
 5using System.Linq;
 6using UnityEngine;
 7
 8namespace DCL.Huds.QuestsTracker
 9{
 10    public interface IQuestsTrackerHUDView
 11    {
 12        void UpdateQuest(string questId, bool hasProgressed);
 13        void RemoveEntry(string questId);
 14        void PinQuest(string questId);
 15        void UnpinQuest(string questId);
 16        void ClearEntries();
 17        void SetVisibility(bool visibility);
 18        void AddReward(string questId, QuestReward reward);
 19        void Dispose();
 20    }
 21
 22    public class QuestsTrackerHUDView : MonoBehaviour, IQuestsTrackerHUDView
 23    {
 124        internal static int ENTRIES_PER_FRAME { get; set; } = 5;
 1025        private static BaseDictionary<string, QuestModel> quests => DataStore.i.Quests.quests;
 226        private static BaseCollection<string> pinnedQuests => DataStore.i.Quests.pinnedQuests;
 27
 28        [SerializeField] internal RectTransform questsContainer;
 29        [SerializeField] internal GameObject questPrefab;
 30        [SerializeField] private DynamicScrollSensitivity dynamicScrollSensitivity;
 31        [SerializeField] internal QuestsNotificationsController notificationsController;
 32
 1533        internal readonly Dictionary<string, QuestsTrackerEntry> currentEntries = new Dictionary<string, QuestsTrackerEn
 34        private bool layoutRebuildRequested;
 35
 36        private bool isDestroyed = false;
 37
 38        public static QuestsTrackerHUDView Create()
 39        {
 140            QuestsTrackerHUDView view = Instantiate(Resources.Load<GameObject>("QuestsTrackerHUD")).GetComponent<QuestsT
 41
 42#if UNITY_EDITOR
 143            view.gameObject.name = "_QuestsTrackerHUDView";
 44#endif
 145            return view;
 46        }
 47
 2848        private void Awake() { StartCoroutine(RemoveEntriesRoutine()); }
 49
 50        public void UpdateQuest(string questId, bool hasProgressed)
 51        {
 152            if (hasProgressed || currentEntries.ContainsKey(questId))
 153                AddOrUpdateQuest(questId, pinnedQuests.Contains(questId));
 154        }
 55
 56        internal void AddOrUpdateQuest(string questId, bool isPinned)
 57        {
 1058            if (!quests.TryGetValue(questId, out QuestModel quest))
 059                return;
 60
 1061            if (quest.isCompleted && !quest.justProgressed)
 62            {
 063                RemoveEntry(questId);
 064                return;
 65            }
 66
 1067            if (!currentEntries.TryGetValue(questId, out QuestsTrackerEntry questEntry))
 68            {
 1069                questEntry = Instantiate(questPrefab, questsContainer).GetComponent<QuestsTrackerEntry>();
 5670                questEntry.OnLayoutRebuildRequested += () => layoutRebuildRequested = true;
 1071                questEntry.OnQuestCompleted += (x => notificationsController.ShowQuestCompleted(x));
 1072                questEntry.OnRewardObtained += (x => notificationsController.ShowRewardObtained(x));
 1073                currentEntries.Add(quest.id, questEntry);
 74            }
 75
 1076            questEntry.transform.SetSiblingIndex(0);
 77
 1078            questEntry.Populate(quest);
 1079            questEntry.SetPinStatus(isPinned);
 1080            layoutRebuildRequested = true;
 1081        }
 82
 83        public void RemoveEntry(string questId)
 84        {
 185            if (!currentEntries.TryGetValue(questId, out QuestsTrackerEntry entry))
 086                return;
 87
 188            currentEntries.Remove(questId);
 189            entry.StartDestroy();
 190        }
 91
 92        public void PinQuest(string questId)
 93        {
 294            if (currentEntries.TryGetValue(questId, out QuestsTrackerEntry entry))
 95            {
 196                entry.SetPinStatus(true);
 197            }
 98            else
 99            {
 1100                AddOrUpdateQuest(questId, pinnedQuests.Contains(questId));
 101            }
 1102        }
 103
 104        public void UnpinQuest(string questId)
 105        {
 1106            if (!currentEntries.TryGetValue(questId, out QuestsTrackerEntry entry))
 0107                return;
 108
 1109            entry.SetPinStatus(false);
 1110        }
 111
 112        private void Update()
 113        {
 137114            if (layoutRebuildRequested)
 115            {
 4116                layoutRebuildRequested = false;
 4117                questsContainer.ForceUpdateLayout( false);
 4118                dynamicScrollSensitivity?.RecalculateSensitivity();
 119            }
 137120        }
 121
 122        public void ClearEntries()
 123        {
 0124            foreach ((string key, QuestsTrackerEntry value) in currentEntries)
 125            {
 0126                Destroy(value.gameObject);
 127            }
 0128            currentEntries.Clear();
 0129        }
 130
 2131        public void SetVisibility(bool visibility) { gameObject.SetActive(visibility); }
 132        public void AddReward(string questId, QuestReward reward)
 133        {
 0134            if (!currentEntries.TryGetValue(questId, out QuestsTrackerEntry entry))
 0135                return;
 0136            entry.AddRewardToGive(reward);
 0137        }
 138
 139        public void Dispose()
 140        {
 1141            if (!isDestroyed)
 1142                Destroy(gameObject);
 1143        }
 144
 28145        private void OnDestroy() { isDestroyed = true; }
 146
 147        private IEnumerator RemoveEntriesRoutine()
 148        {
 10149            while (true)
 150            {
 24151                var entriesToRemove = currentEntries.Where(x => x.Value.isReadyForDisposal).Select(x => x.Key).ToArray()
 48152                foreach (string questId in entriesToRemove)
 153                {
 0154                    RemoveEntry(questId);
 155                }
 24156                yield return WaitForSecondsCache.Get(0.25f);
 157            }
 158        }
 159    }
 160}