| | 1 | | using TMPro; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | public interface IProfileCardComponentView |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Event that will be triggered when the profile card is clicked. |
| | 9 | | /// </summary> |
| | 10 | | Button.ButtonClickedEvent onClick { get; } |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Set the profile picture directly from a sprite. |
| | 14 | | /// </summary> |
| | 15 | | /// <param name="sprite">Profile picture (sprite).</param> |
| | 16 | | void SetProfilePicture(Sprite sprite); |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Set the profile picture from a 2D texture. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="newPicture">Profile picture (2D texture).</param> |
| | 22 | | void SetProfilePicture(Texture2D newPicture); |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Set the profile picture from an uri. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="uri">Profile picture (url).</param> |
| | 28 | | void SetProfilePicture(string uri); |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Set the profile name. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="newName">Profile name.</param> |
| | 34 | | void SetProfileName(string newName); |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Set the profile address. It will only show the last 4 caracteres. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="newAddress">Profile address.</param> |
| | 40 | | void SetProfileAddress(string newAddress); |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Set the profile name. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="newName">Profile name.</param> |
| | 46 | | void SetIsClaimedName(bool isClaimedName); |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Active or deactive the loading indicator. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="isVisible">True for showing the loading indicator.</param> |
| | 52 | | void SetLoadingIndicatorVisible(bool isVisible); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public class ProfileCardComponentView : BaseComponentView, IProfileCardComponentView, IComponentModelConfig |
| | 56 | | { |
| | 57 | | [Header("Prefab References")] |
| | 58 | | [SerializeField] internal Button button; |
| | 59 | | [SerializeField] internal ImageComponentView profileImage; |
| | 60 | | [SerializeField] internal TMP_Text profileName; |
| | 61 | | [SerializeField] internal TMP_Text profileAddress; |
| | 62 | |
|
| | 63 | | [Header("Configuration")] |
| | 64 | | [SerializeField] internal ProfileCardComponentModel model; |
| | 65 | |
|
| 0 | 66 | | public Button.ButtonClickedEvent onClick => button?.onClick; |
| | 67 | |
|
| | 68 | | public override void Start() |
| | 69 | | { |
| 40 | 70 | | if (profileImage != null) |
| 40 | 71 | | profileImage.OnLoaded += OnProfileImageLoaded; |
| 40 | 72 | | } |
| | 73 | |
|
| | 74 | | public void Configure(BaseComponentModel newModel) |
| | 75 | | { |
| 1 | 76 | | model = (ProfileCardComponentModel)newModel; |
| 1 | 77 | | RefreshControl(); |
| 1 | 78 | | } |
| | 79 | |
|
| | 80 | | public override void RefreshControl() |
| | 81 | | { |
| 1 | 82 | | if (model == null) |
| 0 | 83 | | return; |
| | 84 | |
|
| 1 | 85 | | if (model.profilePictureSprite != null) |
| 1 | 86 | | SetProfilePicture(model.profilePictureSprite); |
| 0 | 87 | | else if (model.profilePictureTexture != null) |
| 0 | 88 | | SetProfilePicture(model.profilePictureTexture); |
| 0 | 89 | | else if (!string.IsNullOrEmpty(model.profilePictureUri)) |
| 0 | 90 | | SetProfilePicture(model.profilePictureUri); |
| | 91 | | else |
| 0 | 92 | | SetProfilePicture(sprite: null); |
| | 93 | |
|
| 1 | 94 | | SetProfileName(model.profileName); |
| 1 | 95 | | SetProfileAddress(model.profileAddress); |
| 1 | 96 | | SetIsClaimedName(model.isClaimedName); |
| 1 | 97 | | } |
| | 98 | |
|
| | 99 | | public override void Dispose() |
| | 100 | | { |
| 72 | 101 | | base.Dispose(); |
| | 102 | |
|
| 72 | 103 | | if (profileImage != null) |
| 72 | 104 | | profileImage.OnLoaded += OnProfileImageLoaded; |
| 72 | 105 | | } |
| | 106 | |
|
| | 107 | | public void SetProfilePicture(Sprite sprite) |
| | 108 | | { |
| 3 | 109 | | model.profilePictureSprite = sprite; |
| | 110 | |
|
| 3 | 111 | | if (profileImage == null) |
| 0 | 112 | | return; |
| | 113 | |
|
| 3 | 114 | | profileImage.SetImage(sprite); |
| 3 | 115 | | } |
| | 116 | |
|
| | 117 | | public void SetProfilePicture(Texture2D texture) |
| | 118 | | { |
| 1 | 119 | | model.profilePictureTexture = texture; |
| | 120 | |
|
| 1 | 121 | | if (!Application.isPlaying) |
| 0 | 122 | | return; |
| | 123 | |
|
| 1 | 124 | | if (profileImage == null) |
| 0 | 125 | | return; |
| | 126 | |
|
| 1 | 127 | | profileImage.SetImage(texture); |
| 1 | 128 | | } |
| | 129 | |
|
| | 130 | | public void SetProfilePicture(string uri) |
| | 131 | | { |
| 1 | 132 | | model.profilePictureUri = uri; |
| | 133 | |
|
| 1 | 134 | | if (!Application.isPlaying) |
| 0 | 135 | | return; |
| | 136 | |
|
| 1 | 137 | | if (profileImage == null) |
| 0 | 138 | | return; |
| | 139 | |
|
| 1 | 140 | | profileImage.SetImage(uri); |
| 1 | 141 | | } |
| | 142 | |
|
| | 143 | | public void SetProfileName(string newName) |
| | 144 | | { |
| 7 | 145 | | model.profileName = newName; |
| | 146 | |
|
| 7 | 147 | | if (profileName == null) |
| 0 | 148 | | return; |
| | 149 | |
|
| 7 | 150 | | profileName.text = !string.IsNullOrEmpty(newName) ? (model.isClaimedName ? newName : newName.Split('#')[0]) : st |
| 7 | 151 | | } |
| | 152 | |
|
| | 153 | | public void SetProfileAddress(string newAddress) |
| | 154 | | { |
| 4 | 155 | | model.profileAddress = newAddress; |
| | 156 | |
|
| 4 | 157 | | if (profileAddress == null) |
| 0 | 158 | | return; |
| | 159 | |
|
| 4 | 160 | | if (!string.IsNullOrEmpty(newAddress)) |
| 3 | 161 | | profileAddress.text = newAddress.Length >= 4 ? $"#{newAddress.Substring(newAddress.Length - 4, 4)}" : $"#{ne |
| | 162 | | else |
| 1 | 163 | | profileAddress.text = string.Empty; |
| 1 | 164 | | } |
| | 165 | |
|
| | 166 | | public void SetIsClaimedName(bool isClaimedName) |
| | 167 | | { |
| 3 | 168 | | model.isClaimedName = isClaimedName; |
| | 169 | |
|
| 3 | 170 | | if (profileName != null) |
| 3 | 171 | | profileName.alignment = isClaimedName ? TextAlignmentOptions.Center : TextAlignmentOptions.Right; |
| | 172 | |
|
| 3 | 173 | | if (profileAddress != null) |
| 3 | 174 | | profileAddress.gameObject.SetActive(!isClaimedName); |
| | 175 | |
|
| 3 | 176 | | SetProfileName(model.profileName); |
| 3 | 177 | | } |
| | 178 | |
|
| 4 | 179 | | public void SetLoadingIndicatorVisible(bool isVisible) { profileImage.SetLoadingIndicatorVisible(isVisible); } |
| | 180 | |
|
| 2 | 181 | | internal void OnProfileImageLoaded(Sprite sprite) { SetProfilePicture(sprite); } |
| | 182 | | } |