< Summary

Class:UsersSearchUserViewsHandler
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/Scripts/UsersSearchPrompt/UsersSearchUserViewsHandler.cs
Covered lines:68
Uncovered lines:4
Coverable lines:72
Total lines:167
Line coverage:94.4% (68 of 72)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UsersSearchUserViewsHandler(...)0%110100%
Dispose()0%330100%
SetUserViewsList(...)0%440100%
SetUserViewsList(...)0%440100%
RemoveUserView(...)0%6200%
SetVisibleList(...)0%440100%
SetUsersInRolList(...)0%220100%
GetUserElementViews()0%110100%
PoolUserView(...)0%110100%
GetUserView()0%220100%
SetUserViewList(...)0%550100%
OnAddUserPressed(...)0%220100%
OnRemoveUserPressed(...)0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/Scripts/UsersSearchPrompt/UsersSearchUserViewsHandler.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using UnityEngine;
 5using Object = UnityEngine.Object;
 6
 7internal class UsersSearchUserViewsHandler : IDisposable
 8{
 9    public event Action<string> OnRemoveUser;
 10    public event Action<string> OnAddUser;
 11
 2612    internal readonly Dictionary<string, UserElementView> userElementViews = new Dictionary<string, UserElementView>();
 2613    private readonly Queue<UserElementView> viewPool = new Queue<UserElementView>();
 14
 15    private readonly UserElementView userElementViewBase;
 16    private readonly Transform elementsParent;
 17
 18    private List<string> usersInRolList;
 19
 020    public int userElementViewCount => userElementViews.Count;
 21
 2622    public UsersSearchUserViewsHandler(UserElementView userElementViewBase, Transform elementsParent)
 23    {
 2624        this.userElementViewBase = userElementViewBase;
 2625        this.elementsParent = elementsParent;
 2626        PoolUserView(userElementViewBase);
 2627    }
 28
 29    public void Dispose()
 30    {
 8031        foreach (UserElementView userView in userElementViews.Values)
 32        {
 1433            userView.Dispose();
 34        }
 2635        userElementViews.Clear();
 36
 4637        while (viewPool.Count > 0)
 38        {
 2039            viewPool.Dequeue().Dispose();
 40        }
 2641    }
 42
 43    public void SetUserViewsList(List<UserProfile> profiles)
 44    {
 45        UserProfile profile;
 3246        for (int i = 0; i < profiles.Count; i++)
 47        {
 1148            profile = profiles[i];
 1149            if (profile == null)
 50                continue;
 51
 1152            SetUserViewList(profile.userId,
 53                view =>
 54                {
 1155                    view.SetUserProfile(profile);
 1156                });
 57        }
 558    }
 59
 60    public void SetUserViewsList(UserProfileModel[] profiles)
 61    {
 162        List<UserElementView> newUserList = new List<UserElementView>();
 63
 64        UserProfileModel profile;
 865        for (int i = 0; i < profiles.Length; i++)
 66        {
 367            profile = profiles[i];
 368            if (profile == null)
 69                continue;
 70
 371            var userElementView = SetUserViewList(profile.userId,
 72                view =>
 73                {
 374                    view.SetUserProfile(profile);
 375                });
 376            newUserList.Add(userElementView);
 77        }
 178        SetVisibleList(newUserList);
 179    }
 80
 81    public void RemoveUserView(string userId)
 82    {
 083        if (userElementViews.TryGetValue(userId, out UserElementView userElementView))
 84        {
 085            PoolUserView(userElementView);
 86        }
 087    }
 88
 89    public void SetVisibleList(List<UserElementView> viewsList)
 90    {
 1291        if (viewsList == null)
 92        {
 593            return;
 94        }
 95
 4896        foreach (UserElementView userElementView in userElementViews.Values)
 97        {
 1798            userElementView.SetActive(false);
 99        }
 44100        for (int i = 0; i < viewsList.Count; i++)
 101        {
 15102            viewsList[i].SetActive(true);
 15103            viewsList[i].SetOrder(i);
 104        }
 7105    }
 106
 107    public void SetUsersInRolList(List<string> usersId)
 108    {
 15109        usersInRolList = usersId;
 36110        foreach (KeyValuePair<string, UserElementView> keyValuePair in userElementViews)
 111        {
 3112            keyValuePair.Value.SetIsAdded(usersId.Contains(keyValuePair.Key));
 113        }
 15114    }
 115
 5116    public List<UserElementView> GetUserElementViews() { return userElementViews.Values.ToList(); }
 117
 118    private void PoolUserView(UserElementView userView)
 119    {
 26120        userView.SetActive(false);
 26121        viewPool.Enqueue(userView);
 26122    }
 123
 124    private UserElementView GetUserView()
 125    {
 126        UserElementView userView;
 127
 14128        if (viewPool.Count > 0)
 129        {
 6130            userView = viewPool.Dequeue();
 6131        }
 132        else
 133        {
 8134            userView = Object.Instantiate(userElementViewBase, elementsParent);
 135        }
 14136        userView.ClearThumbnail();
 137
 14138        userView.OnAddPressed -= OnAddUserPressed;
 14139        userView.OnRemovePressed -= OnRemoveUserPressed;
 14140        userView.OnAddPressed += OnAddUserPressed;
 14141        userView.OnRemovePressed += OnRemoveUserPressed;
 142
 14143        return userView;
 144    }
 145
 146    private UserElementView SetUserViewList(string userId, Action<UserElementView> OnAddNewElement)
 147    {
 14148        bool isBlocked = UserProfile.GetOwnUserProfile().blocked.Contains(userId);
 14149        bool isAddedRol = usersInRolList?.Contains(userId) ?? false;
 150
 14151        if (!userElementViews.TryGetValue(userId, out UserElementView userElementView))
 152        {
 14153            userElementView = GetUserView();
 14154            OnAddNewElement?.Invoke(userElementView);
 14155            userElementView.SetAlwaysHighlighted(true);
 14156            userElementViews.Add(userId, userElementView);
 157        }
 158
 14159        userElementView.SetBlocked(isBlocked);
 14160        userElementView.SetIsAdded(isAddedRol);
 14161        return userElementView;
 162    }
 163
 2164    void OnAddUserPressed(string userId) { OnAddUser?.Invoke(userId); }
 165
 2166    void OnRemoveUserPressed(string userId) { OnRemoveUser?.Invoke(userId); }
 167}