From d0b92738d68e45bede7a620572b0cad5b8876d67 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Thu, 1 Feb 2018 00:24:29 +0100 Subject: [PATCH] Fix chart generation for coin without history --- .../Activities/CurrencyDetailsActivity.java | 22 ++++ .../coinfolio/Activities/HomeActivity.java | 71 ++++++++---- .../DataManagers/CurrencyData/Currency.java | 34 +++++- .../CurrencyData/CurrencyDataRetriver.java | 8 +- .../LayoutManagers/HomeLayoutGenerator.java | 101 ++++++++++++++++-- .../res/layout/activity_currency_details.xml | 12 +-- 6 files changed, 203 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/com/nauk/coinfolio/Activities/CurrencyDetailsActivity.java b/app/src/main/java/com/nauk/coinfolio/Activities/CurrencyDetailsActivity.java index bbd97ae..dcdac32 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/CurrencyDetailsActivity.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/CurrencyDetailsActivity.java @@ -16,20 +16,27 @@ import android.widget.TextView; import android.widget.ViewFlipper; import com.daimajia.swipe.SwipeLayout; +import com.db.chart.model.LineSet; +import com.db.chart.view.LineChartView; import com.nauk.coinfolio.DataManagers.CurrencyData.Transaction; import com.nauk.coinfolio.DataManagers.DatabaseManager; import com.nauk.coinfolio.R; import org.w3c.dom.Text; +import java.util.Calendar; import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Locale; + +/**Create a Parcelable**/ public class CurrencyDetailsActivity extends AppCompatActivity { private ViewFlipper viewFlipper; private LinearLayout transactionLayout; + private LinearLayout chartLayout; private DatabaseManager databaseManager; private String symbol; @@ -66,13 +73,28 @@ public class CurrencyDetailsActivity extends AppCompatActivity { viewFlipper = findViewById(R.id.vfCurrencyDetails); transactionLayout = findViewById(R.id.listTransactions); + chartLayout = findViewById(R.id.chartLayout); drawTransactionList(); + //drawChart(); + BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); } + private void drawChart() + { + LineChartView chartView = new LineChartView(this); + LineSet lineSet = new LineSet(); + double valMin; + double valMax; + int counter = 0; + Calendar calendar = Calendar.getInstance(Locale.FRANCE); + String hour; + String minute; + } + private void drawTransactionList() { transactionLayout.removeAllViews(); diff --git a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java index b48c587..2a9e2e8 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java @@ -26,6 +26,7 @@ import android.widget.ProgressBar; import android.widget.TextView; import com.daimajia.swipe.SwipeLayout; +import com.db.chart.view.LineChartView; import com.nauk.coinfolio.DataManagers.BalanceManager; import com.nauk.coinfolio.DataManagers.CurrencyData.Currency; import com.nauk.coinfolio.DataManagers.DatabaseManager; @@ -63,6 +64,7 @@ public class HomeActivity extends AppCompatActivity { private boolean iconChecker; private PreferencesManager preferencesManager; private DatabaseManager databaseManager; + private long lastTimestamp; @Override protected void onCreate(Bundle savedInstanceState) { @@ -153,13 +155,20 @@ public class HomeActivity extends AppCompatActivity { databaseManager = new DatabaseManager(this); updateViewButtonIcon(); + + lastTimestamp = 0; } @Override protected void onResume() { super.onResume(); - updateAll(); + if(System.currentTimeMillis()/1000 - lastTimestamp > 60) + { + lastTimestamp = System.currentTimeMillis()/1000; + + updateAll(); + } } @Override @@ -216,21 +225,31 @@ public class HomeActivity extends AppCompatActivity { { currencyLayout.removeAllViews(); + //layoutGenerator.setCurrencyList(balanceManager.getTotalBalance()); + //layoutGenerator.resetCurrencyList(); + for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) { final Currency currency = balanceManager.getTotalBalance().get(i); - if(!currency.getSymbol().equals("USD") && (currency.getBalance() * currency.getValue()) > 0.001) + if(!currency.getSymbol().equals("USD") && ((currency.getBalance() * currency.getValue()) > 0.001 || currency.getDayPriceHistory() == null)) { if(currency.getIcon() != null) { Palette.Builder builder = Palette.from(currency.getIcon()); + currency.setChartColor(builder.generate().getDominantColor(0)); + + //layoutGenerator.addCurrencyToList(currency); + currencyLayout.addView(layoutGenerator.getInfoLayout(currency, builder.generate().getDominantColor(0))); + //currencyLayout.addView(layoutGenerator.getInfoLayout(i)); } else { + //currency.setChartColor(12369084); currencyLayout.addView(layoutGenerator.getInfoLayout(currency, 12369084)); + //currencyLayout.addView(layoutGenerator.getInfoLayout(i)); } } } @@ -296,6 +315,8 @@ public class HomeActivity extends AppCompatActivity { currencyLayout.removeAllViews(); + //layoutGenerator.setCurrencyList(balanceManager.getTotalBalance()); + for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) { if(!balanceManager.getTotalBalance().get(i).getSymbol().equals("USD") && (balanceManager.getTotalBalance().get(i).getBalance() * balanceManager.getTotalBalance().get(i).getValue()) > 0.001) @@ -303,7 +324,15 @@ public class HomeActivity extends AppCompatActivity { totalValue += balanceManager.getTotalBalance().get(i).getValue() * balanceManager.getTotalBalance().get(i).getBalance(); totalFluctuation += (balanceManager.getTotalBalance().get(i).getValue() * balanceManager.getTotalBalance().get(i).getBalance()) * (balanceManager.getTotalBalance().get(i).getDayFluctuationPercentage() / 100); balanceManager.getTotalBalance().get(i).setIcon(getBitmapFromURL(balanceManager.getIconUrl(balanceManager.getTotalBalance().get(i).getSymbol()))); - currencyLayout.addView(layoutGenerator.getInfoLayout(balanceManager.getTotalBalance().get(i),0)); + //currencyLayout.addView(layoutGenerator.getInfoLayout(i)); + currencyLayout.addView(layoutGenerator.getInfoLayout(balanceManager.getTotalBalance().get(i), 0)); + } + + if(!balanceManager.getTotalBalance().get(i).getSymbol().equals("USD") && balanceManager.getTotalBalance().get(i).getDayPriceHistory() == null) + { + balanceManager.getTotalBalance().get(i).setIcon(getBitmapFromURL(balanceManager.getIconUrl(balanceManager.getTotalBalance().get(i).getSymbol()))); + //currencyLayout.addView(layoutGenerator.getInfoLayout(i)); + currencyLayout.addView(layoutGenerator.getInfoLayout(balanceManager.getTotalBalance().get(i), 0)); } } @@ -328,33 +357,29 @@ public class HomeActivity extends AppCompatActivity { loadingDialog.dismiss(); } } - } - if(balanceManager.getTotalBalance().size() == 0) - { - refreshLayout.setRefreshing(false); - - currencyLayout.removeAllViews(); - - if(loadingDialog.isShowing()) + if(balanceManager.getTotalBalance().size() == 0) { - loadingDialog.dismiss(); - } + refreshLayout.setRefreshing(false); - runOnUiThread(new Runnable() { - @Override - public void run() { - toolbarLayout.setTitle("US$0.00"); + currencyLayout.removeAllViews(); - toolbarSubtitle.setText("US$0.00"); - - toolbarSubtitle.setTextColor(-1275068417); + if(loadingDialog.isShowing()) + { + loadingDialog.dismiss(); } - }); - //toolbarSubtitle.setText("US$0.00"); + runOnUiThread(new Runnable() { + @Override + public void run() { + toolbarLayout.setTitle("US$0.00"); - Log.d(this.getResources().getString(R.string.debug), "Colors : " + toolbarSubtitle.getTextColors()); + toolbarSubtitle.setText("US$0.00"); + + toolbarSubtitle.setTextColor(-1275068417); + } + }); + } } } diff --git a/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/Currency.java b/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/Currency.java index bb3401c..aef1f7d 100644 --- a/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/Currency.java +++ b/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/Currency.java @@ -4,6 +4,8 @@ import android.graphics.Bitmap; import java.util.List; +import static java.sql.Types.NULL; + /** * Created by Tiji on 25/12/2017. */ @@ -19,6 +21,7 @@ public class Currency { private List dayPriceHistory; private CurrencyDataRetriver dataRetriver; private Bitmap icon; + private int chartColor; public Currency(String symbol, double balance) { @@ -48,7 +51,14 @@ public class Currency { setDayPriceHistory(dataChart); updateDayFluctuation(); - setValue(dataChart.get(dataChart.size() - 1).getClose()); + if(dataChart != null) + { + setValue(dataChart.get(dataChart.size() - 1).getClose()); + } + else + { + value = NULL; + } callBack.onSuccess(Currency.this); } @@ -75,6 +85,21 @@ public class Currency { }); } + public void setChartColor(int chartColor) + { + this.chartColor = chartColor; + } + + public int getChartColor() + { + return chartColor; + } + + public CurrencyDataRetriver getDataRetriver() + { + return dataRetriver; + } + public List getDayPriceHistory() { return dayPriceHistory; @@ -142,9 +167,12 @@ public class Currency { private void updateDayFluctuation() { - dayFluctuation = dayPriceHistory.get(dayPriceHistory.size() - 1).getOpen() - dayPriceHistory.get(0).getOpen(); + if(dayPriceHistory != null) + { + dayFluctuation = dayPriceHistory.get(dayPriceHistory.size() - 1).getOpen() - dayPriceHistory.get(0).getOpen(); - dayFluctuationPercentage = (float) (dayFluctuation / dayPriceHistory.get(0).getOpen() * 100); + dayFluctuationPercentage = (float) (dayFluctuation / dayPriceHistory.get(0).getOpen() * 100); + } } public interface CurrencyCallBack { diff --git a/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataRetriver.java b/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataRetriver.java index 0893f78..7ae848f 100644 --- a/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataRetriver.java +++ b/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataRetriver.java @@ -109,8 +109,6 @@ public class CurrencyDataRetriver { { List dataChart = new ArrayList<>(); - Log.d("coinfolio", "Result : " + response); - if(response.length() > 200) { response = response.substring(response.indexOf("Data\":[{") + 7, response.lastIndexOf("}],\"TimeTo")); @@ -145,10 +143,12 @@ public class CurrencyDataRetriver { } else { - for(int i = 1; i <= 1440; i++) + /*for(int i = 1; i <= 1440; i++) { dataChart.add(new CurrencyDataChart(i, 1, 1, 1, 1)); - } + }*/ + + dataChart = null; } return dataChart; diff --git a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java index 48df12d..1cd4bff 100644 --- a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java +++ b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java @@ -8,8 +8,10 @@ import android.graphics.Bitmap; import android.graphics.Color; import android.support.design.widget.Snackbar; import android.support.v7.widget.CardView; +import android.util.Log; import android.view.Gravity; import android.view.View; +import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; @@ -24,12 +26,15 @@ import com.nauk.coinfolio.DataManagers.CurrencyData.Currency; import com.nauk.coinfolio.DataManagers.CurrencyData.CurrencyDataChart; import com.nauk.coinfolio.R; +import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.Locale; import static java.lang.Math.abs; import static java.lang.Math.floorDiv; +import static java.lang.Math.incrementExact; +import static java.sql.Types.NULL; /** * Created by Tiji on 05/01/2018. @@ -45,6 +50,7 @@ public class HomeLayoutGenerator { } public CardView getInfoLayout(final Currency currency, int chartColor) + //public CardView getInfoLayout(int index) { CardView mainCard = new CardView(context); LinearLayout mainLinear = new LinearLayout(context); @@ -57,6 +63,8 @@ public class HomeLayoutGenerator { StateListAnimator stateListAnimator = AnimatorInflater.loadStateListAnimator(context, R.drawable.cardview_animator); mainCard.setStateListAnimator(stateListAnimator); + //mainCard.setTag("full." + index); + /*int[] attrs = new int[] { R.attr.selectableItemBackground }; TypedArray ta = context.obtainStyledAttributes(attrs); Drawable drawable = ta.getDrawable(0); @@ -67,12 +75,14 @@ public class HomeLayoutGenerator { mainCard.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + //switchingView(view); view.animate(); Intent intent = new Intent(context.getApplicationContext(), CurrencyDetailsActivity.class); intent.putExtra("symbol", currency.getSymbol()); context.getApplicationContext().startActivity(intent); } }); + mainCard.setClickable(true); CardView.LayoutParams paramsCard = new CardView.LayoutParams(CardView.LayoutParams.MATCH_PARENT, CardView.LayoutParams.WRAP_CONTENT); @@ -106,27 +116,97 @@ public class HomeLayoutGenerator { mainLinear.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); mainLinear.setOrientation(LinearLayout.VERTICAL); + mainLinear.setTag("mainLinear"); mainCard.setLayoutParams(paramsCard); mainCard.setRadius(8); + //infoLayout.addView(topLayoutGenerator(currencyList.get(index).getName(), currencyList.get(index).getSymbol(), currencyList.get(index).getValue(), currencyList.get(index).getIcon())); + //infoLayout.addView(bottomLayoutGenerator(currencyList.get(index).getSymbol(), currencyList.get(index).getBalance(), currencyList.get(index).getValue() * currencyList.get(index).getBalance(), currencyList.get(index).getDayFluctuationPercentage(), currencyList.get(index).getDayFluctuation())); + infoLayout.addView(topLayoutGenerator(currency.getName(), currency.getSymbol(), currency.getValue(), currency.getIcon())); infoLayout.addView(bottomLayoutGenerator(currency.getSymbol(), currency.getBalance(), currency.getValue() * currency.getBalance(), currency.getDayFluctuationPercentage(), currency.getDayFluctuation())); mainLinear.addView(infoLayout); - LineChartView lineChartView = chartGenerator(currency.getDayPriceHistory(), chartColor); - chartLayout.setTag("chart_layout"); - chartLayout.addView(lineChartView); - lineChartView.show(); mainLinear.addView(separatorLayout); - mainLinear.addView(chartLayout); + + mainLinear.addView(generateChart(currency, chartLayout, chartColor)); + + mainLinear.setClickable(false); mainCard.addView(mainLinear); return mainCard; } + /*public void resetCurrencyList() + { + currencyList = new ArrayList<>(); + } + + public void addCurrencyToList(Currency currency) + { + currencyList.add(currency); + } + + public void setCurrencyList(List currencyList) + { + this.currencyList = currencyList; + } + + private void switchingView(View view) + { + String[] tag = view.getTag().toString().split("\\."); + + Log.d("coinfolio", "Design : " + currencyList.get(Integer.parseInt(tag[1])).getSymbol()); + + if(tag[0].equals("full")) + { + LinearLayout mLayout = view.findViewWithTag("mainLinear"); + mLayout.removeViewAt(mLayout.getChildCount()-1); + mLayout.addView(generateChart(currencyList.get(Integer.parseInt(tag[1])), chartLayout, currencyList.get(Integer.parseInt(tag[1])).getChartColor())); + Log.d("coinfolio", "Data : " + currencyList.get(Integer.parseInt(tag[1])).getSymbol() + " " + currencyList.get(Integer.parseInt(tag[1])).getChartColor()); + view.setTag("half." + tag[1]); + mLayout.findViewWithTag("separator_layout").setVisibility(View.VISIBLE); + } + else + { + LinearLayout mLayout = view.findViewWithTag("mainLinear"); + mLayout.findViewWithTag("chart_layout").setVisibility(View.GONE); + mLayout.findViewWithTag("separator_layout").setVisibility(View.GONE); + view.setTag("full." + tag[1]); + } + }*/ + + private View generateChart(Currency currency, LinearLayout chartLayout, int chartColor) + { + View toReturn; + + if(currency.getDayPriceHistory() != null) + { + LineChartView lineChartView = chartGenerator(currency.getDayPriceHistory(), chartColor); + chartLayout.setTag("chart_layout"); + chartLayout.addView(lineChartView); + lineChartView.show(); + toReturn = chartLayout; + //mainLinear.addView(chartLayout); + } + else + { + TextView errorTextView = new TextView(context); + errorTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 500)); + errorTextView.setText("Error"); + errorTextView.setTag("chart_layout"); + errorTextView.setGravity(Gravity.CENTER); + + toReturn = errorTextView; + //mainLinear.addView(errorTextView); + } + + return toReturn; + } + private LinearLayout topLayoutGenerator(String name, String symbol, double value, Bitmap logo) { LinearLayout mainLayout = new LinearLayout(context); @@ -159,7 +239,15 @@ public class HomeLayoutGenerator { valueTextView.setTextSize(context.getResources().getDimension(R.dimen.mainText)); valueTextView.setTextColor(context.getResources().getColor(R.color.secondaryTextViewColor)); valueTextView.setGravity(Gravity.RIGHT); - valueTextView.setText("US$" + value); + + if(value != NULL) + { + valueTextView.setText("US$" + value); + } + else + { + valueTextView.setText("ERROR"); + } mainLayout.addView(currencyIcon); mainLayout.addView(nameTextView); @@ -212,6 +300,7 @@ public class HomeLayoutGenerator { fluctuationTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); fluctuationTextView.setTextSize(context.getResources().getDimension(R.dimen.secondaryText)); + if(fluctuation > 0) { fluctuationTextView.setTextColor(context.getResources().getColor(R.color.increase)); diff --git a/app/src/main/res/layout/activity_currency_details.xml b/app/src/main/res/layout/activity_currency_details.xml index 06422cb..0c01066 100644 --- a/app/src/main/res/layout/activity_currency_details.xml +++ b/app/src/main/res/layout/activity_currency_details.xml @@ -18,16 +18,10 @@ android:layout_height="match_parent"> - - - - + android:layout_height="wrap_content" + android:orientation="vertical"/>