| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.NotificationModel; |
| | 4 | | using DCL.Tasks; |
| | 5 | | using DCLServices.Lambdas.LandsService; |
| | 6 | | using DCLServices.Lambdas.NamesService; |
| | 7 | | using DCLServices.WearablesCatalogService; |
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Threading; |
| | 11 | | using Type = DCL.NotificationModel.Type; |
| | 12 | |
|
| | 13 | | public class ViewAllComponentController : IDisposable |
| | 14 | | { |
| | 15 | | private const string NAME_TYPE = "name"; |
| | 16 | | private const string EMOTE_TYPE = "emote"; |
| | 17 | | private const string LAND_TYPE = "land"; |
| | 18 | | private const string REQUEST_ERROR_MESSAGE = "There was an error while trying to process your request. Please try ag |
| | 19 | |
|
| | 20 | | public event Action OnBackFromViewAll; |
| | 21 | | public delegate void ClickBuyNftDelegate(NftInfo nftInfo); |
| | 22 | | public event ClickBuyNftDelegate OnClickBuyNft; |
| | 23 | |
|
| | 24 | |
|
| | 25 | | private readonly IWearablesCatalogService wearablesCatalogService; |
| | 26 | | private readonly IViewAllComponentView view; |
| | 27 | | private readonly DataStore_HUDs hudsDataStore; |
| | 28 | | private readonly ILandsService landsService; |
| | 29 | | private readonly INamesService namesService; |
| | 30 | | private readonly NotificationsController notificationsController; |
| 0 | 31 | | private CancellationTokenSource sectionsCts = new (); |
| | 32 | | private bool cleanCachedWearablesPages; |
| | 33 | |
|
| 0 | 34 | | public ViewAllComponentController( |
| | 35 | | IViewAllComponentView view, |
| | 36 | | DataStore_HUDs hudsDataStore, |
| | 37 | | IWearablesCatalogService wearablesCatalogService, |
| | 38 | | ILandsService landsService, |
| | 39 | | INamesService namesService, |
| | 40 | | NotificationsController notificationsController) |
| | 41 | | { |
| 0 | 42 | | this.view = view; |
| 0 | 43 | | this.hudsDataStore = hudsDataStore; |
| 0 | 44 | | this.wearablesCatalogService = wearablesCatalogService; |
| 0 | 45 | | this.landsService = landsService; |
| 0 | 46 | | this.namesService = namesService; |
| 0 | 47 | | this.notificationsController = notificationsController; |
| 0 | 48 | | view.OnBackFromViewAll += BackFromViewAll; |
| 0 | 49 | | view.OnRequestCollectibleElements += RequestCollectibleElements; |
| 0 | 50 | | view.OnClickBuyNft += (nftId) => OnClickBuyNft?.Invoke(nftId); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public void OpenViewAllSection(PassportSection section) |
| | 54 | | { |
| 0 | 55 | | view.Initialize(section); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public void SetViewAllVisibility(bool isVisible) |
| | 59 | | { |
| 0 | 60 | | view.SetVisible(isVisible); |
| | 61 | |
|
| 0 | 62 | | if (isVisible) |
| | 63 | | { |
| 0 | 64 | | sectionsCts = sectionsCts.SafeRestart(); |
| 0 | 65 | | cleanCachedWearablesPages = true; |
| | 66 | | } |
| | 67 | | else |
| | 68 | | { |
| 0 | 69 | | sectionsCts.SafeCancelAndDispose(); |
| 0 | 70 | | sectionsCts = null; |
| | 71 | |
|
| 0 | 72 | | view.SetLoadingActive(false); |
| | 73 | | } |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | private void BackFromViewAll() |
| | 77 | | { |
| 0 | 78 | | OnBackFromViewAll?.Invoke(); |
| 0 | 79 | | SetViewAllVisibility(false); |
| 0 | 80 | | view.CloseAllNftItemInfos(); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | private void ShowNftIcons(List<(NFTIconComponentModel model, WearableItem wearable)> iconsWithWearables) |
| | 84 | | { |
| 0 | 85 | | view.ShowNftIcons(iconsWithWearables); |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | public void Dispose() |
| | 89 | | { |
| 0 | 90 | | view.OnBackFromViewAll -= BackFromViewAll; |
| 0 | 91 | | view.OnRequestCollectibleElements -= RequestCollectibleElements; |
| | 92 | |
|
| 0 | 93 | | sectionsCts.SafeCancelAndDispose(); |
| 0 | 94 | | sectionsCts = null; |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | private void RequestCollectibleElements(PassportSection section, int pageNumber, int pageSize) |
| | 98 | | { |
| 0 | 99 | | view.CloseAllNftItemInfos(); |
| | 100 | |
|
| | 101 | | switch (section) |
| | 102 | | { |
| | 103 | | case PassportSection.Wearables: |
| 0 | 104 | | RequestOwnedWearablesAsync(hudsDataStore.currentPlayerId.Get().playerId, pageNumber, pageSize, sectionsC |
| 0 | 105 | | break; |
| | 106 | | case PassportSection.Names: |
| 0 | 107 | | RequestOwnedNamesAsync(hudsDataStore.currentPlayerId.Get().playerId, pageNumber, pageSize, sectionsCts.T |
| 0 | 108 | | break; |
| | 109 | | case PassportSection.Lands: |
| 0 | 110 | | RequestOwnedLandsAsync(hudsDataStore.currentPlayerId.Get().playerId, pageNumber, pageSize, sectionsCts.T |
| | 111 | | break; |
| | 112 | | } |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | private async UniTask RequestOwnedWearablesAsync(string userId, int pageNumber, int pageSize, CancellationToken ct) |
| | 116 | | { |
| | 117 | | try |
| | 118 | | { |
| 0 | 119 | | view.SetLoadingActive(true); |
| | 120 | |
|
| 0 | 121 | | (IReadOnlyList<WearableItem> wearables, int totalAmount) ownedWearableItems = |
| | 122 | | await wearablesCatalogService.RequestOwnedWearablesAsync(userId, pageNumber, pageSize, cleanCachedWearab |
| | 123 | |
|
| 0 | 124 | | cleanCachedWearablesPages = false; |
| 0 | 125 | | ProcessReceivedWearables(ownedWearableItems.wearables); |
| 0 | 126 | | view.SetTotalElements(ownedWearableItems.totalAmount); |
| 0 | 127 | | view.SetLoadingActive(false); |
| 0 | 128 | | } |
| 0 | 129 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 130 | | { |
| 0 | 131 | | ShowErrorAndGoBack(); |
| 0 | 132 | | } |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | private async UniTask RequestOwnedNamesAsync(string userId, int pageNumber, int pageSize, CancellationToken ct) |
| | 136 | | { |
| | 137 | | try |
| | 138 | | { |
| 0 | 139 | | view.SetLoadingActive(true); |
| | 140 | |
|
| 0 | 141 | | (IReadOnlyList<NamesResponse.NameEntry> names, int totalAmount) ownedNamesItems = |
| | 142 | | await namesService.RequestOwnedNamesAsync(userId, pageNumber, pageSize, cleanCachedWearablesPages, ct); |
| | 143 | |
|
| 0 | 144 | | cleanCachedWearablesPages = false; |
| 0 | 145 | | ProcessReceivedNames(ownedNamesItems.names); |
| 0 | 146 | | view.SetTotalElements(ownedNamesItems.totalAmount); |
| 0 | 147 | | view.SetLoadingActive(false); |
| 0 | 148 | | } |
| 0 | 149 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 150 | | { |
| 0 | 151 | | ShowErrorAndGoBack(); |
| 0 | 152 | | } |
| 0 | 153 | | } |
| | 154 | |
|
| | 155 | | private async UniTask RequestOwnedLandsAsync(string userId, int pageNumber, int pageSize, CancellationToken ct) |
| | 156 | | { |
| | 157 | | try |
| | 158 | | { |
| 0 | 159 | | view.SetLoadingActive(true); |
| | 160 | |
|
| 0 | 161 | | (IReadOnlyList<LandsResponse.LandEntry> lands, int totalAmount) ownedLandsItems = |
| | 162 | | await landsService.RequestOwnedLandsAsync(userId, pageNumber, pageSize, cleanCachedWearablesPages, ct); |
| | 163 | |
|
| 0 | 164 | | cleanCachedWearablesPages = false; |
| 0 | 165 | | ProcessReceivedLands(ownedLandsItems.lands); |
| 0 | 166 | | view.SetTotalElements(ownedLandsItems.totalAmount); |
| 0 | 167 | | view.SetLoadingActive(false); |
| 0 | 168 | | } |
| 0 | 169 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 170 | | { |
| 0 | 171 | | ShowErrorAndGoBack(); |
| 0 | 172 | | } |
| 0 | 173 | | } |
| | 174 | |
|
| | 175 | | private void ProcessReceivedWearables(IReadOnlyList<WearableItem> wearables) |
| | 176 | | { |
| 0 | 177 | | List<(NFTIconComponentModel Model, WearableItem w)> wearableModels = new List<(NFTIconComponentModel Model, Wear |
| 0 | 178 | | foreach (var wearable in wearables) |
| | 179 | | { |
| 0 | 180 | | bool isWearableCollectible = wearable.IsCollectible(); |
| | 181 | |
|
| 0 | 182 | | wearableModels.Add((new NFTIconComponentModel |
| | 183 | | { |
| | 184 | | showMarketplaceButton = isWearableCollectible, |
| | 185 | | showType = isWearableCollectible, |
| | 186 | | type = wearable.data.category, |
| | 187 | | marketplaceURI = "", |
| | 188 | | name = wearable.GetName(), |
| | 189 | | rarity = wearable.rarity, |
| | 190 | | imageURI = wearable.ComposeThumbnailUrl(), |
| | 191 | | nftInfo = wearable.GetNftInfo(), |
| | 192 | | }, wearable)); |
| | 193 | | } |
| 0 | 194 | | ShowNftIcons(wearableModels); |
| 0 | 195 | | } |
| | 196 | |
|
| | 197 | | private void ProcessReceivedEmotes(WearableItem[] emotes) |
| | 198 | | { |
| 0 | 199 | | List<(NFTIconComponentModel Model, WearableItem w)> emoteModels = new List<(NFTIconComponentModel Model, Wearabl |
| 0 | 200 | | foreach (var emote in emotes) |
| | 201 | | { |
| 0 | 202 | | emoteModels.Add((new NFTIconComponentModel |
| | 203 | | { |
| | 204 | | showMarketplaceButton = true, |
| | 205 | | showType = true, |
| | 206 | | type = EMOTE_TYPE, |
| | 207 | | marketplaceURI = "", |
| | 208 | | name = emote.GetName(), |
| | 209 | | rarity = emote.rarity, |
| | 210 | | imageURI = emote.ComposeThumbnailUrl(), |
| | 211 | | nftInfo = emote.GetNftInfo(), |
| | 212 | | }, emote)); |
| | 213 | | } |
| 0 | 214 | | ShowNftIcons(emoteModels); |
| 0 | 215 | | } |
| | 216 | |
|
| | 217 | | private void ProcessReceivedNames(IReadOnlyList<NamesResponse.NameEntry> names) |
| | 218 | | { |
| 0 | 219 | | List<(NFTIconComponentModel Model, WearableItem w)> nameModels = new List<(NFTIconComponentModel Model, Wearable |
| 0 | 220 | | foreach (var name in names) |
| | 221 | | { |
| 0 | 222 | | nameModels.Add((new NFTIconComponentModel |
| | 223 | | { |
| | 224 | | showMarketplaceButton = true, |
| | 225 | | showType = true, |
| | 226 | | type = NAME_TYPE, |
| | 227 | | marketplaceURI = "", |
| | 228 | | name = name.Name, |
| | 229 | | rarity = NAME_TYPE, |
| | 230 | | imageURI = "", |
| | 231 | | nftInfo = name.GetNftInfo(), |
| | 232 | | }, null)); |
| | 233 | | } |
| 0 | 234 | | ShowNftIcons(nameModels); |
| 0 | 235 | | } |
| | 236 | |
|
| | 237 | | private void ProcessReceivedLands(IReadOnlyList<LandsResponse.LandEntry> lands) |
| | 238 | | { |
| 0 | 239 | | List<(NFTIconComponentModel Model, WearableItem w)> landModels = new List<(NFTIconComponentModel Model, Wearable |
| 0 | 240 | | foreach (var land in lands) |
| | 241 | | { |
| 0 | 242 | | landModels.Add((new() |
| | 243 | | { |
| | 244 | | showMarketplaceButton = true, |
| | 245 | | showType = true, |
| | 246 | | type = land.Category, |
| | 247 | | marketplaceURI = "", |
| | 248 | | name = !string.IsNullOrEmpty(land.Name) ? land.Name : land.Category, |
| | 249 | | rarity = LAND_TYPE, |
| | 250 | | imageURI = land.Image, |
| | 251 | | nftInfo = land.GetNftInfo(), |
| | 252 | | }, null)); |
| | 253 | | } |
| 0 | 254 | | ShowNftIcons(landModels); |
| 0 | 255 | | } |
| | 256 | |
|
| | 257 | | private void ShowErrorAndGoBack() |
| | 258 | | { |
| 0 | 259 | | notificationsController.ShowNotification(new Model |
| | 260 | | { |
| | 261 | | message = REQUEST_ERROR_MESSAGE, |
| | 262 | | type = Type.ERROR, |
| | 263 | | timer = 10f, |
| | 264 | | destroyOnFinish = true, |
| | 265 | | }); |
| | 266 | |
|
| 0 | 267 | | BackFromViewAll(); |
| 0 | 268 | | } |
| | 269 | | } |