| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCLServices.EnvironmentProvider; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Backpack |
| | 8 | | { |
| | 9 | | public class VRMDetailsController : IDisposable |
| | 10 | | { |
| | 11 | | private readonly IUserProfileBridge userProfileBridge; |
| | 12 | | private readonly IVRMDetailsComponentView view; |
| 25 | 13 | | private readonly Dictionary<string, NFTDataDTO> nftDataCache = new (); |
| | 14 | | private readonly INFTFetchHelper nftFetchHelper; |
| | 15 | | private IEnvironmentProviderService environmentProviderService; |
| | 16 | |
|
| | 17 | | public event Action<string, UnequipWearableSource> OnWearableUnequipped; |
| | 18 | | public event Action<string, EquipWearableSource> OnWearableEquipped; |
| | 19 | |
|
| 25 | 20 | | public VRMDetailsController( |
| | 21 | | IVRMDetailsComponentView view, |
| | 22 | | IUserProfileBridge userProfileBridge, |
| | 23 | | INFTFetchHelper nftFetchHelper, |
| | 24 | | IEnvironmentProviderService environmentProviderService) |
| | 25 | | { |
| 25 | 26 | | this.view = view; |
| 25 | 27 | | this.userProfileBridge = userProfileBridge; |
| 25 | 28 | | this.nftFetchHelper = nftFetchHelper; |
| 25 | 29 | | this.environmentProviderService = environmentProviderService; |
| | 30 | |
|
| 25 | 31 | | view.OnWearableUnequipped += HandleWearableUnequipped; |
| 25 | 32 | | view.OnWearableEquipped += HandleWearableEquipped; |
| 25 | 33 | | } |
| | 34 | |
|
| | 35 | | public void Dispose() |
| | 36 | | { |
| 0 | 37 | | view.OnWearableUnequipped -= HandleWearableUnequipped; |
| 0 | 38 | | view.OnWearableEquipped -= HandleWearableEquipped; |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | private void HandleWearableEquipped(VRMItemModel vrmItemModel, EquipWearableSource equipWearableSource) |
| | 42 | | { |
| 0 | 43 | | OnWearableEquipped?.Invoke(vrmItemModel.wearableUrn, equipWearableSource); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | private void HandleWearableUnequipped(VRMItemModel vrmItemModel, UnequipWearableSource unEquipWearableSource) |
| | 47 | | { |
| 0 | 48 | | OnWearableUnequipped?.Invoke(vrmItemModel.wearableUrn, unEquipWearableSource); |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Initializes the controller. |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="vrmBlockingWearables"> A dictionary where the Key is the wearable urn and the Value is whether |
| | 55 | | public void Initialize(Dictionary<string, bool> vrmBlockingWearables) |
| | 56 | | { |
| 0 | 57 | | PopulateDetails(vrmBlockingWearables).Forget(); |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | private async UniTask PopulateDetails(Dictionary<string, bool> vrmBlockingWearables) |
| | 61 | | { |
| 0 | 62 | | List<NFTDataDTO> itemsToDisplay = new (); |
| | 63 | |
|
| | 64 | | // Copy the list from params since it's used for other checks elsewhere |
| 0 | 65 | | Dictionary<string, bool> itemsToProcess = new (vrmBlockingWearables); |
| | 66 | |
|
| | 67 | | // Get cached items first, to reduce api calls. |
| 0 | 68 | | foreach ((string id, NFTDataDTO data) in nftDataCache) |
| 0 | 69 | | if (itemsToProcess.ContainsKey(id)) |
| | 70 | | { |
| 0 | 71 | | itemsToDisplay.Add(data); |
| 0 | 72 | | itemsToProcess.Remove(id); |
| | 73 | | } |
| | 74 | |
|
| | 75 | | // If we're still missing items, fetch them. |
| 0 | 76 | | if (itemsToProcess.Count > 0) |
| | 77 | | { |
| 0 | 78 | | string nftItemRawData = await nftFetchHelper.GetNFTItems(new List<string>(itemsToProcess.Keys), environm |
| 0 | 79 | | var nftItemsData = JsonUtility.FromJson<NFTItemsDTO>(nftItemRawData); |
| | 80 | |
|
| 0 | 81 | | for (var i = 0; i < nftItemsData.data.Length; i++) |
| | 82 | | { |
| 0 | 83 | | NFTDataDTO nftDataDto = nftItemsData.data[i]; |
| 0 | 84 | | var creatorProfile = userProfileBridge.Get(nftDataDto.creator); |
| | 85 | |
|
| 0 | 86 | | if (creatorProfile == null) |
| | 87 | | { |
| 0 | 88 | | creatorProfile = await userProfileBridge.RequestFullUserProfileAsync(nftDataDto.creator); |
| | 89 | |
|
| 0 | 90 | | if (creatorProfile == null) |
| | 91 | | { |
| 0 | 92 | | Debug.LogError($"Creator {nftDataDto.creator} profile could not be retrieved!"); |
| 0 | 93 | | nftDataDto.creatorName = "User not found"; |
| 0 | 94 | | nftDataDto.creatorImageUrl = string.Empty; |
| | 95 | | } |
| | 96 | | } |
| | 97 | |
|
| 0 | 98 | | if (creatorProfile != null) |
| | 99 | | { |
| 0 | 100 | | nftDataDto.creatorName = creatorProfile.userName; |
| 0 | 101 | | nftDataDto.creatorImageUrl = creatorProfile.face256SnapshotURL; |
| | 102 | | } |
| | 103 | |
|
| 0 | 104 | | nftDataDto.canBeUnEquipped = itemsToProcess[nftDataDto.urn]; |
| | 105 | |
|
| 0 | 106 | | nftDataCache.Add(nftDataDto.urn, nftDataDto); |
| 0 | 107 | | itemsToDisplay.Add(nftDataDto); |
| 0 | 108 | | } |
| 0 | 109 | | } |
| | 110 | |
|
| 0 | 111 | | view.FillVRMBlockingWearablesList(itemsToDisplay); |
| 0 | 112 | | } |
| | 113 | | } |
| | 114 | | } |