| | 1 | | using TMPro; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | public class FriendRequestsTabView : FriendsTabViewBase |
| | 6 | | { |
| 23 | 7 | | [SerializeField] internal EntryList receivedRequestsList = new EntryList(); |
| 23 | 8 | | [SerializeField] internal EntryList sentRequestsList = new EntryList(); |
| | 9 | |
|
| | 10 | | [SerializeField] internal TMP_InputField friendSearchInputField; |
| | 11 | | [SerializeField] internal Button addFriendButton; |
| | 12 | |
|
| | 13 | | [Header("Notifications")] [SerializeField] |
| | 14 | | internal Notification requestSentNotification; |
| | 15 | |
|
| | 16 | | [SerializeField] internal Notification friendSearchFailedNotification; |
| | 17 | | [SerializeField] internal Notification acceptedFriendNotification; |
| | 18 | | [SerializeField] internal Notification alreadyFriendsNotification; |
| | 19 | |
|
| | 20 | | public event System.Action<FriendRequestEntry> OnCancelConfirmation; |
| | 21 | | public event System.Action<FriendRequestEntry> OnRejectConfirmation; |
| | 22 | | public event System.Action<FriendRequestEntry> OnFriendRequestApproved; |
| | 23 | | public event System.Action<string> OnFriendRequestSent; |
| | 24 | |
|
| | 25 | | public override void Initialize(FriendsHUDView owner, int preinstantiatedEntries) |
| | 26 | | { |
| 22 | 27 | | base.Initialize(owner, preinstantiatedEntries); |
| | 28 | |
|
| 22 | 29 | | receivedRequestsList.toggleTextPrefix = "RECEIVED"; |
| 22 | 30 | | sentRequestsList.toggleTextPrefix = "SENT"; |
| | 31 | |
|
| 22 | 32 | | requestSentNotification.model.timer = owner.notificationsDuration; |
| 22 | 33 | | requestSentNotification.model.groupID = FriendsHUDView.NOTIFICATIONS_ID; |
| | 34 | |
|
| 22 | 35 | | friendSearchFailedNotification.model.timer = owner.notificationsDuration; |
| 22 | 36 | | friendSearchFailedNotification.model.groupID = FriendsHUDView.NOTIFICATIONS_ID; |
| | 37 | |
|
| 22 | 38 | | acceptedFriendNotification.model.timer = owner.notificationsDuration; |
| 22 | 39 | | acceptedFriendNotification.model.groupID = FriendsHUDView.NOTIFICATIONS_ID; |
| | 40 | |
|
| 22 | 41 | | alreadyFriendsNotification.model.timer = owner.notificationsDuration; |
| 22 | 42 | | alreadyFriendsNotification.model.groupID = FriendsHUDView.NOTIFICATIONS_ID; |
| | 43 | |
|
| 22 | 44 | | friendSearchInputField.onSubmit.AddListener(SendFriendRequest); |
| 22 | 45 | | friendSearchInputField.onValueChanged.AddListener(OnSearchInputValueChanged); |
| 22 | 46 | | addFriendButton.onClick.AddListener(() => friendSearchInputField.OnSubmit(null)); |
| 22 | 47 | | } |
| | 48 | |
|
| | 49 | | protected override void OnDisable() |
| | 50 | | { |
| 1 | 51 | | base.OnDisable(); |
| | 52 | |
|
| 1 | 53 | | NotificationsController.i?.DismissAllNotifications(FriendsHUDView.NOTIFICATIONS_ID); |
| 1 | 54 | | } |
| | 55 | |
|
| | 56 | | public void CreateOrUpdateEntry(string userId, FriendEntryBase.Model model, bool isReceived) |
| | 57 | | { |
| 11 | 58 | | CreateEntry(userId); |
| 11 | 59 | | UpdateEntry(userId, model, isReceived); |
| 11 | 60 | | } |
| | 61 | |
|
| | 62 | | protected override bool CreateEntry(string userId) |
| | 63 | | { |
| 11 | 64 | | if (!base.CreateEntry(userId)) |
| 0 | 65 | | return false; |
| | 66 | |
|
| 11 | 67 | | var entry = GetEntry(userId) as FriendRequestEntry; |
| | 68 | |
|
| 11 | 69 | | if (entry == null) |
| 0 | 70 | | return false; |
| | 71 | |
|
| 11 | 72 | | entry.OnAccepted += OnFriendRequestReceivedAccepted; |
| 11 | 73 | | entry.OnRejected += OnEntryRejectButtonPressed; |
| 11 | 74 | | entry.OnCancelled += OnEntryCancelButtonPressed; |
| | 75 | |
|
| 11 | 76 | | return true; |
| | 77 | | } |
| | 78 | |
|
| | 79 | | public override bool RemoveEntry(string userId) |
| | 80 | | { |
| 17 | 81 | | if (!base.RemoveEntry(userId)) |
| 11 | 82 | | return false; |
| | 83 | |
|
| 6 | 84 | | receivedRequestsList.Remove(userId); |
| 6 | 85 | | sentRequestsList.Remove(userId); |
| 6 | 86 | | return true; |
| | 87 | | } |
| | 88 | |
|
| | 89 | | public bool UpdateEntry(string userId, FriendEntry.Model model, bool? isReceived = null) |
| | 90 | | { |
| 12 | 91 | | if (!base.UpdateEntry(userId, model)) |
| 0 | 92 | | return false; |
| | 93 | |
|
| 12 | 94 | | var entry = entries[userId] as FriendRequestEntry; |
| | 95 | |
|
| 12 | 96 | | if (isReceived.HasValue) |
| | 97 | | { |
| 12 | 98 | | entry.SetReceived(isReceived.Value); |
| | 99 | |
|
| 12 | 100 | | if (isReceived.Value) |
| 8 | 101 | | receivedRequestsList.Add(userId, entry); |
| | 102 | | else |
| 4 | 103 | | sentRequestsList.Add(userId, entry); |
| | 104 | | } |
| | 105 | |
|
| 12 | 106 | | return true; |
| | 107 | | } |
| | 108 | |
|
| | 109 | | void SendFriendRequest(string friendUserName) |
| | 110 | | { |
| 1 | 111 | | if (string.IsNullOrEmpty(friendUserName)) |
| 0 | 112 | | return; |
| | 113 | |
|
| 1 | 114 | | friendSearchInputField.placeholder.enabled = true; |
| 1 | 115 | | friendSearchInputField.text = string.Empty; |
| | 116 | |
|
| 1 | 117 | | addFriendButton.gameObject.SetActive(false); |
| | 118 | |
|
| 1 | 119 | | if (!AlreadyFriends(friendUserName)) |
| | 120 | | { |
| 1 | 121 | | requestSentNotification.model.message = $"Your request to {friendUserName} successfully sent!"; |
| 1 | 122 | | NotificationsController.i.ShowNotification(requestSentNotification); |
| | 123 | |
|
| 1 | 124 | | OnFriendRequestSent?.Invoke(friendUserName); |
| 1 | 125 | | } |
| | 126 | | else |
| | 127 | | { |
| 0 | 128 | | NotificationsController.i.ShowNotification(alreadyFriendsNotification); |
| | 129 | | } |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | bool AlreadyFriends(string friendUserName) |
| | 133 | | { |
| 1 | 134 | | var friendUserProfile = UserProfileController.GetProfileByName(friendUserName); |
| | 135 | |
|
| 1 | 136 | | return friendUserProfile != null |
| | 137 | | && FriendsController.i.friends.ContainsKey(friendUserProfile.userId) |
| | 138 | | && FriendsController.i.friends[friendUserProfile.userId].friendshipStatus == FriendshipStatus.FRIEND; |
| | 139 | | } |
| | 140 | |
|
| | 141 | | public void DisplayFriendUserNotFound() |
| | 142 | | { |
| 1 | 143 | | NotificationsController.i.ShowNotification(friendSearchFailedNotification); |
| 1 | 144 | | addFriendButton.interactable = false; |
| 1 | 145 | | } |
| | 146 | |
|
| | 147 | | void OnSearchInputValueChanged(string friendUserName) |
| | 148 | | { |
| 0 | 149 | | if (!addFriendButton.gameObject.activeSelf) |
| 0 | 150 | | addFriendButton.gameObject.SetActive(true); |
| | 151 | |
|
| 0 | 152 | | if (!addFriendButton.interactable) |
| 0 | 153 | | addFriendButton.interactable = true; |
| | 154 | |
|
| 0 | 155 | | if (!string.IsNullOrEmpty(friendUserName)) |
| 0 | 156 | | NotificationsController.i.DismissAllNotifications(FriendsHUDView.NOTIFICATIONS_ID); |
| 0 | 157 | | } |
| | 158 | |
|
| | 159 | | void OnFriendRequestReceivedAccepted(FriendRequestEntry requestEntry) |
| | 160 | | { |
| | 161 | | // Add placeholder friend to avoid affecting UX by roundtrip with kernel |
| 0 | 162 | | FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 163 | | { |
| | 164 | | userId = requestEntry.userId, |
| | 165 | | action = FriendshipAction.APPROVED |
| | 166 | | }); |
| | 167 | |
|
| 0 | 168 | | acceptedFriendNotification.model.message = $"You and {requestEntry.model.userName} are now friends!"; |
| 0 | 169 | | NotificationsController.i.ShowNotification(acceptedFriendNotification); |
| | 170 | |
|
| 0 | 171 | | RemoveEntry(requestEntry.userId); |
| | 172 | |
|
| 0 | 173 | | OnFriendRequestApproved?.Invoke(requestEntry); |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | void OnEntryRejectButtonPressed(FriendRequestEntry requestEntry) |
| | 177 | | { |
| 3 | 178 | | confirmationDialog.SetText($"Are you sure you want to reject {requestEntry.model.userName} friend request?"); |
| 3 | 179 | | confirmationDialog.Show(() => |
| | 180 | | { |
| 1 | 181 | | RemoveEntry(requestEntry.userId); |
| 1 | 182 | | OnRejectConfirmation?.Invoke(requestEntry as FriendRequestEntry); |
| 1 | 183 | | }); |
| 3 | 184 | | } |
| | 185 | |
|
| | 186 | | void OnEntryCancelButtonPressed(FriendRequestEntry requestEntry) |
| | 187 | | { |
| 3 | 188 | | confirmationDialog.SetText($"Are you sure you want to cancel {requestEntry.model.userName} friend request?"); |
| 3 | 189 | | confirmationDialog.Show(() => |
| | 190 | | { |
| 1 | 191 | | RemoveEntry(requestEntry.userId); |
| 1 | 192 | | OnCancelConfirmation?.Invoke(requestEntry as FriendRequestEntry); |
| 1 | 193 | | }); |
| 3 | 194 | | } |
| | 195 | | } |