< Summary

Class:BIWNFTController
Assembly:BuilderInWorldNFT
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/NFTs/BIWNFTController.cs
Covered lines:45
Uncovered lines:9
Coverable lines:54
Total lines:135
Line coverage:83.3% (45 of 54)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWNFTController()0%110100%
Initialize()0%110100%
Dispose()0%220100%
ClearNFTs()0%110100%
IsNFTInUse(...)0%4.024088.89%
StopUsingNFT(...)0%6.046090%
GetNfts()0%2100%
UseNFT(...)0%7.057090%
FetchNftsFromOwner()0%220100%
NftsFeteched(...)0%220100%
FetchNfts()0%440100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/NFTs/BIWNFTController.cs

#LineLine coverage
 1using DCL.Configuration;
 2using DCL.Helpers.NFT;
 3using System;
 4using System.Collections;
 5using System.Collections.Generic;
 6using System.Security.Cryptography;
 7using System.Text;
 8using UnityEngine;
 9
 10public class BIWNFTController
 11{
 12    public event System.Action OnNFTUsageChange;
 13    public event System.Action<List<NFTInfo>> OnNftsFetched;
 14
 15    private NFTOwner nftOwner;
 16
 17    private Coroutine fechNftsCoroutine;
 18
 19    private static BIWNFTController instance;
 20
 121    private List<NFTInfo> nftsAlreadyInUse = new List<NFTInfo>();
 22
 23    private bool desactivateNFT = false;
 24
 25    public static BIWNFTController i
 26    {
 27        get
 28        {
 17029            if (instance == null)
 30            {
 131                instance = new BIWNFTController();
 32            }
 33
 17034            return instance;
 35        }
 36    }
 37
 38    public void Initialize()
 39    {
 2640        UserProfile userProfile = UserProfile.GetOwnUserProfile();
 47141        userProfile.OnUpdate += (x) => FetchNftsFromOwner();
 2642    }
 43
 44    public void Dispose()
 45    {
 2746        if (fechNftsCoroutine != null)
 2747            CoroutineStarter.Stop(fechNftsCoroutine);
 2748    }
 49
 4650    public void ClearNFTs() { nftsAlreadyInUse.Clear(); }
 51
 52    public bool IsNFTInUse(string id)
 53    {
 354        if (desactivateNFT)
 055            return false;
 56
 757        foreach (NFTInfo info in nftsAlreadyInUse)
 58        {
 159            if (info.assetContract.address == id)
 160                return true;
 61        }
 262        return false;
 163    }
 64
 65    public void StopUsingNFT(string id)
 66    {
 167        if (desactivateNFT)
 068            return;
 69
 470        foreach (NFTInfo info in nftOwner.assets)
 71        {
 172            if (info.assetContract.address != id)
 73                continue;
 174            if (!nftsAlreadyInUse.Contains(info))
 75                continue;
 76
 177            nftsAlreadyInUse.Remove(info);
 178            OnNFTUsageChange?.Invoke();
 79        }
 180    }
 81
 082    public List<NFTInfo> GetNfts() { return nftOwner.assets; }
 83
 84    public void UseNFT(string id)
 85    {
 386        if (desactivateNFT || nftOwner.assets == null)
 087            return;
 88
 1289        foreach (NFTInfo info in nftOwner.assets)
 90        {
 391            if (info.assetContract.address != id)
 92                continue;
 393            if (nftsAlreadyInUse.Contains(info))
 94                continue;
 95
 396            nftsAlreadyInUse.Add(info);
 397            OnNFTUsageChange?.Invoke();
 98
 99        }
 3100    }
 101
 102    private void FetchNftsFromOwner()
 103    {
 445104        if (fechNftsCoroutine != null)
 55105            CoroutineStarter.Stop(fechNftsCoroutine);
 445106        fechNftsCoroutine = CoroutineStarter.Start(FetchNfts());
 445107    }
 108
 109    public void NftsFeteched(NFTOwner nftOwner)
 110    {
 4111        this.nftOwner = nftOwner;
 4112        string json = JsonUtility.ToJson(nftOwner);
 4113        desactivateNFT = false;
 4114        OnNftsFetched?.Invoke(this.nftOwner.assets);
 4115    }
 116
 117    private IEnumerator FetchNfts()
 118    {
 445119        UserProfile userProfile = UserProfile.GetOwnUserProfile();
 120
 445121        string userId = userProfile.ethAddress;
 445122        if (!string.IsNullOrEmpty(userId))
 123        {
 55124            yield return NFTHelper.FetchNFTsFromOwner(userId, (nftOwner) =>
 125                {
 0126                    NftsFeteched(nftOwner);
 0127                },
 128                (error) =>
 129                {
 0130                    desactivateNFT = true;
 0131                    Debug.LogError($"error getting NFT from owner:  {error}");
 0132                });
 133        }
 390134    }
 135}