| | 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 CollapsablePublicChannelListComponentView : CollapsableSortedListComponentView<string, PublicChannelEntry> |
| | 9 | | { |
| | 10 | | private const string POOL_NAME_PREFIX = "PublicChannelEntriesPool_"; |
| | 11 | |
|
| | 12 | | [SerializeField] private PublicChannelEntry entryPrefab; |
| | 13 | |
|
| 50 | 14 | | private readonly Dictionary<string, PoolableObject> pooleableEntries = new Dictionary<string, PoolableObject>(); |
| | 15 | | private Pool entryPool; |
| 50 | 16 | | private bool releaseEntriesFromPool = true; |
| | 17 | | private IChatController chatController; |
| | 18 | | private ILastReadMessagesService lastReadMessagesService; |
| | 19 | |
|
| | 20 | | public event Action<PublicChannelEntry> OnOpenChat; |
| | 21 | |
|
| | 22 | | public void Initialize(IChatController chatController, ILastReadMessagesService lastReadMessagesService) |
| | 23 | | { |
| 0 | 24 | | this.chatController = chatController; |
| 0 | 25 | | this.lastReadMessagesService = lastReadMessagesService; |
| 0 | 26 | | } |
| | 27 | |
|
| | 28 | | public void Filter(string search) |
| | 29 | | { |
| 0 | 30 | | var regex = new Regex(search, RegexOptions.IgnoreCase); |
| 0 | 31 | | Filter(entry => regex.IsMatch(entry.Model.name)); |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | public void Clear(bool releaseEntriesFromPool) |
| | 35 | | { |
| | 36 | | // avoids releasing instances from pool just for this clear |
| 9 | 37 | | this.releaseEntriesFromPool = releaseEntriesFromPool; |
| 9 | 38 | | base.Clear(); |
| 9 | 39 | | this.releaseEntriesFromPool = true; |
| 9 | 40 | | pooleableEntries.Clear(); |
| 9 | 41 | | } |
| | 42 | |
|
| | 43 | | public override PublicChannelEntry Remove(string key) |
| | 44 | | { |
| 9 | 45 | | if (releaseEntriesFromPool) |
| | 46 | | { |
| 0 | 47 | | if (pooleableEntries.ContainsKey(key)) |
| 0 | 48 | | pooleableEntries[key].Release(); |
| 0 | 49 | | pooleableEntries.Remove(key); |
| | 50 | | } |
| | 51 | |
|
| 9 | 52 | | return base.Remove(key); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public void Set(string channelId, PublicChannelEntry.PublicChannelEntryModel entryModel) |
| | 56 | | { |
| 14 | 57 | | if (!Contains(entryModel.channelId)) |
| 11 | 58 | | CreateEntry(channelId); |
| | 59 | |
|
| 14 | 60 | | var entry = Get(channelId); |
| 14 | 61 | | entry.Configure(entryModel); |
| 14 | 62 | | } |
| | 63 | |
|
| | 64 | | private void CreateEntry(string channelId) |
| | 65 | | { |
| 11 | 66 | | entryPool = GetEntryPool(); |
| 11 | 67 | | var newFriendEntry = entryPool.Get(); |
| 11 | 68 | | pooleableEntries.Add(channelId, newFriendEntry); |
| 11 | 69 | | var entry = newFriendEntry.gameObject.GetComponent<PublicChannelEntry>(); |
| 11 | 70 | | Add(channelId, entry); |
| 11 | 71 | | entry.Initialize(chatController, lastReadMessagesService); |
| 11 | 72 | | entry.OnOpenChat -= OnEntryOpenChat; |
| 11 | 73 | | entry.OnOpenChat += OnEntryOpenChat; |
| 11 | 74 | | } |
| | 75 | |
|
| 2 | 76 | | private void OnEntryOpenChat(PublicChannelEntry entry) { OnOpenChat?.Invoke(entry); } |
| | 77 | |
|
| | 78 | | private Pool GetEntryPool() |
| | 79 | | { |
| 11 | 80 | | var entryPool = PoolManager.i.GetPool(POOL_NAME_PREFIX + name + GetInstanceID()); |
| 14 | 81 | | if (entryPool != null) return entryPool; |
| | 82 | |
|
| 8 | 83 | | entryPool = PoolManager.i.AddPool( |
| | 84 | | POOL_NAME_PREFIX + name + GetInstanceID(), |
| | 85 | | Instantiate(entryPrefab).gameObject, |
| | 86 | | maxPrewarmCount: 20, |
| | 87 | | isPersistent: true); |
| 8 | 88 | | entryPool.ForcePrewarm(); |
| | 89 | |
|
| 8 | 90 | | return entryPool; |
| | 91 | | } |
| | 92 | | } |