Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.
Prev Previous commit
Next Next commit
#68[2h]. Add MonthReport. Finish bar chart.
  • Loading branch information
Evgenii Kanivets committed Apr 28, 2016
commit 07f2dbcaf8d7b9d4bae55cac833ad09f935f2ca1
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,31 @@

import com.blogspot.e_kanivets.moneytracker.R;
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity;
import com.blogspot.e_kanivets.moneytracker.controller.CurrencyController;
import com.blogspot.e_kanivets.moneytracker.controller.data.ExchangeRateController;
import com.blogspot.e_kanivets.moneytracker.controller.data.RecordController;
import com.blogspot.e_kanivets.moneytracker.entity.data.Record;
import com.blogspot.e_kanivets.moneytracker.report.ReportMaker;
import com.blogspot.e_kanivets.moneytracker.report.chart.BarChartConverter;
import com.blogspot.e_kanivets.moneytracker.report.chart.IMonthReport;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;

public class ChartsActivity extends BaseBackActivity {

@Inject
RecordController recordController;
@Inject
ExchangeRateController exchangeRateController;
@Inject
CurrencyController currencyController;

@Bind(R.id.bar_chart)
BarChart barChart;

Expand All @@ -18,20 +35,51 @@ protected int getContentViewId() {
return R.layout.activity_charts;
}

@Override
protected boolean initData() {
boolean result = super.initData();
getAppComponent().inject(ChartsActivity.this);
return result;
}

@Override
protected void initViews() {
super.initViews();

BarChartConverter barChartConverter = new BarChartConverter(ChartsActivity.this);
ReportMaker reportMaker = new ReportMaker(exchangeRateController);
String currency = currencyController.readDefaultCurrency();
List<Record> recordList = recordController.readAll();
List<String> currencyNeeded = reportMaker.currencyNeeded(currency, recordList);

IMonthReport monthReport = null;
if (currencyNeeded.isEmpty()) monthReport = reportMaker.getMonthReport(currency, recordList);
else barChart.setNoDataText(createRatesNeededList(currency, currencyNeeded));

if (monthReport != null) {
BarChartConverter barChartConverter = new BarChartConverter(ChartsActivity.this,
monthReport);

BarData barData = new BarData(barChartConverter.getXAxisValueList(),
barChartConverter.getBarDataSetList());
barData.setDrawValues(false);

barChart.setData(barData);
barChart.setDescription(null);
barChart.setVisibleXRangeMinimum(8);
barChart.setScaleYEnabled(false);
barChart.setVisibleXRangeMaximum(34);
barChart.setHighlightPerDragEnabled(false);
barChart.setHighlightPerTapEnabled(false);
}
}

protected String createRatesNeededList(String currency, List<String> ratesNeeded) {
StringBuilder sb = new StringBuilder(getString(R.string.error_exchange_rates));

BarData barData = new BarData(barChartConverter.getXAxisValueList(),
barChartConverter.getBarDataSetList());
barData.setDrawValues(false);
for (String str : ratesNeeded) {
sb.append("\n").append(str).append(getString(R.string.arrow)).append(currency);
}

barChart.setData(barData);
barChart.setDescription(null);
barChart.setVisibleXRangeMinimum(8);
barChart.setScaleYEnabled(false);
barChart.setVisibleXRangeMaximum(34);
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected void initViews() {

private void update(String currency) {
ReportMaker reportMaker = new ReportMaker(rateController);
IRecordReport report = reportMaker.getReport(currency, period, recordList);
IRecordReport report = reportMaker.getRecordReport(currency, period, recordList);

ExpandableListReportAdapter adapter = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected void update() {
String currency = currencyController.readDefaultCurrency();

ReportMaker reportMaker = new ReportMaker(rateController);
IRecordReport report = reportMaker.getReport(currency, period, recordList);
IRecordReport report = reportMaker.getRecordReport(currency, period, recordList);
summaryPresenter.update(report, currency, reportMaker.currencyNeeded(currency, recordList));

fillDefaultAccount();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.blogspot.e_kanivets.moneytracker.di;

import com.blogspot.e_kanivets.moneytracker.activity.ChartsActivity;
import com.blogspot.e_kanivets.moneytracker.activity.ExportActivity;
import com.blogspot.e_kanivets.moneytracker.activity.ReportActivity;
import com.blogspot.e_kanivets.moneytracker.activity.SettingsActivity;
Expand Down Expand Up @@ -47,6 +48,8 @@ public interface AppComponent {

void inject(ReportActivity reportActivity);

void inject(ChartsActivity chartsActivity);

void inject(SettingsActivity.SettingsFragment settingsFragment);

void inject(AccountsSummaryPresenter accountsSummaryPresenter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import com.blogspot.e_kanivets.moneytracker.entity.data.ExchangeRate;
import com.blogspot.e_kanivets.moneytracker.entity.Period;
import com.blogspot.e_kanivets.moneytracker.entity.data.Record;
import com.blogspot.e_kanivets.moneytracker.report.base.IAccountsReport;
import com.blogspot.e_kanivets.moneytracker.report.account.AccountsReport;
import com.blogspot.e_kanivets.moneytracker.report.account.IAccountsReport;
import com.blogspot.e_kanivets.moneytracker.report.base.IExchangeRateProvider;
import com.blogspot.e_kanivets.moneytracker.report.chart.IMonthReport;
import com.blogspot.e_kanivets.moneytracker.report.chart.MonthReport;
import com.blogspot.e_kanivets.moneytracker.report.record.IRecordReport;
import com.blogspot.e_kanivets.moneytracker.report.record.RecordReport;

Expand All @@ -32,7 +35,7 @@ public ReportMaker(ExchangeRateController exchangeRateController) {
}

@Nullable
public IRecordReport getReport(String currency, Period period, List<Record> recordList) {
public IRecordReport getRecordReport(String currency, Period period, List<Record> recordList) {
if (currencyNeeded(currency, recordList).size() != 0) return null;

IExchangeRateProvider rateProvider = new ExchangeRateProvider(currency, rateController);
Expand All @@ -47,6 +50,14 @@ public IAccountsReport getAccountsReport(String currency, List<Account> accountL
return new AccountsReport(currency, accountList, rateProvider);
}

@Nullable
public IMonthReport getMonthReport(String currency, List<Record> recordList) {
if (currencyNeeded(currency, recordList).size() != 0) return null;

IExchangeRateProvider rateProvider = new ExchangeRateProvider(currency, rateController);
return new MonthReport(recordList, currency, rateProvider);
}

@NonNull
public List<String> currencyNeeded(String currency, List<Record> recordList) {
Set<String> currencies = new TreeSet<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.blogspot.e_kanivets.moneytracker.report;
package com.blogspot.e_kanivets.moneytracker.report.account;

import android.support.annotation.NonNull;

import com.blogspot.e_kanivets.moneytracker.entity.data.Account;
import com.blogspot.e_kanivets.moneytracker.entity.data.ExchangeRate;
import com.blogspot.e_kanivets.moneytracker.report.base.IAccountsReport;
import com.blogspot.e_kanivets.moneytracker.report.account.IAccountsReport;
import com.blogspot.e_kanivets.moneytracker.report.base.IExchangeRateProvider;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.blogspot.e_kanivets.moneytracker.report.base;
package com.blogspot.e_kanivets.moneytracker.report.account;

import android.support.annotation.NonNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.blogspot.e_kanivets.moneytracker.report.chart;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;

Expand All @@ -8,96 +9,68 @@
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* Util class to convert {@link BarChartReport}
* to {@link com.github.mikephil.charting.charts.BarChart} input data.
* Util class to convert {@link IMonthReport} to {@link com.github.mikephil.charting.charts.BarChart}
* input data.
* Created on 4/27/16.
*
* @author Evgenii Kanivets
*/
public class BarChartConverter {
private final IMonthReport report;

private final int green;
private final int red;
private final String incomesTitle;
private final String expensesTitle;

@SuppressWarnings("deprecation")
public BarChartConverter(@NonNull Context context, @NonNull IMonthReport report) {
this.report = report;

public BarChartConverter(Context context) {
green = context.getResources().getColor(R.color.green_light);
red = context.getResources().getColor(R.color.red_light);
incomesTitle = context.getString(R.string.incomes);
expensesTitle = context.getString(R.string.expenses);
}

@NonNull
public List<String> getXAxisValueList() {
List<String> valueList = new ArrayList<>();
String[] xVals = {"Jan 16", "Feb 16", "Mar 16", "Apr 16", "May 16", "Jun 16", "Jul 16",
"Aug 16", "Sep 16", "Oct 16", "Nov 16", "Dec 16", "Jan 17", "Feb 17", "Mar 17",
"Apr 17", "May 17", "Jun 17", "Jul 17", "Aug 17", "Sep 17", "Oct 17", "Nov 17", "Dec 17"};
valueList.addAll(Arrays.asList(xVals));

@SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("MMM yy");
for (long timestamp : report.getMonthList()) {
valueList.add(sdf.format(new Date(timestamp)));
}

return valueList;
}

@NonNull
public List<IBarDataSet> getBarDataSetList() {
List<BarEntry> list1 = new ArrayList<>();
list1.add(new BarEntry(100, 0));
list1.add(new BarEntry(110, 1));
list1.add(new BarEntry(60, 2));
list1.add(new BarEntry(90, 3));
list1.add(new BarEntry(150, 4));
list1.add(new BarEntry(50, 5));
list1.add(new BarEntry(100, 6));
list1.add(new BarEntry(110, 7));
list1.add(new BarEntry(60, 8));
list1.add(new BarEntry(90, 9));
list1.add(new BarEntry(150, 10));
list1.add(new BarEntry(50, 11));
list1.add(new BarEntry(100, 12));
list1.add(new BarEntry(110, 13));
list1.add(new BarEntry(60, 14));
list1.add(new BarEntry(90, 15));
list1.add(new BarEntry(150, 16));
list1.add(new BarEntry(50, 17));
list1.add(new BarEntry(100, 18));
list1.add(new BarEntry(110, 19));
list1.add(new BarEntry(60, 20));
list1.add(new BarEntry(90, 21));
list1.add(new BarEntry(150, 22));
list1.add(new BarEntry(50, 23));
BarDataSet dataSet1 = new BarDataSet(list1, "Incomes");
dataSet1.setColor(green);

List<BarEntry> list2 = new ArrayList<>();
list2.add(new BarEntry(50, 0));
list2.add(new BarEntry(10, 1));
list2.add(new BarEntry(30, 2));
list2.add(new BarEntry(30, 3));
list2.add(new BarEntry(50, 4));
list2.add(new BarEntry(10, 5));
list2.add(new BarEntry(50, 6));
list2.add(new BarEntry(10, 7));
list2.add(new BarEntry(30, 8));
list2.add(new BarEntry(30, 9));
list2.add(new BarEntry(50, 10));
list2.add(new BarEntry(10, 11));
list2.add(new BarEntry(100, 12));
list2.add(new BarEntry(110, 13));
list2.add(new BarEntry(60, 14));
list2.add(new BarEntry(90, 15));
list2.add(new BarEntry(150, 16));
list2.add(new BarEntry(50, 17));
list2.add(new BarEntry(100, 18));
list2.add(new BarEntry(110, 19));
list2.add(new BarEntry(60, 20));
list2.add(new BarEntry(90, 21));
list2.add(new BarEntry(150, 22));
list2.add(new BarEntry(50, 23));
BarDataSet dataSet2 = new BarDataSet(list2, "Expenses");
List<BarEntry> incomeList = new ArrayList<>();
for (int i = 0; i < report.getIncomeList().size(); i++) {
incomeList.add(new BarEntry(report.getIncomeList().get(i).floatValue(), i));
}

BarDataSet incomeDataSet = new BarDataSet(incomeList, incomesTitle);
incomeDataSet.setColor(green);

List<BarEntry> expenseList = new ArrayList<>();
for (int i = 0; i < report.getExpenseList().size(); i++) {
expenseList.add(new BarEntry(report.getExpenseList().get(i).floatValue(), i));
}

BarDataSet dataSet2 = new BarDataSet(expenseList, expensesTitle);
dataSet2.setColor(red);

List<IBarDataSet> list = new ArrayList<>();
list.add(dataSet1);
list.add(incomeDataSet);
list.add(dataSet2);

return list;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.blogspot.e_kanivets.moneytracker.report.chart;

import android.support.annotation.NonNull;

import java.util.List;

/**
* Interface that represents a contract of access to record report data grouped by month
* for all records. All three methods must return list of the same size.
* Created on 4/28/16.
*
* @author Evgenii Kanivets
*/
public interface IMonthReport {
/**
* @return code of report currency
*/
@NonNull String getCurrency();

/**
* @return list of month timestamps with not zero record count
*/
@NonNull
List<Long> getMonthList();

/**
* @return list of summary month incomes
*/
@NonNull
List<Double> getIncomeList();

/**
* @return list of summary month expenses
*/
@NonNull
List<Double> getExpenseList();
}
Loading