< Summary

Class:UserElementView
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/Scripts/Views/UsersSearchPrompt/UserElementView.cs
Covered lines:56
Uncovered lines:22
Coverable lines:78
Total lines:192
Line coverage:71.7% (56 of 78)
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%220100%
SetUserProfile(...)0%3.243070%
SetUserProfile(...)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/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/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;
 3046        if (profile != null)
 47        {
 1148            profile.OnFaceSnapshotReadyEvent -= SetThumbnail;
 49        }
 3050    }
 51
 52    public void Dispose()
 53    {
 3454        if (!isDestroyed)
 55        {
 3456            ClearThumbnail();
 3457            Destroy(gameObject);
 58        }
 3459    }
 60
 61    public void SetUserProfile(UserProfile userProfile)
 62    {
 1163        if (profile != null)
 64        {
 065            profile.OnFaceSnapshotReadyEvent -= SetThumbnail;
 66        }
 67
 1168        profile = userProfile;
 69
 1170        SetUserId(profile.userId);
 1171        SetUserName(profile.userName);
 72
 1173        if (profile.faceSnapshot != null)
 74        {
 075            SetThumbnail(profile.faceSnapshot);
 076        }
 77        else
 78        {
 1179            profile.OnFaceSnapshotReadyEvent += SetThumbnail;
 80        }
 1181    }
 82
 83    public void SetUserProfile(UserProfileModel profileModel)
 84    {
 385        profile = null;
 86
 387        var prevThumbnailPromise = thumbnailPromise;
 388        if (profileModel.snapshots?.face256 != null)
 89        {
 090            thumbnailPromise = new AssetPromise_Texture(profileModel.snapshots.face256);
 091            thumbnailPromise.OnSuccessEvent += (asset => SetThumbnail(asset.texture));
 092            thumbnailPromise.OnFailEvent += (asset => ClearThumbnail());
 093            AssetPromiseKeeper_Texture.i.Keep(thumbnailPromise);
 94        }
 395        AssetPromiseKeeper_Texture.i.Forget(prevThumbnailPromise);
 96
 397        SetUserId(profileModel.userId);
 398        SetUserName(profileModel.name);
 399    }
 100
 32101    public void SetParent(Transform parent) { transform.SetParent(parent); }
 102
 3103    public Transform GetParent() { return transform.parent; }
 104
 198105    public void SetActive(bool active) { gameObject.SetActive(active); }
 106
 30107    public void SetOrder(int order) { transform.SetSiblingIndex(order); }
 108
 109    public void SetUserId(string userId)
 110    {
 0111        this.userId = userId;
 0112        searchKeywords[(int)KeywordIndex.USER_ID] = userId;
 0113    }
 114
 115    public void SetUserName(string userName)
 116    {
 30117        textUserName.text = userName;
 30118        searchKeywords[(int)KeywordIndex.USER_NAME] = userName;
 30119    }
 120
 0121    public void SetThumbnail(Texture thumbnail) { userThumbnail.texture = thumbnail; }
 122
 72123    public void SetBlocked(bool isBlocked) { blocked.gameObject.SetActive(isBlocked); }
 124
 125    public void SetIsAdded(bool isAdded)
 126    {
 33127        addButton.gameObject.SetActive(!isAdded);
 33128        removeButton.gameObject.SetActive(isAdded);
 33129    }
 130
 131    public void SetAlwaysHighlighted(bool highlighted)
 132    {
 60133        alwaysAsHighlighted = highlighted;
 60134        if (highlighted)
 135        {
 28136            SetHighlighted();
 28137        }
 138        else
 139        {
 32140            SetNormal();
 141        }
 32142    }
 143
 144    public void ClearThumbnail()
 145    {
 64146        if (profile != null)
 147        {
 11148            profile.OnFaceSnapshotReadyEvent -= SetThumbnail;
 149        }
 64150        if (thumbnailPromise != null)
 151        {
 0152            AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise);
 0153            thumbnailPromise = null;
 154        }
 64155        userThumbnail.texture = null;
 64156    }
 157
 158    private void SetHighlighted()
 159    {
 28160        highLight.gameObject.SetActive(true);
 28161        buttonsContainer.SetActive(true);
 28162    }
 163
 164    private void SetNormal()
 165    {
 32166        highLight.gameObject.SetActive(false);
 32167        buttonsContainer.SetActive(false);
 32168    }
 169
 170    int ISortable<UserElementView>.Compare(string sortType, bool isDescendingOrder, UserElementView other)
 171    {
 172        //NOTE: for now we only sort by name
 0173        return String.CompareOrdinal(searchKeywords[(int)KeywordIndex.USER_NAME],
 174            other.searchKeywords[(int)KeywordIndex.USER_NAME]);
 175    }
 176
 177    void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData)
 178    {
 0179        if (alwaysAsHighlighted)
 0180            return;
 181
 0182        SetHighlighted();
 0183    }
 184
 185    void IPointerExitHandler.OnPointerExit(PointerEventData eventData)
 186    {
 0187        if (alwaysAsHighlighted)
 0188            return;
 189
 0190        SetNormal();
 0191    }
 192}