99using System . Collections . Generic ;
1010using System . Linq ;
1111using System . Threading ;
12+ using System . Threading . Tasks ;
1213using UnityEngine ;
1314using UnityEngine . Pool ;
1415
@@ -266,29 +267,9 @@ private async UniTask<int> RequestWearablesAndShowThem(int page, CancellationTok
266267
267268 try
268269 {
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 ) ;
292273
293274 customWearablesBuffer . Clear ( ) ;
294275 }
@@ -299,7 +280,7 @@ await UniTask.WhenAll(FetchCustomWearableCollections(customWearablesBuffer, canc
299280 currentWearables = wearables . Select ( ToWearableGridModel )
300281 . ToDictionary ( item => ExtendedUrnParser . GetShortenedUrn ( item . WearableId ) , model => model ) ;
301282
302- view . SetWearablePages ( page , Mathf . CeilToInt ( ( float ) totalAmount / PAGE_SIZE ) ) ;
283+ view . SetWearablePages ( page , Mathf . CeilToInt ( ( float ) totalAmount / PAGE_SIZE ) ) ;
303284
304285 // TODO: mark the wearables to be disposed if no references left
305286 view . ClearWearables ( ) ;
@@ -313,6 +294,53 @@ await UniTask.WhenAll(FetchCustomWearableCollections(customWearablesBuffer, canc
313294 return 0 ;
314295 }
315296
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+
316344 private async UniTask FetchCustomWearableItems ( ICollection < WearableItem > wearables , CancellationToken cancellationToken )
317345 {
318346 IReadOnlyList < string > customItems = await customNftCollectionService . GetConfiguredCustomNftItemsAsync ( cancellationToken ) ;
@@ -337,30 +365,46 @@ private async UniTask FetchCustomWearableItems(ICollection<WearableItem> wearabl
337365 }
338366 }
339367
340- private async UniTask FetchCustomWearableCollections (
368+ private async UniTask FetchPublishedWearableCollections (
341369 List < WearableItem > wearableBuffer , CancellationToken cancellationToken )
342370 {
343371 IReadOnlyList < string > customCollections =
344372 await customNftCollectionService . GetConfiguredCustomNftCollectionAsync ( cancellationToken ) ;
345373
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 ( ) ;
350375
351376 foreach ( string collectionId in customCollections )
352- {
353377 if ( collectionId . StartsWith ( "urn" , StringComparison . OrdinalIgnoreCase ) )
354- publishedCollections . Add ( collectionId ) ;
355- else
356- collectionsInBuilder . Add ( collectionId ) ;
357- }
378+ collectionsToRequest . Add ( collectionId ) ;
358379
359- await UniTask . WhenAll ( wearablesCatalogService . RequestWearableCollection ( publishedCollections , cancellationToken , wearableBuffer ) ,
360- wearablesCatalogService . RequestWearableCollectionInBuilder ( collectionsInBuilder , cancellationToken , wearableBuffer ) ) ;
380+ await wearablesCatalogService . RequestWearableCollection ( collectionsToRequest , cancellationToken , wearableBuffer ) ;
361381
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 ;
364408 }
365409
366410 private WearableGridItemModel ToWearableGridModel ( WearableItem wearable )
@@ -369,8 +413,7 @@ private WearableGridItemModel ToWearableGridModel(WearableItem wearable)
369413
370414 if ( string . IsNullOrEmpty ( wearable . rarity ) )
371415 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 ) )
374417 {
375418 rarity = NftRarity . None ;
376419 Debug . LogWarning ( $ "Could not parse the rarity \" { wearable . rarity } \" of the wearable '{ wearable . id } '. Fallback to common.") ;
0 commit comments