| | 1 | | using DCL; |
| | 2 | | using DCL.MyAccount; |
| | 3 | | using ExploreV2Analytics; |
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.UI; |
| | 9 | | using Environment = DCL.Environment; |
| | 10 | |
|
| | 11 | | public class ProfileHUDView : BaseComponentView, IProfileHUDView |
| | 12 | | { |
| | 13 | | private const int ADDRESS_CHUNK_LENGTH = 6; |
| | 14 | | private const int NAME_POSTFIX_LENGTH = 4; |
| | 15 | | private const float COPY_TOAST_VISIBLE_TIME = 3; |
| | 16 | |
|
| | 17 | | [SerializeField] private RectTransform mainRootLayout; |
| | 18 | | [SerializeField] private GameObject loadingSpinner; |
| | 19 | | [SerializeField] private ShowHideAnimator copyToast; |
| | 20 | | [SerializeField] private GameObject copyTooltip; |
| | 21 | | [SerializeField] private GameObject expandedObject; |
| | 22 | | [SerializeField] private GameObject profilePicObject; |
| | 23 | | [SerializeField] private InputAction_Trigger closeAction; |
| | 24 | |
|
| | 25 | | [Header("Hide GOs on claimed name")] |
| | 26 | | [SerializeField] internal GameObject[] hideOnNameClaimed; |
| | 27 | |
|
| | 28 | | [Header("Connected wallet sections")] |
| | 29 | | [SerializeField] internal GameObject connectedWalletSection; |
| | 30 | | [SerializeField] internal GameObject nonConnectedWalletSection; |
| | 31 | |
|
| | 32 | | [Header("Thumbnail")] |
| | 33 | | [SerializeField] internal RawImage imageAvatarThumbnail; |
| | 34 | | [SerializeField] protected internal Button buttonToggleMenu; |
| | 35 | |
|
| | 36 | | [Header("Texts")] |
| | 37 | | [SerializeField] internal TextMeshProUGUI textName; |
| | 38 | | [SerializeField] internal TextMeshProUGUI textPostfix; |
| | 39 | | [SerializeField] internal TextMeshProUGUI textAddress; |
| | 40 | |
|
| | 41 | | [Header("Buttons")] |
| | 42 | | [SerializeField] protected internal Button buttonLogOut; |
| | 43 | | [SerializeField] protected internal Button buttonSignUp; |
| | 44 | | [SerializeField] protected internal Button buttonClaimName; |
| | 45 | | [SerializeField] protected internal Button buttonCopyAddress; |
| | 46 | | [SerializeField] protected internal Button_OnPointerDown buttonTermsOfServiceForConnectedWallets; |
| | 47 | | [SerializeField] protected internal Button_OnPointerDown buttonPrivacyPolicyForConnectedWallets; |
| | 48 | | [SerializeField] protected internal Button_OnPointerDown buttonTermsOfServiceForNonConnectedWallets; |
| | 49 | | [SerializeField] protected internal Button_OnPointerDown buttonPrivacyPolicyForNonConnectedWallets; |
| | 50 | |
|
| | 51 | | [Header("Name Edition")] |
| | 52 | | [SerializeField] protected internal Button_OnPointerDown buttonEditName; |
| | 53 | | [SerializeField] protected internal Button_OnPointerDown buttonEditNamePrefix; |
| | 54 | | [SerializeField] internal TMP_InputField inputName; |
| | 55 | | [SerializeField] internal TextMeshProUGUI textCharLimit; |
| | 56 | | [SerializeField] internal ManaCounterView manaCounterView; |
| | 57 | | [SerializeField] internal ManaCounterView polygonManaCounterView; |
| | 58 | |
|
| | 59 | | [Header("Tutorial Config")] |
| | 60 | | [SerializeField] internal RectTransform tutorialTooltipReference; |
| | 61 | |
|
| | 62 | | [Header("Description")] |
| | 63 | | [SerializeField] internal TMP_InputField descriptionEditionInput; |
| | 64 | | [SerializeField] internal GameObject charLimitDescriptionContainer; |
| | 65 | | [SerializeField] internal TextMeshProUGUI textCharLimitDescription; |
| | 66 | | [SerializeField] internal GameObject descriptionContainer; |
| | 67 | |
|
| | 68 | | public event EventHandler ClaimNamePressed; |
| | 69 | | public event EventHandler SignedUpPressed; |
| | 70 | | public event EventHandler LogedOutPressed; |
| | 71 | | public event EventHandler Opened; |
| | 72 | | public event EventHandler Closed; |
| | 73 | | public event EventHandler<string> NameSubmitted; |
| | 74 | | public event EventHandler<string> DescriptionSubmitted; |
| | 75 | | public event EventHandler ManaInfoPressed; |
| | 76 | | public event EventHandler ManaPurchasePressed; |
| | 77 | | public event EventHandler TermsAndServicesPressed; |
| | 78 | | public event EventHandler PrivacyPolicyPressed; |
| | 79 | |
|
| | 80 | | internal bool isStartMenuInitialized = false; |
| | 81 | |
|
| | 82 | | private InputAction_Trigger.Triggered closeActionDelegate; |
| | 83 | | private Coroutine copyToastRoutine = null; |
| | 84 | | private UserProfile profile = null; |
| | 85 | |
|
| | 86 | | private HUDCanvasCameraModeController hudCanvasCameraModeController; |
| | 87 | |
|
| 0 | 88 | | public GameObject GameObject => gameObject; |
| 0 | 89 | | public RectTransform ExpandedMenu => mainRootLayout; |
| 0 | 90 | | public RectTransform MyAccountCardLayout => null; |
| 0 | 91 | | public RectTransform MyAccountCardMenu => null; |
| 0 | 92 | | public MyAccountCardComponentView MyAccountCardView => null; |
| 0 | 93 | | public RectTransform TutorialReference => tutorialTooltipReference; |
| | 94 | |
|
| | 95 | |
|
| 0 | 96 | | public override void RefreshControl() { } |
| | 97 | |
|
| 0 | 98 | | public bool HasManaCounterView() => manaCounterView != null; |
| | 99 | |
|
| 0 | 100 | | public bool HasPolygonManaCounterView() => polygonManaCounterView != null; |
| | 101 | |
|
| 0 | 102 | | public bool IsDesciptionIsLongerThanMaxCharacters() => descriptionEditionInput.characterLimit < descriptionEditionIn |
| | 103 | |
|
| 0 | 104 | | public void SetManaBalance(string balance) => manaCounterView.SetBalance(balance); |
| | 105 | |
|
| 0 | 106 | | public void SetPolygonBalance(double balance) => polygonManaCounterView.SetBalance(balance); |
| | 107 | |
|
| 0 | 108 | | public void SetWalletSectionEnabled(bool isEnabled) => connectedWalletSection.SetActive(isEnabled); |
| | 109 | |
|
| 0 | 110 | | public void SetNonWalletSectionEnabled(bool isEnabled) => nonConnectedWalletSection.SetActive(isEnabled); |
| | 111 | |
|
| 0 | 112 | | public void SetStartMenuButtonActive(bool isActive) => isStartMenuInitialized = isActive; |
| | 113 | |
|
| | 114 | | public void ShowProfileIcon(bool show) |
| | 115 | | { |
| 0 | 116 | | profilePicObject.SetActive(show); |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | public void ShowExpanded(bool show, bool showMyAccountVersion = false) |
| | 120 | | { |
| 0 | 121 | | expandedObject.SetActive(show); |
| 0 | 122 | | if (show && profile) |
| 0 | 123 | | UpdateLayoutByProfile(profile); |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | public void SetProfile(UserProfile userProfile) |
| | 127 | | { |
| 0 | 128 | | profile = userProfile; |
| 0 | 129 | | UpdateLayoutByProfile(userProfile); |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | |
|
| 0 | 133 | | private void OnEnable() => closeAction.OnTriggered += closeActionDelegate; |
| | 134 | |
|
| 0 | 135 | | private void OnDisable() => closeAction.OnTriggered -= closeActionDelegate; |
| | 136 | |
|
| | 137 | | private void Awake() |
| | 138 | | { |
| 0 | 139 | | buttonLogOut.onClick.AddListener(() => LogedOutPressed?.Invoke(this, EventArgs.Empty)); |
| 0 | 140 | | buttonSignUp.onClick.AddListener(() => SignedUpPressed?.Invoke(this, EventArgs.Empty)); |
| 0 | 141 | | buttonClaimName.onClick.AddListener(() => ClaimNamePressed?.Invoke(this, EventArgs.Empty)); |
| | 142 | |
|
| 0 | 143 | | buttonTermsOfServiceForConnectedWallets.onClick.AddListener(() => TermsAndServicesPressed?.Invoke(this, EventArg |
| 0 | 144 | | buttonTermsOfServiceForNonConnectedWallets.onClick.AddListener(() => TermsAndServicesPressed?.Invoke(this, Event |
| 0 | 145 | | buttonPrivacyPolicyForConnectedWallets.onClick.AddListener(() => PrivacyPolicyPressed?.Invoke(this, EventArgs.Em |
| 0 | 146 | | buttonPrivacyPolicyForNonConnectedWallets.onClick.AddListener(() => PrivacyPolicyPressed?.Invoke(this, EventArgs |
| | 147 | |
|
| 0 | 148 | | manaCounterView.buttonManaInfo.onClick.AddListener(() => ManaInfoPressed?.Invoke(this, EventArgs.Empty)); |
| 0 | 149 | | polygonManaCounterView.buttonManaInfo.onClick.AddListener(() => ManaInfoPressed?.Invoke(this, EventArgs.Empty)); |
| 0 | 150 | | manaCounterView.buttonManaPurchase.onClick.AddListener(() => ManaPurchasePressed?.Invoke(this, EventArgs.Empty)) |
| 0 | 151 | | polygonManaCounterView.buttonManaPurchase.onClick.AddListener(() => ManaPurchasePressed?.Invoke(this, EventArgs. |
| | 152 | |
|
| 0 | 153 | | buttonToggleMenu.onClick.AddListener(OpenStartMenu); |
| 0 | 154 | | buttonCopyAddress.onClick.AddListener(CopyAddress); |
| 0 | 155 | | buttonEditName.onPointerDown += () => ActivateProfileNameEditionMode(true); |
| 0 | 156 | | buttonEditNamePrefix.onPointerDown += () => ActivateProfileNameEditionMode(true); |
| 0 | 157 | | inputName.onValueChanged.AddListener(UpdateNameCharLimit); |
| 0 | 158 | | inputName.onEndEdit.AddListener(x => |
| | 159 | | { |
| 0 | 160 | | inputName.OnDeselect(null); |
| 0 | 161 | | }); |
| 0 | 162 | | inputName.onDeselect.AddListener(x => |
| | 163 | | { |
| 0 | 164 | | ActivateProfileNameEditionMode(false); |
| 0 | 165 | | NameSubmitted?.Invoke(this, x); |
| 0 | 166 | | }); |
| | 167 | |
|
| 0 | 168 | | descriptionEditionInput.onTextSelection.AddListener((description, x, y) => |
| | 169 | | { |
| 0 | 170 | | SetDescriptionIsEditing(true); |
| 0 | 171 | | UpdateDescriptionCharLimit(description); |
| 0 | 172 | | }); |
| 0 | 173 | | descriptionEditionInput.onSelect.AddListener(description => |
| | 174 | | { |
| 0 | 175 | | SetDescriptionIsEditing(true); |
| 0 | 176 | | UpdateDescriptionCharLimit(description); |
| 0 | 177 | | }); |
| 0 | 178 | | descriptionEditionInput.onValueChanged.AddListener(description => |
| | 179 | | { |
| 0 | 180 | | UpdateDescriptionCharLimit(description); |
| 0 | 181 | | }); |
| 0 | 182 | | descriptionEditionInput.onEndEdit.AddListener(description => |
| | 183 | | { |
| 0 | 184 | | descriptionEditionInput.OnDeselect(null); |
| 0 | 185 | | }); |
| 0 | 186 | | descriptionEditionInput.onDeselect.AddListener(description => |
| | 187 | | { |
| 0 | 188 | | SetDescriptionIsEditing(false); |
| 0 | 189 | | DescriptionSubmitted?.Invoke(this, description); |
| 0 | 190 | | }); |
| 0 | 191 | | copyToast.gameObject.SetActive(false); |
| 0 | 192 | | hudCanvasCameraModeController = new HUDCanvasCameraModeController(GetComponent<Canvas>(), DataStore.i.camera.hud |
| 0 | 193 | | } |
| | 194 | |
|
| | 195 | | private void UpdateLayoutByProfile(UserProfile userProfile) |
| | 196 | | { |
| 0 | 197 | | if (userProfile.hasClaimedName) |
| 0 | 198 | | HandleClaimedProfileName(userProfile); |
| | 199 | | else |
| 0 | 200 | | HandleUnverifiedProfileName(userProfile); |
| | 201 | |
|
| 0 | 202 | | SetConnectedWalletSectionActive(userProfile.hasConnectedWeb3); |
| 0 | 203 | | HandleProfileAddress(userProfile); |
| 0 | 204 | | HandleProfileSnapshot(userProfile); |
| 0 | 205 | | SetDescription(userProfile.description); |
| 0 | 206 | | SetDescriptionEnabled(userProfile.hasConnectedWeb3); |
| 0 | 207 | | } |
| | 208 | |
|
| 0 | 209 | | private void UpdateNameCharLimit(string newValue) => textCharLimit.text = $"{newValue.Length}/{inputName.characterLi |
| | 210 | |
|
| | 211 | | private void UpdateDescriptionCharLimit(string newValue) => |
| 0 | 212 | | textCharLimitDescription.text = $"{newValue.Length}/{descriptionEditionInput.characterLimit}"; |
| | 213 | |
|
| | 214 | | private void SetDescriptionEnabled(bool enabled) |
| | 215 | | { |
| 0 | 216 | | if (descriptionContainer.activeSelf != enabled) |
| 0 | 217 | | StartCoroutine(EnableNextFrame(descriptionContainer, enabled)); |
| 0 | 218 | | } |
| | 219 | |
|
| | 220 | | private void OpenStartMenu() |
| | 221 | | { |
| 0 | 222 | | if (isStartMenuInitialized) |
| | 223 | | { |
| 0 | 224 | | if (!DataStore.i.exploreV2.isOpen.Get()) |
| | 225 | | { |
| 0 | 226 | | var exploreV2Analytics = new ExploreV2Analytics.ExploreV2Analytics(); |
| 0 | 227 | | exploreV2Analytics.SendStartMenuVisibility(true, ExploreUIVisibilityMethod.FromClick); |
| | 228 | | } |
| 0 | 229 | | DataStore.i.exploreV2.isOpen.Set(true); |
| | 230 | | } |
| 0 | 231 | | } |
| | 232 | |
|
| | 233 | | private void HandleProfileSnapshot(UserProfile userProfile) |
| | 234 | | { |
| 0 | 235 | | loadingSpinner.SetActive(true); |
| 0 | 236 | | userProfile.snapshotObserver.AddListener(SetProfileImage); |
| 0 | 237 | | } |
| | 238 | |
|
| | 239 | | private void HandleClaimedProfileName(UserProfile userProfile) |
| | 240 | | { |
| 0 | 241 | | textName.text = userProfile.userName; |
| 0 | 242 | | SetActiveUnverifiedNameGOs(false); |
| 0 | 243 | | } |
| | 244 | |
|
| | 245 | | private void HandleUnverifiedProfileName(UserProfile userProfile) |
| | 246 | | { |
| 0 | 247 | | textName.text = string.IsNullOrEmpty(userProfile.userName) || userProfile.userName.Length <= NAME_POSTFIX_LENGTH |
| | 248 | | ? userProfile.userName |
| | 249 | | : userProfile.userName.Substring(0, userProfile.userName.Length - NAME_POSTFIX_LENGTH - 1); |
| | 250 | |
|
| 0 | 251 | | textPostfix.text = $"#{userProfile.userId.Substring(userProfile.userId.Length - NAME_POSTFIX_LENGTH)}"; |
| 0 | 252 | | SetActiveUnverifiedNameGOs(true); |
| 0 | 253 | | } |
| | 254 | |
|
| | 255 | | private void SetConnectedWalletSectionActive(bool active) |
| | 256 | | { |
| 0 | 257 | | connectedWalletSection.SetActive(active); |
| 0 | 258 | | nonConnectedWalletSection.SetActive(!active); |
| 0 | 259 | | buttonLogOut.gameObject.SetActive(active); |
| 0 | 260 | | } |
| | 261 | |
|
| | 262 | | private void SetActiveUnverifiedNameGOs(bool active) |
| | 263 | | { |
| 0 | 264 | | for (int i = 0; i < hideOnNameClaimed.Length; i++) |
| 0 | 265 | | hideOnNameClaimed[i].SetActive(active); |
| 0 | 266 | | } |
| | 267 | |
|
| | 268 | | private void HandleProfileAddress(UserProfile userProfile) |
| | 269 | | { |
| 0 | 270 | | string address = userProfile.userId; |
| 0 | 271 | | string start = address.Substring(0, ADDRESS_CHUNK_LENGTH); |
| 0 | 272 | | string end = address.Substring(address.Length - ADDRESS_CHUNK_LENGTH); |
| 0 | 273 | | textAddress.text = $"{start}...{end}"; |
| 0 | 274 | | } |
| | 275 | |
|
| | 276 | | private void SetProfileImage(Texture2D texture) |
| | 277 | | { |
| 0 | 278 | | loadingSpinner.SetActive(false); |
| 0 | 279 | | imageAvatarThumbnail.texture = texture; |
| 0 | 280 | | } |
| | 281 | |
|
| | 282 | | private void OnDestroy() |
| | 283 | | { |
| 0 | 284 | | hudCanvasCameraModeController?.Dispose(); |
| 0 | 285 | | if (profile) |
| 0 | 286 | | profile.snapshotObserver.RemoveListener(SetProfileImage); |
| 0 | 287 | | } |
| | 288 | |
|
| | 289 | | private void CopyAddress() |
| | 290 | | { |
| 0 | 291 | | if (!profile) |
| 0 | 292 | | return; |
| | 293 | |
|
| 0 | 294 | | Environment.i.platform.clipboard.WriteText(profile.userId); |
| | 295 | |
|
| 0 | 296 | | copyTooltip.SetActive(false); |
| 0 | 297 | | if (copyToastRoutine != null) |
| 0 | 298 | | StopCoroutine(copyToastRoutine); |
| | 299 | |
|
| 0 | 300 | | copyToastRoutine = StartCoroutine(ShowCopyToast()); |
| 0 | 301 | | } |
| | 302 | |
|
| | 303 | | private IEnumerator ShowCopyToast() |
| | 304 | | { |
| 0 | 305 | | if (!copyToast.gameObject.activeSelf) |
| 0 | 306 | | copyToast.gameObject.SetActive(true); |
| | 307 | |
|
| 0 | 308 | | copyToast.Show(); |
| 0 | 309 | | yield return new WaitForSeconds(COPY_TOAST_VISIBLE_TIME); |
| 0 | 310 | | copyToast.Hide(); |
| 0 | 311 | | } |
| | 312 | |
|
| | 313 | | internal void ActivateProfileNameEditionMode(bool activate) |
| | 314 | | { |
| 0 | 315 | | if (profile != null && profile.hasClaimedName) |
| 0 | 316 | | return; |
| | 317 | |
|
| 0 | 318 | | textName.gameObject.SetActive(!activate); |
| 0 | 319 | | inputName.gameObject.SetActive(activate); |
| | 320 | |
|
| 0 | 321 | | if (activate) |
| | 322 | | { |
| 0 | 323 | | inputName.text = textName.text; |
| 0 | 324 | | inputName.Select(); |
| | 325 | | } |
| 0 | 326 | | } |
| | 327 | |
|
| | 328 | | public void SetDescriptionIsEditing(bool isEditing) |
| | 329 | | { |
| 0 | 330 | | if (charLimitDescriptionContainer.activeSelf != isEditing) |
| 0 | 331 | | StartCoroutine(EnableNextFrame(charLimitDescriptionContainer, isEditing)); |
| 0 | 332 | | } |
| | 333 | |
|
| | 334 | | private void SetDescription(string description) |
| | 335 | | { |
| 0 | 336 | | if (descriptionEditionInput.text != description) |
| 0 | 337 | | descriptionEditionInput.text = description; |
| 0 | 338 | | } |
| | 339 | |
|
| | 340 | | private IEnumerator EnableNextFrame(GameObject go, bool shouldEnable) |
| | 341 | | { |
| 0 | 342 | | yield return null; |
| 0 | 343 | | go.SetActive(shouldEnable); |
| 0 | 344 | | } |
| | 345 | | } |