| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Helpers.NFT; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public interface INFTInfoLoadHelper : IDisposable |
| | 7 | | { |
| | 8 | | public event Action<NFTInfo> OnFetchInfoSuccess; |
| | 9 | | public event Action OnFetchInfoFail; |
| | 10 | | void FetchNFTInfo(string address, string id); |
| | 11 | | } |
| | 12 | |
|
| | 13 | | public class NFTInfoLoadHelper : INFTInfoLoadHelper |
| | 14 | | { |
| | 15 | | public event Action<NFTInfo> OnFetchInfoSuccess; |
| | 16 | | public event Action OnFetchInfoFail; |
| | 17 | | internal Coroutine fetchCoroutine; |
| | 18 | |
|
| | 19 | | public void Dispose() |
| | 20 | | { |
| 3 | 21 | | if (fetchCoroutine != null) |
| 2 | 22 | | CoroutineStarter.Stop(fetchCoroutine); |
| | 23 | |
|
| 3 | 24 | | fetchCoroutine = null; |
| 3 | 25 | | } |
| | 26 | |
|
| | 27 | | public void FetchNFTInfo(string address, string id) |
| | 28 | | { |
| 5 | 29 | | if (fetchCoroutine != null) |
| 0 | 30 | | CoroutineStarter.Stop(fetchCoroutine); |
| | 31 | |
|
| 5 | 32 | | fetchCoroutine = CoroutineStarter.Start(FetchNFTInfoCoroutine(address, id)); |
| 5 | 33 | | } |
| | 34 | |
|
| | 35 | | private IEnumerator FetchNFTInfoCoroutine(string address, string id) |
| | 36 | | { |
| 5 | 37 | | yield return NFTUtils.FetchNFTInfo(address, id, |
| 0 | 38 | | (info) => { OnFetchInfoSuccess?.Invoke(info); }, |
| | 39 | | (error) => |
| | 40 | | { |
| 0 | 41 | | Debug.LogError($"Couldn't fetch NFT: '{address}/{id}' {error}"); |
| 0 | 42 | | OnFetchInfoFail?.Invoke(); |
| 0 | 43 | | }); |
| 3 | 44 | | } |
| | 45 | | } |