< Summary

Class:FriendsTabViewBase
Assembly:FriendsHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/FriendsHUD/Scripts/Tabs/FriendsTabViewBase.cs
Covered lines:88
Uncovered lines:28
Coverable lines:116
Total lines:279
Line coverage:75.8% (88 of 116)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
EntryList()0%110100%
Count()0%2100%
Add(...)0%2.012087.5%
Remove(...)0%220100%
UpdateToggle()0%110100%
AddOrUpdateLastTimestamp(...)0%26.836016.67%
RemoveLastTimestamp(...)0%2.262060%
ReorderingFriendEntries()0%4.123050%
FriendsTabViewBase()0%110100%
Awake()0%110100%
GetAllEntries()0%2100%
GetEntry(...)0%220100%
OnEnable()0%220100%
OnDisable()0%110100%
UpdateLayout()0%220100%
UpdateCoroutine()0%330100%
OnPointerDown(...)0%12300%
Initialize(...)0%220100%
OnDestroy()0%220100%
OnPressDeleteButton(...)0%2100%
OnPressBlockButton(...)0%6200%
CreateEntry(...)0%2.012088.89%
UpdateEntry(...)0%2.012085.71%
RemoveEntry(...)0%330100%
UpdateEmptyListObjects()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/FriendsHUD/Scripts/Tabs/FriendsTabViewBase.cs

#LineLine coverage
 1using System.Collections;
 2using DCL.Helpers;
 3using DCL.Configuration;
 4using System.Collections.Generic;
 5using System.Linq;
 6using TMPro;
 7using UnityEngine;
 8using UnityEngine.EventSystems;
 9using DCL;
 10
 11public class FriendsTabViewBase : MonoBehaviour, IPointerDownHandler
 12{
 13    [System.Serializable]
 14    public class LastFriendTimestampModel
 15    {
 16        public string userId;
 17        public ulong lastMessageTimestamp;
 18    }
 19
 20    [System.Serializable]
 21    public class EntryList
 22    {
 23        public string toggleTextPrefix;
 24        public GameObject toggleButton;
 25        public TextMeshProUGUI toggleText;
 26        public Transform container;
 17627        private Dictionary<string, FriendEntryBase> entries = new Dictionary<string, FriendEntryBase>();
 28
 29        // This list store each friendId with the greatest timestamp from his related messages
 17630        private List<LastFriendTimestampModel> latestTimestampsOrdered = new List<LastFriendTimestampModel>();
 31
 032        public int Count() { return entries.Count; }
 33
 34        public void Add(string userId, FriendEntryBase entry)
 35        {
 3136            if (entries.ContainsKey(userId))
 037                return;
 38
 3139            entries.Add(userId, entry);
 40
 3141            entry.transform.SetParent(container, false);
 3142            entry.transform.localScale = Vector3.one;
 43
 3144            UpdateToggle();
 3145            ReorderingFriendEntries();
 3146        }
 47
 48        public FriendEntryBase Remove(string userId)
 49        {
 3950            if (!entries.ContainsKey(userId))
 2951                return null;
 52
 1053            var entry = entries[userId];
 54
 1055            entries.Remove(userId);
 56
 1057            UpdateToggle();
 58
 1059            return entry;
 60        }
 61
 62        void UpdateToggle()
 63        {
 4164            toggleText.text = $"{toggleTextPrefix} ({Count()})";
 4165            toggleButton.SetActive(Count() != 0);
 4166        }
 67
 68        public void AddOrUpdateLastTimestamp(LastFriendTimestampModel timestamp, bool reorderFriendEntries = true)
 69        {
 1970            if (timestamp == null)
 1971                return;
 72
 073            LastFriendTimestampModel existingTimestamp = latestTimestampsOrdered.FirstOrDefault(t => t.userId == timesta
 074            if (existingTimestamp == null)
 75            {
 076                latestTimestampsOrdered.Add(timestamp);
 077            }
 078            else if (timestamp.lastMessageTimestamp > existingTimestamp.lastMessageTimestamp)
 79            {
 080                existingTimestamp.lastMessageTimestamp = timestamp.lastMessageTimestamp;
 81            }
 82
 083            if (reorderFriendEntries)
 84            {
 085                latestTimestampsOrdered = latestTimestampsOrdered.OrderByDescending(f => f.lastMessageTimestamp).ToList(
 086                ReorderingFriendEntries();
 87            }
 088        }
 89
 90        public LastFriendTimestampModel RemoveLastTimestamp(string userId)
 91        {
 2792            LastFriendTimestampModel timestampToRemove = latestTimestampsOrdered.FirstOrDefault(t => t.userId == userId)
 2793            if (timestampToRemove == null)
 2794                return null;
 95
 096            latestTimestampsOrdered.Remove(timestampToRemove);
 97
 098            return timestampToRemove;
 99        }
 100
 101        private void ReorderingFriendEntries()
 102        {
 62103            foreach (var item in latestTimestampsOrdered)
 104            {
 0105                if (entries.ContainsKey(item.userId))
 0106                    entries[item.userId].transform.SetAsLastSibling();
 107            }
 31108        }
 109    }
 110
 111    private const string FRIEND_ENTRIES_POOL_NAME = "FriendEntriesPool_";
 112    [SerializeField] protected GameObject entryPrefab;
 113    [SerializeField] protected GameObject emptyListImage;
 114
 115    protected RectTransform rectTransform;
 116    protected FriendsHUDView owner;
 117
 118    public UserContextMenu contextMenuPanel;
 119    public UserContextConfirmationDialog confirmationDialog;
 120
 88121    protected Dictionary<string, FriendEntryBase> entries = new Dictionary<string, FriendEntryBase>();
 88122    protected Dictionary<string, PoolableObject> instantiatedFriendEntries = new Dictionary<string, PoolableObject>();
 123    protected Pool friendEntriesPool;
 124    protected bool layoutIsDirty = false;
 125
 126    private Coroutine updateRoutine = null;
 127
 128    private void Awake()
 129    {
 130        //Use a coroutine instead of an Update method to load the entries in the background while the gameobject is disa
 23131        updateRoutine = CoroutineStarter.Start(UpdateCoroutine());
 23132    }
 133
 0134    internal List<FriendEntryBase> GetAllEntries() { return entries.Values.ToList(); }
 135
 136    internal FriendEntryBase GetEntry(string userId)
 137    {
 88138        if (!entries.ContainsKey(userId))
 32139            return null;
 140
 56141        return entries[userId];
 142    }
 143
 144    protected virtual void OnEnable()
 145    {
 25146        if (rectTransform == null)
 22147            rectTransform = transform as RectTransform;
 148
 25149        layoutIsDirty = true;
 25150    }
 151
 152    protected virtual void OnDisable()
 153    {
 25154        confirmationDialog.Hide();
 25155        contextMenuPanel.Hide();
 25156    }
 157
 158    protected virtual void UpdateLayout()
 159    {
 16160        if (layoutIsDirty)
 161        {
 15162            layoutIsDirty = false;
 15163            rectTransform.ForceUpdateLayout();
 164        }
 16165    }
 166
 167    private IEnumerator UpdateCoroutine()
 168    {
 43169        while (true)
 170        {
 66171            UpdateLayout();
 66172            yield return null;
 173        }
 174    }
 175
 176    public void OnPointerDown(PointerEventData eventData)
 177    {
 0178        if (eventData.pointerPressRaycast.gameObject == null || eventData.pointerPressRaycast.gameObject.layer != Physic
 0179            contextMenuPanel.Hide();
 0180    }
 181
 182    public virtual void Initialize(FriendsHUDView owner, int preinstantiatedEntries)
 183    {
 44184        this.owner = owner;
 185
 44186        friendEntriesPool = PoolManager.i.GetPool(FRIEND_ENTRIES_POOL_NAME + this.name + this.GetInstanceID());
 44187        if (friendEntriesPool == null)
 188        {
 44189            friendEntriesPool = PoolManager.i.AddPool(
 190                FRIEND_ENTRIES_POOL_NAME + this.name + this.GetInstanceID(),
 191                Instantiate(entryPrefab),
 192                maxPrewarmCount: preinstantiatedEntries,
 193                isPersistent: true);
 44194            friendEntriesPool.ForcePrewarm();
 195        }
 196
 44197        rectTransform = transform as RectTransform;
 198
 44199        contextMenuPanel.OnBlock += OnPressBlockButton;
 44200        contextMenuPanel.OnUnfriend += OnPressDeleteButton;
 44201    }
 202
 203    public virtual void OnDestroy()
 204    {
 23205        contextMenuPanel.OnBlock -= OnPressBlockButton;
 23206        contextMenuPanel.OnUnfriend -= OnPressDeleteButton;
 23207        if (updateRoutine != null)
 208        {
 23209            CoroutineStarter.Stop(updateRoutine);
 23210            updateRoutine = null;
 211        }
 23212    }
 213
 0214    protected virtual void OnPressDeleteButton(string userId) { }
 215
 216    protected virtual void OnPressBlockButton(string userId, bool blockUser)
 217    {
 0218        FriendEntryBase friendEntryToBlock = GetEntry(userId);
 0219        if (friendEntryToBlock != null)
 220        {
 0221            friendEntryToBlock.model.blocked = blockUser;
 0222            friendEntryToBlock.Populate(friendEntryToBlock.model);
 223        }
 0224    }
 225
 226    protected virtual bool CreateEntry(string userId)
 227    {
 30228        if (entries.ContainsKey(userId))
 0229            return false;
 230
 30231        PoolableObject newFriendEntry = friendEntriesPool.Get();
 30232        instantiatedFriendEntries.Add(userId, newFriendEntry);
 30233        var entry = newFriendEntry.gameObject.GetComponent<FriendEntryBase>();
 30234        entries.Add(userId, entry);
 235
 30236        entry.OnMenuToggle += (x) =>
 237        {
 4238            contextMenuPanel.transform.position = entry.menuPositionReference.position;
 4239            contextMenuPanel.Show(userId);
 4240        };
 241
 30242        UpdateEmptyListObjects();
 30243        return true;
 244    }
 245
 246    public virtual bool UpdateEntry(string userId, FriendEntryBase.Model model)
 247    {
 31248        if (!entries.ContainsKey(userId))
 0249            return false;
 250
 31251        var entry = entries[userId];
 252
 31253        entry.Populate(model);
 31254        entry.userId = userId;
 255
 31256        layoutIsDirty = true;
 31257        return true;
 258    }
 259
 260    public virtual bool RemoveEntry(string userId)
 261    {
 21262        if (!entries.ContainsKey(userId))
 11263            return false;
 264
 10265        if (instantiatedFriendEntries.TryGetValue(userId, out PoolableObject go))
 266        {
 10267            friendEntriesPool.Release(go);
 10268            instantiatedFriendEntries.Remove(userId);
 269        }
 10270        entries.Remove(userId);
 271
 10272        UpdateEmptyListObjects();
 273
 10274        layoutIsDirty = true;
 10275        return true;
 276    }
 277
 80278    protected virtual void UpdateEmptyListObjects() { emptyListImage.SetActive(entries.Count == 0); }
 279}