| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Interface; |
| | 5 | | using JetBrains.Annotations; |
| | 6 | | using Newtonsoft.Json; |
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Threading; |
| | 11 | | using UnityEngine; |
| | 12 | |
|
| | 13 | | namespace DCLServices.CustomNftCollection |
| | 14 | | { |
| | 15 | | public class WebInterfaceCustomNftCatalogBridge : MonoBehaviour, ICustomNftCollectionService |
| | 16 | | { |
| | 17 | | private string[] items; |
| | 18 | | private string[] collections; |
| | 19 | |
|
| | 20 | | public static WebInterfaceCustomNftCatalogBridge GetOrCreate() |
| | 21 | | { |
| 425 | 22 | | var bridgeObj = SceneReferences.i?.bridgeGameObject; |
| | 23 | |
|
| 425 | 24 | | return SceneReferences.i?.bridgeGameObject == null |
| | 25 | | ? new GameObject("Bridge").AddComponent<WebInterfaceCustomNftCatalogBridge>() |
| | 26 | | : bridgeObj.GetOrCreateComponent<WebInterfaceCustomNftCatalogBridge>(); |
| | 27 | | } |
| | 28 | |
|
| 425 | 29 | | public void Dispose() { } |
| | 30 | |
|
| 425 | 31 | | public void Initialize() { } |
| | 32 | |
|
| | 33 | | public async UniTask<IReadOnlyList<string>> GetConfiguredCustomNftCollectionAsync(CancellationToken cancellation |
| | 34 | | { |
| 0 | 35 | | if (collections != null) |
| 0 | 36 | | return collections; |
| | 37 | |
|
| 0 | 38 | | WebInterface.GetWithCollectionsUrlParam(); |
| | 39 | |
|
| | 40 | | // Reworked into this approach. Using a UniTaskCompletionSource<IReadOnlyList<string>> emits an invalid stri |
| | 41 | | // on WebGL. Seems like a IL2CPP issue, not sure. This workaround is the only one i found to make it work on |
| 0 | 42 | | await UniTask.WaitUntil(() => collections != null, cancellationToken: cancellationToken) |
| | 43 | | .Timeout(TimeSpan.FromSeconds(30)); |
| | 44 | |
|
| 0 | 45 | | return collections; |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | public async UniTask<IReadOnlyList<string>> GetConfiguredCustomNftItemsAsync(CancellationToken cancellationToken |
| | 49 | | { |
| 0 | 50 | | if (items != null) |
| 0 | 51 | | return items; |
| | 52 | |
|
| 0 | 53 | | WebInterface.GetWithItemsUrlParam(); |
| | 54 | |
|
| | 55 | | // Reworked into this approach. Using a UniTaskCompletionSource<IReadOnlyList<string>> emits an invalid stri |
| | 56 | | // on WebGL. Seems like a IL2CPP issue, not sure. This workaround is the only one i found to make it work on |
| 0 | 57 | | await UniTask.WaitUntil(() => items != null, cancellationToken: cancellationToken) |
| | 58 | | .Timeout(TimeSpan.FromSeconds(30)); |
| | 59 | |
|
| 0 | 60 | | return items; |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | [PublicAPI("Kernel response for GetParametrizedCustomNftCollectionAsync")] |
| | 64 | | public void SetWithCollectionsParam(string json) |
| | 65 | | { |
| 0 | 66 | | CollectionIdsPayload payload = JsonConvert.DeserializeObject<CollectionIdsPayload>(json); |
| 0 | 67 | | string[] collectionIds = payload.collectionIds.Where(s => !string.IsNullOrEmpty(s)).ToArray(); |
| 0 | 68 | | collections = collectionIds; |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | [PublicAPI("Kernel response for GetConfiguredCustomNftItemsAsync")] |
| | 72 | | public void SetWithItemsParam(string json) |
| | 73 | | { |
| 0 | 74 | | ItemIdsPayload payload = JsonConvert.DeserializeObject<ItemIdsPayload>(json); |
| | 75 | |
|
| 0 | 76 | | string[] itemIds = payload.itemIds.Where(s => !string.IsNullOrEmpty(s)).ToArray(); |
| 0 | 77 | | items = itemIds; |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | [Serializable] |
| | 81 | | private struct CollectionIdsPayload |
| | 82 | | { |
| | 83 | | public string[] collectionIds; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | [Serializable] |
| | 87 | | private struct ItemIdsPayload |
| | 88 | | { |
| | 89 | | public string[] itemIds; |
| | 90 | | } |
| | 91 | | } |
| | 92 | | } |