| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | public class ViewAllComponentView : BaseComponentView, IViewAllComponentView |
| | 8 | | { |
| | 9 | | private const int ELEMENTS_PER_PAGE = 20; |
| | 10 | | private const string NFT_ELEMENTS_POOL_NAME_PREFIX = "NFTElementsEntriesPool_"; |
| 0 | 11 | | private static readonly Vector3 NFT_ICON_SCALE = new Vector3(0.75f, 0.75f, 0.75f); |
| | 12 | |
|
| | 13 | | [SerializeField] private TMP_Text sectionName; |
| | 14 | | [SerializeField] private TMP_Text sectionAmount; |
| | 15 | | [SerializeField] private ButtonComponentView backButton; |
| | 16 | | [SerializeField] private Transform itemsContainer; |
| | 17 | | [SerializeField] private UIPageSelector pageSelector; |
| | 18 | | [SerializeField] private GameObject nftPageElement; |
| | 19 | | [SerializeField] private GameObject loadingSpinner; |
| | 20 | | [SerializeField] internal NFTItemInfo nftItemInfo; |
| | 21 | |
|
| | 22 | | public event Action OnBackFromViewAll; |
| | 23 | | public event Action<PassportSection, int, int> OnRequestCollectibleElements; |
| | 24 | | public event Action<NftInfo> OnClickBuyNft; |
| | 25 | |
|
| 0 | 26 | | private List<PoolableObject> nftElementsPoolableQueue = new List<PoolableObject>(); |
| | 27 | | private Pool nftElementsEntryPool; |
| | 28 | | private PassportSection section; |
| 0 | 29 | | private readonly List<NFTIconComponentView> nftWearableViews = new List<NFTIconComponentView>(); |
| | 30 | | private string poolEntryId; |
| | 31 | |
|
| | 32 | | public override void Awake() |
| | 33 | | { |
| 0 | 34 | | base.Awake(); |
| | 35 | |
|
| 0 | 36 | | backButton.onClick.RemoveAllListeners(); |
| 0 | 37 | | backButton.onClick.AddListener(()=> |
| | 38 | | { |
| 0 | 39 | | ClearNftPool(); |
| 0 | 40 | | OnBackFromViewAll?.Invoke(); |
| 0 | 41 | | }); |
| 0 | 42 | | pageSelector.OnValueChanged += RequestPage; |
| | 43 | |
|
| 0 | 44 | | poolEntryId = NFT_ELEMENTS_POOL_NAME_PREFIX + name + GetInstanceID(); |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | public void Initialize(PassportSection passportSection) |
| | 48 | | { |
| 0 | 49 | | nftElementsEntryPool = GetNftElementsEntryPool(); |
| 0 | 50 | | section = passportSection; |
| 0 | 51 | | sectionName.text = passportSection.ToString(); |
| 0 | 52 | | sectionAmount.gameObject.SetActive(false); |
| 0 | 53 | | pageSelector.SelectPage(0); |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | public void SetTotalElements(int totalElements) |
| | 57 | | { |
| 0 | 58 | | pageSelector.Setup((totalElements + ELEMENTS_PER_PAGE - 1) / ELEMENTS_PER_PAGE); |
| 0 | 59 | | sectionAmount.text = $"({totalElements})"; |
| 0 | 60 | | if (!sectionAmount.gameObject.activeSelf) |
| 0 | 61 | | sectionAmount.gameObject.SetActive(true); |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | private void RequestPage(int pageNumber) |
| | 65 | | { |
| 0 | 66 | | ClearNftPool(); |
| 0 | 67 | | OnRequestCollectibleElements?.Invoke(section, pageNumber + 1, ELEMENTS_PER_PAGE); |
| 0 | 68 | | } |
| | 69 | |
|
| 0 | 70 | | public override void RefreshControl() { } |
| | 71 | |
|
| | 72 | | public void SetVisible(bool isVisible) |
| | 73 | | { |
| 0 | 74 | | gameObject.SetActive(isVisible); |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | public void ShowNftIcons(List<(NFTIconComponentModel model, WearableItem wearable)> iconsWithWearables) |
| | 78 | | { |
| 0 | 79 | | nftWearableViews.Clear(); |
| | 80 | |
|
| 0 | 81 | | foreach (var wearableData in iconsWithWearables) |
| | 82 | | { |
| 0 | 83 | | PoolableObject poolableObject = nftElementsEntryPool.Get(); |
| 0 | 84 | | nftElementsPoolableQueue.Add(poolableObject); |
| 0 | 85 | | poolableObject.gameObject.transform.SetParent(itemsContainer, false); |
| 0 | 86 | | poolableObject.gameObject.transform.localScale = NFT_ICON_SCALE; |
| 0 | 87 | | NFTIconComponentView nftIconComponentView = poolableObject.gameObject.GetComponent<NFTIconComponentView>(); |
| 0 | 88 | | nftIconComponentView.onMarketplaceButtonClick.RemoveAllListeners(); |
| 0 | 89 | | nftIconComponentView.onMarketplaceButtonClick.AddListener(() => ClickOnBuyWearable(wearableData.model.nftInf |
| 0 | 90 | | nftIconComponentView.Configure(wearableData.model); |
| 0 | 91 | | nftIconComponentView.onFocused -= FocusOnNFTIconView; |
| 0 | 92 | | nftIconComponentView.onFocused += FocusOnNFTIconView; |
| | 93 | |
|
| 0 | 94 | | if (wearableData.wearable != null) |
| | 95 | | { |
| 0 | 96 | | nftIconComponentView.onDetailInfoButtonClick.AddListener(() => nftIconComponentView.SetNFTItemInfoActive |
| 0 | 97 | | nftIconComponentView.ConfigureNFTItemInfo(nftItemInfo, wearableData.wearable, !wearableData.wearable.IsE |
| | 98 | | } |
| | 99 | | else |
| 0 | 100 | | nftIconComponentView.onDetailInfoButtonClick.RemoveAllListeners(); |
| | 101 | |
|
| 0 | 102 | | nftWearableViews.Add(nftIconComponentView); |
| | 103 | | } |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | public void SetLoadingActive(bool isLoading) |
| | 107 | | { |
| 0 | 108 | | loadingSpinner.SetActive(isLoading); |
| 0 | 109 | | itemsContainer.gameObject.SetActive(!isLoading); |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | public void CloseAllNftItemInfos() |
| | 113 | | { |
| 0 | 114 | | foreach (var nftView in nftWearableViews) |
| 0 | 115 | | nftView.SetNFTItemInfoActive(false); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | private void FocusOnNFTIconView(bool isFocused) |
| | 119 | | { |
| 0 | 120 | | if (!isFocused) |
| 0 | 121 | | return; |
| | 122 | |
|
| 0 | 123 | | CloseAllNftItemInfos(); |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | private void ClickOnBuyWearable(NftInfo nftInfo) |
| | 127 | | { |
| 0 | 128 | | OnClickBuyNft?.Invoke(nftInfo); |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | private Pool GetNftElementsEntryPool() |
| | 132 | | { |
| 0 | 133 | | var pool = PoolManager.i.GetPool(poolEntryId); |
| 0 | 134 | | if (pool != null) return pool; |
| | 135 | |
|
| 0 | 136 | | pool = PoolManager.i.AddPool( |
| | 137 | | poolEntryId, |
| | 138 | | Instantiate(nftPageElement).gameObject, |
| | 139 | | maxPrewarmCount: ELEMENTS_PER_PAGE, |
| | 140 | | isPersistent: true); |
| | 141 | |
|
| 0 | 142 | | pool.ForcePrewarm(); |
| 0 | 143 | | return pool; |
| | 144 | | } |
| | 145 | |
|
| | 146 | | private void ClearNftPool() |
| | 147 | | { |
| 0 | 148 | | foreach (var poolObject in nftElementsPoolableQueue) |
| 0 | 149 | | nftElementsEntryPool.Release(poolObject); |
| | 150 | |
|
| 0 | 151 | | nftElementsPoolableQueue = new List<PoolableObject>(); |
| 0 | 152 | | } |
| | 153 | | } |