<commit_before>package com.breadwallet.tools.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.breadwallet.R;
import com.breadwallet.presenter.customviews.BRText;
import com.breadwallet.tools.manager.BRSharedPrefs;
import com.breadwallet.tools.util.BRConnectivityStatus;
import com.breadwallet.tools.util.CurrencyUtils;
import com.breadwallet.wallet.WalletsMaster;
import com.breadwallet.wallet.abstracts.BaseWalletManager;
import com.breadwallet.wallet.wallets.bitcoin.WalletBitcoinManager;

import java.math.BigDecimal;
import java.util.ArrayList;

/**
 * Created by byfieldj on 1/31/18.
 */

public class WalletListAdapter extends RecyclerView.Adapter<WalletListAdapter.WalletItemViewHolder> {

    public static final String TAG = WalletListAdapter.class.getName();

    private final Context mContext;
    private ArrayList<BaseWalletManager> mWalletList;
    private String currentlySyncingWallet;
    private int currentlySyncingWalletPosition;


    public WalletListAdapter(Context context, ArrayList<BaseWalletManager> walletList) {
        this.mContext = context;
        this.mWalletList = walletList;
    }

    @Override
    public WalletItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        View convertView = inflater.inflate(R.layout.item_wallet, parent, false);

        return new WalletItemViewHolder(convertView);
    }

    public BaseWalletManager getItemAt(int pos) {
        return mWalletList.get(pos);
    }

    @Override
    public void onBindViewHolder(final WalletItemViewHolder holder, int position) {

        final BaseWalletManager wallet = mWalletList.get(position);
        String name = wallet.getName(mContext);
        String exchangeRate = CurrencyUtils.getFormattedCurrencyString(mContext, BRSharedPrefs.getPreferredFiatIso(mContext), new BigDecimal(wallet.getFiatExchangeRate(mContext)));
        String fiatBalance = CurrencyUtils.getFormattedCurrencyString(mContext, BRSharedPrefs.getPreferredFiatIso(mContext), new BigDecimal(wallet.getFiatBalance(mContext)));

        String cryptoBalance = CurrencyUtils.getFormattedCurrencyString(mContext, wallet.getIso(mContext), new BigDecimal(wallet.getCachedBalance(mContext)));
        final String iso = wallet.getIso(mContext);

        // Set wallet fields
        holder.mWalletName.setText(name);
        holder.mTradePrice.setText(exchangeRate);
        holder.mWalletBalanceUSD.setText(fiatBalance);
        holder.mWalletBalanceCurrency.setText(cryptoBalance);
        holder.mSyncingProgressBar.setVisibility(View.VISIBLE);
        holder.mSyncing.setText("Waiting to Sync");
        holder.mWalletBalanceCurrency.setVisibility(View.INVISIBLE);

        // TODO : Align the "waiting to sync" text with the balance USD

        if (wallet.getIso(mContext).equalsIgnoreCase(WalletBitcoinManager.getInstance(mContext).getIso(mContext))) {
            holder.mParent.setBackground(mContext.getResources().getDrawable(R.drawable.btc_card_shape, null));
        } else {
            holder.mParent.setBackground(mContext.getResources().getDrawable(R.drawable.bch_card_shape, null));

        }

        String lastUsedWalletIso = BRSharedPrefs.getCurrentWalletIso(mContext);

        BRConnectivityStatus connectivityChecker = new BRConnectivityStatus(mContext);

        int connectionStatus = connectivityChecker.isMobileDataOrWifiConnected();

        Log.d(TAG, "Current wallet ISO -> " + iso + ", last used wallet ISO -> " + lastUsedWalletIso);
        Log.d(TAG, "Connection status -> " + connectionStatus);


        // Mobile or Wifi is ON, sync the last used wallet
        if (iso.equals(lastUsedWalletIso)) {
            Log.d(TAG, "Current wallet is last used, Connection Status -> " + connectionStatus);
            if (connectionStatus == BRConnectivityStatus.MOBILE_ON || connectionStatus == BRConnectivityStatus.WIFI_ON || connectionStatus == BRConnectivityStatus.MOBILE_WIFI_ON) {

                syncWallet(wallet, holder);

            }
        }
        else{

        }


    }

    private void syncWallet(final BaseWalletManager wallet, final WalletItemViewHolder holder) {

        wallet.getPeerManager().connect();

        final Handler progressHandler = new Handler();
        Runnable progressRunnable = new Runnable() {
            @Override<commit_msg>Check wallet connection status before starting sync<commit_after>package com.breadwallet.tools.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.breadwallet.R;
import com.breadwallet.core.BRCorePeer;
import com.breadwallet.presenter.customviews.BRText;
import com.breadwallet.tools.manager.BRSharedPrefs;
import com.breadwallet.tools.threads.BRExecutor;
import com.breadwallet.tools.util.BRConnectivityStatus;
import com.breadwallet.tools.util.CurrencyUtils;
import com.breadwallet.wallet.WalletsMaster;
import com.breadwallet.wallet.abstracts.BaseWalletManager;
import com.breadwallet.wallet.wallets.bitcoin.WalletBitcoinManager;

import java.math.BigDecimal;
import java.util.ArrayList;

/**
 * Created by byfieldj on 1/31/18.
 */

public class WalletListAdapter extends RecyclerView.Adapter<WalletListAdapter.WalletItemViewHolder> {

    public static final String TAG = WalletListAdapter.class.getName();

    private final Context mContext;
    private ArrayList<BaseWalletManager> mWalletList;
    private String currentlySyncingWallet;
    private int currentlySyncingWalletPosition;


    public WalletListAdapter(Context context, ArrayList<BaseWalletManager> walletList) {
        this.mContext = context;
        this.mWalletList = walletList;
    }

    @Override
    public WalletItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        View convertView = inflater.inflate(R.layout.item_wallet, parent, false);

        return new WalletItemViewHolder(convertView);
    }

    public BaseWalletManager getItemAt(int pos) {
        return mWalletList.get(pos);
    }

    @Override
    public void onBindViewHolder(final WalletItemViewHolder holder, int position) {

        final BaseWalletManager wallet = mWalletList.get(position);
        String name = wallet.getName(mContext);
        String exchangeRate = CurrencyUtils.getFormattedCurrencyString(mContext, BRSharedPrefs.getPreferredFiatIso(mContext), new BigDecimal(wallet.getFiatExchangeRate(mContext)));
        String fiatBalance = CurrencyUtils.getFormattedCurrencyString(mContext, BRSharedPrefs.getPreferredFiatIso(mContext), new BigDecimal(wallet.getFiatBalance(mContext)));

        String cryptoBalance = CurrencyUtils.getFormattedCurrencyString(mContext, wallet.getIso(mContext), new BigDecimal(wallet.getCachedBalance(mContext)));
        final String iso = wallet.getIso(mContext);

        // Set wallet fields
        holder.mWalletName.setText(name);
        holder.mTradePrice.setText(exchangeRate);
        holder.mWalletBalanceUSD.setText(fiatBalance);
        holder.mWalletBalanceCurrency.setText(cryptoBalance);
        holder.mSyncingProgressBar.setVisibility(View.VISIBLE);
        holder.mSyncing.setText("Waiting to Sync");
        holder.mWalletBalanceCurrency.setVisibility(View.INVISIBLE);

        // TODO : Align the "waiting to sync" text with the balance USD

        if (wallet.getIso(mContext).equalsIgnoreCase(WalletBitcoinManager.getInstance(mContext).getIso(mContext))) {
            holder.mParent.setBackground(mContext.getResources().getDrawable(R.drawable.btc_card_shape, null));
        } else {
            holder.mParent.setBackground(mContext.getResources().getDrawable(R.drawable.bch_card_shape, null));

        }

        String lastUsedWalletIso = BRSharedPrefs.getCurrentWalletIso(mContext);

        BRConnectivityStatus connectivityChecker = new BRConnectivityStatus(mContext);

        int connectionStatus = connectivityChecker.isMobileDataOrWifiConnected();

        Log.d(TAG, "Current wallet ISO -> " + iso + ", last used wallet ISO -> " + lastUsedWalletIso);
        Log.d(TAG, "Connection status -> " + connectionStatus);


        // Mobile or Wifi is ON, sync the last used wallet
        if (iso.equals(lastUsedWalletIso)) {
            Log.d(TAG, "Current wallet is last used, Connection Status -> " + connectionStatus);
            if (connectionStatus == BRConnectivityStatus.MOBILE_ON || connectionStatus == BRConnectivityStatus.WIFI_ON || connectionStatus == BRConnectivityStatus.MOBILE_WIFI_ON) {

                syncWallet(wallet, holder);

            }
        }
        else{

        }


    }

    private void syncWallet(final BaseWalletManager wallet, final WalletItemViewHolder holder) {

        //wallet.getPeerManager().connect();
        BRExecutor.getInstance().forBackgroundTasks().execute(new Runnable() {
            @Override
            public void run() {
                if (wallet.getPeerManager().getConnectStatus() == BRCorePeer.ConnectStatus.Disconnected) {
                    wallet.getPeerManager().connect();
                    Log.e(TAG, "run: core connecting");
                }
            }
        });

        final Handler progressHandler = new Handler();
        Runnable progressRunnable = new Runnable() {
            @Override
{'fraction_non_alphanumeric': 0.09102875658161198, 'fraction_numerical': 0.001215066828675577, 'mean_word_length': 3.8487972508591066, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '1464683', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2750, 'n_words': 515}