| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Text.RegularExpressions; |
| | 4 | | using DCL; |
| | 5 | | using UIComponents.CollapsableSortedList; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | public class CollapsableDirectChatListComponentView : CollapsableSortedListComponentView<string, PrivateChatEntry> |
| | 9 | | { |
| | 10 | | private const string POOL_NAME_PREFIX = "DirectChatEntriesPool_"; |
| | 11 | |
|
| | 12 | | [SerializeField] private PrivateChatEntry entryPrefab; |
| | 13 | | [SerializeField] private UserContextMenu userContextMenu; |
| | 14 | |
|
| 50 | 15 | | private readonly Dictionary<string, PoolableObject> pooleableEntries = new Dictionary<string, PoolableObject>(); |
| | 16 | | private Pool entryPool; |
| | 17 | | private IChatController chatController; |
| | 18 | | private ILastReadMessagesService lastReadMessagesService; |
| 50 | 19 | | private bool releaseEntriesFromPool = true; |
| | 20 | |
|
| | 21 | | public event Action<PrivateChatEntry> OnOpenChat; |
| | 22 | | public event Action<string> OnUnfriend |
| | 23 | | { |
| 0 | 24 | | add => userContextMenu.OnUnfriend += value; |
| 0 | 25 | | remove => userContextMenu.OnUnfriend -= value; |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public void Initialize(IChatController chatController, ILastReadMessagesService lastReadMessagesService) |
| | 29 | | { |
| 0 | 30 | | this.chatController = chatController; |
| 0 | 31 | | this.lastReadMessagesService = lastReadMessagesService; |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | public void Filter(string search) |
| | 35 | | { |
| 0 | 36 | | if (!gameObject.activeInHierarchy) return; |
| 0 | 37 | | var regex = new Regex(search, RegexOptions.IgnoreCase); |
| 0 | 38 | | Filter(entry => regex.IsMatch(entry.Model.userName)); |
| | 39 | | // throttling may intruduce race conditions & artifacts into the ui |
| | 40 | | // StartCoroutine(FilterAsync(entry => regex.IsMatch(entry.Model.userName))); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | public void Clear(bool releaseEntriesFromPool) |
| | 44 | | { |
| | 45 | | // avoids releasing instances from pool just for this clear |
| 9 | 46 | | this.releaseEntriesFromPool = releaseEntriesFromPool; |
| 9 | 47 | | base.Clear(); |
| 9 | 48 | | this.releaseEntriesFromPool = true; |
| 9 | 49 | | pooleableEntries.Clear(); |
| 9 | 50 | | } |
| | 51 | |
|
| | 52 | | public override PrivateChatEntry Remove(string key) |
| | 53 | | { |
| 32 | 54 | | if (releaseEntriesFromPool) |
| | 55 | | { |
| 9 | 56 | | if (pooleableEntries.ContainsKey(key)) |
| 1 | 57 | | pooleableEntries[key].Release(); |
| 9 | 58 | | pooleableEntries.Remove(key); |
| | 59 | | } |
| | 60 | |
|
| 32 | 61 | | return base.Remove(key); |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public void Set(string userId, PrivateChatEntry.PrivateChatEntryModel entryModel) |
| | 65 | | { |
| 29 | 66 | | if (!Contains(entryModel.userId)) |
| 17 | 67 | | CreateEntry(userId); |
| | 68 | |
|
| 29 | 69 | | var entry = Get(userId); |
| 29 | 70 | | entry.Configure(entryModel); |
| 29 | 71 | | } |
| | 72 | |
|
| | 73 | | public void RefreshBlockedEntries(List<string> blockedUsers) |
| | 74 | | { |
| 0 | 75 | | foreach (var pair in Entries) |
| | 76 | | { |
| 0 | 77 | | pair.Value.SetBlockStatus(blockedUsers.Contains(pair.Key)); |
| | 78 | | } |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | private void CreateEntry(string userId) |
| | 82 | | { |
| 17 | 83 | | entryPool = GetEntryPool(); |
| 17 | 84 | | var newFriendEntry = entryPool.Get(); |
| 17 | 85 | | pooleableEntries.Add(userId, newFriendEntry); |
| 17 | 86 | | var entry = newFriendEntry.gameObject.GetComponent<PrivateChatEntry>(); |
| 17 | 87 | | Add(userId, entry); |
| 17 | 88 | | entry.Initialize(chatController, userContextMenu, lastReadMessagesService); |
| 17 | 89 | | entry.OnOpenChat -= OnEntryOpenChat; |
| 17 | 90 | | entry.OnOpenChat += OnEntryOpenChat; |
| 17 | 91 | | } |
| | 92 | |
|
| 2 | 93 | | private void OnEntryOpenChat(PrivateChatEntry entry) { OnOpenChat?.Invoke(entry); } |
| | 94 | |
|
| | 95 | | private Pool GetEntryPool() |
| | 96 | | { |
| 17 | 97 | | var entryPool = PoolManager.i.GetPool(POOL_NAME_PREFIX + name + GetInstanceID()); |
| 26 | 98 | | if (entryPool != null) return entryPool; |
| | 99 | |
|
| 8 | 100 | | entryPool = PoolManager.i.AddPool( |
| | 101 | | POOL_NAME_PREFIX + name + GetInstanceID(), |
| | 102 | | Instantiate(entryPrefab).gameObject, |
| | 103 | | maxPrewarmCount: 20, |
| | 104 | | isPersistent: true); |
| 8 | 105 | | entryPool.ForcePrewarm(); |
| | 106 | |
|
| 8 | 107 | | return entryPool; |
| | 108 | | } |
| | 109 | | } |