Add coin icon preview for market capitalization details

This commit is contained in:
Tanguy Herbron 2018-05-26 16:12:07 +02:00
parent 4986091644
commit 9845edd8cb
6 changed files with 124 additions and 22 deletions

View File

@ -1,7 +1,10 @@
package com.nauk.moodl.Activities.HomeActivityFragments; package com.nauk.moodl.Activities.HomeActivityFragments;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color; import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.view.GravityCompat; import android.support.v4.view.GravityCompat;
@ -14,6 +17,7 @@ import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.Entry;
@ -23,7 +27,10 @@ import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter; import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.highlight.Highlight; import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener; import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.nauk.moodl.Activities.HomeActivity;
import com.nauk.moodl.DataManagers.BalanceManager;
import com.nauk.moodl.DataManagers.CurrencyData.Currency; import com.nauk.moodl.DataManagers.CurrencyData.Currency;
import com.nauk.moodl.DataManagers.CurrencyData.CurrencyDetailsList;
import com.nauk.moodl.DataManagers.MarketCapManager; import com.nauk.moodl.DataManagers.MarketCapManager;
import com.nauk.moodl.DataManagers.PreferencesManager; import com.nauk.moodl.DataManagers.PreferencesManager;
import com.nauk.moodl.LayoutManagers.CustomPieChart; import com.nauk.moodl.LayoutManagers.CustomPieChart;
@ -44,19 +51,22 @@ import static java.lang.Math.abs;
public class MarketCapitalization extends Fragment { public class MarketCapitalization extends Fragment {
private int marketCapCounter;
private PreferencesManager preferencesManager; private PreferencesManager preferencesManager;
private MarketCapManager marketCapManager; private MarketCapManager marketCapManager;
private HashMap<String, Integer> dominantCurrenciesColors; private HashMap<String, Integer> dominantCurrenciesColors;
private SwipeRefreshLayout refreshLayout; private SwipeRefreshLayout refreshLayout;
private long lastTimestamp; private long lastTimestamp;
private String defaultCurrency; private String defaultCurrency;
private CurrencyDetailsList currencyDetailsList;
private boolean isDetailsUpdated;
private boolean isTopCurrenciesUpdated;
private boolean isMarketpCapUpdated;
private int iconCounter;
private View view; private View view;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{ {
view = inflater.inflate(R.layout.fragment_marketcap_homeactivity, container, false); view = inflater.inflate(R.layout.fragment_marketcap_homeactivity, container, false);
@ -65,6 +75,15 @@ public class MarketCapitalization extends Fragment {
preferencesManager = new PreferencesManager(getContext()); preferencesManager = new PreferencesManager(getContext());
marketCapManager = new MarketCapManager(getContext()); marketCapManager = new MarketCapManager(getContext());
currencyDetailsList = new CurrencyDetailsList(getContext());
currencyDetailsList.update(new BalanceManager.IconCallBack() {
@Override
public void onSuccess() {
isDetailsUpdated = true;
countCompletedMarketCapRequest();
}
});
defaultCurrency = preferencesManager.getDefaultCurrency(); defaultCurrency = preferencesManager.getDefaultCurrency();
lastTimestamp = 0; lastTimestamp = 0;
@ -159,7 +178,10 @@ public class MarketCapitalization extends Fragment {
refreshLayout.setRefreshing(true); refreshLayout.setRefreshing(true);
} }
marketCapCounter = 0; iconCounter = 0;
isTopCurrenciesUpdated = false;
isMarketpCapUpdated = false;
lastTimestamp = System.currentTimeMillis() / 1000; lastTimestamp = System.currentTimeMillis() / 1000;
@ -167,6 +189,7 @@ public class MarketCapitalization extends Fragment {
@Override @Override
public void onSuccess() public void onSuccess()
{ {
isTopCurrenciesUpdated = true;
countCompletedMarketCapRequest(); countCompletedMarketCapRequest();
} }
}, preferencesManager.getDefaultCurrency()); }, preferencesManager.getDefaultCurrency());
@ -174,6 +197,7 @@ public class MarketCapitalization extends Fragment {
marketCapManager.updateMarketCap(new MarketCapManager.VolleyCallBack() { marketCapManager.updateMarketCap(new MarketCapManager.VolleyCallBack() {
@Override @Override
public void onSuccess() { public void onSuccess() {
isMarketpCapUpdated = true;
countCompletedMarketCapRequest(); countCompletedMarketCapRequest();
} }
}, preferencesManager.getDefaultCurrency()); }, preferencesManager.getDefaultCurrency());
@ -242,11 +266,19 @@ public class MarketCapitalization extends Fragment {
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
private void countCompletedMarketCapRequest() private void countCompletedMarketCapRequest()
{ {
marketCapCounter++; if(isTopCurrenciesUpdated && isMarketpCapUpdated && isDetailsUpdated)
if(marketCapCounter == 2)
{ {
updateIcons(); updateIcons();
//refreshDisplayedData();
}
}
private void countIcons()
{
iconCounter++;
if(iconCounter >= marketCapManager.getTopCurrencies().size())
{
refreshDisplayedData(); refreshDisplayedData();
} }
} }
@ -255,7 +287,29 @@ public class MarketCapitalization extends Fragment {
{ {
for(int i = 0; i < marketCapManager.getTopCurrencies().size(); i++) for(int i = 0; i < marketCapManager.getTopCurrencies().size(); i++)
{ {
Log.d("moodl", "> " + marketCapManager.getTopCurrencies().get(i).getSymbol()); final Currency localCurrency = marketCapManager.getTopCurrencies().get(i);
final int index = i;
String iconUrl = MoodlBox.getIconUrl(marketCapManager.getTopCurrencies().get(i).getSymbol(), 500, currencyDetailsList);
if(iconUrl != null)
{
MoodlBox.getBitmapFromURL(iconUrl, localCurrency.getSymbol(), getResources(), getContext(), new HomeActivity.IconCallBack() {
@Override
public void onSuccess(Bitmap bitmapIcon) {
marketCapManager.getTopCurrencies().get(index).setIcon(bitmapIcon);
countIcons();
}
});
}
else
{
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_moodl);
icon = Bitmap.createScaledBitmap(icon, 500, 500, false);
localCurrency.setIcon(icon);
countIcons();
}
} }
} }
@ -299,6 +353,8 @@ public class MarketCapitalization extends Fragment {
if(e.getData() != null) if(e.getData() != null)
{ {
view.findViewById(R.id.layoutMarketDetails).setVisibility(View.VISIBLE); view.findViewById(R.id.layoutMarketDetails).setVisibility(View.VISIBLE);
view.findViewById(R.id.currencyIcon).setVisibility(View.VISIBLE);
Currency currency = marketCapManager.getCurrencyFromSymbol((String) e.getData()); Currency currency = marketCapManager.getCurrencyFromSymbol((String) e.getData());
((TextView) view.findViewById(R.id.textViewMarketCap)) ((TextView) view.findViewById(R.id.textViewMarketCap))
@ -307,10 +363,16 @@ public class MarketCapitalization extends Fragment {
.setText(PlaceholderManager.getValueString(MoodlBox.numberConformer(currency.getVolume24h()), getContext())); .setText(PlaceholderManager.getValueString(MoodlBox.numberConformer(currency.getVolume24h()), getContext()));
((TextView) view.findViewById(R.id.textViewNameSymbol)) ((TextView) view.findViewById(R.id.textViewNameSymbol))
.setText(currency.getName() + " (" + currency.getSymbol() + ")"); .setText(currency.getName() + " (" + currency.getSymbol() + ")");
((ImageView) view.findViewById(R.id.currencyIcon))
.setImageBitmap(currency.getIcon());
pieChart.setDrawCenterText(false);
} }
else else
{ {
view.findViewById(R.id.currencyIcon).setVisibility(View.GONE);
view.findViewById(R.id.layoutMarketDetails).setVisibility(View.GONE); view.findViewById(R.id.layoutMarketDetails).setVisibility(View.GONE);
pieChart.setDrawCenterText(true);
} }
} }
}); });
@ -318,8 +380,9 @@ public class MarketCapitalization extends Fragment {
@Override @Override
public void onNothingSelected() { public void onNothingSelected() {
view.findViewById(R.id.currencyIcon).setVisibility(View.GONE);
view.findViewById(R.id.layoutMarketDetails).setVisibility(View.GONE); view.findViewById(R.id.layoutMarketDetails).setVisibility(View.GONE);
pieChart.setDrawCenterText(true);
} }
}); });

View File

@ -39,6 +39,8 @@ import com.nauk.moodl.MoodlBox;
import com.nauk.moodl.PlaceholderManager; import com.nauk.moodl.PlaceholderManager;
import com.nauk.moodl.R; import com.nauk.moodl.R;
import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
@ -615,7 +617,7 @@ public class Summary extends Fragment implements HideBalanceSwitch {
{ {
final Currency localCurrency = balanceManager.getTotalBalance().get(i); final Currency localCurrency = balanceManager.getTotalBalance().get(i);
String iconUrl = balanceManager.getIconUrl(localCurrency.getSymbol()); String iconUrl = MoodlBox.getIconUrl(localCurrency.getSymbol(), balanceManager.getCurrencyDetailList());
if(iconUrl != null) if(iconUrl != null)
{ {

View File

@ -255,6 +255,11 @@ public class Watchlist extends Fragment {
{ {
WatchlistUpdater watchlistUpdater = new WatchlistUpdater(); WatchlistUpdater watchlistUpdater = new WatchlistUpdater();
watchlistUpdater.execute(); watchlistUpdater.execute();
if(watchlistManager.getWatchlist().size() == 0)
{
generateCards();
}
} }
} }
@ -351,7 +356,8 @@ public class Watchlist extends Fragment {
currency.updatePrice(getActivity(), preferencesManager.getDefaultCurrency(), new Currency.CurrencyCallBack() { currency.updatePrice(getActivity(), preferencesManager.getDefaultCurrency(), new Currency.CurrencyCallBack() {
@Override @Override
public void onSuccess(final Currency sucessCurrency) { public void onSuccess(final Currency sucessCurrency) {
String iconUrl = getIconUrl(sucessCurrency.getSymbol());
String iconUrl = MoodlBox.getIconUrl(sucessCurrency.getSymbol(), currencyDetailsList);
if(iconUrl != null) if(iconUrl != null)
{ {

View File

@ -295,6 +295,11 @@ public class BalanceManager {
return url; return url;
} }
public CurrencyDetailsList getCurrencyDetailList()
{
return currencyDetailsList;
}
public String getCurrencyName(String symbol) public String getCurrencyName(String symbol)
{ {
String currencyName = null; String currencyName = null;

View File

@ -11,6 +11,7 @@ import android.view.animation.Animation;
import android.view.animation.Transformation; import android.view.animation.Transformation;
import com.nauk.moodl.Activities.HomeActivity; import com.nauk.moodl.Activities.HomeActivity;
import com.nauk.moodl.DataManagers.CurrencyData.CurrencyDetailsList;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -206,9 +207,10 @@ public class MoodlBox {
} }
} }
public static void getBitmapFromURL(String src, String symbol, Resources resources, Context context, HomeActivity.IconCallBack callBack) { public static void getBitmapFromURL(String src, String symbol, Resources resources, Context context, HomeActivity.IconCallBack callBack)
{
String filepath = context.getCacheDir() + "/" + symbol + ".png"; String size = src.substring(src.lastIndexOf("=") + 1, src.length());
String filepath = context.getCacheDir() + "/" + symbol + "x" + size + ".png";
Bitmap result; Bitmap result;
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
@ -231,23 +233,29 @@ public class MoodlBox {
result.compress(Bitmap.CompressFormat.PNG, 100, out); result.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (IOException e) { } catch (IOException e) {
Log.d("moodl", "Error while downloading " + symbol + " icon"); Log.d("moodl", "Error while downloading " + symbol + " icon > " + e.getMessage());
result = BitmapFactory.decodeResource(resources, result = BitmapFactory.decodeResource(resources,
R.mipmap.ic_launcher_moodl); R.mipmap.ic_launcher_moodl);
result = Bitmap.createScaledBitmap(result, 50, 50, false); result = Bitmap.createScaledBitmap(result, Integer.valueOf(size), Integer.valueOf(size), false);
} }
} }
callBack.onSuccess(result); callBack.onSuccess(result);
} }
public static String getIconUrl(String imageUrl) public static String getIconUrl(String symbol, CurrencyDetailsList currencyDetailsList)
{
return getIconUrl(symbol, 50, currencyDetailsList);
}
public static String getIconUrl(String symbol, int size, CurrencyDetailsList currencyDetailsList)
{ {
String url; String url;
try { try {
url = "https://www.cryptocompare.com" + imageUrl + "?width=50"; JSONObject jsonObject = new JSONObject(currencyDetailsList.getCoinInfosHashmap().get(symbol));
} catch (NullPointerException e) { url = "https://www.cryptocompare.com" + jsonObject.getString("ImageUrl") + "?width=" + size;
} catch (JSONException | NullPointerException e) {
url = null; url = null;
} }

View File

@ -100,10 +100,28 @@
android:textSize="18dp" android:textSize="18dp"
android:gravity="center_horizontal"/> android:gravity="center_horizontal"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="350dp">
<com.nauk.moodl.LayoutManagers.CustomPieChart <com.nauk.moodl.LayoutManagers.CustomPieChart
android:id="@+id/marketCapPieChart" android:id="@+id/marketCapPieChart"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="350dp"/> android:layout_height="match_parent"/>
<ImageView
android:id="@+id/currencyIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:layout_gravity="center"
android:visibility="gone"
android:maxHeight="110dp"
android:maxWidth="110dp"
android:adjustViewBounds="true"/>
</FrameLayout>
<LinearLayout <LinearLayout
android:id="@+id/layoutMarketDetails" android:id="@+id/layoutMarketDetails"