Fix chart generation for coin without history

This commit is contained in:
Tanguy Herbron 2018-02-01 00:24:29 +01:00
parent f3e24d2a34
commit d0b92738d6
6 changed files with 203 additions and 45 deletions

View File

@ -16,20 +16,27 @@ import android.widget.TextView;
import android.widget.ViewFlipper; import android.widget.ViewFlipper;
import com.daimajia.swipe.SwipeLayout; 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.CurrencyData.Transaction;
import com.nauk.coinfolio.DataManagers.DatabaseManager; import com.nauk.coinfolio.DataManagers.DatabaseManager;
import com.nauk.coinfolio.R; import com.nauk.coinfolio.R;
import org.w3c.dom.Text; import org.w3c.dom.Text;
import java.util.Calendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale;
/**Create a Parcelable**/
public class CurrencyDetailsActivity extends AppCompatActivity { public class CurrencyDetailsActivity extends AppCompatActivity {
private ViewFlipper viewFlipper; private ViewFlipper viewFlipper;
private LinearLayout transactionLayout; private LinearLayout transactionLayout;
private LinearLayout chartLayout;
private DatabaseManager databaseManager; private DatabaseManager databaseManager;
private String symbol; private String symbol;
@ -66,13 +73,28 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
viewFlipper = findViewById(R.id.vfCurrencyDetails); viewFlipper = findViewById(R.id.vfCurrencyDetails);
transactionLayout = findViewById(R.id.listTransactions); transactionLayout = findViewById(R.id.listTransactions);
chartLayout = findViewById(R.id.chartLayout);
drawTransactionList(); drawTransactionList();
//drawChart();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); 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() private void drawTransactionList()
{ {
transactionLayout.removeAllViews(); transactionLayout.removeAllViews();

View File

@ -26,6 +26,7 @@ import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import com.daimajia.swipe.SwipeLayout; import com.daimajia.swipe.SwipeLayout;
import com.db.chart.view.LineChartView;
import com.nauk.coinfolio.DataManagers.BalanceManager; import com.nauk.coinfolio.DataManagers.BalanceManager;
import com.nauk.coinfolio.DataManagers.CurrencyData.Currency; import com.nauk.coinfolio.DataManagers.CurrencyData.Currency;
import com.nauk.coinfolio.DataManagers.DatabaseManager; import com.nauk.coinfolio.DataManagers.DatabaseManager;
@ -63,6 +64,7 @@ public class HomeActivity extends AppCompatActivity {
private boolean iconChecker; private boolean iconChecker;
private PreferencesManager preferencesManager; private PreferencesManager preferencesManager;
private DatabaseManager databaseManager; private DatabaseManager databaseManager;
private long lastTimestamp;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -153,14 +155,21 @@ public class HomeActivity extends AppCompatActivity {
databaseManager = new DatabaseManager(this); databaseManager = new DatabaseManager(this);
updateViewButtonIcon(); updateViewButtonIcon();
lastTimestamp = 0;
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
if(System.currentTimeMillis()/1000 - lastTimestamp > 60)
{
lastTimestamp = System.currentTimeMillis()/1000;
updateAll(); updateAll();
} }
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
@ -216,21 +225,31 @@ public class HomeActivity extends AppCompatActivity {
{ {
currencyLayout.removeAllViews(); currencyLayout.removeAllViews();
//layoutGenerator.setCurrencyList(balanceManager.getTotalBalance());
//layoutGenerator.resetCurrencyList();
for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) for(int i = 0; i < balanceManager.getTotalBalance().size(); i++)
{ {
final Currency currency = balanceManager.getTotalBalance().get(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) if(currency.getIcon() != null)
{ {
Palette.Builder builder = Palette.from(currency.getIcon()); 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(currency, builder.generate().getDominantColor(0)));
//currencyLayout.addView(layoutGenerator.getInfoLayout(i));
} }
else else
{ {
//currency.setChartColor(12369084);
currencyLayout.addView(layoutGenerator.getInfoLayout(currency, 12369084)); currencyLayout.addView(layoutGenerator.getInfoLayout(currency, 12369084));
//currencyLayout.addView(layoutGenerator.getInfoLayout(i));
} }
} }
} }
@ -296,6 +315,8 @@ public class HomeActivity extends AppCompatActivity {
currencyLayout.removeAllViews(); currencyLayout.removeAllViews();
//layoutGenerator.setCurrencyList(balanceManager.getTotalBalance());
for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) 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) if(!balanceManager.getTotalBalance().get(i).getSymbol().equals("USD") && (balanceManager.getTotalBalance().get(i).getBalance() * balanceManager.getTotalBalance().get(i).getValue()) > 0.001)
@ -303,6 +324,14 @@ public class HomeActivity extends AppCompatActivity {
totalValue += balanceManager.getTotalBalance().get(i).getValue() * balanceManager.getTotalBalance().get(i).getBalance(); 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); 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()))); 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));
}
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)); currencyLayout.addView(layoutGenerator.getInfoLayout(balanceManager.getTotalBalance().get(i), 0));
} }
} }
@ -328,7 +357,6 @@ public class HomeActivity extends AppCompatActivity {
loadingDialog.dismiss(); loadingDialog.dismiss();
} }
} }
}
if(balanceManager.getTotalBalance().size() == 0) if(balanceManager.getTotalBalance().size() == 0)
{ {
@ -351,10 +379,7 @@ public class HomeActivity extends AppCompatActivity {
toolbarSubtitle.setTextColor(-1275068417); toolbarSubtitle.setTextColor(-1275068417);
} }
}); });
}
//toolbarSubtitle.setText("US$0.00");
Log.d(this.getResources().getString(R.string.debug), "Colors : " + toolbarSubtitle.getTextColors());
} }
} }

View File

@ -4,6 +4,8 @@ import android.graphics.Bitmap;
import java.util.List; import java.util.List;
import static java.sql.Types.NULL;
/** /**
* Created by Tiji on 25/12/2017. * Created by Tiji on 25/12/2017.
*/ */
@ -19,6 +21,7 @@ public class Currency {
private List<CurrencyDataChart> dayPriceHistory; private List<CurrencyDataChart> dayPriceHistory;
private CurrencyDataRetriver dataRetriver; private CurrencyDataRetriver dataRetriver;
private Bitmap icon; private Bitmap icon;
private int chartColor;
public Currency(String symbol, double balance) public Currency(String symbol, double balance)
{ {
@ -48,7 +51,14 @@ public class Currency {
setDayPriceHistory(dataChart); setDayPriceHistory(dataChart);
updateDayFluctuation(); updateDayFluctuation();
if(dataChart != null)
{
setValue(dataChart.get(dataChart.size() - 1).getClose()); setValue(dataChart.get(dataChart.size() - 1).getClose());
}
else
{
value = NULL;
}
callBack.onSuccess(Currency.this); 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<CurrencyDataChart> getDayPriceHistory() public List<CurrencyDataChart> getDayPriceHistory()
{ {
return dayPriceHistory; return dayPriceHistory;
@ -141,11 +166,14 @@ public class Currency {
} }
private void updateDayFluctuation() private void updateDayFluctuation()
{
if(dayPriceHistory != null)
{ {
dayFluctuation = dayPriceHistory.get(dayPriceHistory.size() - 1).getOpen() - dayPriceHistory.get(0).getOpen(); 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 { public interface CurrencyCallBack {
void onSuccess(Currency currency); void onSuccess(Currency currency);

View File

@ -109,8 +109,6 @@ public class CurrencyDataRetriver {
{ {
List<CurrencyDataChart> dataChart = new ArrayList<>(); List<CurrencyDataChart> dataChart = new ArrayList<>();
Log.d("coinfolio", "Result : " + response);
if(response.length() > 200) if(response.length() > 200)
{ {
response = response.substring(response.indexOf("Data\":[{") + 7, response.lastIndexOf("}],\"TimeTo")); response = response.substring(response.indexOf("Data\":[{") + 7, response.lastIndexOf("}],\"TimeTo"));
@ -145,10 +143,12 @@ public class CurrencyDataRetriver {
} }
else 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.add(new CurrencyDataChart(i, 1, 1, 1, 1));
} }*/
dataChart = null;
} }
return dataChart; return dataChart;

View File

@ -8,8 +8,10 @@ import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v7.widget.CardView; import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; 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.DataManagers.CurrencyData.CurrencyDataChart;
import com.nauk.coinfolio.R; import com.nauk.coinfolio.R;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import static java.lang.Math.abs; import static java.lang.Math.abs;
import static java.lang.Math.floorDiv; 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. * 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(final Currency currency, int chartColor)
//public CardView getInfoLayout(int index)
{ {
CardView mainCard = new CardView(context); CardView mainCard = new CardView(context);
LinearLayout mainLinear = new LinearLayout(context); LinearLayout mainLinear = new LinearLayout(context);
@ -57,6 +63,8 @@ public class HomeLayoutGenerator {
StateListAnimator stateListAnimator = AnimatorInflater.loadStateListAnimator(context, R.drawable.cardview_animator); StateListAnimator stateListAnimator = AnimatorInflater.loadStateListAnimator(context, R.drawable.cardview_animator);
mainCard.setStateListAnimator(stateListAnimator); mainCard.setStateListAnimator(stateListAnimator);
//mainCard.setTag("full." + index);
/*int[] attrs = new int[] { R.attr.selectableItemBackground }; /*int[] attrs = new int[] { R.attr.selectableItemBackground };
TypedArray ta = context.obtainStyledAttributes(attrs); TypedArray ta = context.obtainStyledAttributes(attrs);
Drawable drawable = ta.getDrawable(0); Drawable drawable = ta.getDrawable(0);
@ -67,12 +75,14 @@ public class HomeLayoutGenerator {
mainCard.setOnClickListener(new View.OnClickListener() { mainCard.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
//switchingView(view);
view.animate(); view.animate();
Intent intent = new Intent(context.getApplicationContext(), CurrencyDetailsActivity.class); Intent intent = new Intent(context.getApplicationContext(), CurrencyDetailsActivity.class);
intent.putExtra("symbol", currency.getSymbol()); intent.putExtra("symbol", currency.getSymbol());
context.getApplicationContext().startActivity(intent); context.getApplicationContext().startActivity(intent);
} }
}); });
mainCard.setClickable(true); mainCard.setClickable(true);
CardView.LayoutParams paramsCard = new CardView.LayoutParams(CardView.LayoutParams.MATCH_PARENT, CardView.LayoutParams.WRAP_CONTENT); 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.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
mainLinear.setOrientation(LinearLayout.VERTICAL); mainLinear.setOrientation(LinearLayout.VERTICAL);
mainLinear.setTag("mainLinear");
mainCard.setLayoutParams(paramsCard); mainCard.setLayoutParams(paramsCard);
mainCard.setRadius(8); 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(topLayoutGenerator(currency.getName(), currency.getSymbol(), currency.getValue(), currency.getIcon()));
infoLayout.addView(bottomLayoutGenerator(currency.getSymbol(), currency.getBalance(), currency.getValue() * currency.getBalance(), currency.getDayFluctuationPercentage(), currency.getDayFluctuation())); infoLayout.addView(bottomLayoutGenerator(currency.getSymbol(), currency.getBalance(), currency.getValue() * currency.getBalance(), currency.getDayFluctuationPercentage(), currency.getDayFluctuation()));
mainLinear.addView(infoLayout); mainLinear.addView(infoLayout);
LineChartView lineChartView = chartGenerator(currency.getDayPriceHistory(), chartColor);
chartLayout.setTag("chart_layout");
chartLayout.addView(lineChartView);
lineChartView.show();
mainLinear.addView(separatorLayout); mainLinear.addView(separatorLayout);
mainLinear.addView(chartLayout);
mainLinear.addView(generateChart(currency, chartLayout, chartColor));
mainLinear.setClickable(false);
mainCard.addView(mainLinear); mainCard.addView(mainLinear);
return mainCard; return mainCard;
} }
/*public void resetCurrencyList()
{
currencyList = new ArrayList<>();
}
public void addCurrencyToList(Currency currency)
{
currencyList.add(currency);
}
public void setCurrencyList(List<Currency> 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) private LinearLayout topLayoutGenerator(String name, String symbol, double value, Bitmap logo)
{ {
LinearLayout mainLayout = new LinearLayout(context); LinearLayout mainLayout = new LinearLayout(context);
@ -159,7 +239,15 @@ public class HomeLayoutGenerator {
valueTextView.setTextSize(context.getResources().getDimension(R.dimen.mainText)); valueTextView.setTextSize(context.getResources().getDimension(R.dimen.mainText));
valueTextView.setTextColor(context.getResources().getColor(R.color.secondaryTextViewColor)); valueTextView.setTextColor(context.getResources().getColor(R.color.secondaryTextViewColor));
valueTextView.setGravity(Gravity.RIGHT); valueTextView.setGravity(Gravity.RIGHT);
if(value != NULL)
{
valueTextView.setText("US$" + value); valueTextView.setText("US$" + value);
}
else
{
valueTextView.setText("ERROR");
}
mainLayout.addView(currencyIcon); mainLayout.addView(currencyIcon);
mainLayout.addView(nameTextView); mainLayout.addView(nameTextView);
@ -212,6 +300,7 @@ public class HomeLayoutGenerator {
fluctuationTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); fluctuationTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
fluctuationTextView.setTextSize(context.getResources().getDimension(R.dimen.secondaryText)); fluctuationTextView.setTextSize(context.getResources().getDimension(R.dimen.secondaryText));
if(fluctuation > 0) if(fluctuation > 0)
{ {
fluctuationTextView.setTextColor(context.getResources().getColor(R.color.increase)); fluctuationTextView.setTextColor(context.getResources().getColor(R.color.increase));

View File

@ -18,16 +18,10 @@
android:layout_height="match_parent"> android:layout_height="match_parent">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:id="@+id/chartLayout"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:orientation="vertical"/>
android:text="Charts"/>
</LinearLayout>
</ScrollView> </ScrollView>