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