| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.IO; |
| | 5 | | using DCL; |
| | 6 | | using DCL.Helpers.NFT; |
| | 7 | | using Newtonsoft.Json; |
| | 8 | | using NFTShape_Internal; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityGLTF.Loader; |
| | 11 | |
|
| | 12 | | public interface INFTInfoFetcher |
| | 13 | | { |
| | 14 | | void FetchNFTImage(string address, string id, Action<NFTInfo> OnSuccess, Action OnFail); |
| | 15 | | void Dispose(); |
| | 16 | | } |
| | 17 | |
|
| | 18 | | public class NFTInfoFetcher : INFTInfoFetcher |
| | 19 | | { |
| | 20 | | internal Coroutine fetchCoroutine; |
| | 21 | |
|
| | 22 | | public void Dispose() |
| | 23 | | { |
| 18 | 24 | | if (fetchCoroutine != null) |
| 12 | 25 | | CoroutineStarter.Stop(fetchCoroutine); |
| | 26 | |
|
| 18 | 27 | | fetchCoroutine = null; |
| 18 | 28 | | } |
| | 29 | |
|
| | 30 | | public void FetchNFTImage(string address, string id, Action<NFTInfo> OnSuccess, Action OnFail) |
| | 31 | | { |
| 12 | 32 | | if (fetchCoroutine != null) |
| 0 | 33 | | CoroutineStarter.Stop(fetchCoroutine); |
| | 34 | |
|
| 12 | 35 | | fetchCoroutine = CoroutineStarter.Start(FetchNFTImageCoroutine(address, id, OnSuccess, OnFail)); |
| 12 | 36 | | } |
| | 37 | |
|
| | 38 | | private IEnumerator FetchNFTImageCoroutine(string address, string id, Action<NFTInfo> OnSuccess, Action OnFail) |
| | 39 | | { |
| 12 | 40 | | yield return NFTUtils.FetchNFTInfo(address, id, |
| | 41 | | (info) => |
| | 42 | | { |
| 7 | 43 | | OnSuccess?.Invoke(info); |
| 7 | 44 | | }, |
| | 45 | | (error) => |
| | 46 | | { |
| 0 | 47 | | Debug.LogError($"Couldn't fetch NFT: '{address}/{id}' {error}"); |
| 0 | 48 | | OnFail?.Invoke(); |
| 0 | 49 | | }); |
| 7 | 50 | | } |
| | 51 | | } |