< Summary

Class:UsersSearchUserViewsHandler
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/UsersSearchPrompt/UsersSearchUserViewsHandler.cs
Covered lines:68
Uncovered lines:4
Coverable lines:72
Total lines:171
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/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/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        }
 35
 2636        userElementViews.Clear();
 37
 4638        while (viewPool.Count > 0)
 39        {
 2040            viewPool.Dequeue().Dispose();
 41        }
 2642    }
 43
 44    public void SetUserViewsList(List<UserProfile> profiles)
 45    {
 46        UserProfile profile;
 3247        for (int i = 0; i < profiles.Count; i++)
 48        {
 1149            profile = profiles[i];
 1150            if (profile == null)
 51                continue;
 52
 1153            SetUserViewList(profile.userId,
 54                view =>
 55                {
 1156                    view.SetUserProfile(profile);
 1157                });
 58        }
 559    }
 60
 61    public void SetUserViewsList(UserProfileModel[] profiles)
 62    {
 163        List<UserElementView> newUserList = new List<UserElementView>();
 64
 65        UserProfileModel profile;
 866        for (int i = 0; i < profiles.Length; i++)
 67        {
 368            profile = profiles[i];
 369            if (profile == null)
 70                continue;
 71
 372            var userElementView = SetUserViewList(profile.userId,
 73                view =>
 74                {
 375                    view.SetUserProfileModel(profile);
 376                });
 377            newUserList.Add(userElementView);
 78        }
 79
 180        SetVisibleList(newUserList);
 181    }
 82
 83    public void RemoveUserView(string userId)
 84    {
 085        if (userElementViews.TryGetValue(userId, out UserElementView userElementView))
 86        {
 087            PoolUserView(userElementView);
 88        }
 089    }
 90
 91    public void SetVisibleList(List<UserElementView> viewsList)
 92    {
 1293        if (viewsList == null)
 94        {
 595            return;
 96        }
 97
 4898        foreach (UserElementView userElementView in userElementViews.Values)
 99        {
 17100            userElementView.SetActive(false);
 101        }
 102
 44103        for (int i = 0; i < viewsList.Count; i++)
 104        {
 15105            viewsList[i].SetActive(true);
 15106            viewsList[i].SetOrder(i);
 107        }
 7108    }
 109
 110    public void SetUsersInRolList(List<string> usersId)
 111    {
 15112        usersInRolList = usersId;
 36113        foreach (KeyValuePair<string, UserElementView> keyValuePair in userElementViews)
 114        {
 3115            keyValuePair.Value.SetIsAdded(usersId.Contains(keyValuePair.Key));
 116        }
 15117    }
 118
 5119    public List<UserElementView> GetUserElementViews() { return userElementViews.Values.ToList(); }
 120
 121    private void PoolUserView(UserElementView userView)
 122    {
 26123        userView.SetActive(false);
 26124        viewPool.Enqueue(userView);
 26125    }
 126
 127    private UserElementView GetUserView()
 128    {
 129        UserElementView userView;
 130
 14131        if (viewPool.Count > 0)
 132        {
 6133            userView = viewPool.Dequeue();
 6134        }
 135        else
 136        {
 8137            userView = Object.Instantiate(userElementViewBase, elementsParent);
 138        }
 139
 14140        userView.ClearThumbnail();
 141
 14142        userView.OnAddPressed -= OnAddUserPressed;
 14143        userView.OnRemovePressed -= OnRemoveUserPressed;
 14144        userView.OnAddPressed += OnAddUserPressed;
 14145        userView.OnRemovePressed += OnRemoveUserPressed;
 146
 14147        return userView;
 148    }
 149
 150    private UserElementView SetUserViewList(string userId, Action<UserElementView> OnAddNewElement)
 151    {
 14152        bool isBlocked = UserProfile.GetOwnUserProfile().blocked.Contains(userId);
 14153        bool isAddedRol = usersInRolList?.Contains(userId) ?? false;
 154
 14155        if (!userElementViews.TryGetValue(userId, out UserElementView userElementView))
 156        {
 14157            userElementView = GetUserView();
 14158            OnAddNewElement?.Invoke(userElementView);
 14159            userElementView.SetAlwaysHighlighted(true);
 14160            userElementViews.Add(userId, userElementView);
 161        }
 162
 14163        userElementView.SetBlocked(isBlocked);
 14164        userElementView.SetIsAdded(isAddedRol);
 14165        return userElementView;
 166    }
 167
 2168    void OnAddUserPressed(string userId) { OnAddUser?.Invoke(userId); }
 169
 2170    void OnRemoveUserPressed(string userId) { OnRemoveUser?.Invoke(userId); }
 171}