| | 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 CancelFriendRequestHUDComponentView : BaseComponentView, ICancelFriendRequestHUDView |
| | 10 | | { |
| | 11 | | [SerializeField] internal GameObject defaultContainer; |
| | 12 | | [SerializeField] internal GameObject failedContainer; |
| | 13 | | [SerializeField] internal TMP_Text nameLabel; |
| | 14 | | [SerializeField] internal Button[] closeButtons; |
| | 15 | | [SerializeField] internal Button cancelButton; |
| | 16 | | [SerializeField] internal Button retryButton; |
| | 17 | | [SerializeField] internal Button openPassportButton; |
| | 18 | | [SerializeField] internal TMP_InputField messageBodyInput; |
| | 19 | | [SerializeField] internal ImageComponentView profileImage; |
| | 20 | | [SerializeField] internal ImageComponentView senderProfileImage; |
| | 21 | | [SerializeField] internal TMP_Text dateLabel; |
| | 22 | | [SerializeField] internal GameObject bodyMessageContainer; |
| | 23 | | [SerializeField] internal ShowHideAnimator confirmationToast; |
| | 24 | | [SerializeField] internal Button rejectOperationButton; |
| | 25 | |
|
| 12 | 26 | | private readonly Model model = new Model(); |
| | 27 | | private ILazyTextureObserver lastRecipientProfilePictureObserver; |
| | 28 | | private ILazyTextureObserver lastSenderProfilePictureObserver; |
| | 29 | | private bool cancelConfirmed; |
| | 30 | |
|
| | 31 | | public event Action OnCancel; |
| | 32 | | public event Action OnClose; |
| | 33 | | public event Action OnOpenProfile; |
| | 34 | |
|
| | 35 | | public static CancelFriendRequestHUDComponentView Create() => |
| 11 | 36 | | Instantiate( |
| | 37 | | Resources.Load<CancelFriendRequestHUDComponentView>("FriendRequests/CancelFriendRequestHUD")); |
| | 38 | |
|
| | 39 | | public override void Awake() |
| | 40 | | { |
| 11 | 41 | | base.Awake(); |
| | 42 | |
|
| 88 | 43 | | foreach (var button in closeButtons) |
| 33 | 44 | | button.onClick.AddListener(() => OnClose?.Invoke()); |
| | 45 | |
|
| 11 | 46 | | cancelButton.onClick.AddListener(() => |
| | 47 | | { |
| 0 | 48 | | if (cancelConfirmed) |
| | 49 | | { |
| 0 | 50 | | OnCancel?.Invoke(); |
| 0 | 51 | | cancelConfirmed = false; |
| | 52 | | } |
| | 53 | | else |
| | 54 | | { |
| 0 | 55 | | cancelConfirmed = true; |
| 0 | 56 | | ShowConfirmationToast(); |
| | 57 | | } |
| 0 | 58 | | }); |
| | 59 | |
|
| 11 | 60 | | rejectOperationButton.onClick.AddListener(() => |
| | 61 | | { |
| 0 | 62 | | cancelConfirmed = false; |
| 0 | 63 | | HideConfirmationToast(); |
| 0 | 64 | | }); |
| 11 | 65 | | retryButton.onClick.AddListener(() => OnCancel?.Invoke()); |
| 11 | 66 | | openPassportButton.onClick.AddListener(() => OnOpenProfile?.Invoke()); |
| 11 | 67 | | } |
| | 68 | |
|
| | 69 | | public override void Dispose() |
| | 70 | | { |
| 22 | 71 | | base.Dispose(); |
| 22 | 72 | | model.RecipientProfilePictureObserver?.RemoveListener(profileImage.SetImage); |
| 4 | 73 | | } |
| | 74 | |
|
| | 75 | | public override void RefreshControl() |
| | 76 | | { |
| 17 | 77 | | defaultContainer.SetActive(model.State is Model.LayoutState.Default or Model.LayoutState.Pending); |
| 17 | 78 | | failedContainer.SetActive(model.State == Model.LayoutState.Failed); |
| 17 | 79 | | cancelButton.interactable = model.State != Model.LayoutState.Pending; |
| | 80 | |
|
| 136 | 81 | | foreach (Button button in closeButtons) |
| 51 | 82 | | button.interactable = model.State != Model.LayoutState.Pending; |
| | 83 | |
|
| 17 | 84 | | nameLabel.text = model.Name; |
| 17 | 85 | | messageBodyInput.text = model.BodyMessage; |
| 17 | 86 | | bodyMessageContainer.SetActive(!string.IsNullOrEmpty(model.BodyMessage)); |
| | 87 | |
|
| | 88 | | // the load of the profile picture gets stuck if the same listener is registered many times |
| 17 | 89 | | if (lastRecipientProfilePictureObserver != model.RecipientProfilePictureObserver) |
| | 90 | | { |
| 4 | 91 | | lastRecipientProfilePictureObserver?.RemoveListener(profileImage.SetImage); |
| 4 | 92 | | model.RecipientProfilePictureObserver?.AddListener(profileImage.SetImage); |
| 4 | 93 | | lastRecipientProfilePictureObserver = model.RecipientProfilePictureObserver; |
| | 94 | | } |
| | 95 | |
|
| | 96 | | // the load of the profile picture gets stuck if the same listener is registered many times |
| 17 | 97 | | if (lastSenderProfilePictureObserver != model.SenderProfilePictureObserver) |
| | 98 | | { |
| 4 | 99 | | lastSenderProfilePictureObserver?.RemoveListener(profileImage.SetImage); |
| 4 | 100 | | model.SenderProfilePictureObserver?.AddListener(senderProfileImage.SetImage); |
| 4 | 101 | | lastSenderProfilePictureObserver = model.SenderProfilePictureObserver; |
| | 102 | | } |
| 17 | 103 | | } |
| | 104 | |
|
| | 105 | | public void Close() |
| | 106 | | { |
| 1 | 107 | | base.Hide(instant: true); |
| 1 | 108 | | gameObject.SetActive(false); |
| 1 | 109 | | } |
| | 110 | |
|
| | 111 | | public void SetSenderProfilePicture(ILazyTextureObserver textureObserver) |
| | 112 | | { |
| 6 | 113 | | model.SenderProfilePictureObserver = textureObserver; |
| 6 | 114 | | RefreshControl(); |
| 6 | 115 | | } |
| | 116 | |
|
| | 117 | | public void Show() |
| | 118 | | { |
| 1 | 119 | | cancelConfirmed = false; |
| 1 | 120 | | gameObject.SetActive(true); |
| 1 | 121 | | base.Show(instant: true); |
| 1 | 122 | | HideConfirmationToast(); |
| 1 | 123 | | model.State = Model.LayoutState.Default; |
| 1 | 124 | | RefreshControl(); |
| 1 | 125 | | } |
| | 126 | |
|
| | 127 | | public void SetRecipientName(string name) |
| | 128 | | { |
| 1 | 129 | | model.Name = name; |
| 1 | 130 | | RefreshControl(); |
| 1 | 131 | | } |
| | 132 | |
|
| | 133 | | public void SetRecipientProfilePicture(ILazyTextureObserver textureObserver) |
| | 134 | | { |
| 6 | 135 | | model.RecipientProfilePictureObserver = textureObserver; |
| 6 | 136 | | RefreshControl(); |
| 6 | 137 | | } |
| | 138 | |
|
| | 139 | | public void ShowPendingToCancel() |
| | 140 | | { |
| 1 | 141 | | model.State = Model.LayoutState.Pending; |
| 1 | 142 | | HideConfirmationToast(); |
| 1 | 143 | | RefreshControl(); |
| 1 | 144 | | } |
| | 145 | |
|
| | 146 | | public void ShowCancelFailed() |
| | 147 | | { |
| 0 | 148 | | model.State = Model.LayoutState.Failed; |
| 0 | 149 | | HideConfirmationToast(); |
| 0 | 150 | | RefreshControl(); |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | public void SetBodyMessage(string messageBody) |
| | 154 | | { |
| 2 | 155 | | model.BodyMessage = messageBody; |
| 2 | 156 | | RefreshControl(); |
| 2 | 157 | | } |
| | 158 | |
|
| | 159 | | public void SetTimestamp(DateTime date) |
| | 160 | | { |
| 1 | 161 | | dateLabel.text = date.Date.ToString("M"); |
| 1 | 162 | | } |
| | 163 | |
|
| | 164 | | private void ShowConfirmationToast() |
| | 165 | | { |
| 0 | 166 | | rejectOperationButton.gameObject.SetActive(true); |
| 0 | 167 | | confirmationToast.Show(); |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | |
|
| | 171 | | private void HideConfirmationToast() |
| | 172 | | { |
| 2 | 173 | | rejectOperationButton.gameObject.SetActive(false); |
| 2 | 174 | | confirmationToast.Hide(); |
| 2 | 175 | | } |
| | 176 | |
|
| | 177 | | private class Model |
| | 178 | | { |
| | 179 | | public string Name; |
| | 180 | | public LayoutState State; |
| | 181 | | public ILazyTextureObserver RecipientProfilePictureObserver; |
| | 182 | | public ILazyTextureObserver SenderProfilePictureObserver; |
| | 183 | | public string BodyMessage; |
| | 184 | |
|
| | 185 | | public enum LayoutState |
| | 186 | | { |
| | 187 | | Default, |
| | 188 | | Pending, |
| | 189 | | Failed |
| | 190 | | } |
| | 191 | | } |
| | 192 | | } |
| | 193 | | } |