< 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        {
 16429            if (instance == null)
 30            {
 131                instance = new BIWNFTController();
 32            }
 33
 16434            return instance;
 35        }
 36    }
 37
 38    public void Initialize()
 39    {
 2540        UserProfile userProfile = UserProfile.GetOwnUserProfile();
 52841        userProfile.OnUpdate += (x) => FetchNftsFromOwner();
 2542    }
 43
 44    public void Dispose()
 45    {
 2546        if (fechNftsCoroutine != null)
 2547            CoroutineStarter.Stop(fechNftsCoroutine);
 2548    }
 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        }
 62
 263        return false;
 164    }
 65
 66    public void StopUsingNFT(string id)
 67    {
 168        if (desactivateNFT)
 069            return;
 70
 471        foreach (NFTInfo info in nftOwner.assets)
 72        {
 173            if (info.assetContract.address != id)
 74                continue;
 175            if (!nftsAlreadyInUse.Contains(info))
 76                continue;
 77
 178            nftsAlreadyInUse.Remove(info);
 179            OnNFTUsageChange?.Invoke();
 80        }
 181    }
 82
 083    public List<NFTInfo> GetNfts() { return nftOwner.assets; }
 84
 85    public void UseNFT(string id)
 86    {
 387        if (desactivateNFT || nftOwner.assets == null)
 088            return;
 89
 1290        foreach (NFTInfo info in nftOwner.assets)
 91        {
 392            if (info.assetContract.address != id)
 93                continue;
 394            if (nftsAlreadyInUse.Contains(info))
 95                continue;
 96
 397            nftsAlreadyInUse.Add(info);
 398            OnNFTUsageChange?.Invoke();
 99        }
 3100    }
 101
 102    private void FetchNftsFromOwner()
 103    {
 503104        if (fechNftsCoroutine != null)
 53105            CoroutineStarter.Stop(fechNftsCoroutine);
 503106        fechNftsCoroutine = CoroutineStarter.Start(FetchNfts());
 503107    }
 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    {
 503119        UserProfile userProfile = UserProfile.GetOwnUserProfile();
 120
 503121        string userId = userProfile.ethAddress;
 503122        if (!string.IsNullOrEmpty(userId))
 123        {
 53124            yield return NFTUtils.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        }
 450134    }
 135}