| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | public class FriendRequestsTabComponentView : BaseComponentView |
| | 11 | | { |
| | 12 | | private const string FRIEND_ENTRIES_POOL_NAME_PREFIX = "FriendRequestEntriesPool_"; |
| | 13 | | private const string NOTIFICATIONS_ID = "Friends"; |
| | 14 | | private const int PRE_INSTANTIATED_ENTRIES = 25; |
| | 15 | | private const float NOTIFICATIONS_DURATION = 3; |
| | 16 | | private const int AVATAR_SNAPSHOTS_PER_FRAME = 5; |
| | 17 | | private const int CREATION_AMOUNT_PER_FRAME = 5; |
| | 18 | |
|
| | 19 | | [SerializeField] private GameObject enabledHeader; |
| | 20 | | [SerializeField] private GameObject disabledHeader; |
| | 21 | | [SerializeField] private GameObject emptyStateContainer; |
| | 22 | | [SerializeField] private GameObject filledStateContainer; |
| | 23 | | [SerializeField] private FriendRequestEntry entryPrefab; |
| | 24 | | [SerializeField] private CollapsableSortedFriendEntryList receivedRequestsList; |
| | 25 | | [SerializeField] private CollapsableSortedFriendEntryList sentRequestsList; |
| | 26 | | [SerializeField] private SearchBarComponentView searchBar; |
| | 27 | | [SerializeField] private TMP_Text receivedRequestsCountText; |
| | 28 | | [SerializeField] private TMP_Text sentRequestsCountText; |
| | 29 | | [SerializeField] private UserContextMenu contextMenuPanel; |
| | 30 | | [SerializeField] private RectTransform viewport; |
| | 31 | |
|
| | 32 | | [Header("Notifications")] [SerializeField] |
| | 33 | | private Notification requestSentNotification; |
| | 34 | |
|
| | 35 | | [SerializeField] private Notification friendSearchFailedNotification; |
| | 36 | | [SerializeField] private Notification acceptedFriendNotification; |
| | 37 | | [SerializeField] private Notification alreadyFriendsNotification; |
| | 38 | | [SerializeField] private Model model; |
| | 39 | |
|
| | 40 | | [Header("Load More Entries")] |
| | 41 | | [SerializeField] internal Button loadMoreEntriesButton; |
| | 42 | | [SerializeField] internal GameObject loadMoreEntriesContainer; |
| | 43 | | [SerializeField] internal TMP_Text loadMoreEntriesLabel; |
| | 44 | |
|
| 24 | 45 | | private readonly Dictionary<string, PoolableObject> pooleableEntries = new Dictionary<string, PoolableObject>(); |
| 24 | 46 | | private readonly Dictionary<string, FriendRequestEntry> entries = new Dictionary<string, FriendRequestEntry>(); |
| 24 | 47 | | private readonly Dictionary<string, FriendRequestEntryModel> creationQueue = |
| | 48 | | new Dictionary<string, FriendRequestEntryModel>(); |
| | 49 | | private Pool entryPool; |
| | 50 | | private string lastRequestSentUserName; |
| | 51 | | private int currentAvatarSnapshotIndex; |
| | 52 | | private bool isLayoutDirty; |
| | 53 | |
|
| 0 | 54 | | public Dictionary<string, FriendRequestEntry> Entries => entries; |
| | 55 | |
|
| 14 | 56 | | public int Count => Entries.Count + creationQueue.Keys.Count(s => !Entries.ContainsKey(s)); |
| | 57 | |
|
| | 58 | | public event Action<FriendRequestEntryModel> OnCancelConfirmation; |
| | 59 | | public event Action<FriendRequestEntryModel> OnRejectConfirmation; |
| | 60 | | public event Action<FriendRequestEntryModel> OnFriendRequestApproved; |
| | 61 | | public event Action<string> OnFriendRequestSent; |
| | 62 | | public event Action OnRequireMoreEntries; |
| | 63 | |
|
| | 64 | | public override void OnEnable() |
| | 65 | | { |
| 7 | 66 | | base.OnEnable(); |
| 7 | 67 | | searchBar.Configure(new SearchBarComponentModel {placeHolderText = "Search a friend you want to add"}); |
| 7 | 68 | | searchBar.OnSubmit += SendFriendRequest; |
| 7 | 69 | | searchBar.OnSearchText += OnSearchInputValueChanged; |
| 7 | 70 | | contextMenuPanel.OnBlock += HandleFriendBlockRequest; |
| 7 | 71 | | loadMoreEntriesButton.onClick.AddListener(RequestMoreEntries); |
| 7 | 72 | | UpdateLayout(); |
| 7 | 73 | | } |
| | 74 | |
|
| | 75 | | public override void OnDisable() |
| | 76 | | { |
| 7 | 77 | | base.OnDisable(); |
| 7 | 78 | | searchBar.OnSubmit -= SendFriendRequest; |
| 7 | 79 | | searchBar.OnSearchText -= OnSearchInputValueChanged; |
| 7 | 80 | | contextMenuPanel.OnBlock -= HandleFriendBlockRequest; |
| 7 | 81 | | loadMoreEntriesButton.onClick.RemoveListener(RequestMoreEntries); |
| 7 | 82 | | NotificationsController.i?.DismissAllNotifications(NOTIFICATIONS_ID); |
| 7 | 83 | | } |
| | 84 | |
|
| | 85 | | public override void Update() |
| | 86 | | { |
| 6 | 87 | | base.Update(); |
| | 88 | |
|
| 6 | 89 | | if (isLayoutDirty) |
| 6 | 90 | | Utils.ForceRebuildLayoutImmediate((RectTransform) filledStateContainer.transform); |
| 6 | 91 | | isLayoutDirty = false; |
| | 92 | |
|
| 6 | 93 | | SetQueuedEntries(); |
| 6 | 94 | | FetchProfilePicturesForVisibleEntries(); |
| 6 | 95 | | } |
| | 96 | |
|
| | 97 | | public void Expand() |
| | 98 | | { |
| 23 | 99 | | receivedRequestsList.Expand(); |
| 23 | 100 | | sentRequestsList.Expand(); |
| 23 | 101 | | } |
| | 102 | |
|
| | 103 | | public void Show() |
| | 104 | | { |
| 7 | 105 | | gameObject.SetActive(true); |
| 7 | 106 | | enabledHeader.SetActive(true); |
| 7 | 107 | | disabledHeader.SetActive(false); |
| 7 | 108 | | searchBar.ClearSearch(); |
| 7 | 109 | | } |
| | 110 | |
|
| | 111 | | public void Hide() |
| | 112 | | { |
| 8 | 113 | | gameObject.SetActive(false); |
| 8 | 114 | | enabledHeader.SetActive(false); |
| 8 | 115 | | disabledHeader.SetActive(true); |
| 8 | 116 | | contextMenuPanel.Hide(); |
| 8 | 117 | | } |
| | 118 | |
|
| | 119 | | public override void RefreshControl() |
| | 120 | | { |
| 0 | 121 | | Clear(); |
| | 122 | |
|
| 0 | 123 | | foreach (var friend in model.friends) |
| 0 | 124 | | Set(friend.userId, friend.model); |
| | 125 | |
|
| 0 | 126 | | if (model.isReceivedRequestsExpanded) |
| 0 | 127 | | receivedRequestsList.Expand(); |
| | 128 | | else |
| 0 | 129 | | receivedRequestsList.Collapse(); |
| | 130 | |
|
| 0 | 131 | | if (model.isSentRequestsExpanded) |
| 0 | 132 | | sentRequestsList.Expand(); |
| | 133 | | else |
| 0 | 134 | | sentRequestsList.Collapse(); |
| 0 | 135 | | } |
| | 136 | |
|
| | 137 | | public void Remove(string userId) |
| | 138 | | { |
| 11 | 139 | | if (creationQueue.ContainsKey(userId)) |
| 0 | 140 | | creationQueue.Remove(userId); |
| | 141 | |
|
| 21 | 142 | | if (!entries.ContainsKey(userId)) return; |
| | 143 | |
|
| 1 | 144 | | if (pooleableEntries.TryGetValue(userId, out var pooleableObject)) |
| | 145 | | { |
| 1 | 146 | | entryPool.Release(pooleableObject); |
| 1 | 147 | | pooleableEntries.Remove(userId); |
| | 148 | | } |
| | 149 | |
|
| 1 | 150 | | entries.Remove(userId); |
| 1 | 151 | | receivedRequestsList.Remove(userId); |
| 1 | 152 | | sentRequestsList.Remove(userId); |
| | 153 | |
|
| 1 | 154 | | UpdateEmptyOrFilledState(); |
| 1 | 155 | | UpdateCounterLabel(); |
| 1 | 156 | | UpdateLayout(); |
| 1 | 157 | | } |
| | 158 | |
|
| | 159 | | public void Clear() |
| | 160 | | { |
| 0 | 161 | | entries.ToList().ForEach(pair => Remove(pair.Key)); |
| 0 | 162 | | receivedRequestsList.Clear(); |
| 0 | 163 | | sentRequestsList.Clear(); |
| 0 | 164 | | UpdateEmptyOrFilledState(); |
| 0 | 165 | | UpdateCounterLabel(); |
| 0 | 166 | | } |
| | 167 | |
|
| 37 | 168 | | public FriendRequestEntry Get(string userId) => entries.ContainsKey(userId) ? entries[userId] : null; |
| | 169 | |
|
| 5 | 170 | | public void Enqueue(string userId, FriendRequestEntryModel model) => creationQueue[userId] = model; |
| | 171 | |
|
| | 172 | | public void Set(string userId, FriendRequestEntryModel model) |
| | 173 | | { |
| 5 | 174 | | if (creationQueue.ContainsKey(userId)) |
| | 175 | | { |
| 0 | 176 | | creationQueue[userId] = model; |
| 0 | 177 | | return; |
| | 178 | | } |
| | 179 | |
|
| 5 | 180 | | if (!entries.ContainsKey(userId)) |
| | 181 | | { |
| 5 | 182 | | CreateEntry(userId); |
| 5 | 183 | | UpdateLayout(); |
| | 184 | | } |
| | 185 | |
|
| 5 | 186 | | Populate(userId, model); |
| 5 | 187 | | UpdateEmptyOrFilledState(); |
| 5 | 188 | | UpdateCounterLabel(); |
| 5 | 189 | | } |
| | 190 | |
|
| | 191 | | public void Populate(string userId, FriendRequestEntryModel model) |
| | 192 | | { |
| 5 | 193 | | if (!entries.ContainsKey(userId)) |
| | 194 | | { |
| 0 | 195 | | if (creationQueue.ContainsKey(userId)) |
| 0 | 196 | | creationQueue[userId] = model; |
| 0 | 197 | | return; |
| | 198 | | } |
| | 199 | |
|
| 5 | 200 | | var entry = entries[userId]; |
| 5 | 201 | | entry.Populate(model); |
| | 202 | |
|
| 5 | 203 | | if (model.isReceived) |
| 3 | 204 | | receivedRequestsList.Add(userId, entry); |
| | 205 | | else |
| 2 | 206 | | sentRequestsList.Add(userId, entry); |
| 2 | 207 | | } |
| | 208 | |
|
| | 209 | | public void ShowUserNotFoundNotification() |
| | 210 | | { |
| 0 | 211 | | friendSearchFailedNotification.model.timer = NOTIFICATIONS_DURATION; |
| 0 | 212 | | friendSearchFailedNotification.model.groupID = NOTIFICATIONS_ID; |
| 0 | 213 | | NotificationsController.i?.ShowNotification(friendSearchFailedNotification); |
| 0 | 214 | | } |
| | 215 | |
|
| 0 | 216 | | private void UpdateLayout() => isLayoutDirty = true; |
| | 217 | |
|
| | 218 | | private void CreateEntry(string userId) |
| | 219 | | { |
| 5 | 220 | | entryPool = GetEntryPool(); |
| 5 | 221 | | var newFriendEntry = entryPool.Get(); |
| 5 | 222 | | pooleableEntries.Add(userId, newFriendEntry); |
| 5 | 223 | | var entry = newFriendEntry.gameObject.GetComponent<FriendRequestEntry>(); |
| 5 | 224 | | if (entry == null) return; |
| 5 | 225 | | entries.Add(userId, entry); |
| | 226 | |
|
| 5 | 227 | | entry.OnAccepted -= OnFriendRequestReceivedAccepted; |
| 5 | 228 | | entry.OnAccepted += OnFriendRequestReceivedAccepted; |
| 5 | 229 | | entry.OnRejected -= OnEntryRejectButtonPressed; |
| 5 | 230 | | entry.OnRejected += OnEntryRejectButtonPressed; |
| 5 | 231 | | entry.OnCancelled -= OnEntryCancelButtonPressed; |
| 5 | 232 | | entry.OnCancelled += OnEntryCancelButtonPressed; |
| 5 | 233 | | entry.OnMenuToggle -= OnEntryMenuToggle; |
| 5 | 234 | | entry.OnMenuToggle += OnEntryMenuToggle; |
| 5 | 235 | | } |
| | 236 | |
|
| | 237 | | private Pool GetEntryPool() |
| | 238 | | { |
| 5 | 239 | | var entryPool = PoolManager.i.GetPool(FRIEND_ENTRIES_POOL_NAME_PREFIX + name + GetInstanceID()); |
| 5 | 240 | | if (entryPool != null) return entryPool; |
| | 241 | |
|
| 5 | 242 | | entryPool = PoolManager.i.AddPool( |
| | 243 | | FRIEND_ENTRIES_POOL_NAME_PREFIX + name + GetInstanceID(), |
| | 244 | | Instantiate(entryPrefab).gameObject, |
| | 245 | | maxPrewarmCount: PRE_INSTANTIATED_ENTRIES, |
| | 246 | | isPersistent: true); |
| 5 | 247 | | entryPool.ForcePrewarm(); |
| | 248 | |
|
| 5 | 249 | | return entryPool; |
| | 250 | | } |
| | 251 | |
|
| | 252 | | private void SendFriendRequest(string friendUserName) |
| | 253 | | { |
| 0 | 254 | | if (string.IsNullOrEmpty(friendUserName)) return; |
| | 255 | |
|
| 0 | 256 | | searchBar.ClearSearch(); |
| 0 | 257 | | lastRequestSentUserName = friendUserName; |
| 0 | 258 | | OnFriendRequestSent?.Invoke(friendUserName); |
| 0 | 259 | | } |
| | 260 | |
|
| | 261 | | public void ShowAlreadyFriendsNotification() |
| | 262 | | { |
| 0 | 263 | | alreadyFriendsNotification.model.timer = NOTIFICATIONS_DURATION; |
| 0 | 264 | | alreadyFriendsNotification.model.groupID = NOTIFICATIONS_ID; |
| 0 | 265 | | NotificationsController.i?.ShowNotification(alreadyFriendsNotification); |
| 0 | 266 | | } |
| | 267 | |
|
| | 268 | | public void ShowRequestSuccessfullySentNotification() |
| | 269 | | { |
| 0 | 270 | | requestSentNotification.model.timer = NOTIFICATIONS_DURATION; |
| 0 | 271 | | requestSentNotification.model.groupID = NOTIFICATIONS_ID; |
| 0 | 272 | | requestSentNotification.model.message = $"Your request to {lastRequestSentUserName} successfully sent!"; |
| 0 | 273 | | NotificationsController.i?.ShowNotification(requestSentNotification); |
| 0 | 274 | | } |
| | 275 | |
|
| | 276 | | public void ShowMoreFriendsToLoadHint(int pendingFriendsCount) |
| | 277 | | { |
| 1 | 278 | | loadMoreEntriesLabel.SetText( |
| | 279 | | $"{pendingFriendsCount} request hidden. Click below to show more."); |
| 1 | 280 | | ShowMoreFriendsToLoadHint(); |
| 1 | 281 | | } |
| | 282 | |
|
| | 283 | | public void HideMoreFriendsToLoadHint() |
| | 284 | | { |
| 1 | 285 | | loadMoreEntriesContainer.SetActive(false); |
| 1 | 286 | | UpdateLayout(); |
| 1 | 287 | | } |
| | 288 | |
|
| | 289 | | private void ShowMoreFriendsToLoadHint() |
| | 290 | | { |
| 1 | 291 | | loadMoreEntriesContainer.SetActive(true); |
| 1 | 292 | | UpdateLayout(); |
| 1 | 293 | | } |
| | 294 | |
|
| | 295 | | private void UpdateEmptyOrFilledState() |
| | 296 | | { |
| 6 | 297 | | emptyStateContainer.SetActive(entries.Count == 0); |
| 6 | 298 | | filledStateContainer.SetActive(entries.Count > 0); |
| 6 | 299 | | } |
| | 300 | |
|
| | 301 | | private void OnSearchInputValueChanged(string friendUserName) |
| | 302 | | { |
| 8 | 303 | | if (!string.IsNullOrEmpty(friendUserName)) |
| 0 | 304 | | NotificationsController.i?.DismissAllNotifications(NOTIFICATIONS_ID); |
| 8 | 305 | | } |
| | 306 | |
|
| | 307 | | private void OnFriendRequestReceivedAccepted(FriendRequestEntry requestEntry) |
| | 308 | | { |
| | 309 | | // Add placeholder friend to avoid affecting UX by roundtrip with kernel |
| 0 | 310 | | FriendsController.i?.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage |
| | 311 | | { |
| | 312 | | userId = requestEntry.Model.userId, |
| | 313 | | action = FriendshipAction.APPROVED |
| | 314 | | }); |
| | 315 | |
|
| 0 | 316 | | ShowFriendAcceptedNotification(requestEntry); |
| 0 | 317 | | Remove(requestEntry.Model.userId); |
| 0 | 318 | | OnFriendRequestApproved?.Invoke((FriendRequestEntryModel) requestEntry.Model); |
| 0 | 319 | | } |
| | 320 | |
|
| | 321 | | private void ShowFriendAcceptedNotification(FriendRequestEntry requestEntry) |
| | 322 | | { |
| 0 | 323 | | acceptedFriendNotification.model.timer = NOTIFICATIONS_DURATION; |
| 0 | 324 | | acceptedFriendNotification.model.groupID = NOTIFICATIONS_ID; |
| 0 | 325 | | acceptedFriendNotification.model.message = $"You and {requestEntry.Model.userName} are now friends!"; |
| 0 | 326 | | NotificationsController.i?.ShowNotification(acceptedFriendNotification); |
| 0 | 327 | | } |
| | 328 | |
|
| | 329 | | private void OnEntryRejectButtonPressed(FriendRequestEntry requestEntry) |
| | 330 | | { |
| 0 | 331 | | Remove(requestEntry.Model.userId); |
| 0 | 332 | | OnRejectConfirmation?.Invoke((FriendRequestEntryModel) requestEntry.Model); |
| 0 | 333 | | } |
| | 334 | |
|
| | 335 | | private void OnEntryCancelButtonPressed(FriendRequestEntry requestEntry) |
| | 336 | | { |
| 0 | 337 | | Remove(requestEntry.Model.userId); |
| 0 | 338 | | OnCancelConfirmation?.Invoke((FriendRequestEntryModel) requestEntry.Model); |
| 0 | 339 | | } |
| | 340 | |
|
| | 341 | | private void OnEntryMenuToggle(FriendEntryBase friendEntry) |
| | 342 | | { |
| 0 | 343 | | contextMenuPanel.Show(friendEntry.Model.userId); |
| 0 | 344 | | friendEntry.Dock(contextMenuPanel); |
| 0 | 345 | | } |
| | 346 | |
|
| | 347 | | private void UpdateCounterLabel() |
| | 348 | | { |
| 6 | 349 | | receivedRequestsCountText.SetText("RECEIVED ({0})", receivedRequestsList.Count()); |
| 6 | 350 | | sentRequestsCountText.SetText("SENT ({0})", sentRequestsList.Count()); |
| 6 | 351 | | } |
| | 352 | |
|
| | 353 | | private void HandleFriendBlockRequest(string userId, bool blockUser) |
| | 354 | | { |
| 0 | 355 | | var friendEntryToBlock = Get(userId); |
| 0 | 356 | | if (friendEntryToBlock == null) return; |
| | 357 | | // instantly refresh ui |
| 0 | 358 | | friendEntryToBlock.Model.blocked = blockUser; |
| 0 | 359 | | Set(userId, (FriendRequestEntryModel) friendEntryToBlock.Model); |
| 0 | 360 | | } |
| | 361 | |
|
| | 362 | | private void FetchProfilePicturesForVisibleEntries() |
| | 363 | | { |
| 22 | 364 | | foreach (var entry in entries.Values.Skip(currentAvatarSnapshotIndex).Take(AVATAR_SNAPSHOTS_PER_FRAME)) |
| | 365 | | { |
| 5 | 366 | | if (entry.IsVisible(viewport)) |
| 5 | 367 | | entry.EnableAvatarSnapshotFetching(); |
| | 368 | | else |
| 0 | 369 | | entry.DisableAvatarSnapshotFetching(); |
| | 370 | | } |
| | 371 | |
|
| 6 | 372 | | currentAvatarSnapshotIndex += AVATAR_SNAPSHOTS_PER_FRAME; |
| | 373 | |
|
| 6 | 374 | | if (currentAvatarSnapshotIndex >= entries.Count) |
| 6 | 375 | | currentAvatarSnapshotIndex = 0; |
| 6 | 376 | | } |
| | 377 | |
|
| | 378 | | private void SetQueuedEntries() |
| | 379 | | { |
| 7 | 380 | | if (creationQueue.Count == 0) return; |
| | 381 | |
|
| 20 | 382 | | for (var i = 0; i < CREATION_AMOUNT_PER_FRAME && creationQueue.Count != 0; i++) |
| | 383 | | { |
| 5 | 384 | | var pair = creationQueue.FirstOrDefault(); |
| 5 | 385 | | creationQueue.Remove(pair.Key); |
| 5 | 386 | | Set(pair.Key, pair.Value); |
| | 387 | | } |
| 5 | 388 | | } |
| | 389 | |
|
| 1 | 390 | | private void RequestMoreEntries() => OnRequireMoreEntries?.Invoke(); |
| | 391 | |
|
| | 392 | | [Serializable] |
| | 393 | | private class Model |
| | 394 | | { |
| | 395 | | [Serializable] |
| | 396 | | public struct UserIdAndEntry |
| | 397 | | { |
| | 398 | | public string userId; |
| | 399 | | public bool received; |
| | 400 | | public SerializableEntryModel model; |
| | 401 | | } |
| | 402 | |
|
| | 403 | | [Serializable] |
| | 404 | | public class SerializableEntryModel : FriendRequestEntryModel |
| | 405 | | { |
| | 406 | | } |
| | 407 | |
|
| | 408 | | public UserIdAndEntry[] friends; |
| 24 | 409 | | public bool isReceivedRequestsExpanded = true; |
| 24 | 410 | | public bool isSentRequestsExpanded = true; |
| | 411 | | } |
| | 412 | | } |