diff --git a/app/src/main/java/org/gnucash/android/ui/report/ReportsActivity.java b/app/src/main/java/org/gnucash/android/ui/report/ReportsActivity.java index 3c4f6daeb..99613f9ed 100644 --- a/app/src/main/java/org/gnucash/android/ui/report/ReportsActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/report/ReportsActivity.java @@ -48,8 +48,8 @@ import org.gnucash.android.ui.common.Refreshable; import org.gnucash.android.ui.util.dialog.DateRangePickerDialogFragment; import org.joda.time.LocalDate; -import org.joda.time.LocalDateTime; +import java.util.Date; import java.util.List; import timber.log.Timber; @@ -67,7 +67,9 @@ public class ReportsActivity extends BaseDrawerActivity implements AdapterView.O DatePickerDialog.OnDateSetListener, DateRangePickerDialogFragment.OnDateRangeSetListener, Refreshable { - private static final String STATE_REPORT_TYPE = "STATE_REPORT_TYPE"; + private static final String STATE_REPORT_TYPE = "report_type"; + private static final String STATE_REPORT_START = "report_start"; + private static final String STATE_REPORT_END = "report_end"; private TransactionsDbAdapter mTransactionsDbAdapter; private AccountType mAccountType = AccountType.EXPENSE; @@ -168,6 +170,8 @@ public void onNothingSelected(AdapterView adapterView) { } else { mReportType = (ReportType) savedInstanceState.getSerializable(STATE_REPORT_TYPE); } + mReportPeriodStart = LocalDate.fromDateFields(new Date(savedInstanceState.getLong(STATE_REPORT_START))); + mReportPeriodEnd = LocalDate.fromDateFields(new Date(savedInstanceState.getLong(STATE_REPORT_END))); } } @@ -338,17 +342,17 @@ public void onItemSelected(AdapterView parent, View view, int position, long case 1: // last 3 months. x-2, x-1, x mReportPeriodStart = now.minusMonths(2).dayOfMonth().withMinimumValue(); break; - case 2: + case 2: // last 6 months mReportPeriodStart = now.minusMonths(5).dayOfMonth().withMinimumValue(); break; - case 3: + case 3: // last year mReportPeriodStart = now.minusMonths(11).dayOfMonth().withMinimumValue(); break; case 4: //ALL TIME mReportPeriodStart = new LocalDate(-1L); mReportPeriodEnd = new LocalDate(-1L); break; - case 5: + case 5: // custom range String currencyCode = GnuCashApplication.getDefaultCurrencyCode(); long earliest = mTransactionsDbAdapter.getTimestampOfEarliestTransaction(mAccountType, currencyCode); long latest = mTransactionsDbAdapter.getTimestampOfLatestTransaction(mAccountType, currencyCode); @@ -429,5 +433,7 @@ protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putSerializable(STATE_REPORT_TYPE, mReportType); + outState.putLong(STATE_REPORT_START, mReportPeriodStart.toDate().getTime()); + outState.putLong(STATE_REPORT_END, mReportPeriodEnd.toDate().getTime()); } } diff --git a/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java b/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java index aa2581de8..b35235b87 100644 --- a/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java @@ -49,7 +49,6 @@ import org.gnucash.android.model.AccountType; import org.gnucash.android.model.Money; import org.gnucash.android.ui.report.piechart.PieChartFragment; -import org.joda.time.LocalDate; import org.joda.time.LocalDateTime; import java.util.ArrayList; @@ -120,7 +119,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) { legend.setTextSize(LEGEND_TEXT_SIZE); legend.setTextColor(textColorPrimary); - final Context context = requireContext(); + final Context context = mBinding.btnPieChart.getContext(); ColorStateList csl = new ColorStateList(new int[][]{StateSet.WILD_CARD}, new int[]{ContextCompat.getColor(context, R.color.account_green)}); setButtonTint(mBinding.btnPieChart, csl); csl = new ColorStateList(new int[][]{StateSet.WILD_CARD}, new int[]{ContextCompat.getColor(context, R.color.account_red)}); @@ -138,7 +137,8 @@ public void onPrepareOptionsMenu(@NonNull Menu menu) { @Override protected void generateReport() { - PieData pieData = PieChartFragment.groupSmallerSlices(getData(), requireContext()); + final Context context = mBinding.pieChart.getContext(); + PieData pieData = PieChartFragment.groupSmallerSlices(getData(), context); if (pieData.getYValCount() != 0) { mBinding.pieChart.setData(pieData); float sum = mBinding.pieChart.getData().getYValueSum(); diff --git a/app/src/main/java/org/gnucash/android/ui/report/barchart/StackedBarChartFragment.java b/app/src/main/java/org/gnucash/android/ui/report/barchart/StackedBarChartFragment.java index 564aa0426..23832e199 100644 --- a/app/src/main/java/org/gnucash/android/ui/report/barchart/StackedBarChartFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/report/barchart/StackedBarChartFragment.java @@ -239,11 +239,11 @@ private BarData getEmptyData() { * @return the start data */ private LocalDate getStartDate(AccountType accountType) { - TransactionsDbAdapter adapter = TransactionsDbAdapter.getInstance(); - String code = mCommodity.getCurrencyCode(); LocalDate startDate; if (mReportPeriodStart == -1) { - startDate = new LocalDate(adapter.getTimestampOfEarliestTransaction(accountType, code)); + TransactionsDbAdapter adapter = TransactionsDbAdapter.getInstance(); + String currencyCode = mCommodity.getCurrencyCode(); + startDate = new LocalDate(adapter.getTimestampOfEarliestTransaction(accountType, currencyCode)); } else { startDate = new LocalDate(mReportPeriodStart); } @@ -259,11 +259,11 @@ private LocalDate getStartDate(AccountType accountType) { * @return the end data */ private LocalDate getEndDate(AccountType accountType) { - TransactionsDbAdapter adapter = TransactionsDbAdapter.getInstance(); - String code = mCommodity.getCurrencyCode(); LocalDate endDate; if (mReportPeriodEnd == -1) { - endDate = new LocalDate(adapter.getTimestampOfLatestTransaction(accountType, code)); + TransactionsDbAdapter adapter = TransactionsDbAdapter.getInstance(); + String currencyCode = mCommodity.getCurrencyCode(); + endDate = new LocalDate(adapter.getTimestampOfLatestTransaction(accountType, currencyCode)); } else { endDate = new LocalDate(mReportPeriodEnd); } diff --git a/app/src/main/java/org/gnucash/android/ui/report/linechart/CashFlowLineChartFragment.java b/app/src/main/java/org/gnucash/android/ui/report/linechart/CashFlowLineChartFragment.java index 625c60d0f..d1c24056e 100644 --- a/app/src/main/java/org/gnucash/android/ui/report/linechart/CashFlowLineChartFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/report/linechart/CashFlowLineChartFragment.java @@ -19,7 +19,6 @@ import static org.gnucash.android.util.ColorExtKt.parseColor; -import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; @@ -41,7 +40,6 @@ import org.gnucash.android.R; import org.gnucash.android.databinding.FragmentLineChartBinding; -import org.gnucash.android.db.adapter.AccountsDbAdapter; import org.gnucash.android.db.adapter.TransactionsDbAdapter; import org.gnucash.android.model.Account; import org.gnucash.android.model.AccountType; @@ -52,10 +50,8 @@ import org.joda.time.LocalDateTime; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; @@ -73,12 +69,12 @@ public class CashFlowLineChartFragment extends BaseReportFragment { private static final int ANIMATION_DURATION = 3000; private static final int NO_DATA_BAR_COUNTS = 5; private static final int[] LINE_COLORS = { - parseColor("#68F1AF"), parseColor("#cc1f09"), parseColor("#EE8600"), - parseColor("#1469EB"), parseColor("#B304AD"), + parseColor("#68F1AF"), parseColor("#cc1f09"), parseColor("#EE8600"), + parseColor("#1469EB"), parseColor("#B304AD"), }; private static final int[] FILL_COLORS = { - parseColor("#008000"), parseColor("#FF0000"), parseColor("#BE6B00"), - parseColor("#0065FF"), parseColor("#8F038A"), + parseColor("#008000"), parseColor("#FF0000"), parseColor("#BE6B00"), + parseColor("#0065FF"), parseColor("#8F038A"), }; private Map mEarliestTimestampsMap = new HashMap<>(); @@ -86,9 +82,15 @@ public class CashFlowLineChartFragment extends BaseReportFragment { private long mEarliestTransactionTimestamp; private long mLatestTransactionTimestamp; private boolean mChartDataPresent = true; + private final List accountTypes = new ArrayList<>(2); private FragmentLineChartBinding mBinding; + public CashFlowLineChartFragment() { + accountTypes.add(AccountType.INCOME); + accountTypes.add(AccountType.EXPENSE); + } + @Override public View inflateView(LayoutInflater inflater, ViewGroup container) { mBinding = FragmentLineChartBinding.inflate(inflater, container, false); @@ -127,8 +129,9 @@ public ReportType getReportType() { * @param accountTypeList account's types which will be displayed * @return a {@code LineData} instance that represents a user data */ + @NonNull private LineData getData(List accountTypeList) { - Timber.i("getData"); + Timber.i("getData for %s", accountTypeList); calculateEarliestAndLatestTimestamps(accountTypeList); // LocalDateTime? LocalDate startDate; @@ -148,7 +151,7 @@ private LineData getData(List accountTypeList) { switch (mGroupInterval) { case MONTH: xValues.add(startDate.toString(X_AXIS_PATTERN)); - Timber.d("X-axis " + startDate.toString("MM yy")); + Timber.d("X-axis %s", startDate.toString("MM yy")); startDate = startDate.plusMonths(1); break; case QUARTER: @@ -159,10 +162,9 @@ private LineData getData(List accountTypeList) { break; case YEAR: xValues.add(startDate.toString("yyyy")); - Timber.d("X-axis " + startDate.toString("yyyy")); + Timber.d("X-axis %s", startDate.toString("yyyy")); startDate = startDate.plusYears(1); break; -// default: } } @@ -219,8 +221,8 @@ private List getEntryList(AccountType accountType) { List accountUIDList = new ArrayList<>(); for (Account account : mAccountsDbAdapter.getSimpleAccountList()) { if (account.getAccountType() == accountType - && !account.isPlaceholderAccount() - && account.getCommodity().equals(mCommodity)) { + && !account.isPlaceholderAccount() + && account.getCommodity().equals(mCommodity)) { accountUIDList.add(account.getUID()); } } @@ -275,25 +277,25 @@ private List getEntryList(AccountType accountType) { /** * Calculates the earliest and latest transaction's timestamps of the specified account types * - * @param accountTypeList account's types which will be processed + * @param accountTypes account's types which will be processed */ - private void calculateEarliestAndLatestTimestamps(List accountTypeList) { + private void calculateEarliestAndLatestTimestamps(List accountTypes) { if (mReportPeriodStart != -1 && mReportPeriodEnd != -1) { mEarliestTransactionTimestamp = mReportPeriodStart; mLatestTransactionTimestamp = mReportPeriodEnd; return; } + mEarliestTimestampsMap.clear(); + mLatestTimestampsMap.clear(); TransactionsDbAdapter dbAdapter = TransactionsDbAdapter.getInstance(); - for (Iterator iter = accountTypeList.iterator(); iter.hasNext(); ) { - AccountType type = iter.next(); - long earliest = dbAdapter.getTimestampOfEarliestTransaction(type, mCommodity.getCurrencyCode()); - long latest = dbAdapter.getTimestampOfLatestTransaction(type, mCommodity.getCurrencyCode()); + final String currencyCode = mCommodity.getCurrencyCode(); + for (AccountType type : accountTypes) { + long earliest = dbAdapter.getTimestampOfEarliestTransaction(type, currencyCode); + long latest = dbAdapter.getTimestampOfLatestTransaction(type, currencyCode); if (earliest > 0 && latest > 0) { mEarliestTimestampsMap.put(type, earliest); mLatestTimestampsMap.put(type, latest); - } else { - iter.remove(); } } @@ -315,13 +317,9 @@ public boolean requiresAccountTypeOptions() { @Override protected void generateReport() { - LineData lineData = getData(new ArrayList<>(Arrays.asList(AccountType.INCOME, AccountType.EXPENSE))); - if (lineData != null) { - mBinding.lineChart.setData(lineData); - mChartDataPresent = true; - } else { - mChartDataPresent = false; - } + LineData lineData = getData(accountTypes); + mBinding.lineChart.setData(lineData); + mChartDataPresent = true; } @Override @@ -343,7 +341,7 @@ public void onTimeRangeUpdated(long start, long end) { if (mReportPeriodStart != start || mReportPeriodEnd != end) { mReportPeriodStart = start; mReportPeriodEnd = end; - mBinding.lineChart.setData(getData(new ArrayList<>(Arrays.asList(AccountType.INCOME, AccountType.EXPENSE)))); + mBinding.lineChart.setData(getData(accountTypes)); mBinding.lineChart.invalidate(); } } @@ -352,7 +350,7 @@ public void onTimeRangeUpdated(long start, long end) { public void onGroupingUpdated(GroupInterval groupInterval) { if (mGroupInterval != groupInterval) { mGroupInterval = groupInterval; - mBinding.lineChart.setData(getData(new ArrayList<>(Arrays.asList(AccountType.INCOME, AccountType.EXPENSE)))); + mBinding.lineChart.setData(getData(accountTypes)); mBinding.lineChart.invalidate(); } }