| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.EventSystems; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | internal 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 | |
|
| 3 | 28 | | string[] ISearchable.keywords => searchKeywords; |
| 68 | 29 | | 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 | | { |
| 31 | 38 | | addButton.onClick.AddListener(() => OnAddPressed?.Invoke(userId)); |
| 34 | 39 | | removeButton.onClick.AddListener(() => OnRemovePressed?.Invoke(userId)); |
| 30 | 40 | | SetAlwaysHighlighted(alwaysAsHighlighted); |
| 30 | 41 | | } |
| | 42 | |
|
| | 43 | | private void OnDestroy() |
| | 44 | | { |
| 30 | 45 | | isDestroyed = true; |
| | 46 | |
|
| 30 | 47 | | if (profile != null) |
| 11 | 48 | | profile.snapshotObserver.RemoveListener(SetThumbnail); |
| 30 | 49 | | } |
| | 50 | |
|
| | 51 | | public void Dispose() |
| | 52 | | { |
| 34 | 53 | | if (isDestroyed) |
| 0 | 54 | | return; |
| | 55 | |
|
| 34 | 56 | | ClearThumbnail(); |
| 34 | 57 | | Destroy(gameObject); |
| | 58 | |
|
| 34 | 59 | | if (profile != null) |
| 11 | 60 | | profile.snapshotObserver.RemoveListener(SetThumbnail); |
| 34 | 61 | | } |
| | 62 | |
|
| | 63 | | public void SetUserProfile(UserProfile userProfile) |
| | 64 | | { |
| 11 | 65 | | if ( profile == userProfile ) |
| 0 | 66 | | return; |
| | 67 | |
|
| 11 | 68 | | SetUserId(userProfile.userId); |
| 11 | 69 | | SetUserName(userProfile.userName); |
| | 70 | |
|
| 11 | 71 | | userProfile.snapshotObserver.AddListener(SetThumbnail); |
| | 72 | |
|
| 11 | 73 | | if (profile != null) |
| 0 | 74 | | profile.snapshotObserver.RemoveListener(SetThumbnail); |
| | 75 | |
|
| 11 | 76 | | profile = userProfile; |
| 11 | 77 | | } |
| | 78 | |
|
| | 79 | | public void SetUserProfileModel(UserProfileModel profileModel) |
| | 80 | | { |
| 3 | 81 | | profile = null; |
| | 82 | |
|
| 3 | 83 | | var prevThumbnailPromise = thumbnailPromise; |
| 3 | 84 | | if (profileModel.snapshots?.face256 != null) |
| | 85 | | { |
| 0 | 86 | | thumbnailPromise = new AssetPromise_Texture(profileModel.snapshots.face256); |
| 0 | 87 | | thumbnailPromise.OnSuccessEvent += asset => SetThumbnail(asset.texture); |
| 0 | 88 | | thumbnailPromise.OnFailEvent += (asset, error) => ClearThumbnail(); |
| 0 | 89 | | AssetPromiseKeeper_Texture.i.Keep(thumbnailPromise); |
| | 90 | | } |
| | 91 | |
|
| 3 | 92 | | AssetPromiseKeeper_Texture.i.Forget(prevThumbnailPromise); |
| | 93 | |
|
| 3 | 94 | | SetUserId(profileModel.userId); |
| 3 | 95 | | SetUserName(profileModel.name); |
| 3 | 96 | | } |
| | 97 | |
|
| 32 | 98 | | public void SetParent(Transform parent) { transform.SetParent(parent); } |
| | 99 | |
|
| 3 | 100 | | public Transform GetParent() { return transform.parent; } |
| | 101 | |
|
| 198 | 102 | | public void SetActive(bool active) { gameObject.SetActive(active); } |
| | 103 | |
|
| 30 | 104 | | public void SetOrder(int order) { transform.SetSiblingIndex(order); } |
| | 105 | |
|
| | 106 | | public void SetUserId(string userId) |
| | 107 | | { |
| 0 | 108 | | this.userId = userId; |
| 0 | 109 | | searchKeywords[(int)KeywordIndex.USER_ID] = userId; |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | public void SetUserName(string userName) |
| | 113 | | { |
| 30 | 114 | | textUserName.text = userName; |
| 30 | 115 | | searchKeywords[(int)KeywordIndex.USER_NAME] = userName; |
| 30 | 116 | | } |
| | 117 | |
|
| 0 | 118 | | public void SetThumbnail(Texture thumbnail) { userThumbnail.texture = thumbnail; } |
| | 119 | |
|
| 72 | 120 | | public void SetBlocked(bool isBlocked) { blocked.gameObject.SetActive(isBlocked); } |
| | 121 | |
|
| | 122 | | public void SetIsAdded(bool isAdded) |
| | 123 | | { |
| 33 | 124 | | addButton.gameObject.SetActive(!isAdded); |
| 33 | 125 | | removeButton.gameObject.SetActive(isAdded); |
| 33 | 126 | | } |
| | 127 | |
|
| | 128 | | public void SetAlwaysHighlighted(bool highlighted) |
| | 129 | | { |
| 60 | 130 | | alwaysAsHighlighted = highlighted; |
| 60 | 131 | | if (highlighted) |
| | 132 | | { |
| 28 | 133 | | SetHighlighted(); |
| 28 | 134 | | } |
| | 135 | | else |
| | 136 | | { |
| 32 | 137 | | SetNormal(); |
| | 138 | | } |
| 32 | 139 | | } |
| | 140 | |
|
| | 141 | | public void ClearThumbnail() |
| | 142 | | { |
| 64 | 143 | | if (profile != null) |
| 11 | 144 | | profile.snapshotObserver.RemoveListener(SetThumbnail); |
| | 145 | |
|
| 64 | 146 | | if (thumbnailPromise != null) |
| | 147 | | { |
| 0 | 148 | | AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise); |
| 0 | 149 | | thumbnailPromise = null; |
| | 150 | | } |
| | 151 | |
|
| 64 | 152 | | userThumbnail.texture = null; |
| 64 | 153 | | } |
| | 154 | |
|
| | 155 | | private void SetHighlighted() |
| | 156 | | { |
| 28 | 157 | | highLight.gameObject.SetActive(true); |
| 28 | 158 | | buttonsContainer.SetActive(true); |
| 28 | 159 | | } |
| | 160 | |
|
| | 161 | | private void SetNormal() |
| | 162 | | { |
| 32 | 163 | | highLight.gameObject.SetActive(false); |
| 32 | 164 | | buttonsContainer.SetActive(false); |
| 32 | 165 | | } |
| | 166 | |
|
| | 167 | | int ISortable<UserElementView>.Compare(string sortType, bool isDescendingOrder, UserElementView other) |
| | 168 | | { |
| | 169 | | //NOTE: for now we only sort by name |
| 0 | 170 | | return String.CompareOrdinal(searchKeywords[(int)KeywordIndex.USER_NAME], |
| | 171 | | other.searchKeywords[(int)KeywordIndex.USER_NAME]); |
| | 172 | | } |
| | 173 | |
|
| | 174 | | void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData) |
| | 175 | | { |
| 0 | 176 | | if (alwaysAsHighlighted) |
| 0 | 177 | | return; |
| | 178 | |
|
| 0 | 179 | | SetHighlighted(); |
| 0 | 180 | | } |
| | 181 | |
|
| | 182 | | void IPointerExitHandler.OnPointerExit(PointerEventData eventData) |
| | 183 | | { |
| 0 | 184 | | if (alwaysAsHighlighted) |
| 0 | 185 | | return; |
| | 186 | |
|
| 0 | 187 | | SetNormal(); |
| 0 | 188 | | } |
| | 189 | | } |