<commit_before>﻿using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reactive.Linq;
using System.Threading.Tasks;
using RapidBase.Client;
using NBitcoin;
using System.Collections.ObjectModel;
using RapidBase.Models;
using System.Threading;
using System.ComponentModel;
using PowerWallet.Controls;
using PowerWallet.Messages;
using System.Windows.Input;
using System.Net;
using NBitcoin.OpenAsset;

namespace PowerWallet.ViewModel
{
    public class CoinsViewModel : PWViewModelBase
    {
        RapidBaseClientFactory _Factory;
        public CoinsViewModel(RapidBaseClientFactory factory)
        {
            _Factory = factory;
            SearchedCoins = "akSjSW57xhGp86K6JFXXroACfRCw7SPv637";
            //15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe (me)
            //akSjSW57xhGp86K6JFXXroACfRCw7SPv637 (colored)
            Search.Execute(null);
        }

        ICommand _Search;
        public ICommand Search
        {
            get
            {
                if (_Search == null)
                    _Search = new AsyncCommand(async c =>
                    {
                        var client = _Factory.CreateClient();
                        BalanceModel balance = null;
                        try
                        {
                            balance = await client.GetBalance(BitcoinAddress.Create(SearchedCoins), true);
                        }
                        catch (FormatException)
                        {
                        }
                        if (balance == null)
                            try
                            {
                                balance = await client.GetBalance(new BitcoinColoredAddress(SearchedCoins), true);
                            }
                            catch (FormatException)
                            {
                            }
                        if (balance == null)
                            balance = await client.GetBalance(SearchedCoins, true);
                        if (balance == null)
                            throw new WebException("Error 404");
                        PopulateCoins(balance);
                    })
                    .Notify(MessengerInstance);
                return _Search;
            }
        }



        private string _SearchedCoins;
        public string SearchedCoins
        {
            get
            {
                return _SearchedCoins;
            }
            set
            {
                if (value != _SearchedCoins)
                {
                    _SearchedCoins = value;
                    OnPropertyChanged(() => this.SearchedCoins);
                }
            }
        }

        private readonly ObservableCollection<CoinViewModel> _Coins = new ObservableCollection<CoinViewModel>();
        public ObservableCollection<CoinViewModel> Coins
        {
            get
            {
                return _Coins;
            }
        }

        private void PopulateCoins(BalanceModel balance)
        {
            Coins.Clear();
            foreach (var op in balance.Operations)
            {
                foreach (var coin in op.ReceivedCoins)
                    Coins.Add(new CoinViewModel(coin, op));
            }
        }
    }

    public class CoinViewModel : PWViewModelBase
    {
        public CoinViewModel(ICoin coin, BalanceOperation op)
        {
            var colored = coin as ColoredCoin;
            BTCValue = coin.Amount.ToUnit(MoneyUnit.BTC).ToString() + " BTC";
            if (colored == null)
                Value = BTCValue;
            else
            {
                Value = colored.Asset.Quantity.ToString() + " Assets";
                Type = colored.Asset.Id.GetWif(App.Network).ToString();
            }
            Confirmations = op.Confirmations;
            Coin = coin;
            Op = op;<commit_msg>fix bug, incorrect BTC amount for colored coins<commit_after>﻿using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reactive.Linq;
using System.Threading.Tasks;
using RapidBase.Client;
using NBitcoin;
using System.Collections.ObjectModel;
using RapidBase.Models;
using System.Threading;
using System.ComponentModel;
using PowerWallet.Controls;
using PowerWallet.Messages;
using System.Windows.Input;
using System.Net;
using NBitcoin.OpenAsset;

namespace PowerWallet.ViewModel
{
    public class CoinsViewModel : PWViewModelBase
    {
        RapidBaseClientFactory _Factory;
        public CoinsViewModel(RapidBaseClientFactory factory)
        {
            _Factory = factory;
            SearchedCoins = "akSjSW57xhGp86K6JFXXroACfRCw7SPv637";
            //15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe (me)
            //akSjSW57xhGp86K6JFXXroACfRCw7SPv637 (colored)
            Search.Execute(null);
        }

        ICommand _Search;
        public ICommand Search
        {
            get
            {
                if (_Search == null)
                    _Search = new AsyncCommand(async c =>
                    {
                        var client = _Factory.CreateClient();
                        BalanceModel balance = null;
                        try
                        {
                            balance = await client.GetBalance(BitcoinAddress.Create(SearchedCoins), true);
                        }
                        catch (FormatException)
                        {
                        }
                        if (balance == null)
                            try
                            {
                                balance = await client.GetBalance(new BitcoinColoredAddress(SearchedCoins), true);
                            }
                            catch (FormatException)
                            {
                            }
                        if (balance == null)
                            balance = await client.GetBalance(SearchedCoins, true);
                        if (balance == null)
                            throw new WebException("Error 404");
                        PopulateCoins(balance);
                    })
                    .Notify(MessengerInstance);
                return _Search;
            }
        }



        private string _SearchedCoins;
        public string SearchedCoins
        {
            get
            {
                return _SearchedCoins;
            }
            set
            {
                if (value != _SearchedCoins)
                {
                    _SearchedCoins = value;
                    OnPropertyChanged(() => this.SearchedCoins);
                }
            }
        }

        private readonly ObservableCollection<CoinViewModel> _Coins = new ObservableCollection<CoinViewModel>();
        public ObservableCollection<CoinViewModel> Coins
        {
            get
            {
                return _Coins;
            }
        }

        private void PopulateCoins(BalanceModel balance)
        {
            Coins.Clear();
            foreach (var op in balance.Operations)
            {
                foreach (var coin in op.ReceivedCoins)
                    Coins.Add(new CoinViewModel(coin, op));
            }
        }
    }

    public class CoinViewModel : PWViewModelBase
    {
        public CoinViewModel(ICoin coin, BalanceOperation op)
        {
            var colored = coin as ColoredCoin;
            BTCValue = coin.TxOut.Value.ToUnit(MoneyUnit.BTC).ToString() + " BTC";
            if (colored == null)
                Value = BTCValue;
            else
            {
                Value = colored.Asset.Quantity.ToString() + " Assets";
                Type = colored.Asset.Id.GetWif(App.Network).ToString();
            }
            Confirmations = op.Confirmations;
            Coin = coin;
            Op = op;
{'fraction_non_alphanumeric': 0.06951668146644678, 'fraction_numerical': 0.006342762907522516, 'mean_word_length': 1.2378654555776327, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4138058', 'n_tokens_mistral': 2035, 'n_tokens_neox': 1904, 'n_words': 390}