| | 1 | | using System.Collections.Generic; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.Chat.HUD |
| | 5 | | { |
| | 6 | | [CreateAssetMenu(fileName = "PoolPrivateChatEntryFactory", menuName = "DCL/Social/PoolPrivateChatEntryFactory")] |
| | 7 | | public class PoolPrivateChatEntryFactory : ScriptableObject, IChatEntryFactory |
| | 8 | | { |
| | 9 | | private const string POOL_NAME_PREFIX = "ChatDateSeparators_"; |
| | 10 | | private const int PRE_INSTANTIATED_ENTRIES = 30; |
| | 11 | |
|
| | 12 | | [SerializeField] private PoolChatEntryFactory factory; |
| | 13 | | [SerializeField] private DateSeparatorEntry separatorEntryPrefab; |
| | 14 | |
|
| 1 | 15 | | private readonly Dictionary<ChatEntry, PoolableObject> pooledObjects = |
| | 16 | | new Dictionary<ChatEntry, PoolableObject>(); |
| | 17 | |
|
| | 18 | | public DateSeparatorEntry CreateDateSeparator() |
| | 19 | | { |
| 0 | 20 | | var pool = GetPool(); |
| 0 | 21 | | var pooledObj = pool.Get(); |
| 0 | 22 | | var entry = pooledObj.gameObject.GetComponent<DateSeparatorEntry>(); |
| 0 | 23 | | pooledObjects[entry] = pooledObj; |
| 0 | 24 | | return entry; |
| | 25 | | } |
| | 26 | |
|
| 0 | 27 | | public ChatEntry Create(ChatEntryModel model) => factory.Create(model); |
| | 28 | |
|
| | 29 | | public void Destroy(ChatEntry entry) |
| | 30 | | { |
| 0 | 31 | | if (pooledObjects.TryGetValue(entry, out var pooledObj)) |
| | 32 | | { |
| 0 | 33 | | pooledObj.Release(); |
| 0 | 34 | | pooledObjects.Remove(entry); |
| | 35 | | } |
| | 36 | |
|
| 0 | 37 | | factory.Destroy(entry); |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | private Pool GetPool() |
| | 41 | | { |
| 0 | 42 | | var poolId = POOL_NAME_PREFIX + GetInstanceID(); |
| 0 | 43 | | var entryPool = PoolManager.i.GetPool(poolId); |
| 0 | 44 | | if (entryPool != null) return entryPool; |
| | 45 | |
|
| 0 | 46 | | entryPool = PoolManager.i.AddPool( |
| | 47 | | poolId, |
| | 48 | | Instantiate(separatorEntryPrefab).gameObject, |
| | 49 | | maxPrewarmCount: PRE_INSTANTIATED_ENTRIES, |
| | 50 | | isPersistent: true); |
| 0 | 51 | | entryPool.ForcePrewarm(); |
| | 52 | |
|
| 0 | 53 | | return entryPool; |
| | 54 | | } |
| | 55 | | } |
| | 56 | | } |