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