| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Tasks; |
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.Wallet |
| | 9 | | { |
| | 10 | | public class WalletCardHUDController |
| | 11 | | { |
| | 12 | | private readonly IWalletCardHUDComponentView view; |
| | 13 | | private readonly IUserProfileBridge userProfileBridge; |
| | 14 | | private readonly ITheGraph theGraph; |
| | 15 | | private readonly DataStore dataStore; |
| | 16 | |
|
| | 17 | | private CancellationTokenSource fetchEthereumManaCancellationToken; |
| | 18 | | private CancellationTokenSource fetchPolygonManaCancellationToken; |
| | 19 | |
|
| 14 | 20 | | private UserProfile ownUserProfile => userProfileBridge.GetOwn(); |
| | 21 | |
|
| 6 | 22 | | public WalletCardHUDController( |
| | 23 | | IWalletCardHUDComponentView view, |
| | 24 | | IUserProfileBridge userProfileBridge, |
| | 25 | | ITheGraph theGraph, |
| | 26 | | DataStore dataStore) |
| | 27 | | { |
| 6 | 28 | | this.view = view; |
| 6 | 29 | | this.userProfileBridge = userProfileBridge; |
| 6 | 30 | | this.theGraph = theGraph; |
| 6 | 31 | | this.dataStore = dataStore; |
| | 32 | |
|
| 6 | 33 | | dataStore.wallet.currentEthereumManaBalance.OnChange += OnCurrentEthereumManaBalanceChanged; |
| 6 | 34 | | OnCurrentEthereumManaBalanceChanged(dataStore.wallet.currentEthereumManaBalance.Get(), 0f); |
| 6 | 35 | | dataStore.wallet.currentPolygonManaBalance.OnChange += OnCurrentPolygonManaBalanceChanged; |
| 6 | 36 | | OnCurrentPolygonManaBalanceChanged(dataStore.wallet.currentPolygonManaBalance.Get(), 0f); |
| | 37 | |
|
| 6 | 38 | | dataStore.wallet.isWalletCardVisible.OnChange += OnWalletCardVisible; |
| 6 | 39 | | } |
| | 40 | |
|
| | 41 | | public void Dispose() |
| | 42 | | { |
| 6 | 43 | | dataStore.wallet.currentEthereumManaBalance.OnChange -= OnCurrentEthereumManaBalanceChanged; |
| 6 | 44 | | dataStore.wallet.currentPolygonManaBalance.OnChange -= OnCurrentPolygonManaBalanceChanged; |
| 6 | 45 | | dataStore.wallet.isWalletCardVisible.OnChange -= OnWalletCardVisible; |
| | 46 | |
|
| 6 | 47 | | fetchEthereumManaCancellationToken.SafeCancelAndDispose(); |
| 6 | 48 | | fetchPolygonManaCancellationToken.SafeCancelAndDispose(); |
| 6 | 49 | | } |
| | 50 | |
|
| | 51 | | private void OnCurrentEthereumManaBalanceChanged(double currentBalance, double _) => |
| 9 | 52 | | view.SetEthereumManaBalance(currentBalance); |
| | 53 | |
|
| | 54 | | private void OnCurrentPolygonManaBalanceChanged(double currentBalance, double _) => |
| 9 | 55 | | view.SetPolygonManaBalance(currentBalance); |
| | 56 | |
|
| | 57 | | private void OnWalletCardVisible(bool isVisible, bool _) |
| | 58 | | { |
| 2 | 59 | | if (ownUserProfile.isGuest) |
| 0 | 60 | | return; |
| | 61 | |
|
| 2 | 62 | | if (isVisible) |
| | 63 | | { |
| 2 | 64 | | fetchEthereumManaCancellationToken = fetchEthereumManaCancellationToken.SafeRestart(); |
| 2 | 65 | | fetchPolygonManaCancellationToken = fetchPolygonManaCancellationToken.SafeRestart(); |
| 2 | 66 | | RequestManaAsync(TheGraphNetwork.Ethereum, fetchEthereumManaCancellationToken.Token).Forget(); |
| 2 | 67 | | RequestManaAsync(TheGraphNetwork.Polygon, fetchPolygonManaCancellationToken.Token).Forget(); |
| | 68 | | } |
| | 69 | | else |
| | 70 | | { |
| 0 | 71 | | fetchEthereumManaCancellationToken.SafeCancelAndDispose(); |
| 0 | 72 | | fetchPolygonManaCancellationToken.SafeCancelAndDispose(); |
| | 73 | | } |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | private async UniTask RequestManaAsync(TheGraphNetwork network, CancellationToken cancellationToken) |
| | 77 | | { |
| | 78 | | while (true) |
| | 79 | | { |
| 12 | 80 | | await UniTask.WaitUntil(() => |
| 4 | 81 | | ownUserProfile != null && |
| | 82 | | !string.IsNullOrEmpty(ownUserProfile.userId) && |
| | 83 | | !dataStore.wallet.isWalletSectionVisible.Get(), |
| | 84 | | cancellationToken: cancellationToken); |
| | 85 | |
|
| 4 | 86 | | double ethereumManaBalanceResult = dataStore.wallet.currentEthereumManaBalance.Get(); |
| 4 | 87 | | double polygonManaBalanceResult = dataStore.wallet.currentPolygonManaBalance.Get(); |
| | 88 | |
|
| | 89 | | try |
| | 90 | | { |
| 4 | 91 | | if (network == TheGraphNetwork.Ethereum) |
| 2 | 92 | | view.SetEthereumManaLoadingActive(true); |
| | 93 | | else |
| 2 | 94 | | view.SetPolygonManaLoadingActive(true); |
| | 95 | |
|
| 4 | 96 | | Promise<double> promise = theGraph.QueryMana(ownUserProfile.userId, network); |
| | 97 | |
|
| 4 | 98 | | if (promise != null) |
| | 99 | | { |
| 4 | 100 | | await promise; |
| | 101 | |
|
| 4 | 102 | | if (network == TheGraphNetwork.Ethereum) |
| 2 | 103 | | ethereumManaBalanceResult = promise.value; |
| | 104 | | else |
| 2 | 105 | | polygonManaBalanceResult = promise.value; |
| | 106 | | } |
| 4 | 107 | | } |
| 0 | 108 | | catch (OperationCanceledException) { } |
| 0 | 109 | | catch (Exception) |
| | 110 | | { |
| 0 | 111 | | Debug.LogError(network == TheGraphNetwork.Ethereum ? |
| | 112 | | "Error requesting Ethereum MANA balance from TheGraph!" : |
| | 113 | | "Error requesting Polygon MANA balance from TheGraph!"); |
| 0 | 114 | | } |
| | 115 | | finally |
| | 116 | | { |
| 4 | 117 | | if (network == TheGraphNetwork.Ethereum) |
| | 118 | | { |
| 2 | 119 | | dataStore.wallet.currentEthereumManaBalance.Set(ethereumManaBalanceResult); |
| 2 | 120 | | view.SetEthereumManaLoadingActive(false); |
| | 121 | | } |
| | 122 | | else |
| | 123 | | { |
| 2 | 124 | | dataStore.wallet.currentPolygonManaBalance.Set(polygonManaBalanceResult); |
| 2 | 125 | | view.SetPolygonManaLoadingActive(false); |
| | 126 | | } |
| | 127 | | } |
| | 128 | |
|
| 12 | 129 | | await UniTask.Delay(TimeSpan.FromSeconds(WalletUtils.FETCH_MANA_INTERVAL), cancellationToken: cancellati |
| | 130 | |
|
| 0 | 131 | | if (cancellationToken.IsCancellationRequested) |
| 0 | 132 | | break; |
| | 133 | | } |
| 0 | 134 | | } |
| | 135 | | } |
| | 136 | | } |