Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report must remember its date range when configuration changed. #216

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)));
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)});
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -73,22 +69,28 @@ 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<AccountType, Long> mEarliestTimestampsMap = new HashMap<>();
private Map<AccountType, Long> mLatestTimestampsMap = new HashMap<>();
private long mEarliestTransactionTimestamp;
private long mLatestTransactionTimestamp;
private boolean mChartDataPresent = true;
private final List<AccountType> 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);
Expand Down Expand Up @@ -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<AccountType> accountTypeList) {
Timber.i("getData");
Timber.i("getData for %s", accountTypeList);
calculateEarliestAndLatestTimestamps(accountTypeList);
// LocalDateTime?
LocalDate startDate;
Expand All @@ -148,7 +151,7 @@ private LineData getData(List<AccountType> 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:
Expand All @@ -159,10 +162,9 @@ private LineData getData(List<AccountType> 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:
}
}

Expand Down Expand Up @@ -219,8 +221,8 @@ private List<Entry> getEntryList(AccountType accountType) {
List<String> 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());
}
}
Expand Down Expand Up @@ -275,25 +277,25 @@ private List<Entry> 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<AccountType> accountTypeList) {
private void calculateEarliestAndLatestTimestamps(List<AccountType> accountTypes) {
if (mReportPeriodStart != -1 && mReportPeriodEnd != -1) {
mEarliestTransactionTimestamp = mReportPeriodStart;
mLatestTransactionTimestamp = mReportPeriodEnd;
return;
}

mEarliestTimestampsMap.clear();
mLatestTimestampsMap.clear();
TransactionsDbAdapter dbAdapter = TransactionsDbAdapter.getInstance();
for (Iterator<AccountType> 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();
}
}

Expand All @@ -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
Expand All @@ -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();
}
}
Expand All @@ -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();
}
}
Expand Down
Loading