< Summary

Class:DCL.Huds.QuestsPanel.QuestsPanelHUDView
Assembly:QuestsPanelHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/QuestsPanelHUD/QuestsPanelHUDView.cs
Covered lines:94
Uncovered lines:21
Coverable lines:115
Total lines:241
Line coverage:81.7% (94 of 115)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
QuestsPanelHUDView()0%110100%
QuestsPanelHUDView()0%110100%
Create()0%110100%
Awake()0%110100%
RequestAddOrUpdateQuest(...)0%220100%
AddOrUpdateQuest(...)0%5.145082.35%
RemoveQuest(...)0%3.013091.67%
ClearQuests()0%220100%
ShowQuestPopup(...)0%220100%
Update()0%6.846071.43%
OrderQuests()0%12120100%
SetVisibility(...)0%110100%
Dispose()0%220100%
SetAsFullScreenMenuMode(...)0%4.312016.67%
OnDestroy()0%110100%
CloseQuestsPanel()0%2100%

File(s)

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

#LineLine coverage
 1using DCL.Helpers;
 2using System.Collections.Generic;
 3using System.Linq;
 4using UnityEngine;
 5using UnityEngine.UI;
 6
 7namespace DCL.Huds.QuestsPanel
 8{
 9    public interface IQuestsPanelHUDView
 10    {
 11        void RequestAddOrUpdateQuest(string questId);
 12        void RemoveQuest(string questId);
 13        void ClearQuests();
 14        void SetVisibility(bool active);
 15        bool isVisible { get; }
 16        void Dispose();
 17        void SetAsFullScreenMenuMode(Transform parentTransform);
 18    }
 19
 20    public class QuestsPanelHUDView : MonoBehaviour, IQuestsPanelHUDView
 21    {
 122        internal static int ENTRIES_PER_FRAME { get; set; } = 5;
 23        private const string VIEW_PATH = "QuestsPanelHUD";
 24
 25        [SerializeField] internal RectTransform availableQuestsContainer;
 26        [SerializeField] internal RectTransform completedQuestsContainer;
 27        [SerializeField] private GameObject questsContainerSeparators;
 28        [SerializeField] private GameObject questPrefab;
 29        [SerializeField] private GameObject noQuestsTitle;
 30        [SerializeField] internal QuestsPanelPopup questPopup;
 31        [SerializeField] private Button closeButton;
 32        [SerializeField] private Button backgroundButton;
 33        [SerializeField] private DynamicScrollSensitivity dynamicScrollSensitivity;
 34
 3435        private static BaseDictionary<string, QuestModel> quests => DataStore.i.Quests.quests;
 36
 2037        private string currentQuestInPopup = "";
 2038        internal readonly Dictionary<string, QuestsPanelEntry> questEntries =  new Dictionary<string, QuestsPanelEntry>(
 39        private bool orderQuestsRequested = false;
 40        private bool layoutRebuildRequested = false;
 2041        internal readonly List<string> questsToBeAdded = new List<string>();
 42        private bool isDestroyed = false;
 43
 44        internal static QuestsPanelHUDView Create()
 45        {
 146            var view = Instantiate(Resources.Load<GameObject>(VIEW_PATH)).GetComponent<QuestsPanelHUDView>();
 47#if UNITY_EDITOR
 148            view.gameObject.name = "_QuestsPanelHUDView";
 49#endif
 150            return view;
 51        }
 52
 53        public void Awake()
 54        {
 1955            questPopup.gameObject.SetActive(false);
 1956            noQuestsTitle.SetActive(false);
 1957            closeButton.onClick.AddListener(CloseQuestsPanel);
 1958            backgroundButton.onClick.AddListener(CloseQuestsPanel);
 1959        }
 60
 61        public void RequestAddOrUpdateQuest(string questId)
 62        {
 463            if (questsToBeAdded.Contains(questId))
 64            {
 165                AddOrUpdateQuest(questId);
 166                return;
 67            }
 68
 369            questsToBeAdded.Add(questId);
 370        }
 71
 72        internal void AddOrUpdateQuest(string questId)
 73        {
 1874            if (!quests.TryGetValue(questId, out QuestModel quest))
 75            {
 176                Debug.LogError($"Couldn't find quest with ID {questId} in DataStore");
 177                return;
 78            }
 79
 80            //Quest has no available tasks, we remove it.
 1781            if (!quest.hasAvailableTasks)
 82            {
 083                RemoveQuest(questId);
 084                return;
 85            }
 86
 1787            if (!questEntries.TryGetValue(questId, out QuestsPanelEntry questEntry))
 88            {
 1689                questEntry = Instantiate(questPrefab).GetComponent<QuestsPanelEntry>();
 1690                questEntry.OnReadMoreClicked += ShowQuestPopup;
 1691                questEntries.Add(questId, questEntry);
 92            }
 93
 1794            questEntry.transform.localScale = Vector3.one;
 1795            questEntry.Populate(quest);
 1796            if (currentQuestInPopup == questId)
 097                questPopup.Populate(quest);
 98
 1799            orderQuestsRequested = true;
 17100            layoutRebuildRequested = true;
 17101        }
 102
 103        public void RemoveQuest(string questId)
 104        {
 2105            questsToBeAdded.Remove(questId);
 106
 2107            if (!questEntries.TryGetValue(questId, out QuestsPanelEntry questEntry))
 1108                return;
 1109            questEntries.Remove(questId);
 1110            questEntry.Unparent();
 1111            questEntry.SelfDestroy();
 112
 1113            if (currentQuestInPopup == questId)
 0114                questPopup.Close();
 115
 1116            questsContainerSeparators.SetActive(completedQuestsContainer.childCount > 0);
 1117            noQuestsTitle.SetActive(availableQuestsContainer.childCount == 0 );
 1118            layoutRebuildRequested = true;
 1119        }
 120
 121        public void ClearQuests()
 122        {
 21123            questPopup.Close();
 72124            foreach (QuestsPanelEntry questEntry in questEntries.Values)
 125            {
 15126                questEntry.transform.SetParent(null);
 15127                Destroy(questEntry.gameObject);
 128            }
 129
 21130            questEntries.Clear();
 21131            questsToBeAdded.Clear();
 21132            questsContainerSeparators.SetActive(completedQuestsContainer.childCount > 0);
 21133            noQuestsTitle.SetActive(availableQuestsContainer.childCount == 0 );
 21134            layoutRebuildRequested = true;
 21135        }
 136
 137        internal void ShowQuestPopup(string questId)
 138        {
 8139            if (!quests.TryGetValue(questId, out QuestModel quest))
 140            {
 1141                Debug.Log($"Couldnt find quest with id {questId}");
 1142                return;
 143            }
 144
 7145            questPopup.SetPositionToReadMoreButton(questEntries[questId].readMorePosition);
 146
 7147            currentQuestInPopup = questId;
 7148            questPopup.Populate(quest);
 7149            questPopup.Show();
 7150        }
 151
 152        internal void Update()
 153        {
 4154            if (layoutRebuildRequested)
 155            {
 2156                layoutRebuildRequested = false;
 2157                Utils.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
 2158                dynamicScrollSensitivity?.RecalculateSensitivity();
 159            }
 160
 4161            if (orderQuestsRequested)
 162            {
 1163                orderQuestsRequested = false;
 1164                OrderQuests();
 165            }
 166
 8167            for (int i = 0; i < ENTRIES_PER_FRAME && questsToBeAdded.Count > 0; i++)
 168            {
 0169                string questId = questsToBeAdded.First();
 0170                questsToBeAdded.RemoveAt(0);
 0171                AddOrUpdateQuest(questId);
 172            }
 4173        }
 174
 175        private void OrderQuests()
 176        {
 9177            var questModels = questEntries.Keys.Select(x => quests.Get(x));
 178
 14179            string[] availableIdsSorted = questModels.Where(x => !x.isCompleted).OrderBy(x => x.assignmentTime).ThenBy(x
 8180            for (int i = 0; i < availableIdsSorted.Length; i++)
 181            {
 3182                questEntries[availableIdsSorted[i]].transform.SetParent(availableQuestsContainer);
 3183                questEntries[availableIdsSorted[i]].transform.localScale = Vector3.one;
 3184                questEntries[availableIdsSorted[i]].transform.SetSiblingIndex(i);
 185            }
 186
 8187            string[] completedQuestsSorted = questModels.Where(x => x.isCompleted).OrderBy(x => x.completionTime).ThenBy
 4188            for (int i = 0; i < completedQuestsSorted.Length; i++)
 189            {
 1190                questEntries[completedQuestsSorted[i]].transform.SetParent(completedQuestsContainer);
 1191                questEntries[completedQuestsSorted[i]].transform.localScale = Vector3.one;
 1192                questEntries[completedQuestsSorted[i]].transform.SetSiblingIndex(i);
 193            }
 194
 1195            questsContainerSeparators.SetActive(completedQuestsContainer.childCount > 0);
 1196            noQuestsTitle.SetActive(availableQuestsContainer.childCount == 0 );
 1197        }
 198
 4199        public void SetVisibility(bool active) { gameObject.SetActive(active); }
 200
 0201        public bool isVisible => gameObject.activeSelf;
 202
 203        public void Dispose()
 204        {
 1205            if (!isDestroyed)
 206            {
 1207                ClearQuests();
 1208                Destroy(gameObject);
 209            }
 1210        }
 211
 212        public void SetAsFullScreenMenuMode(Transform parentTransform)
 213        {
 1214            if (parentTransform == null)
 1215                return;
 216
 0217            transform.SetParent(parentTransform);
 0218            transform.localScale = Vector3.one;
 219
 0220            RectTransform rectTransform = transform as RectTransform;
 0221            rectTransform.anchorMin = Vector2.zero;
 0222            rectTransform.anchorMax = Vector2.one;
 0223            rectTransform.pivot = new Vector2(0.5f, 0.5f);
 0224            rectTransform.localPosition = Vector2.zero;
 0225            rectTransform.offsetMax = Vector2.zero;
 0226            rectTransform.offsetMin = Vector2.zero;
 0227        }
 228
 229        private void OnDestroy()
 230        {
 19231            ClearQuests();
 19232            isDestroyed = true;
 19233        }
 234
 235        private void CloseQuestsPanel()
 236        {
 0237            QuestsUIAnalytics.SendQuestLogVisibiltyChanged(false, "quest_log_close_button");
 0238            DataStore.i.HUDs.questsPanelVisible.Set(false);
 0239        }
 240    }
 241}