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