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