| | 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; set; } |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Fill the model and updates the profile card with this data. |
| | 14 | | /// </summary> |
| | 15 | | /// <param name="model">Data to configure the profile card.</param> |
| | 16 | | void Configure(ProfileCardComponentModel model); |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Set the profile picture directly from a sprite. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="sprite">Profile picture (sprite).</param> |
| | 22 | | void SetProfilePicture(Sprite sprite); |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Set the profile picture from a 2D texture. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="newPicture">Profile picture (2D texture).</param> |
| | 28 | | void SetProfilePicture(Texture2D newPicture); |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Set the profile picture from an uri. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="uri">Profile picture (url).</param> |
| | 34 | | void SetProfilePicture(string uri); |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Set the profile name. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="newName">Profile name.</param> |
| | 40 | | void SetProfileName(string newName); |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Set the profile address. It will only show the last 4 caracteres. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="newAddress">Profile address.</param> |
| | 46 | | void SetProfileAddress(string newAddress); |
| | 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 |
| | 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 | |
|
| | 66 | | public Button.ButtonClickedEvent onClick |
| | 67 | | { |
| | 68 | | get |
| | 69 | | { |
| 1 | 70 | | if (button == null) |
| 0 | 71 | | return null; |
| | 72 | |
|
| 1 | 73 | | return button.onClick; |
| | 74 | | } |
| | 75 | | set |
| | 76 | | { |
| 19 | 77 | | model.onClick = value; |
| | 78 | |
|
| 19 | 79 | | if (button != null) |
| | 80 | | { |
| 19 | 81 | | button.onClick.RemoveAllListeners(); |
| 19 | 82 | | button.onClick.AddListener(() => |
| | 83 | | { |
| 0 | 84 | | value?.Invoke(); |
| 0 | 85 | | }); |
| | 86 | | } |
| 19 | 87 | | } |
| | 88 | | } |
| | 89 | |
|
| | 90 | | public override void PostInitialization() |
| | 91 | | { |
| 17 | 92 | | if (profileImage != null) |
| 17 | 93 | | profileImage.OnLoaded += OnProfileImageLoaded; |
| | 94 | |
|
| 17 | 95 | | Configure(model); |
| 17 | 96 | | } |
| | 97 | |
|
| | 98 | | public void Configure(ProfileCardComponentModel model) |
| | 99 | | { |
| 18 | 100 | | this.model = model; |
| 18 | 101 | | RefreshControl(); |
| 18 | 102 | | } |
| | 103 | |
|
| | 104 | | public override void RefreshControl() |
| | 105 | | { |
| 19 | 106 | | if (model == null) |
| 0 | 107 | | return; |
| | 108 | |
|
| 19 | 109 | | if (model.profilePictureSprite != null) |
| 19 | 110 | | SetProfilePicture(model.profilePictureSprite); |
| 0 | 111 | | else if (model.profilePictureTexture != null) |
| 0 | 112 | | SetProfilePicture(model.profilePictureTexture); |
| 0 | 113 | | else if (!string.IsNullOrEmpty(model.profilePictureUri)) |
| 0 | 114 | | SetProfilePicture(model.profilePictureUri); |
| | 115 | | else |
| 0 | 116 | | SetProfilePicture(sprite: null); |
| | 117 | |
|
| 19 | 118 | | SetProfileName(model.profileName); |
| 19 | 119 | | SetProfileAddress(model.profileAddress); |
| 19 | 120 | | onClick = model.onClick; |
| 19 | 121 | | } |
| | 122 | |
|
| | 123 | | public override void Dispose() |
| | 124 | | { |
| 40 | 125 | | base.Dispose(); |
| | 126 | |
|
| 40 | 127 | | if (profileImage != null) |
| 40 | 128 | | profileImage.OnLoaded += OnProfileImageLoaded; |
| | 129 | |
|
| 40 | 130 | | if (button == null) |
| 0 | 131 | | return; |
| | 132 | |
|
| 40 | 133 | | button.onClick.RemoveAllListeners(); |
| 40 | 134 | | } |
| | 135 | |
|
| | 136 | | public void SetProfilePicture(Sprite sprite) |
| | 137 | | { |
| 20 | 138 | | model.profilePictureSprite = sprite; |
| | 139 | |
|
| 20 | 140 | | if (profileImage == null) |
| 0 | 141 | | return; |
| | 142 | |
|
| 20 | 143 | | profileImage.SetImage(sprite); |
| 20 | 144 | | } |
| | 145 | |
|
| | 146 | | public void SetProfilePicture(Texture2D texture) |
| | 147 | | { |
| 0 | 148 | | model.profilePictureTexture = texture; |
| | 149 | |
|
| 0 | 150 | | if (!Application.isPlaying) |
| 0 | 151 | | return; |
| | 152 | |
|
| 0 | 153 | | if (profileImage == null) |
| 0 | 154 | | return; |
| | 155 | |
|
| 0 | 156 | | profileImage.SetImage(texture); |
| 0 | 157 | | } |
| | 158 | |
|
| | 159 | | public void SetProfilePicture(string uri) |
| | 160 | | { |
| 0 | 161 | | model.profilePictureUri = uri; |
| | 162 | |
|
| 0 | 163 | | if (!Application.isPlaying) |
| 0 | 164 | | return; |
| | 165 | |
|
| 0 | 166 | | if (profileImage == null) |
| 0 | 167 | | return; |
| | 168 | |
|
| 0 | 169 | | profileImage.SetImage(uri); |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | public void SetProfileName(string newName) |
| | 173 | | { |
| 21 | 174 | | model.profileName = newName; |
| | 175 | |
|
| 21 | 176 | | if (profileName == null) |
| 0 | 177 | | return; |
| | 178 | |
|
| 21 | 179 | | profileName.text = !string.IsNullOrEmpty(newName) ? newName : string.Empty; |
| 21 | 180 | | } |
| | 181 | |
|
| | 182 | | public void SetProfileAddress(string newAddress) |
| | 183 | | { |
| 22 | 184 | | model.profileAddress = newAddress; |
| | 185 | |
|
| 22 | 186 | | if (profileAddress == null) |
| 0 | 187 | | return; |
| | 188 | |
|
| 22 | 189 | | if (!string.IsNullOrEmpty(newAddress)) |
| 21 | 190 | | profileAddress.text = newAddress.Length >= 4 ? $"#{newAddress.Substring(newAddress.Length - 4, 4)}" : $"#{ne |
| | 191 | | else |
| 1 | 192 | | profileAddress.text = string.Empty; |
| 1 | 193 | | } |
| | 194 | |
|
| 4 | 195 | | public void SetLoadingIndicatorVisible(bool isVisible) { profileImage.SetLoadingIndicatorVisible(isVisible); } |
| | 196 | |
|
| 0 | 197 | | internal void OnProfileImageLoaded(Sprite sprite) { SetProfilePicture(sprite); } |
| | 198 | | } |