9
9
using System . Collections . Generic ;
10
10
using System . Linq ;
11
11
using System . Threading ;
12
+ using System . Threading . Tasks ;
12
13
using UnityEngine ;
13
14
using UnityEngine . Pool ;
14
15
@@ -266,29 +267,9 @@ private async UniTask<int> RequestWearablesAndShowThem(int page, CancellationTok
266
267
267
268
try
268
269
{
269
- int ownedWearablesCount = wearables . Count ;
270
- int ownedWearablesTotalAmount = totalAmount ;
271
-
272
- customWearablesBuffer . Clear ( ) ;
273
-
274
- await UniTask . WhenAll ( FetchCustomWearableCollections ( customWearablesBuffer , cancellationToken ) ,
275
- FetchCustomWearableItems ( customWearablesBuffer , cancellationToken ) ) ;
276
-
277
- int customWearablesCount = customWearablesBuffer . Count ;
278
- totalAmount += customWearablesCount ;
279
-
280
- if ( ownedWearablesCount < PAGE_SIZE && customWearablesCount > 0 )
281
- {
282
- int ownedWearablesStartingPage = ( ownedWearablesTotalAmount / PAGE_SIZE ) + 1 ;
283
- int customWearablesOffsetPage = page - ownedWearablesStartingPage ;
284
- int skip = customWearablesOffsetPage * PAGE_SIZE ;
285
- // Fill the page considering the existing owned wearables
286
- int count = PAGE_SIZE - ownedWearablesCount ;
287
- int until = skip + count ;
288
-
289
- for ( int i = skip ; i < customWearablesBuffer . Count && i < until ; i ++ )
290
- wearables . Add ( customWearablesBuffer [ i ] ) ;
291
- }
270
+ totalAmount += await MergePublishedWearableCollections ( page , wearables , totalAmount , cancellationToken ) ;
271
+ totalAmount += await MergeBuilderWearableCollections ( page , wearables , totalAmount , cancellationToken ) ;
272
+ totalAmount += await MergeCustomWearableItems ( page , wearables , totalAmount , cancellationToken ) ;
292
273
293
274
customWearablesBuffer . Clear ( ) ;
294
275
}
@@ -299,7 +280,7 @@ await UniTask.WhenAll(FetchCustomWearableCollections(customWearablesBuffer, canc
299
280
currentWearables = wearables . Select ( ToWearableGridModel )
300
281
. ToDictionary ( item => ExtendedUrnParser . GetShortenedUrn ( item . WearableId ) , model => model ) ;
301
282
302
- view . SetWearablePages ( page , Mathf . CeilToInt ( ( float ) totalAmount / PAGE_SIZE ) ) ;
283
+ view . SetWearablePages ( page , Mathf . CeilToInt ( ( float ) totalAmount / PAGE_SIZE ) ) ;
303
284
304
285
// TODO: mark the wearables to be disposed if no references left
305
286
view . ClearWearables ( ) ;
@@ -313,6 +294,53 @@ await UniTask.WhenAll(FetchCustomWearableCollections(customWearablesBuffer, canc
313
294
return 0 ;
314
295
}
315
296
297
+ private async UniTask < int > MergeCustomWearableItems ( int page , List < WearableItem > wearables ,
298
+ int totalAmount , CancellationToken cancellationToken ) =>
299
+ await MergeToWearableResults ( page , wearables , totalAmount , FetchCustomWearableItems , cancellationToken ) ;
300
+
301
+ private async UniTask < int > MergePublishedWearableCollections ( int page , List < WearableItem > wearables , int totalAmount ,
302
+ CancellationToken cancellationToken ) =>
303
+ await MergeToWearableResults ( page , wearables , totalAmount , FetchPublishedWearableCollections , cancellationToken ) ;
304
+
305
+ private async UniTask < int > MergeToWearableResults ( int page , List < WearableItem > wearables , int totalAmount ,
306
+ Func < List < WearableItem > , CancellationToken , UniTask > fetchOperation ,
307
+ CancellationToken cancellationToken )
308
+ {
309
+ int startingPage = ( totalAmount / PAGE_SIZE ) + 1 ;
310
+ int pageOffset = page - startingPage ;
311
+ int pageSize = PAGE_SIZE - wearables . Count ;
312
+ int skip = pageOffset * PAGE_SIZE ;
313
+ int until = skip + pageSize ;
314
+
315
+ customWearablesBuffer . Clear ( ) ;
316
+
317
+ await fetchOperation . Invoke ( customWearablesBuffer , cancellationToken ) ;
318
+
319
+ if ( skip < 0 ) return customWearablesBuffer . Count ;
320
+
321
+ for ( int i = skip ; i < customWearablesBuffer . Count && i < until ; i ++ )
322
+ wearables . Add ( customWearablesBuffer [ i ] ) ;
323
+
324
+ return customWearablesBuffer . Count ;
325
+ }
326
+
327
+ private async UniTask < int > MergeBuilderWearableCollections ( int page , List < WearableItem > wearables , int totalAmount , CancellationToken cancellationToken )
328
+ {
329
+ int startingPage = ( totalAmount / PAGE_SIZE ) + 1 ;
330
+ int pageOffset = Mathf . Max ( 1 , page - startingPage ) ;
331
+ int pageSize = PAGE_SIZE - wearables . Count ;
332
+
333
+ customWearablesBuffer . Clear ( ) ;
334
+
335
+ int collectionsWearableCount = await FetchBuilderWearableCollections ( pageOffset , PAGE_SIZE ,
336
+ customWearablesBuffer , cancellationToken ) ;
337
+
338
+ for ( var i = 0 ; i < pageSize && i < customWearablesBuffer . Count ; i ++ )
339
+ wearables . Add ( customWearablesBuffer [ i ] ) ;
340
+
341
+ return collectionsWearableCount ;
342
+ }
343
+
316
344
private async UniTask FetchCustomWearableItems ( ICollection < WearableItem > wearables , CancellationToken cancellationToken )
317
345
{
318
346
IReadOnlyList < string > customItems = await customNftCollectionService . GetConfiguredCustomNftItemsAsync ( cancellationToken ) ;
@@ -337,30 +365,46 @@ private async UniTask FetchCustomWearableItems(ICollection<WearableItem> wearabl
337
365
}
338
366
}
339
367
340
- private async UniTask FetchCustomWearableCollections (
368
+ private async UniTask FetchPublishedWearableCollections (
341
369
List < WearableItem > wearableBuffer , CancellationToken cancellationToken )
342
370
{
343
371
IReadOnlyList < string > customCollections =
344
372
await customNftCollectionService . GetConfiguredCustomNftCollectionAsync ( cancellationToken ) ;
345
373
346
- Debug . Log ( $ "FetchCustomWearableCollections: { customCollections } ") ;
347
-
348
- HashSet < string > publishedCollections = HashSetPool < string > . Get ( ) ;
349
- HashSet < string > collectionsInBuilder = HashSetPool < string > . Get ( ) ;
374
+ HashSet < string > collectionsToRequest = HashSetPool < string > . Get ( ) ;
350
375
351
376
foreach ( string collectionId in customCollections )
352
- {
353
377
if ( collectionId . StartsWith ( "urn" , StringComparison . OrdinalIgnoreCase ) )
354
- publishedCollections . Add ( collectionId ) ;
355
- else
356
- collectionsInBuilder . Add ( collectionId ) ;
357
- }
378
+ collectionsToRequest . Add ( collectionId ) ;
358
379
359
- await UniTask . WhenAll ( wearablesCatalogService . RequestWearableCollection ( publishedCollections , cancellationToken , wearableBuffer ) ,
360
- wearablesCatalogService . RequestWearableCollectionInBuilder ( collectionsInBuilder , cancellationToken , wearableBuffer ) ) ;
380
+ await wearablesCatalogService . RequestWearableCollection ( collectionsToRequest , cancellationToken , wearableBuffer ) ;
361
381
362
- HashSetPool < string > . Release ( publishedCollections ) ;
363
- HashSetPool < string > . Release ( collectionsInBuilder ) ;
382
+ HashSetPool < string > . Release ( collectionsToRequest ) ;
383
+ }
384
+
385
+ private async UniTask < int > FetchBuilderWearableCollections (
386
+ int pageNumber , int pageSize ,
387
+ List < WearableItem > wearableBuffer ,
388
+ CancellationToken cancellationToken )
389
+ {
390
+ IReadOnlyList < string > customCollections =
391
+ await customNftCollectionService . GetConfiguredCustomNftCollectionAsync ( cancellationToken ) ;
392
+
393
+ HashSet < string > collectionsToRequest = HashSetPool < string > . Get ( ) ;
394
+
395
+ foreach ( string collectionId in customCollections )
396
+ if ( ! collectionId . StartsWith ( "urn" , StringComparison . OrdinalIgnoreCase ) )
397
+ collectionsToRequest . Add ( collectionId ) ;
398
+
399
+ ( IReadOnlyList < WearableItem > _ , int totalAmount ) = await wearablesCatalogService . RequestWearableCollectionInBuilder (
400
+ collectionsToRequest , cancellationToken ,
401
+ collectionBuffer : wearableBuffer ,
402
+ nameFilter : nameFilter ,
403
+ pageNumber : pageNumber , pageSize : pageSize ) ;
404
+
405
+ HashSetPool < string > . Release ( collectionsToRequest ) ;
406
+
407
+ return totalAmount ;
364
408
}
365
409
366
410
private WearableGridItemModel ToWearableGridModel ( WearableItem wearable )
@@ -369,8 +413,7 @@ private WearableGridItemModel ToWearableGridModel(WearableItem wearable)
369
413
370
414
if ( string . IsNullOrEmpty ( wearable . rarity ) )
371
415
rarity = NftRarity . None ;
372
- else
373
- if ( ! Enum . TryParse ( wearable . rarity , true , out NftRarity result ) )
416
+ else if ( ! Enum . TryParse ( wearable . rarity , true , out NftRarity result ) )
374
417
{
375
418
rarity = NftRarity . None ;
376
419
Debug . LogWarning ( $ "Could not parse the rarity \" { wearable . rarity } \" of the wearable '{ wearable . id } '. Fallback to common.") ;
0 commit comments