Monday 2 August 2021

how to load coinmarketcap api data faster in Android Studio?

My code is working but every time people open the coin activity it takes about 5 seconds to load 500 coins (name, symbolName, price, vol and marketcap). I used a facebook shimmer to let people know that something is loading but 5 seconds is a lot tho because the app is loading more heavy stuff within 1 to 2 seconds, for example, wordpress data with images. But, these 500 coins in 5 seconds is not cool... Here is my code...

    public void getCoinList() {
    
    //posts = 500
    ApiInterface apiInterfaceCoin5 = 
    APIClientCoin.getClient().create(ApiInterface.class);
    Map<String, String> params = new HashMap<>();
    params.put("limit", posts+"");

     Call<CryptoList> call5 = apiInterfaceCoin5.doGetUserListAll(params);
  
    call5.enqueue(new Callback<CryptoList>() {
        @Override
        public void onResponse(Call<CryptoList> call, Response<CryptoList> response) 
    {

            shimmerFrameLayout.stopShimmer();
            shimmerFrameLayout.setVisibility(View.GONE);
            swipeRefreshLayout5.setRefreshing(false);

            int beforeCoinSize = cryptoList5.size();

            CryptoList list5 = response.body();


            cryptoList5.addAll(list5.getData());

            recyclerView4.setAdapter(adapterCoin5);

        }

        @Override
        public void onFailure(Call<CryptoList> call, Throwable t) {
            //Toast.makeText(CryptoListAllCoinActivity.this, "onFailure", 
          Toast.LENGTH_SHORT).show();
          //  Log.d("XXXX", t.getLocalizedMessage());
            call.cancel();
            progressBar3.setVisibility(View.GONE);
            shimmerFrameLayout.setVisibility(View.GONE);
            swipeRefreshLayout5.setRefreshing(false);
        }
      });
   }



@Headers("X-CMC_PRO_API_KEY: HIDDEN")
@GET("/v1/cryptocurrency/listings/latest")
Call<CryptoList> doGetUserListAll(@QueryMap Map<String, String> params);

Adapter:

    // Involves populating data into the item through holder
    // binds the data to the TextView in each row
    @Override
    public void onBindViewHolder(ViewHolder holder, int 
    position) {
    // Get the data model based on position
    Datum datum = mData.get(position);

    //load coin icon
    //if the link doesn't work then you have to upload it into 
    your own server
    Glide.with(context)
            .load(new 



     StringBuilder
     ("https://s2.coinmarketcap.com/static/img/coins/64x64/")

    .append(datum.getId())
    .append(".png").toString())
.placeholder(R.drawable.money_icon).into(holder.coin_icon);
    
    Glide.with(context)
            .load(new 


  StringBuilder
    ("https://s3.coinmarketcap.com/generated/
    sparklines/web/7d/usd/")
                    .append(datum.getId())
                    .append(".png").toString())
            .placeholder(R.drawable.line_24)
            .into(holder.sparkline);


    TextView symbolName = holder.symbolName;
    symbolName.setText(datum.getSymbol());

    // Set item views based on your views and data model
    TextView name = holder.name;
    name.setText(datum.getName());

    TextView price = holder.price;
    TextView priceDetails = holder.priceDetails;

    TextView marketCap = holder.marketCap;
    marketCap.setText("$" + 
   formatNumber(datum.getQuote().getUSD().getMarketCap()));


    ImageView coin_icon = holder.coin_icon;
    ImageView sparkline = holder.sparkline;


    if(datum.getQuote().getUSD().getPrice() >= 1) {
        price.setText("$" + 
 formatNumber(datum.getQuote().getUSD().getPrice()));
    }else{
        price.setText("$" + String.format("%f", 
  datum.getQuote().getUSD().getPrice()));
    }

    TextView textView24h = holder.textView24h;
    textView24h.setText(String.format("%.2f", 
   datum.getQuote().getUSD().getPercentChange24h()) + "%");


    if(datum.getQuote().getUSD().getPercentChange24h() <0.000).{
        //red
        textView24h.setTextColor(Color.parseColor("#EA3943"));
        arrowImage.setImageResource(R.drawable.arrow_down);
        sparkline.setColorFilter(Color.RED, 
   PorterDuff.Mode.MULTIPLY);
        sparkline.setImageResource(R.drawable.btc_spike);

        //changeImageColor(context, sparkline,000);

    }else{
        //green
        textView24h.setTextColor(Color.parseColor("#18C784"));
        arrowImage.setImageResource(R.drawable.arrow_up);
        sparkline.setColorFilter(Color.GREEN, 
 PorterDuff.Mode.MULTIPLY);
        sparkline.setImageResource(R.drawable.btc_spike);

    }


}

    Glide.with(context)
            .load(new 
     StringBuilder
    ("https://s2.coinmarketcap.com/static/img/coins/64x64/")
                    .append(datum.getId())
                    .append(".png").toString())
            .placeholder(R.drawable.money_icon)
            .into(holder.coin_icon);


from how to load coinmarketcap api data faster in Android Studio?

No comments:

Post a Comment