< Summary

Class:DCL.Social.Chat.ChatMentionSuggestionComponentView
Assembly:ChatHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ChatWidgetHUD/ChatMentionSuggestionComponentView.cs
Covered lines:29
Uncovered lines:30
Coverable lines:59
Total lines:128
Line coverage:49.1% (29 of 59)
Covered branches:0
Total branches:0
Covered methods:10
Total methods:13
Method coverage:76.9% (10 of 13)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ChatMentionSuggestionComponentView()0%110100%
Clear()0%2.152066.67%
Show()0%110100%
Hide()0%110100%
Set(...)0%220100%
SelectFirstEntry()0%220100%
SelectNextEntry()0%20400%
SelectPreviousEntry()0%30500%
HandleEntrySubmit(...)0%220100%
GetEntryFromPool()0%110100%
GetPool(...)0%220100%
SubmitSelectedEntry()0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ChatWidgetHUD/ChatMentionSuggestionComponentView.cs

#LineLine coverage
 1using DCL.Helpers;
 2using System;
 3using System.Collections.Generic;
 4using System.Linq;
 5using UnityEngine;
 6
 7namespace DCL.Social.Chat
 8{
 9    public class ChatMentionSuggestionComponentView : MonoBehaviour
 10    {
 11        private const string POOL_NAME_PREFIX = "ChatMentionSuggestions";
 12
 13        [SerializeField] internal ChatMentionSuggestionEntryComponentView mentionSuggestionPrefab;
 14        [SerializeField] internal RectTransform entryContainer;
 15        [SerializeField] internal RectTransform layout;
 16
 3817        private readonly Dictionary<ChatMentionSuggestionEntryComponentView, PoolableObject> pooledObjects = new ();
 18
 19        public event Action<ChatMentionSuggestionModel> OnEntrySubmit;
 20
 221        public bool IsVisible => gameObject.activeInHierarchy;
 22
 23        public void Clear()
 24        {
 425            foreach ((ChatMentionSuggestionEntryComponentView view, PoolableObject poolObj) in pooledObjects)
 026                poolObj.Release();
 227            pooledObjects.Clear();
 228        }
 29
 30        public void Show() =>
 331            gameObject.SetActive(true);
 32
 33        public void Hide() =>
 434            gameObject.SetActive(false);
 35
 36        public void Set(List<ChatMentionSuggestionModel> suggestions)
 37        {
 1238            foreach (ChatMentionSuggestionModel suggestion in suggestions)
 39            {
 440                ChatMentionSuggestionEntryComponentView entry = GetEntryFromPool();
 441                entry.transform.SetParent(entryContainer, false);
 442                entry.SetModel(suggestion);
 443                entry.Show(true);
 444                entry.OnSubmitted -= HandleEntrySubmit;
 445                entry.OnSubmitted += HandleEntrySubmit;
 46            }
 47
 248            layout.ForceUpdateLayout();
 249        }
 50
 51        public void SelectFirstEntry() =>
 252            pooledObjects.Keys.FirstOrDefault()?.Select();
 53
 54        public void SelectNextEntry()
 55        {
 056            var selectionFound = false;
 057            ChatMentionSuggestionEntryComponentView[] views = pooledObjects.Keys.ToArray();
 58
 059            for (var i = 0; i < views.Length; i++)
 60            {
 061                ChatMentionSuggestionEntryComponentView view = views[i];
 062                if (!view.IsSelected) continue;
 063                int nextIndex = (i + 1) % views.Length;
 064                view.Deselect();
 065                views[nextIndex].Select();
 066                selectionFound = true;
 067                break;
 68            }
 69
 070            if (!selectionFound)
 071                SelectFirstEntry();
 072        }
 73
 74        public void SelectPreviousEntry()
 75        {
 076            var selectionFound = false;
 077            ChatMentionSuggestionEntryComponentView[] views = pooledObjects.Keys.ToArray();
 78
 079            for (var i = 0; i < views.Length; i++)
 80            {
 081                ChatMentionSuggestionEntryComponentView view = views[i];
 082                if (!view.IsSelected) continue;
 083                int nextIndex = i - 1 < 0 ? views.Length - 1 : i - 1;
 084                view.Deselect();
 085                views[nextIndex].Select();
 086                selectionFound = true;
 087                break;
 88            }
 89
 090            if (!selectionFound)
 091                SelectFirstEntry();
 092        }
 93
 94        private void HandleEntrySubmit(ChatMentionSuggestionModel model) =>
 195            OnEntrySubmit?.Invoke(model);
 96
 97        private ChatMentionSuggestionEntryComponentView GetEntryFromPool()
 98        {
 499            var pool = GetPool(mentionSuggestionPrefab);
 4100            var pooledObj = pool.Get();
 4101            var entry = pooledObj.gameObject.GetComponent<ChatMentionSuggestionEntryComponentView>();
 4102            pooledObjects[entry] = pooledObj;
 4103            return entry;
 104        }
 105
 106        private Pool GetPool(ChatMentionSuggestionEntryComponentView prefab)
 107        {
 4108            string poolId = POOL_NAME_PREFIX + GetInstanceID();
 4109            Pool entryPool = PoolManager.i.GetPool(poolId);
 6110            if (entryPool != null) return entryPool;
 111
 2112            entryPool = PoolManager.i.AddPool(
 113                poolId,
 114                Instantiate(prefab).gameObject,
 115                maxPrewarmCount: 0,
 116                isPersistent: true);
 2117            entryPool.ForcePrewarm();
 118
 2119            return entryPool;
 120        }
 121
 122        public void SubmitSelectedEntry()
 123        {
 0124            ChatMentionSuggestionEntryComponentView view = pooledObjects.Keys.FirstOrDefault(view => view.IsSelected);
 0125            view?.OnSubmit(null);
 0126        }
 127    }
 128}