< Summary

Class:DCL.Wallet.WalletCardHUDController
Assembly:WalletHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WalletHUD/WalletCardHUDController.cs
Covered lines:46
Uncovered lines:11
Coverable lines:57
Total lines:136
Line coverage:80.7% (46 of 57)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:7
Method coverage:100% (7 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WalletCardHUDController(...)0%110100%
Dispose()0%110100%
OnCurrentEthereumManaBalanceChanged(...)0%110100%
OnCurrentPolygonManaBalanceChanged(...)0%110100%
OnWalletCardVisible(...)0%3.583060%
RequestManaAsync()0%23.1216069.7%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WalletHUD/WalletCardHUDController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Helpers;
 3using DCL.Tasks;
 4using System;
 5using System.Threading;
 6using UnityEngine;
 7
 8namespace 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
 1420        private UserProfile ownUserProfile => userProfileBridge.GetOwn();
 21
 622        public WalletCardHUDController(
 23            IWalletCardHUDComponentView view,
 24            IUserProfileBridge userProfileBridge,
 25            ITheGraph theGraph,
 26            DataStore dataStore)
 27        {
 628            this.view = view;
 629            this.userProfileBridge = userProfileBridge;
 630            this.theGraph = theGraph;
 631            this.dataStore = dataStore;
 32
 633            dataStore.wallet.currentEthereumManaBalance.OnChange += OnCurrentEthereumManaBalanceChanged;
 634            OnCurrentEthereumManaBalanceChanged(dataStore.wallet.currentEthereumManaBalance.Get(), 0f);
 635            dataStore.wallet.currentPolygonManaBalance.OnChange += OnCurrentPolygonManaBalanceChanged;
 636            OnCurrentPolygonManaBalanceChanged(dataStore.wallet.currentPolygonManaBalance.Get(), 0f);
 37
 638            dataStore.wallet.isWalletCardVisible.OnChange += OnWalletCardVisible;
 639        }
 40
 41        public void Dispose()
 42        {
 643            dataStore.wallet.currentEthereumManaBalance.OnChange -= OnCurrentEthereumManaBalanceChanged;
 644            dataStore.wallet.currentPolygonManaBalance.OnChange -= OnCurrentPolygonManaBalanceChanged;
 645            dataStore.wallet.isWalletCardVisible.OnChange -= OnWalletCardVisible;
 46
 647            fetchEthereumManaCancellationToken.SafeCancelAndDispose();
 648            fetchPolygonManaCancellationToken.SafeCancelAndDispose();
 649        }
 50
 51        private void OnCurrentEthereumManaBalanceChanged(double currentBalance, double _) =>
 952            view.SetEthereumManaBalance(currentBalance);
 53
 54        private void OnCurrentPolygonManaBalanceChanged(double currentBalance, double _) =>
 955            view.SetPolygonManaBalance(currentBalance);
 56
 57        private void OnWalletCardVisible(bool isVisible, bool _)
 58        {
 259            if (ownUserProfile.isGuest)
 060                return;
 61
 262            if (isVisible)
 63            {
 264                fetchEthereumManaCancellationToken = fetchEthereumManaCancellationToken.SafeRestart();
 265                fetchPolygonManaCancellationToken = fetchPolygonManaCancellationToken.SafeRestart();
 266                RequestManaAsync(TheGraphNetwork.Ethereum, fetchEthereumManaCancellationToken.Token).Forget();
 267                RequestManaAsync(TheGraphNetwork.Polygon, fetchPolygonManaCancellationToken.Token).Forget();
 68            }
 69            else
 70            {
 071                fetchEthereumManaCancellationToken.SafeCancelAndDispose();
 072                fetchPolygonManaCancellationToken.SafeCancelAndDispose();
 73            }
 074        }
 75
 76        private async UniTask RequestManaAsync(TheGraphNetwork network, CancellationToken cancellationToken)
 77        {
 78            while (true)
 79            {
 1280                await UniTask.WaitUntil(() =>
 481                        ownUserProfile != null &&
 82                        !string.IsNullOrEmpty(ownUserProfile.userId) &&
 83                        !dataStore.wallet.isWalletSectionVisible.Get(),
 84                    cancellationToken: cancellationToken);
 85
 486                double ethereumManaBalanceResult = dataStore.wallet.currentEthereumManaBalance.Get();
 487                double polygonManaBalanceResult = dataStore.wallet.currentPolygonManaBalance.Get();
 88
 89                try
 90                {
 491                    if (network == TheGraphNetwork.Ethereum)
 292                        view.SetEthereumManaLoadingActive(true);
 93                    else
 294                        view.SetPolygonManaLoadingActive(true);
 95
 496                    Promise<double> promise = theGraph.QueryMana(ownUserProfile.userId, network);
 97
 498                    if (promise != null)
 99                    {
 4100                        await promise;
 101
 4102                        if (network == TheGraphNetwork.Ethereum)
 2103                            ethereumManaBalanceResult = promise.value;
 104                        else
 2105                            polygonManaBalanceResult = promise.value;
 106                    }
 4107                }
 0108                catch (OperationCanceledException) { }
 0109                catch (Exception)
 110                {
 0111                    Debug.LogError(network == TheGraphNetwork.Ethereum ?
 112                        "Error requesting Ethereum MANA balance from TheGraph!" :
 113                        "Error requesting Polygon MANA balance from TheGraph!");
 0114                }
 115                finally
 116                {
 4117                    if (network == TheGraphNetwork.Ethereum)
 118                    {
 2119                        dataStore.wallet.currentEthereumManaBalance.Set(ethereumManaBalanceResult);
 2120                        view.SetEthereumManaLoadingActive(false);
 121                    }
 122                    else
 123                    {
 2124                        dataStore.wallet.currentPolygonManaBalance.Set(polygonManaBalanceResult);
 2125                        view.SetPolygonManaLoadingActive(false);
 126                    }
 127                }
 128
 12129                await UniTask.Delay(TimeSpan.FromSeconds(WalletUtils.FETCH_MANA_INTERVAL), cancellationToken: cancellati
 130
 0131                if (cancellationToken.IsCancellationRequested)
 0132                    break;
 133            }
 0134        }
 135    }
 136}