From bff5b583533b56dd044305e024b128d375a2cc8a Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Sun, 8 Apr 2018 02:25:03 +0200 Subject: [PATCH] Fix and improvement -Add hide balance feature for cardview -Fix color arrow changing color for every cardview --- .../coinfolio/Activities/HomeActivity.java | 18 +- .../CurrencyCardViewAdapter.java | 173 ++++++++++ .../LayoutManagers/CurrencyViewHolder.java | 50 +++ ...CardView.java => HomeLayoutGenerator.java} | 45 +-- .../SummaryCurrencyCardView.java | 304 ++++++++++++++++++ app/src/main/res/layout/cardview_currency.xml | 43 ++- .../res/layout/content_currency_summary.xml | 2 +- 7 files changed, 594 insertions(+), 41 deletions(-) create mode 100644 app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyCardViewAdapter.java create mode 100644 app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyViewHolder.java rename app/src/main/java/com/nauk/coinfolio/LayoutManagers/{CurrencyCardView.java => HomeLayoutGenerator.java} (87%) create mode 100644 app/src/main/java/com/nauk/coinfolio/LayoutManagers/SummaryCurrencyCardView.java 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 1e988ba..791d9de 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java @@ -6,20 +6,18 @@ import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; -import android.graphics.drawable.AnimationDrawable; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Looper; -import android.support.annotation.NonNull; import android.support.design.widget.AppBarLayout; -import android.support.design.widget.BottomNavigationView; import android.support.design.widget.CollapsingToolbarLayout; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.graphics.Palette; +import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.text.SpannableString; import android.util.Log; @@ -48,7 +46,8 @@ import com.nauk.coinfolio.DataManagers.BalanceManager; import com.nauk.coinfolio.DataManagers.CurrencyData.Currency; import com.nauk.coinfolio.DataManagers.MarketCapManager; import com.nauk.coinfolio.DataManagers.PreferencesManager; -import com.nauk.coinfolio.LayoutManagers.CurrencyCardView; +import com.nauk.coinfolio.LayoutManagers.HomeLayoutGenerator; +import com.nauk.coinfolio.LayoutManagers.SummaryCurrencyCardView; import com.nauk.coinfolio.R; import java.io.IOException; @@ -95,6 +94,7 @@ public class HomeActivity extends AppCompatActivity { private Handler handler; private Runnable updateRunnable; private ViewFlipper viewFlipper; + private HomeLayoutGenerator layoutGenerator; private HashMap dominantCurrenciesColors; @@ -149,6 +149,8 @@ public class HomeActivity extends AppCompatActivity { viewFlipper = findViewById(R.id.viewFlipperSummary); viewFlipper.setDisplayedChild(1); + layoutGenerator = new HomeLayoutGenerator(this); + ImageButton addCurrencyButton = findViewById(R.id.floatingAddButton); ImageButton detailsButton = findViewById(R.id.switch_button); ImageButton settingsButton = findViewById(R.id.settings_button); @@ -422,7 +424,9 @@ public class HomeActivity extends AppCompatActivity { if(!currency.getSymbol().equals("USD") && ((currency.getBalance() * currency.getValue()) > 0.001 || currency.getHistoryMinutes() == null)) { - currencyLayout.addView(new CurrencyCardView(this, currencyLayout).getInfoLayout(currency, isDetailed, totalValue, preferencesManager.isBalanceHidden())); + //currencyLayout.addView(new HomeLayoutGenerator(this, currencyLayout).getInfoLayout(currency, isDetailed, totalValue, preferencesManager.isBalanceHidden())); + //currencyLayout.addView(new SummaryCurrencyCardView(this, currency, isDetailed, totalValue, preferencesManager.isBalanceHidden())); + currencyLayout.addView(layoutGenerator.getInfoLayout(currency, isDetailed, totalValue, preferencesManager.isBalanceHidden())); } } @@ -829,7 +833,9 @@ public class HomeActivity extends AppCompatActivity { Currency currency = balanceManager.getTotalBalance().get(i); if(!currency.getSymbol().equals("USD") && (currency.getBalance() * currency.getValue()) > 0.001) { - currencyLayout.addView(new CurrencyCardView(getApplicationContext(), currencyLayout).getInfoLayout(currency, isDetailed, totalValue, preferencesManager.isBalanceHidden())); + //currencyLayout.addView(new HomeLayoutGenerator(getApplicationContext(), currencyLayout).getInfoLayout(currency, isDetailed, totalValue, preferencesManager.isBalanceHidden())); + //currencyLayout.addView(new SummaryCurrencyCardView(getApplicationContext(), currency, isDetailed, totalValue, preferencesManager.isBalanceHidden())); + currencyLayout.addView(layoutGenerator.getInfoLayout(currency, isDetailed, totalValue, preferencesManager.isBalanceHidden())); } } } diff --git a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyCardViewAdapter.java b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyCardViewAdapter.java new file mode 100644 index 0000000..1315313 --- /dev/null +++ b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyCardViewAdapter.java @@ -0,0 +1,173 @@ +package com.nauk.coinfolio.LayoutManagers; + +import android.content.Context; +import android.content.Intent; +import android.graphics.Color; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffColorFilter; +import android.support.annotation.NonNull; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ProgressBar; + +import com.github.mikephil.charting.charts.LineChart; +import com.github.mikephil.charting.data.Entry; +import com.github.mikephil.charting.data.LineData; +import com.github.mikephil.charting.data.LineDataSet; +import com.nauk.coinfolio.Activities.CurrencyDetailsActivity; +import com.nauk.coinfolio.Activities.HomeActivity; +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.List; +import java.util.Locale; + +import static java.lang.Math.abs; + +/** + * Created by Tiji on 07/04/2018. + */ + +public class CurrencyCardViewAdapter extends RecyclerView.Adapter { + + List currencies; + Context context; + boolean isBalanceHidden; + float totalValue; + + public CurrencyCardViewAdapter(Context context, List currencies, boolean isExtended, float totalValue, boolean isBalanceHidden) + { + this.context = context; + this.currencies = currencies; + this.totalValue = totalValue; + this.isBalanceHidden = isBalanceHidden; + } + + @NonNull + @Override + public CurrencyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_currency, parent, false); + return new CurrencyViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull CurrencyViewHolder holder, int position) { + holder.currencyIcon.setImageBitmap(currencies.get(position).getIcon()); + holder.currencyNameTextView.setText(currencies.get(position).getName()); + holder.currencySymbolTextView.setText(context.getResources().getString(R.string.currencySymbolPlaceholder, currencies.get(position).getSymbol())); + holder.currencyOwnedTextView.setText(context.getResources().getString(R.string.currencyBalancePlaceholder, numberConformer(currencies.get(position).getBalance()), currencies.get(position).getSymbol())); + holder.currencyValueOwnedTextView.setText(context.getResources().getString(R.string.currencyDollarParenthesisPlaceholder, numberConformer(currencies.get(position).getValue() * currencies.get(position).getBalance()))); + holder.currencyValueTextView.setText(context.getResources().getString(R.string.currencyDollarPlaceholder, numberConformer(currencies.get(position).getValue()))); + holder.currencyFluctuationPercentageTextView.setText(currencies.get(position).getName()); + holder.currencyNameTextView.setText(context.getResources().getString(R.string.currencyPercentagePlaceholder, numberConformer(currencies.get(position).getDayFluctuationPercentage()))); + holder.currencyFluctuationTextView.setText(context.getResources().getString(R.string.currencyDollarParenthesisPlaceholder, numberConformer(currencies.get(position).getDayFluctuation()))); + holder.detailsArrow.getDrawable().setColorFilter(new PorterDuffColorFilter(currencies.get(position).getChartColor(), PorterDuff.Mode.SRC_IN)); + + if(isBalanceHidden) + { + double value = currencies.get(position).getValue() * currencies.get(position).getBalance(); + double percentage = value / totalValue * 100; + + holder.dominancePercentageProgrressBar.setVisibility(View.VISIBLE); + holder.dominancePercentageProgrressBar.setProgress((int) Math.round(percentage)); + holder.dominancePercentageProgrressBar.getIndeterminateDrawable().setColorFilter(currencies.get(position).getChartColor(), PorterDuff.Mode.SRC_ATOP); + } + else + { + holder.dominancePercentageProgrressBar.setVisibility(View.GONE); + } + } + + @Override + public int getItemCount() { + return 0; + } + + private void setupLineChart(View view, final Currency currency) + { + LineChart lineChart = view.findViewById(R.id.LineChartView); + + lineChart.setDrawGridBackground(false); + lineChart.setDrawBorders(false); + lineChart.setDrawMarkers(false); + lineChart.setDoubleTapToZoomEnabled(false); + lineChart.setPinchZoom(false); + lineChart.setScaleEnabled(false); + lineChart.setDragEnabled(false); + lineChart.getDescription().setEnabled(false); + lineChart.getAxisLeft().setEnabled(false); + lineChart.getAxisRight().setEnabled(false); + lineChart.getLegend().setEnabled(false); + lineChart.getXAxis().setEnabled(false); + lineChart.setViewPortOffsets(0, 0, 0, 0); + lineChart.setData(generateData(currency)); + + /*lineChart.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(HomeActivity.class, CurrencyDetailsActivity.class); + intent.putExtra("currency", currency); + context.getApplicationContext().startActivity(intent); + } + });*/ + } + + private LineData generateData(Currency currency) + { + LineDataSet dataSet; + List dataChartList = currency.getHistoryMinutes(); + ArrayList values = new ArrayList<>(); + + for(int i = 0; i < dataChartList.size(); i+=10) + { + values.add(new Entry(i, (float) dataChartList.get(i).getOpen())); + } + + dataSet = new LineDataSet(values, "History"); + dataSet.setDrawIcons(false); + dataSet.setColor(currency.getChartColor()); + dataSet.setLineWidth(1); + dataSet.setDrawFilled(true); + dataSet.setFillColor(getColorWithAplha(currency.getChartColor(), 0.5f)); + dataSet.setFormLineWidth(1); + dataSet.setFormSize(15); + dataSet.setDrawCircles(false); + dataSet.setDrawValues(false); + dataSet.setHighlightEnabled(false); + + return new LineData(dataSet); + } + + private int getColorWithAplha(int color, float ratio) + { + int transColor; + int alpha = Math.round(Color.alpha(color) * ratio); + int r = Color.red(color); + int g = Color.green(color); + int b = Color.blue(color); + + transColor = Color.argb(alpha, r, g, b); + + return transColor ; + } + + private String numberConformer(double number) + { + String str; + + if(abs(number) > 1) + { + str = String.format( Locale.UK, "%.2f", number); + } + else + { + str = String.format( Locale.UK, "%.4f", number); + } + + return str; + } +} diff --git a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyViewHolder.java b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyViewHolder.java new file mode 100644 index 0000000..5d1d434 --- /dev/null +++ b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyViewHolder.java @@ -0,0 +1,50 @@ +package com.nauk.coinfolio.LayoutManagers; + +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.ProgressBar; +import android.widget.TextView; + +import com.github.mikephil.charting.charts.LineChart; +import com.nauk.coinfolio.R; + +/** + * Created by Tiji on 07/04/2018. + */ + +public class CurrencyViewHolder extends RecyclerView.ViewHolder { + + protected ProgressBar dominancePercentageProgrressBar; + protected ImageView currencyIcon; + protected TextView currencyNameTextView; + protected TextView currencySymbolTextView; + protected TextView currencyValueTextView; + protected TextView currencyOwnedTextView; + protected TextView currencyValueOwnedTextView; + protected TextView currencyFluctuationPercentageTextView; + protected TextView currencyFluctuationTextView; + protected LinearLayout collapsableLayout; + protected LineChart lineChart; + protected ImageView detailsArrow; + + public CurrencyViewHolder(View v) + { + super(v); + + dominancePercentageProgrressBar = v.findViewById(R.id.currencyPortfolioDominance); + currencyIcon = v.findViewById(R.id.currencyIcon); + currencyNameTextView = v.findViewById(R.id.currencyNameTextView); + currencySymbolTextView = v.findViewById(R.id.currencySymbolTextView); + currencyValueTextView = v.findViewById(R.id.currencyValueTextView); + currencyOwnedTextView = v.findViewById(R.id.currencyOwnedTextView); + currencyValueOwnedTextView = v.findViewById(R.id.currencyValueOwnedTextView); + currencyFluctuationPercentageTextView = v.findViewById(R.id.currencyFluctuationPercentageTextView); + currencyFluctuationTextView = v.findViewById(R.id.currencyFluctuationTextView); + collapsableLayout = v.findViewById(R.id.collapsableLayout); + lineChart = v.findViewById(R.id.LineChartView); + detailsArrow = v.findViewById(R.id.detailsArrow); + } + +} diff --git a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyCardView.java b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java similarity index 87% rename from app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyCardView.java rename to app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java index 45de5fc..2b9e0e7 100644 --- a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyCardView.java +++ b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java @@ -5,24 +5,16 @@ import android.content.Intent; import android.graphics.Color; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; -import android.os.Bundle; +import android.graphics.drawable.Drawable; import android.support.v7.widget.CardView; -import android.support.v7.widget.RecyclerView; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; -import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.Transformation; import android.widget.ImageView; -import android.widget.LinearLayout; import android.widget.ProgressBar; 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; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.LineData; @@ -32,9 +24,8 @@ import com.nauk.coinfolio.DataManagers.CurrencyData.Currency; import com.nauk.coinfolio.DataManagers.CurrencyData.CurrencyDataChart; import com.nauk.coinfolio.R; +import java.text.DecimalFormat; import java.util.ArrayList; -import java.util.Calendar; -import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -44,21 +35,18 @@ import static java.lang.Math.abs; * Created by Tiji on 05/01/2018. */ -public class CurrencyCardView extends CardView { +public class HomeLayoutGenerator { - android.content.Context context; - private LinearLayout rootLayout; + private android.content.Context context; - public CurrencyCardView(Context context, LinearLayout rootLayout) + public HomeLayoutGenerator(Context context) { - super(context); this.context = context; - this.rootLayout = rootLayout; } public View getInfoLayout(final Currency currency, boolean isExtended, float totalValue, boolean isBalanceHidden) { - View view = LayoutInflater.from(context).inflate(R.layout.cardview_currency, rootLayout, true); + View view = LayoutInflater.from(context).inflate(R.layout.cardview_currency, null, true); view.setOnClickListener(new View.OnClickListener() { @Override @@ -194,21 +182,36 @@ public class CurrencyCardView extends CardView { .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)); + + Drawable arrowDrawable = ((ImageView) view.findViewById(R.id.detailsArrow)).getDrawable(); + arrowDrawable.mutate(); + arrowDrawable.setColorFilter(new PorterDuffColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_IN)); + arrowDrawable.invalidateSelf(); + + Drawable progressBarDrawable = ((ProgressBar) view.findViewById(R.id.currencyPortfolioDominance)).getProgressDrawable(); + progressBarDrawable.mutate(); + progressBarDrawable.setColorFilter(new PorterDuffColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_IN)); + progressBarDrawable.invalidateSelf(); if(isBalanceHidden) { double value = currency.getValue() * currency.getBalance(); double percentage = value / totalValue * 100; + DecimalFormat df = new DecimalFormat(".##"); view.findViewById(R.id.currencyPortfolioDominance).setVisibility(View.VISIBLE); ((ProgressBar) view.findViewById(R.id.currencyPortfolioDominance)).setProgress((int) Math.round(percentage)); - ((ProgressBar) view.findViewById(R.id.currencyPortfolioDominance)).getIndeterminateDrawable().setColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_ATOP); + + view.findViewById(R.id.percentageOwnedTextView).setVisibility(View.VISIBLE); + ((TextView) view.findViewById(R.id.percentageOwnedTextView)).setText(context.getResources().getString(R.string.currencyPercentagePlaceholder, df.format(percentage))); + + view.findViewById(R.id.currencyOwnedInfoLayout).setVisibility(View.GONE); } else { view.findViewById(R.id.currencyPortfolioDominance).setVisibility(View.GONE); + view.findViewById(R.id.percentageOwnedTextView).setVisibility(View.GONE); + view.findViewById(R.id.currencyOwnedInfoLayout).setVisibility(View.VISIBLE); } } diff --git a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/SummaryCurrencyCardView.java b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/SummaryCurrencyCardView.java new file mode 100644 index 0000000..eced15e --- /dev/null +++ b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/SummaryCurrencyCardView.java @@ -0,0 +1,304 @@ +package com.nauk.coinfolio.LayoutManagers; + +import android.content.Context; +import android.content.Intent; +import android.graphics.Color; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffColorFilter; +import android.support.v7.widget.CardView; +import android.support.v7.widget.RecyclerView; +import android.util.AttributeSet; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.animation.Animation; +import android.view.animation.Transformation; +import android.widget.ImageView; +import android.widget.ProgressBar; +import android.widget.TextView; + +import com.github.mikephil.charting.charts.LineChart; +import com.github.mikephil.charting.data.Entry; +import com.github.mikephil.charting.data.LineData; +import com.github.mikephil.charting.data.LineDataSet; +import com.nauk.coinfolio.Activities.CurrencyDetailsActivity; +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.List; +import java.util.Locale; + +import static java.lang.Math.abs; + +/** + * Created by Tiji on 06/04/2018. + */ + +public class SummaryCurrencyCardView extends CardView implements View.OnClickListener { + + public SummaryCurrencyCardView(Context context) + { + this(context, null, false, 0, false); + } + + public SummaryCurrencyCardView(Context context, final Currency currency, boolean isExtended, float totalValue, boolean isBalanceHidden) + { + super(context, null, 0); + + LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + inflater.inflate(R.layout.cardview_currency, this, true); + + setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Log.d("coinfolio", "Clicked"); + if(view.findViewById(R.id.collapsableLayout).getVisibility() == View.VISIBLE) + { + collapseView(); + } + else + { + extendView(); + } + } + }); + + //updateCardViewInfos(currency, totalValue, isBalanceHidden); + updateCardViewInfos(currency, totalValue, true); + + setupLineChart(currency); + + if(isExtended) + { + extendView(); + } + else + { + collapseView(); + } + + updateColor(currency); + } + + @Override + public void onClick(View view) + { + Log.d("coinfolio", "Clicked"); + + if(view.findViewById(R.id.collapsableLayout).getVisibility() == View.VISIBLE) + { + collapseView(); + } + else + { + extendView(); + } + } + + public static void expand(final View v) { + v.measure(CardView.LayoutParams.MATCH_PARENT, CardView.LayoutParams.WRAP_CONTENT); + final int targetHeight = v.getMeasuredHeight(); + + // Older versions of android (pre API 21) cancel animations for views with a height of 0. + v.getLayoutParams().height = 1; + v.setVisibility(View.VISIBLE); + Animation a = new Animation() + { + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + v.getLayoutParams().height = interpolatedTime == 1 + ? CardView.LayoutParams.WRAP_CONTENT + : (int)(targetHeight * interpolatedTime); + v.requestLayout(); + } + + @Override + public boolean willChangeBounds() { + return true; + } + }; + + // 1dp/ms + a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density)); + v.startAnimation(a); + } + + public static void collapse(final View v) { + final int initialHeight = v.getMeasuredHeight(); + + Animation a = new Animation() + { + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + if(interpolatedTime == 1){ + v.setVisibility(View.GONE); + }else{ + v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime); + v.requestLayout(); + } + } + + @Override + public boolean willChangeBounds() { + return true; + } + }; + + // 1dp/ms + a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density)); + v.startAnimation(a); + } + + private void setupLineChart(final Currency currency) + { + LineChart lineChart = findViewById(R.id.LineChartView); + + lineChart.setDrawGridBackground(false); + lineChart.setDrawBorders(false); + lineChart.setDrawMarkers(false); + lineChart.setDoubleTapToZoomEnabled(false); + lineChart.setPinchZoom(false); + lineChart.setScaleEnabled(false); + lineChart.setDragEnabled(false); + lineChart.getDescription().setEnabled(false); + lineChart.getAxisLeft().setEnabled(false); + lineChart.getAxisRight().setEnabled(false); + lineChart.getLegend().setEnabled(false); + lineChart.getXAxis().setEnabled(false); + lineChart.setViewPortOffsets(0, 0, 0, 0); + lineChart.setData(generateData(currency)); + + lineChart.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(getContext(), CurrencyDetailsActivity.class); + intent.putExtra("currency", currency); + getContext().startActivity(intent); + } + }); + } + + private void updateCardViewInfos(Currency currency, float totalValue, boolean isBalanceHidden) + { + ((ImageView) findViewById(R.id.currencyIcon)) + .setImageBitmap(currency.getIcon()); + ((TextView) findViewById(R.id.currencyNameTextView)) + .setText(currency.getName()); + ((TextView) findViewById(R.id.currencySymbolTextView)) + .setText(getResources().getString(R.string.currencySymbolPlaceholder, currency.getSymbol())); + ((TextView) findViewById(R.id.currencyOwnedTextView)) + .setText(getResources().getString(R.string.currencyBalancePlaceholder, numberConformer(currency.getBalance()), currency.getSymbol())); + ((TextView) findViewById(R.id.currencyValueOwnedTextView)) + .setText(getResources().getString(R.string.currencyDollarParenthesisPlaceholder, numberConformer(currency.getValue() * currency.getBalance()))); + + ((TextView) findViewById(R.id.currencyValueTextView)) + .setText(getResources().getString(R.string.currencyDollarPlaceholder, numberConformer(currency.getValue()))); + ((TextView) findViewById(R.id.currencyFluctuationPercentageTextView)) + .setText(getResources().getString(R.string.currencyPercentagePlaceholder, numberConformer(currency.getDayFluctuationPercentage()))); + ((TextView) findViewById(R.id.currencyFluctuationTextView)) + .setText(getResources().getString(R.string.currencyDollarParenthesisPlaceholder, numberConformer(currency.getDayFluctuation()))); + ((ImageView) findViewById(R.id.detailsArrow)) + .getDrawable().setColorFilter(new PorterDuffColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_IN)); + + if(isBalanceHidden) + { + double value = currency.getValue() * currency.getBalance(); + double percentage = value / totalValue * 100; + + findViewById(R.id.currencyPortfolioDominance).setVisibility(View.VISIBLE); + ((ProgressBar) findViewById(R.id.currencyPortfolioDominance)).setProgress((int) Math.round(percentage)); + ((ProgressBar) findViewById(R.id.currencyPortfolioDominance)).getIndeterminateDrawable().setColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_ATOP); + } + else + { + findViewById(R.id.currencyPortfolioDominance).setVisibility(View.GONE); + } + } + + private void collapseView() + { + collapse(findViewById(R.id.collapsableLayout)); + } + + private void extendView() + { + expand(findViewById(R.id.collapsableLayout)); + findViewById(R.id.LineChartView).invalidate(); + } + + private void updateColor(Currency currency) + { + if(currency.getDayFluctuationPercentage() > 0) + { + ((TextView) findViewById(R.id.currencyFluctuationPercentageTextView)) + .setTextColor(getResources().getColor(R.color.increase)); + ((TextView) findViewById(R.id.currencyFluctuationTextView)) + .setTextColor(getResources().getColor(R.color.increase)); + } + else + { + ((TextView) findViewById(R.id.currencyFluctuationPercentageTextView)) + .setTextColor(getResources().getColor(R.color.decrease)); + ((TextView) findViewById(R.id.currencyFluctuationTextView)) + .setTextColor(getResources().getColor(R.color.decrease)); + } + } + + private LineData generateData(Currency currency) + { + LineDataSet dataSet; + List dataChartList = currency.getHistoryMinutes(); + ArrayList values = new ArrayList<>(); + + for(int i = 0; i < dataChartList.size(); i+=10) + { + values.add(new Entry(i, (float) dataChartList.get(i).getOpen())); + } + + dataSet = new LineDataSet(values, "History"); + dataSet.setDrawIcons(false); + dataSet.setColor(currency.getChartColor()); + dataSet.setLineWidth(1); + dataSet.setDrawFilled(true); + dataSet.setFillColor(getColorWithAplha(currency.getChartColor(), 0.5f)); + dataSet.setFormLineWidth(1); + dataSet.setFormSize(15); + dataSet.setDrawCircles(false); + dataSet.setDrawValues(false); + dataSet.setHighlightEnabled(false); + + return new LineData(dataSet); + } + + private int getColorWithAplha(int color, float ratio) + { + int transColor; + int alpha = Math.round(Color.alpha(color) * ratio); + int r = Color.red(color); + int g = Color.green(color); + int b = Color.blue(color); + + transColor = Color.argb(alpha, r, g, b); + + return transColor ; + } + + private String numberConformer(double number) + { + String str; + + if(abs(number) > 1) + { + str = String.format( Locale.UK, "%.2f", number); + } + else + { + str = String.format( Locale.UK, "%.4f", number); + } + + return str; + } +} diff --git a/app/src/main/res/layout/cardview_currency.xml b/app/src/main/res/layout/cardview_currency.xml index cebcffb..3ebfcc7 100644 --- a/app/src/main/res/layout/cardview_currency.xml +++ b/app/src/main/res/layout/cardview_currency.xml @@ -86,27 +86,44 @@ android:orientation="horizontal"> - - + android:textSize="@dimen/cardViewSecondaryText" + android:visibility="gone"/> + + + + + + + + + android:orientation="vertical" />