| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers.NFT; |
| | 4 | | using Object = UnityEngine.Object; |
| | 5 | |
|
| | 6 | | internal class OwnersInfoController : IDisposable |
| | 7 | | { |
| 7 | 8 | | private readonly Queue<IOwnerInfoElement> elementsPool = new Queue<IOwnerInfoElement>(); |
| 7 | 9 | | private readonly List<IOwnerInfoElement> activeElements = new List<IOwnerInfoElement>(); |
| | 10 | | private readonly OwnerInfoElement ownerElementPrefab; |
| | 11 | |
|
| 21 | 12 | | public OwnersInfoController(OwnerInfoElement ownerElementPrefab) { this.ownerElementPrefab = ownerElementPrefab; } |
| | 13 | |
|
| | 14 | | public void Dispose() |
| | 15 | | { |
| 0 | 16 | | while (elementsPool.Count > 0) |
| | 17 | | { |
| 0 | 18 | | elementsPool.Dequeue().Dispose(); |
| | 19 | | } |
| 0 | 20 | | for (int i = 0; i < activeElements.Count; i++) |
| | 21 | | { |
| 0 | 22 | | activeElements[i].Dispose(); |
| | 23 | | } |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | public void SetOwners(NFTInfoSingleAsset.Owners[] owners) |
| | 27 | | { |
| 0 | 28 | | int activeElementsCount = activeElements.Count; |
| 0 | 29 | | for (int i = activeElementsCount - 1; i >= 0; i--) |
| | 30 | | { |
| 0 | 31 | | if (i < owners.Length) |
| | 32 | | { |
| 0 | 33 | | activeElements[i].SetOwner(owners[i].owner); |
| 0 | 34 | | } |
| | 35 | | else |
| | 36 | | { |
| 0 | 37 | | PoolElement(activeElements[i]); |
| 0 | 38 | | activeElements.RemoveAt(i); |
| | 39 | | } |
| | 40 | | } |
| | 41 | |
|
| 0 | 42 | | if (activeElementsCount < owners.Length) |
| | 43 | | { |
| 0 | 44 | | for (int i = activeElementsCount; i < owners.Length; i++) |
| | 45 | | { |
| 0 | 46 | | var element = GetElement(); |
| 0 | 47 | | element.SetOwner(owners[i].owner); |
| 0 | 48 | | activeElements.Add(element); |
| | 49 | | } |
| | 50 | | } |
| 0 | 51 | | } |
| | 52 | |
|
| 0 | 53 | | public List<IOwnerInfoElement> GetElements() { return activeElements; } |
| | 54 | |
|
| | 55 | | private IOwnerInfoElement GetElement() |
| | 56 | | { |
| | 57 | | IOwnerInfoElement ret; |
| 0 | 58 | | if (elementsPool.Count > 0) |
| | 59 | | { |
| 0 | 60 | | ret = elementsPool.Dequeue(); |
| 0 | 61 | | } |
| | 62 | | else |
| | 63 | | { |
| 0 | 64 | | ret = Object.Instantiate(ownerElementPrefab); |
| | 65 | | } |
| 0 | 66 | | return ret; |
| | 67 | | } |
| | 68 | |
|
| | 69 | | private void PoolElement(IOwnerInfoElement element) |
| | 70 | | { |
| 0 | 71 | | elementsPool.Enqueue(element); |
| 0 | 72 | | element.SetActive(false); |
| 0 | 73 | | } |
| | 74 | | } |