< Summary

Class:UserElementView
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/Views/UsersSearchPrompt/UserElementView.cs
Covered lines:58
Uncovered lines:22
Coverable lines:80
Total lines:189
Line coverage:72.5% (58 of 80)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
get_keywords()0%110100%
UserElementView()0%110100%
Awake()0%110100%
OnDestroy()0%220100%
Dispose()0%3.033085.71%
SetUserProfile(...)0%3.13077.78%
SetUserProfileModel(...)0%4.774063.64%
SetParent(...)0%110100%
GetParent()0%110100%
SetActive(...)0%110100%
SetOrder(...)0%110100%
SetUserId(...)0%2100%
SetUserName(...)0%110100%
SetThumbnail(...)0%2100%
SetBlocked(...)0%110100%
SetIsAdded(...)0%110100%
SetAlwaysHighlighted(...)0%220100%
ClearThumbnail()0%3.213071.43%
SetHighlighted()0%110100%
SetNormal()0%110100%
ISortable<UserElementView>.Compare(...)0%2100%
OnPointerEnter(...)0%6200%
OnPointerExit(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/Views/UsersSearchPrompt/UserElementView.cs

#LineLine coverage
 1using System;
 2using DCL;
 3using TMPro;
 4using UnityEngine;
 5using UnityEngine.EventSystems;
 6using UnityEngine.UI;
 7
 8internal class UserElementView : MonoBehaviour, ISearchable, ISortable<UserElementView>, IPointerEnterHandler, IPointerE
 9{
 10    enum KeywordIndex
 11    {
 12        USER_ID = 0,
 13        USER_NAME
 14    }
 15
 16    public event Action<string> OnAddPressed;
 17    public event Action<string> OnRemovePressed;
 18
 19    [SerializeField] internal bool alwaysAsHighlighted;
 20    [SerializeField] internal RawImage userThumbnail;
 21    [SerializeField] internal TextMeshProUGUI textUserName;
 22    [SerializeField] internal Button addButton;
 23    [SerializeField] internal Button removeButton;
 24    [SerializeField] internal GameObject buttonsContainer;
 25    [SerializeField] internal Image highLight;
 26    [SerializeField] internal Image blocked;
 27
 328    string[] ISearchable.keywords => searchKeywords;
 6829    private readonly string[] searchKeywords = new string[] { null, null };
 30
 31    private string userId;
 32    private bool isDestroyed = false;
 33    private UserProfile profile;
 34    private AssetPromise_Texture thumbnailPromise = null;
 35
 36    private void Awake()
 37    {
 3138        addButton.onClick.AddListener(() => OnAddPressed?.Invoke(userId));
 3439        removeButton.onClick.AddListener(() => OnRemovePressed?.Invoke(userId));
 3040        SetAlwaysHighlighted(alwaysAsHighlighted);
 3041    }
 42
 43    private void OnDestroy()
 44    {
 3045        isDestroyed = true;
 46
 3047        if (profile != null)
 1148            profile.snapshotObserver.RemoveListener(SetThumbnail);
 3049    }
 50
 51    public void Dispose()
 52    {
 3453        if (isDestroyed)
 054            return;
 55
 3456        ClearThumbnail();
 3457        Destroy(gameObject);
 58
 3459        if (profile != null)
 1160            profile.snapshotObserver.RemoveListener(SetThumbnail);
 3461    }
 62
 63    public void SetUserProfile(UserProfile userProfile)
 64    {
 1165        if ( profile == userProfile )
 066            return;
 67
 1168        SetUserId(userProfile.userId);
 1169        SetUserName(userProfile.userName);
 70
 1171        userProfile.snapshotObserver.AddListener(SetThumbnail);
 72
 1173        if (profile != null)
 074            profile.snapshotObserver.RemoveListener(SetThumbnail);
 75
 1176        profile = userProfile;
 1177    }
 78
 79    public void SetUserProfileModel(UserProfileModel profileModel)
 80    {
 381        profile = null;
 82
 383        var prevThumbnailPromise = thumbnailPromise;
 384        if (profileModel.snapshots?.face256 != null)
 85        {
 086            thumbnailPromise = new AssetPromise_Texture(profileModel.snapshots.face256);
 087            thumbnailPromise.OnSuccessEvent += asset => SetThumbnail(asset.texture);
 088            thumbnailPromise.OnFailEvent += (asset, error) => ClearThumbnail();
 089            AssetPromiseKeeper_Texture.i.Keep(thumbnailPromise);
 90        }
 91
 392        AssetPromiseKeeper_Texture.i.Forget(prevThumbnailPromise);
 93
 394        SetUserId(profileModel.userId);
 395        SetUserName(profileModel.name);
 396    }
 97
 3298    public void SetParent(Transform parent) { transform.SetParent(parent); }
 99
 3100    public Transform GetParent() { return transform.parent; }
 101
 198102    public void SetActive(bool active) { gameObject.SetActive(active); }
 103
 30104    public void SetOrder(int order) { transform.SetSiblingIndex(order); }
 105
 106    public void SetUserId(string userId)
 107    {
 0108        this.userId = userId;
 0109        searchKeywords[(int)KeywordIndex.USER_ID] = userId;
 0110    }
 111
 112    public void SetUserName(string userName)
 113    {
 30114        textUserName.text = userName;
 30115        searchKeywords[(int)KeywordIndex.USER_NAME] = userName;
 30116    }
 117
 0118    public void SetThumbnail(Texture thumbnail) { userThumbnail.texture = thumbnail; }
 119
 72120    public void SetBlocked(bool isBlocked) { blocked.gameObject.SetActive(isBlocked); }
 121
 122    public void SetIsAdded(bool isAdded)
 123    {
 33124        addButton.gameObject.SetActive(!isAdded);
 33125        removeButton.gameObject.SetActive(isAdded);
 33126    }
 127
 128    public void SetAlwaysHighlighted(bool highlighted)
 129    {
 60130        alwaysAsHighlighted = highlighted;
 60131        if (highlighted)
 132        {
 28133            SetHighlighted();
 28134        }
 135        else
 136        {
 32137            SetNormal();
 138        }
 32139    }
 140
 141    public void ClearThumbnail()
 142    {
 64143        if (profile != null)
 11144            profile.snapshotObserver.RemoveListener(SetThumbnail);
 145
 64146        if (thumbnailPromise != null)
 147        {
 0148            AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise);
 0149            thumbnailPromise = null;
 150        }
 151
 64152        userThumbnail.texture = null;
 64153    }
 154
 155    private void SetHighlighted()
 156    {
 28157        highLight.gameObject.SetActive(true);
 28158        buttonsContainer.SetActive(true);
 28159    }
 160
 161    private void SetNormal()
 162    {
 32163        highLight.gameObject.SetActive(false);
 32164        buttonsContainer.SetActive(false);
 32165    }
 166
 167    int ISortable<UserElementView>.Compare(string sortType, bool isDescendingOrder, UserElementView other)
 168    {
 169        //NOTE: for now we only sort by name
 0170        return String.CompareOrdinal(searchKeywords[(int)KeywordIndex.USER_NAME],
 171            other.searchKeywords[(int)KeywordIndex.USER_NAME]);
 172    }
 173
 174    void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData)
 175    {
 0176        if (alwaysAsHighlighted)
 0177            return;
 178
 0179        SetHighlighted();
 0180    }
 181
 182    void IPointerExitHandler.OnPointerExit(PointerEventData eventData)
 183    {
 0184        if (alwaysAsHighlighted)
 0185            return;
 186
 0187        SetNormal();
 0188    }
 189}