| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using TMPro; |
| | 4 | | using UIComponents.Scripts.Components; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | namespace DCL.Social.Friends |
| | 9 | | { |
| | 10 | | public class SendFriendRequestHUDComponentView : BaseComponentView<SendFriendRequestHUDModel>, ISendFriendRequestHUD |
| | 11 | | { |
| | 12 | | [SerializeField] internal ShowHideAnimator showHideAnimatorForDefaultState; |
| | 13 | | [SerializeField] internal GameObject pendingToSendContainer; |
| | 14 | | [SerializeField] internal ShowHideAnimator showHideAnimatorForSuccessState; |
| | 15 | | [SerializeField] internal TMP_Text nameLabel; |
| | 16 | | [SerializeField] internal TMP_Text successStateLabel; |
| | 17 | | [SerializeField] internal Button[] cancelButtons; |
| | 18 | | [SerializeField] internal Button sendButton; |
| | 19 | | [SerializeField] internal TMP_InputField messageBodyInput; |
| | 20 | | [SerializeField] internal TMP_Text messageBodyLengthLabel; |
| | 21 | | [SerializeField] internal Image messageBodyMaxLimitMark; |
| | 22 | | [SerializeField] internal ImageComponentView profileImage; |
| | 23 | | [SerializeField] internal Color bodyMaxLimitColor; |
| | 24 | |
|
| | 25 | | private ILazyTextureObserver lastProfilePictureObserver; |
| | 26 | | private Color messageBodyLengthOriginalColor; |
| | 27 | |
|
| | 28 | | public event Action<string> OnMessageBodyChanged; |
| | 29 | | public event Action OnSend; |
| | 30 | | public event Action OnCancel; |
| | 31 | |
|
| | 32 | | public static SendFriendRequestHUDComponentView Create() => |
| 0 | 33 | | Instantiate( |
| | 34 | | Resources.Load<SendFriendRequestHUDComponentView>("FriendRequests/SendFriendRequestHUD")); |
| | 35 | |
|
| | 36 | | public override void Awake() |
| | 37 | | { |
| 0 | 38 | | base.Awake(); |
| | 39 | |
|
| 0 | 40 | | foreach (var button in cancelButtons) |
| 0 | 41 | | button.onClick.AddListener(() => OnCancel?.Invoke()); |
| | 42 | |
|
| 0 | 43 | | messageBodyInput.onValueChanged.AddListener(s => |
| | 44 | | { |
| 0 | 45 | | RefreshMessageBodyValidations(s); |
| 0 | 46 | | OnMessageBodyChanged?.Invoke(s); |
| 0 | 47 | | }); |
| 0 | 48 | | sendButton.onClick.AddListener(() => OnSend?.Invoke()); |
| | 49 | |
|
| 0 | 50 | | messageBodyLengthOriginalColor = messageBodyLengthLabel.color; |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public override void Dispose() |
| | 54 | | { |
| 0 | 55 | | base.Dispose(); |
| 0 | 56 | | model.ProfilePictureObserver?.RemoveListener(profileImage.SetImage); |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | public override void RefreshControl() |
| | 60 | | { |
| 0 | 61 | | sendButton.gameObject.SetActive(model.State != SendFriendRequestHUDModel.LayoutState.Pending); |
| 0 | 62 | | sendButton.interactable = model.State != SendFriendRequestHUDModel.LayoutState.Pending; |
| | 63 | |
|
| 0 | 64 | | foreach (Button cancelButton in cancelButtons) |
| 0 | 65 | | cancelButton.interactable = model.State != SendFriendRequestHUDModel.LayoutState.Pending; |
| | 66 | |
|
| 0 | 67 | | nameLabel.text = model.Name; |
| 0 | 68 | | successStateLabel.text = $"Friend request sent to {model.Name}"; |
| 0 | 69 | | messageBodyLengthLabel.text = $"{messageBodyInput.text.Length}/{messageBodyInput.characterLimit}"; |
| 0 | 70 | | RefreshMessageBodyValidations(messageBodyInput.text); |
| | 71 | |
|
| | 72 | | // the load of the profile picture gets stuck if the same listener is registered many times |
| 0 | 73 | | if (lastProfilePictureObserver != model.ProfilePictureObserver) |
| | 74 | | { |
| 0 | 75 | | lastProfilePictureObserver?.RemoveListener(profileImage.SetImage); |
| 0 | 76 | | model.ProfilePictureObserver?.AddListener(profileImage.SetImage); |
| 0 | 77 | | lastProfilePictureObserver = model.ProfilePictureObserver; |
| | 78 | | } |
| 0 | 79 | | } |
| | 80 | |
|
| 0 | 81 | | public void Close() => base.Hide(); |
| | 82 | |
|
| | 83 | | public override void Show(bool instant = false) |
| | 84 | | { |
| 0 | 85 | | base.Show(instant); |
| 0 | 86 | | model.State = SendFriendRequestHUDModel.LayoutState.Default; |
| 0 | 87 | | showHideAnimatorForDefaultState.Show(true); |
| 0 | 88 | | pendingToSendContainer.SetActive(false); |
| 0 | 89 | | showHideAnimatorForSuccessState.Hide(true); |
| 0 | 90 | | RefreshControl(); |
| 0 | 91 | | } |
| | 92 | |
|
| | 93 | | public void SetName(string name) |
| | 94 | | { |
| 0 | 95 | | model.Name = name; |
| 0 | 96 | | RefreshControl(); |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | public void SetProfilePicture(ILazyTextureObserver textureObserver) |
| | 100 | | { |
| 0 | 101 | | model.ProfilePictureObserver = textureObserver; |
| 0 | 102 | | RefreshControl(); |
| 0 | 103 | | } |
| | 104 | |
|
| | 105 | | public void ShowPendingToSend() |
| | 106 | | { |
| 0 | 107 | | model.State = SendFriendRequestHUDModel.LayoutState.Pending; |
| 0 | 108 | | showHideAnimatorForDefaultState.Show(true); |
| 0 | 109 | | pendingToSendContainer.SetActive(true); |
| 0 | 110 | | showHideAnimatorForSuccessState.Hide(true); |
| 0 | 111 | | RefreshControl(); |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | public void ShowSendSuccess() |
| | 115 | | { |
| 0 | 116 | | model.State = SendFriendRequestHUDModel.LayoutState.Success; |
| 0 | 117 | | showHideAnimatorForDefaultState.Hide(true); |
| 0 | 118 | | pendingToSendContainer.SetActive(false); |
| 0 | 119 | | showHideAnimatorForSuccessState.Show(); |
| 0 | 120 | | RefreshControl(); |
| 0 | 121 | | } |
| | 122 | |
|
| 0 | 123 | | public void ClearInputField() => messageBodyInput.text = ""; |
| | 124 | |
|
| | 125 | | private void RefreshMessageBodyValidations(string message) |
| | 126 | | { |
| 0 | 127 | | messageBodyLengthLabel.text = $"{message.Length}/{messageBodyInput.characterLimit}"; |
| 0 | 128 | | messageBodyMaxLimitMark.gameObject.SetActive(message.Length >= messageBodyInput.characterLimit); |
| 0 | 129 | | messageBodyMaxLimitMark.color = messageBodyMaxLimitMark.gameObject.activeSelf ? bodyMaxLimitColor : messageB |
| 0 | 130 | | messageBodyLengthLabel.color = messageBodyMaxLimitMark.gameObject.activeSelf ? bodyMaxLimitColor : messageBo |
| 0 | 131 | | } |
| | 132 | | } |
| | 133 | | } |