From 3af7cf8e9ca43d0334004a3ea32faf18157cc492 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Wed, 7 Feb 2018 23:55:38 +0100 Subject: [PATCH] Add per currency chart display | Click on card to collapse/extend view | Click on chart to open details --- .idea/misc.xml | 2 +- .../coinfolio/Activities/HomeActivity.java | 41 ++--- .../LayoutManagers/HomeLayoutGenerator.java | 156 +++++++++++++++++- .../ic_keyboard_arrow_right_grey_48dp.xml | 9 + .../res/layout/activity_currency_summary.xml | 3 +- app/src/main/res/layout/cardview_currency.xml | 48 ++++-- app/src/main/res/values/dimens.xml | 1 + 7 files changed, 218 insertions(+), 42 deletions(-) create mode 100644 app/src/main/res/drawable/ic_keyboard_arrow_right_grey_48dp.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 75dac50..3963879 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -24,7 +24,7 @@ - + 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 aa4ee96..23a0c2b 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java @@ -15,6 +15,7 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.graphics.Palette; import android.support.v7.widget.CardView; import android.support.v7.widget.Toolbar; +import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.Menu; @@ -237,30 +238,18 @@ public class HomeActivity extends AppCompatActivity { private void adaptView() { - if(isDetailed) + + currencyLayout.removeAllViews(); + + for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) { - currencyLayout.removeAllViews(); + final Currency currency = balanceManager.getTotalBalance().get(i); - for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) + if(!currency.getSymbol().equals("USD") && ((currency.getBalance() * currency.getValue()) > 0.001 || currency.getHistoryMinutes() == null)) { - final Currency currency = balanceManager.getTotalBalance().get(i); - - if(!currency.getSymbol().equals("USD") && ((currency.getBalance() * currency.getValue()) > 0.001 || currency.getHistoryMinutes() == null)) - { - //currencyLayout.addView(layoutGenerator.getInfoLayout(currency)); - currencyLayout.addView(layoutGenerator.getInfoLayout(currency, true)); - } + //currencyLayout.addView(layoutGenerator.getInfoLayout(currency)); + currencyLayout.addView(layoutGenerator.getInfoLayout(currency, isDetailed)); } - - //currencyLayout.addView(layoutGenerator.getInfoLayout(balanceManager.getTotalBalance().get(0), true)); - } - else - { - /*for(int i = 0; i < currencyLayout.getChildCount(); i++) - { - currencyLayout.getChildAt(i).findViewWithTag("chart_layout").setVisibility(View.GONE); - currencyLayout.getChildAt(i).findViewWithTag("separator_layout").setVisibility(View.GONE); - }*/ } updateViewButtonIcon(); @@ -301,6 +290,8 @@ public class HomeActivity extends AppCompatActivity { private void getBitmapFromURL(String src, IconCallBack callBack) { Bitmap result; + Log.d("coinfolio", "Downloading bitmap"); + try { java.net.URL url = new java.net.URL(src); HttpURLConnection connection = (HttpURLConnection) url @@ -321,12 +312,17 @@ public class HomeActivity extends AppCompatActivity { { iconCounter++; + Log.d("coinfolio", "Icon ++"); + if(balanceManager.getTotalBalance() != null) { if(iconCounter == balanceManager.getTotalBalance().size() - 1) { + Log.d("coinfolio", "1"); if(coinCounter == balanceManager.getTotalBalance().size() - 1 && detailsChecker) { + Log.d("coinfolio", "Loading heavy"); + UiHeavyLoadCalculator uiHeavyLoadCalculator = new UiHeavyLoadCalculator(); uiHeavyLoadCalculator.execute(); } @@ -389,6 +385,8 @@ public class HomeActivity extends AppCompatActivity { private void countCoins(boolean isCoin, boolean isDetails) { + Log.d("coinfolio", "Coin++ " + coinCounter + " " + balanceManager.getTotalBalance().size()); + if(isCoin) { coinCounter++; @@ -409,6 +407,8 @@ public class HomeActivity extends AppCompatActivity { if(balanceManager.getIconUrl(localCurrency.getSymbol()) != null) { + Log.d("coinfolio", "Downloading bitmap"); + getBitmapFromURL(balanceManager.getIconUrl(localCurrency.getSymbol()), new IconCallBack() { @Override public void onSuccess(Bitmap bitmapIcon) { @@ -622,6 +622,7 @@ public class HomeActivity extends AppCompatActivity { if(balanceManager.getTotalBalance().size() > 0) { + for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) { balance.get(i).updateHistoryMinutes(getApplicationContext(), new Currency.CurrencyCallBack() { 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 cb9aaae..43be3d1 100644 --- a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java +++ b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java @@ -6,7 +6,10 @@ import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Color; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffColorFilter; import android.support.v7.widget.CardView; +import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; @@ -14,6 +17,7 @@ import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import com.db.chart.model.ChartSet; import com.db.chart.model.LineSet; import com.db.chart.renderer.AxisRenderer; import com.db.chart.view.LineChartView; @@ -22,12 +26,16 @@ import com.nauk.coinfolio.DataManagers.CurrencyData.Currency; import com.nauk.coinfolio.DataManagers.CurrencyData.CurrencyDataChart; import com.nauk.coinfolio.R; +import org.w3c.dom.Text; + +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.floorMod; import static java.lang.Math.incrementExact; import static java.sql.Types.NULL; @@ -49,15 +57,21 @@ public class HomeLayoutGenerator { View view = LayoutInflater.from(context).inflate(R.layout.cardview_currency, null); - - view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - view.animate(); + /*view.animate(); Intent intent = new Intent(context.getApplicationContext(), CurrencyDetailsActivity.class); intent.putExtra("currency", currency); - context.getApplicationContext().startActivity(intent); + context.getApplicationContext().startActivity(intent);*/ + if(view.findViewById(R.id.LineChartView).getVisibility() == View.VISIBLE || view.findViewById(R.id.errorTextView).getVisibility() == View.VISIBLE) + { + collapseView(view); + } + else + { + extendView(currency, view); + } } }); @@ -78,12 +92,98 @@ public class HomeLayoutGenerator { .setText(context.getResources().getString(R.string.currencyPercentagePlaceholder, numberConformer(currency.getDayFluctuationPercentage()))); ((TextView) view.findViewById(R.id.currencyFluctuationTextView)) .setText(context.getResources().getString(R.string.currencyDollarParenthesisPlaceholder, numberConformer(currency.getDayFluctuation()))); + ((ImageView) view.findViewById(R.id.detailsArrow)) + .getDrawable().setColorFilter(new PorterDuffColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_IN)); + + List borders = getAxisBorders(currency); + LineChartView chartView = (LineChartView) view.findViewById(R.id.LineChartView); + + chartView.setAxisBorderValues(borders.get(0).floatValue(), borders.get(1).floatValue()) + .setYLabels(AxisRenderer.LabelPosition.NONE) + .setYAxis(false) + .setXAxis(false) + .setVisibility(View.VISIBLE); + + chartView.addData(generateChartSet(currency)); + + chartView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(context.getApplicationContext(), CurrencyDetailsActivity.class); + intent.putExtra("currency", currency); + context.getApplicationContext().startActivity(intent); + } + }); + + if(isExtended) + { + extendView(currency, view); + } + else + { + collapseView(view); + } updateColor(view, currency); return view; } + private void collapseView(View view) + { + view.findViewById(R.id.separationLayout).setVisibility(View.GONE); + view.findViewById(R.id.frameLayoutChart).setVisibility(View.GONE); + view.findViewById(R.id.LineChartView).setVisibility(View.GONE); + view.findViewById(R.id.errorTextView).setVisibility(View.GONE); + view.findViewById(R.id.detailsArrow).setVisibility(View.GONE); + } + + private void extendView(Currency currency, View view) + { + view.findViewById(R.id.separationLayout).setVisibility(View.VISIBLE); + view.findViewById(R.id.detailsArrow).setVisibility(View.VISIBLE); + view.findViewById(R.id.frameLayoutChart).setVisibility(View.VISIBLE); + + if(currency.getHistoryMinutes() != null) + { + ((LineChartView) view.findViewById(R.id.LineChartView)).setVisibility(View.VISIBLE); + ((LineChartView) view.findViewById(R.id.LineChartView)).show(); + view.findViewById(R.id.errorTextView).setVisibility(View.GONE); + } + else + { + view.findViewById(R.id.LineChartView).setVisibility(View.GONE); + + view.findViewById(R.id.errorTextView).setVisibility(View.VISIBLE); + } + + } + + private List getAxisBorders(Currency currency) + { + List borders = new ArrayList<>(); + + List dataChartList = currency.getHistoryMinutes(); + + borders.add(0, currency.getHistoryMinutes().get(0).getOpen()); + borders.add(1, currency.getHistoryMinutes().get(0).getOpen()); + + for(int i = 0; i < dataChartList.size(); i++) + { + if(borders.get(0) > dataChartList.get(i).getOpen()) + { + borders.set(0, dataChartList.get(i).getOpen()); + } + + if(borders.get(1) < dataChartList.get(i).getOpen()) + { + borders.set(1, dataChartList.get(i).getOpen()); + } + } + + return borders; + } + private void updateColor(View view, Currency currency) { if(currency.getDayFluctuationPercentage() > 0) @@ -102,6 +202,54 @@ public class HomeLayoutGenerator { } } + private ChartSet generateChartSet(Currency currency) + { + List dataChartList = currency.getHistoryMinutes(); + LineSet lineSet = new LineSet(); + int counter = 0; + Calendar calendar = Calendar.getInstance(Locale.FRANCE); + String hour; + String minute; + + for(int i = 0; i < dataChartList.size(); i+=10) + { + if(counter == 30) + { + calendar.setTimeInMillis(dataChartList.get(i).getTimestamp()*1000); + + hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)); + minute = String.valueOf(calendar.get(Calendar.MINUTE)); + + if(hour.length() < 2) + { + hour = "0" + hour; + } + + if(minute.length() < 2) + { + minute = "0" + minute; + } + + lineSet.addPoint(hour + ":" + minute, (float) dataChartList.get(i).getOpen()); + counter = 0; + } + else + { + counter++; + lineSet.addPoint("", (float) dataChartList.get(i).getOpen()); + } + + + } + + lineSet.setSmooth(true); + lineSet.setThickness(4); + lineSet.setFill(getColorWithAplha(currency.getChartColor(), 0.5f)); + lineSet.setColor(currency.getChartColor()); + + return lineSet; + } + public CardView getInfoLayout(final Currency currency) //public CardView getInfoLayout(int index) { diff --git a/app/src/main/res/drawable/ic_keyboard_arrow_right_grey_48dp.xml b/app/src/main/res/drawable/ic_keyboard_arrow_right_grey_48dp.xml new file mode 100644 index 0000000..36d3ce0 --- /dev/null +++ b/app/src/main/res/drawable/ic_keyboard_arrow_right_grey_48dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/activity_currency_summary.xml b/app/src/main/res/layout/activity_currency_summary.xml index d961574..764889a 100644 --- a/app/src/main/res/layout/activity_currency_summary.xml +++ b/app/src/main/res/layout/activity_currency_summary.xml @@ -56,8 +56,7 @@ android:id="@+id/switch_button" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="@string/quick_button" - android:visibility="gone"/> + android:text="@string/quick_button"/> diff --git a/app/src/main/res/layout/cardview_currency.xml b/app/src/main/res/layout/cardview_currency.xml index 9f084d7..346a8ee 100644 --- a/app/src/main/res/layout/cardview_currency.xml +++ b/app/src/main/res/layout/cardview_currency.xml @@ -5,15 +5,14 @@ android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:clickable="true" - android:focusable="true"> + android:focusable="true" + android:paddingBottom="4dp"> @@ -21,7 +20,6 @@ android:id="@+id/mainLinear" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_margin="5dp" android:clickable="false" android:orientation="vertical" android:tag="mainLinear"> @@ -30,6 +28,7 @@ android:id="@+id/currencyInfoLayout" android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_margin="5dp" android:orientation="vertical"> - + android:layout_height="@dimen/cardViewChartSize" + android:clickable="true" + android:focusable="true" + android:visibility="gone"> - + + + + + + + diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 40d6529..a2c8ea1 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -1,6 +1,7 @@ 15sp 12sp + 150dp 180dp 16dp