Skip to content

Commit

Permalink
Merge branch 'release/v2.0.0' of https://github.com/codinguser/gnucas…
Browse files Browse the repository at this point in the history
…h-android into release/v2.0.0
  • Loading branch information
codinguser committed Oct 9, 2015
2 parents 835623c + 4430b86 commit 8a7bbf5
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import org.gnucash.android.db.TransactionsDbAdapter;
import org.gnucash.android.model.Account;
import org.gnucash.android.model.AccountType;
import org.gnucash.android.model.Money;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.Months;
Expand Down Expand Up @@ -96,6 +95,8 @@ public class BarChartFragment extends Fragment implements OnChartValueSelectedLi

private Currency mCurrency;

private AccountType mAccountType;

private boolean mUseAccountColor = true;
private boolean mTotalPercentageMode = true;
private boolean mChartDataPresent = true;
Expand Down Expand Up @@ -137,6 +138,11 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {

mCurrency = Currency.getInstance(GnuCashApplication.getDefaultCurrencyCode());

ReportsActivity reportsActivity = (ReportsActivity) getActivity();
mReportStartTime = reportsActivity.getReportStartTime();
mReportEndTime = reportsActivity.getReportEndTime();
mAccountType = reportsActivity.getAccountType();

mChart.setOnChartValueSelectedListener(this);
mChart.setDescription("");
// mChart.setDrawValuesForWholeStack(false);
Expand All @@ -146,27 +152,27 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
mChart.getAxisLeft().setValueFormatter(new LargeValueFormatter(mCurrency.getSymbol(Locale.getDefault())));
Legend chartLegend = mChart.getLegend();
chartLegend.setForm(Legend.LegendForm.CIRCLE);
chartLegend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART_INSIDE);
// chartLegend.setWordWrapEnabled(true); in MPAndroidChart 2.1.3 legend wrapping cause app crash
chartLegend.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
chartLegend.setWordWrapEnabled(true);

mChart.setData(getData(((ReportsActivity) getActivity()).getAccountType()));
mChart.setData(getData());
displayChart();
}


/**
* Returns a data object that represents a user data of the specified account types
* @param accountType account's type which will be displayed
* @return a {@code BarData} instance that represents a user data
*/
private BarData getData(AccountType accountType) {
private BarData getData() {
List<BarEntry> values = new ArrayList<>();
List<String> labels = new ArrayList<>();
List<Integer> colors = new ArrayList<>();
Map<String, Integer> accountToColorMap = new LinkedHashMap<>();
List<String> xValues = new ArrayList<>();
LocalDateTime tmpDate = new LocalDateTime(getStartDate(accountType).toDate().getTime());
int count = getDateDiff(new LocalDateTime(getStartDate(accountType).toDate().getTime()), new LocalDateTime(getEndDate(accountType).toDate().getTime()));
LocalDateTime tmpDate = new LocalDateTime(getStartDate(mAccountType).toDate().getTime());
int count = getDateDiff(new LocalDateTime(getStartDate(mAccountType).toDate().getTime()),
new LocalDateTime(getEndDate(mAccountType).toDate().getTime()));
for (int i = 0; i <= count; i++) {
long start = 0;
long end = 0;
Expand Down Expand Up @@ -196,7 +202,7 @@ private BarData getData(AccountType accountType) {
}
List<Float> stack = new ArrayList<>();
for (Account account : mAccountsDbAdapter.getSimpleAccountList()) {
if (account.getAccountType() == accountType
if (account.getAccountType() == mAccountType
&& !account.isPlaceholderAccount()
&& account.getCurrency() == mCurrency) {

Expand All @@ -218,7 +224,7 @@ private BarData getData(AccountType accountType) {
stack.add((float) balance);
labels.add(account.getName());
colors.add(accountToColorMap.get(account.getUID()));
Log.d(TAG, accountType + tmpDate.toString(" MMMM yyyy ") + account.getName() + " = " + stack.get(stack.size() - 1));
Log.d(TAG, mAccountType + tmpDate.toString(" MMMM yyyy ") + account.getName() + " = " + stack.get(stack.size() - 1));
}
}
}
Expand Down Expand Up @@ -344,6 +350,7 @@ private float[] floatListToArray(List<Float> list) {
private void displayChart() {
mChart.highlightValues(null);
setCustomLegend();
mChart.notifyDataSetChanged();

mChart.getAxisLeft().setDrawLabels(mChartDataPresent);
mChart.getXAxis().setDrawLabels(mChartDataPresent);
Expand Down Expand Up @@ -380,24 +387,31 @@ private void setCustomLegend() {

@Override
public void onTimeRangeUpdated(long start, long end) {
mReportStartTime = start;
mReportEndTime = end;
if (mReportStartTime != start || mReportEndTime != end) {
mReportStartTime = start;
mReportEndTime = end;

mChart.setData(getData(((ReportsActivity) getActivity()).getAccountType()));
displayChart();
mChart.setData(getData());
displayChart();
}
}

@Override
public void onGroupingUpdated(GroupInterval groupInterval) {
mGroupInterval = groupInterval;
mChart.setData(getData(((ReportsActivity) getActivity()).getAccountType()));
displayChart();
if (mGroupInterval != groupInterval) {
mGroupInterval = groupInterval;
mChart.setData(getData());
displayChart();
}
}

@Override
public void onAccountTypeUpdated(AccountType accountType) {
mChart.setData(getData(accountType));
displayChart();
if (mAccountType != accountType) {
mAccountType = accountType;
mChart.setData(getData());
displayChart();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
Expand Down Expand Up @@ -48,7 +47,6 @@
import org.gnucash.android.db.TransactionsDbAdapter;
import org.gnucash.android.model.Account;
import org.gnucash.android.model.AccountType;
import org.gnucash.android.model.Money;
import org.gnucash.android.ui.report.ReportsActivity.GroupInterval;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
Expand Down Expand Up @@ -132,6 +130,10 @@ public void onActivityCreated(Bundle savedInstanceState) {

mCurrency = Currency.getInstance(GnuCashApplication.getDefaultCurrencyCode());

ReportsActivity reportsActivity = (ReportsActivity) getActivity();
mReportStartTime = reportsActivity.getReportStartTime();
mReportEndTime = reportsActivity.getReportEndTime();

mChart.setOnChartValueSelectedListener(this);
mChart.setDescription("");
mChart.getXAxis().setDrawGridLines(false);
Expand Down Expand Up @@ -171,6 +173,7 @@ public void onResume() {
* @return a {@code LineData} instance that represents a user data
*/
private LineData getData(List<AccountType> accountTypeList) {
Log.w(TAG, "getData");
calculateEarliestAndLatestTimestamps(accountTypeList);
// LocalDateTime?
LocalDate startDate;
Expand Down Expand Up @@ -376,20 +379,21 @@ private void calculateEarliestAndLatestTimestamps(List<AccountType> accountTypeL

@Override
public void onTimeRangeUpdated(long start, long end) {
mReportStartTime = start;
mReportEndTime = end;

mChart.setData(getData(new ArrayList<>(Arrays.asList(AccountType.INCOME, AccountType.EXPENSE))));
mChart.invalidate();
if (mReportStartTime != start || mReportEndTime != end) {
mReportStartTime = start;
mReportEndTime = end;
mChart.setData(getData(new ArrayList<>(Arrays.asList(AccountType.INCOME, AccountType.EXPENSE))));
mChart.invalidate();
}
}

@Override
public void onGroupingUpdated(GroupInterval groupInterval) {
mGroupInterval = groupInterval;
Log.d(TAG, "GroupInterval " + groupInterval);

mChart.setData(getData(new ArrayList<>(Arrays.asList(AccountType.INCOME, AccountType.EXPENSE))));
mChart.invalidate();
if (mGroupInterval != groupInterval) {
mGroupInterval = groupInterval;
mChart.setData(getData(new ArrayList<>(Arrays.asList(AccountType.INCOME, AccountType.EXPENSE))));
mChart.invalidate();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
import org.gnucash.android.db.TransactionsDbAdapter;
import org.gnucash.android.model.Account;
import org.gnucash.android.model.AccountType;
import org.gnucash.android.model.Money;
import org.joda.time.LocalDateTime;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -69,7 +67,6 @@ public class PieChartFragment extends Fragment implements OnChartValueSelectedLi
ReportOptionsListener {

public static final String SELECTED_VALUE_PATTERN = "%s - %.2f (%.2f %%)";
public static final String DATE_PATTERN = "MMMM\nYYYY";
public static final String TOTAL_VALUE_LABEL_PATTERN = "%s\n%.2f %s";
private static final int ANIMATION_DURATION = 1800;
public static final int NO_DATA_COLOR = Color.LTGRAY;
Expand All @@ -83,18 +80,13 @@ public class PieChartFragment extends Fragment implements OnChartValueSelectedLi
*/
private static final double GROUPING_SMALLER_SLICES_THRESHOLD = 5;

private LocalDateTime mChartDate = new LocalDateTime();

@Bind(R.id.pie_chart) PieChart mChart;
@Bind(R.id.selected_chart_slice) TextView mSelectedValueTextView;

private AccountsDbAdapter mAccountsDbAdapter;
private TransactionsDbAdapter mTransactionsDbAdapter;

private LocalDateTime mEarliestTransactionDate;
private LocalDateTime mLatestTransactionDate;

private AccountType mAccountType = AccountType.EXPENSE;
private AccountType mAccountType;

private boolean mChartDataPresent = true;

Expand Down Expand Up @@ -126,8 +118,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(R.string.title_pie_chart);

((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(R.string.title_pie_chart);
setHasOptionsMenu(true);

mUseAccountColor = PreferenceManager.getDefaultSharedPreferences(getActivity())
Expand All @@ -143,8 +135,12 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
mChart.getLegend().setWordWrapEnabled(true);
mChart.setOnChartValueSelectedListener(this);

mAccountType = ((ReportsActivity)getActivity()).getAccountType();
onAccountTypeUpdated(mAccountType);
ReportsActivity reportsActivity = (ReportsActivity) getActivity();
mReportStartTime = reportsActivity.getReportStartTime();
mReportEndTime = reportsActivity.getReportEndTime();
mAccountType = reportsActivity.getAccountType();

displayChart();
}

/**
Expand Down Expand Up @@ -196,7 +192,8 @@ private PieData getData() {
&& !account.isPlaceholderAccount()
&& account.getCurrency() == Currency.getInstance(mCurrencyCode)) {

double balance = mAccountsDbAdapter.getAccountsBalance(Collections.singletonList(account.getUID()), mReportStartTime, mReportEndTime).absolute().asDouble();
double balance = mAccountsDbAdapter.getAccountsBalance(Collections.singletonList(account.getUID()),
mReportStartTime, mReportEndTime).absolute().asDouble();
if (balance != 0) {
dataSet.addEntry(new Entry((float) balance, dataSet.getEntryCount()));
colors.add(mUseAccountColor && account.getColorHexCode() != null
Expand All @@ -213,24 +210,24 @@ private PieData getData() {

@Override
public void onTimeRangeUpdated(long start, long end) {
mReportStartTime = start;
mReportEndTime = end;
displayChart();
if (mReportStartTime != start || mReportEndTime != end) {
mReportStartTime = start;
mReportEndTime = end;
displayChart();
}
}

@Override
public void onGroupingUpdated(ReportsActivity.GroupInterval groupInterval) {
//TODO: Does this make sense for a pie chart? Don't think so
//nothing to see here, this doesn't make sense for a pie chart
}

@Override
public void onAccountTypeUpdated(AccountType accountType) {
mAccountType = accountType;
mEarliestTransactionDate = new LocalDateTime(mTransactionsDbAdapter.getTimestampOfEarliestTransaction(mAccountType, mCurrencyCode));
mLatestTransactionDate = new LocalDateTime(mTransactionsDbAdapter.getTimestampOfLatestTransaction(mAccountType, mCurrencyCode));
mChartDate = mLatestTransactionDate;

displayChart();
if (mAccountType != accountType) {
mAccountType = accountType;
displayChart();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@
import org.gnucash.android.model.Account;
import org.gnucash.android.model.AccountType;
import org.gnucash.android.model.Money;
import org.gnucash.android.model.Transaction;
import org.gnucash.android.ui.transaction.TransactionsActivity;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalDate;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -63,6 +62,9 @@
* @author Ngewi Fet <ngewif@gmail.com>
*/
public class ReportSummaryFragment extends Fragment {

public static final int LEGEND_TEXT_SIZE = 14;

@Bind(R.id.btn_pie_chart) Button mPieChartButton;
@Bind(R.id.btn_bar_chart) Button mBarChartButton;
@Bind(R.id.btn_line_chart) Button mLineChartButton;
Expand All @@ -73,7 +75,7 @@ public class ReportSummaryFragment extends Fragment {
@Bind(R.id.total_liabilities) TextView mTotalLiabilities;
@Bind(R.id.net_worth) TextView mNetWorth;

AccountsDbAdapter mAccountsDbAdapter;
private AccountsDbAdapter mAccountsDbAdapter;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -138,8 +140,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
mChart.setDescription("");
mChart.getLegend().setEnabled(true);
mChart.getLegend().setPosition(Legend.LegendPosition.RIGHT_OF_CHART_CENTER);
mChart.getLegend().setTextSize(14);
// mChart.setOnChartValueSelectedListener(this);
mChart.getLegend().setTextSize(LEGEND_TEXT_SIZE);

ColorStateList csl = new ColorStateList(new int[][]{new int[0]}, new int[]{getResources().getColor(R.color.account_green)});
setButtonTint(mPieChartButton, csl);
Expand Down Expand Up @@ -180,7 +181,6 @@ public void onPrepareOptionsMenu(Menu menu) {
*/
private PieData getData() {
String mCurrencyCode = GnuCashApplication.getDefaultCurrencyCode();
LocalDateTime mChartDate = new LocalDateTime();
PieDataSet dataSet = new PieDataSet(null, "");
List<String> labels = new ArrayList<>();
List<Integer> colors = new ArrayList<>();
Expand All @@ -189,10 +189,8 @@ private PieData getData() {
&& !account.isPlaceholderAccount()
&& account.getCurrency() == Currency.getInstance(mCurrencyCode)) {

long start = -1; long end = -1;
start = mChartDate.minusMonths(3).dayOfMonth().withMinimumValue().millisOfDay().withMinimumValue().toDate().getTime();
end = mChartDate.dayOfMonth().withMaximumValue().millisOfDay().withMaximumValue().toDate().getTime();

long start = new LocalDate().minusMonths(2).dayOfMonth().withMinimumValue().toDate().getTime();
long end = new LocalDate().plusDays(1).toDate().getTime();
double balance = mAccountsDbAdapter.getAccountsBalance(Collections.singletonList(account.getUID()), start, end).absolute().asDouble();
if (balance != 0) {
dataSet.addEntry(new Entry((float) balance, dataSet.getEntryCount()));
Expand Down Expand Up @@ -255,8 +253,7 @@ public void setButtonTint(Button button, ColorStateList tint) {

private void loadFragment(Fragment fragment){
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(null);
Expand Down
Loading

0 comments on commit 8a7bbf5

Please sign in to comment.