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