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