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

New UI for the transaction form page, allowing inversed/reversed transactions to track refunds and debts #186

Merged
merged 25 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift click to select a range
6406421
feat(ux/ui): Change the transaction form UX
enrique-lozano Jul 22, 2024
83ca3ce
refactor: Rename `TransactionType` enum
enrique-lozano Jul 22, 2024
148ab42
fix: Reduce currency font size also on negative amounts
enrique-lozano Jul 22, 2024
61b9e91
feat: Add tr. type to the transactions table in DB
enrique-lozano Jul 23, 2024
c10f523
fix: Adjust graphs to accept reversed transactions
enrique-lozano Jul 23, 2024
c246e89
fix: Regenerate database queries
enrique-lozano Jul 23, 2024
2d97d00
feat: Add indicator for reversed transactions
enrique-lozano Jul 23, 2024
d7dfb3f
Merge branch 'develop' into feat/allow-positive-expenses
enrique-lozano Jul 24, 2024
1d06fbc
fix: Sign when changing the transaction type
enrique-lozano Jul 24, 2024
4cfb56e
feat: Disable the status selector on future transactions
enrique-lozano Jul 24, 2024
6cd6ea7
fix: Transfer form validations
enrique-lozano Jul 24, 2024
d17b3a4
feat: Hide negative toggle button on transfers
enrique-lozano Jul 24, 2024
9f1ccdf
feat(literals): Change note textarea placeholder
enrique-lozano Jul 24, 2024
4d33135
fix: Use account currency
enrique-lozano Jul 24, 2024
a0b9afc
fix: Prevent transaction form to resize on keyboard
enrique-lozano Jul 24, 2024
4af5c46
fix: Status selector scrolling issues
enrique-lozano Jul 24, 2024
a610d8e
Merge branch 'develop' into feat/allow-positive-expenses
enrique-lozano Jul 25, 2024
0a9bf03
fix(ux/ui): Description textarea color
enrique-lozano Jul 25, 2024
6134d1c
Merge branch 'develop' into feat/allow-positive-expenses
enrique-lozano Aug 1, 2024
8120682
fix: Category picker only displaying multi-type items
enrique-lozano Aug 1, 2024
db12db6
Merge branch 'develop' into feat/allow-positive-expenses
enrique-lozano Aug 2, 2024
1396732
feat: Improve tag selection in the transaction form
enrique-lozano Aug 2, 2024
9f91ef1
feat: Reversed transaction descriptions and indicators
enrique-lozano Aug 2, 2024
05f4d8e
feat: Improve brand blue color on dark mode
enrique-lozano Aug 3, 2024
6b09675
feat: Value in destiny modal
enrique-lozano Aug 3, 2024
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
Prev Previous commit
Next Next commit
fix: Adjust graphs to accept reversed transactions
  • Loading branch information
enrique-lozano committed Jul 23, 2024
commit c10f5233c028d7a6d0be906a9c0407fbb1e2ce55
33 changes: 21 additions & 12 deletions lib/app/stats/widgets/balance_bar_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 7,7 @@ import 'package:intl/intl.dart';
import 'package:monekin/core/database/services/account/account_service.dart';
import 'package:monekin/core/database/services/currency/currency_service.dart';
import 'package:monekin/core/extensions/color.extensions.dart';
import 'package:monekin/core/extensions/lists.extensions.dart';
import 'package:monekin/core/models/date-utils/date_period.dart';
import 'package:monekin/core/models/date-utils/date_period_state.dart';
import 'package:monekin/core/models/date-utils/period_type.dart';
Expand Down Expand Up @@ -77,7 78,9 @@ class _BalanceBarChartState extends State<BalanceBarChart> {
await accountService
.getAccountsBalance(
filters: widget.filters.copyWith(
transactionTypes: [TransactionType.I],
transactionTypes: [TransactionType.I]
.intersectionWithNullable(widget.filters.transactionTypes)
.toList(),
minDate: startDate,
maxDate: endDate,
),
Expand All @@ -88,7 91,9 @@ class _BalanceBarChartState extends State<BalanceBarChart> {
await accountService
.getAccountsBalance(
filters: widget.filters.copyWith(
transactionTypes: [TransactionType.E],
transactionTypes: [TransactionType.E]
.intersectionWithNullable(widget.filters.transactionTypes)
.toList(),
minDate: startDate,
maxDate: endDate,
),
Expand Down Expand Up @@ -220,6 225,16 @@ class _BalanceBarChartState extends State<BalanceBarChart> {
);
}

BorderRadius getBarRadius({required double radius, bool isNegative = false}) {
Radius circularRadius = Radius.circular(radius);

return BorderRadius.only(
topLeft: isNegative ? Radius.zero : circularRadius,
topRight: isNegative ? Radius.zero : circularRadius,
bottomLeft: isNegative ? circularRadius : Radius.zero,
bottomRight: isNegative ? circularRadius : Radius.zero);
}

BarChartGroupData makeGroupData(
int x,
double income,
Expand All @@ -229,8 244,6 @@ class _BalanceBarChartState extends State<BalanceBarChart> {
}) {
bool isTouched = touchedBarGroupIndex == x;

Radius radius = Radius.circular(width / 6);

return BarChartGroupData(
x: x,
barRods: [
Expand All @@ -240,21 253,17 @@ class _BalanceBarChartState extends State<BalanceBarChart> {
? AppColors.of(context).success.lighten(0.2)
: AppColors.of(context).success,
width: width * (isTouched ? 1.2 : 1),
borderRadius: BorderRadius.only(
topLeft: radius,
topRight: radius,
),
borderRadius:
getBarRadius(radius: width / 6, isNegative: income.isNegative),
),
BarChartRodData(
toY: -expense,
color: isTouched
? AppColors.of(context).danger.lighten(0.2)
: AppColors.of(context).danger,
width: width * (isTouched ? 1.2 : 1),
borderRadius: BorderRadius.only(
topLeft: radius,
topRight: radius,
),
borderRadius: getBarRadius(
radius: width / 6, isNegative: (-expense).isNegative),
)
],
showingTooltipIndicators: showTooltips,
Expand Down
15 changes: 11 additions & 4 deletions lib/app/stats/widgets/income_expense_comparason.dart
Original file line number Diff line number Diff line change
@@ -1,5 1,8 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:monekin/core/database/services/account/account_service.dart';
import 'package:monekin/core/extensions/lists.extensions.dart';
import 'package:monekin/core/presentation/widgets/animated_progress_bar.dart';
import 'package:monekin/core/presentation/widgets/number_ui_formatters/currency_displayer.dart';
import 'package:monekin/core/presentation/widgets/skeleton.dart';
Expand Down Expand Up @@ -61,14 64,18 @@ class IncomeExpenseComparason extends StatelessWidget {
stream: Rx.combineLatest2(
AccountService.instance.getAccountsBalance(
filters: filters.copyWith(
transactionTypes: [TransactionType.I],
transactionTypes: [TransactionType.I]
.intersectionWithNullable(filters.transactionTypes)
.toList(),
minDate: startDate,
maxDate: endDate,
),
),
AccountService.instance.getAccountsBalance(
filters: filters.copyWith(
transactionTypes: [TransactionType.E],
transactionTypes: [TransactionType.E]
.intersectionWithNullable(filters.transactionTypes)
.toList(),
minDate: startDate,
maxDate: endDate,
),
Expand All @@ -80,7 87,7 @@ class IncomeExpenseComparason extends StatelessWidget {
}

final income = snapshot.data![0];
final expense = snapshot.data![1].abs();
final expense = snapshot.data![1] * -1;

return Column(children: [
IncomeExpenseTile(
Expand Down Expand Up @@ -141,7 148,7 @@ class IncomeExpenseTile extends StatelessWidget {
),
]),
subtitle: AnimatedProgressBar(
value: total > 0 ? (value / total) : 0,
value: total > 0 ? max(value / total, 0) : 0,
color: type.color(context),
),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 1,14 @@
import 'dart:math';

import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:monekin/app/stats/widgets/movements_distribution/chart_by_categories.dart';
import 'package:monekin/app/transactions/transactions.page.dart';
import 'package:monekin/core/database/services/exchange-rate/exchange_rate_service.dart';
import 'package:monekin/core/extensions/color.extensions.dart';
import 'package:monekin/core/models/category/category.dart';
import 'package:monekin/core/models/supported-icon/icon_displayer.dart';
import 'package:monekin/core/models/supported-icon/supported_icon.dart';
import 'package:monekin/core/models/transaction/transaction_type.enum.dart';
import 'package:monekin/core/presentation/widgets/animated_progress_bar.dart';
import 'package:monekin/core/presentation/widgets/number_ui_formatters/currency_displayer.dart';
import 'package:monekin/core/presentation/widgets/transaction_filter/transaction_filters.dart';
Expand Down Expand Up @@ -52,11 54,8 @@ class CategoryStatsModal extends StatelessWidget {
final categoryToEdit = subcategories
.firstWhereOrNull((x) => x.id == transaction.category!.id);

final trValue = await ExchangeRateService.instance
.calculateExchangeRateToPreferredCurrency(
fromCurrency: transaction.account.currencyId,
amount: transaction.value.abs())
.first;
final trValue = transaction.currentValueInPreferredCurrency *
(transaction.type == TransactionType.E ? -1 : 1);

if (categoryToEdit != null) {
categoryToEdit.value = trValue;
Expand Down Expand Up @@ -145,43 144,47 @@ class CategoryStatsModal extends StatelessWidget {
),
const Divider(),
FutureBuilder(
future: getSubcategoriesData(context),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const LinearProgressIndicator();
}

final subcategories = snapshot.data!;

return Column(
children: List.generate(subcategories.length, (index) {
final subcategoryData = subcategories[index];

return ListTile(
leading: IconDisplayer.fromCategory(
context,
category: Category.fromDB(
categoryData.category
.copyWith(iconId: subcategoryData.icon.id),
categoryData.category,
),
),
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(subcategoryData.name),
CurrencyDisplayer(
amountToConvert: subcategoryData.value)
],
future: getSubcategoriesData(context),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const LinearProgressIndicator();
}

final subcategories = snapshot.data!;

// Filter subcategories with negative values to correctly display the bars:
final totalSum =
subcategories.where((e) => e.value > 0).map((e) => e.value).sum;

return Column(
children: List.generate(subcategories.length, (index) {
final subcategoryData = subcategories[index];

return ListTile(
leading: IconDisplayer.fromCategory(
context,
category: Category.fromDB(
categoryData.category
.copyWith(iconId: subcategoryData.icon.id),
categoryData.category,
),
subtitle: AnimatedProgressBar(
value: subcategoryData.value / categoryData.value,
color: ColorHex.get(categoryData.category.color),
),
);
}),
);
})
),
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(subcategoryData.name),
CurrencyDisplayer(amountToConvert: subcategoryData.value)
],
),
subtitle: AnimatedProgressBar(
value: max(subcategoryData.value / totalSum, 0),
color: ColorHex.get(categoryData.category.color),
),
);
}),
);
},
)
],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 6,7 @@ import 'package:monekin/app/stats/widgets/movements_distribution/category_stats_
import 'package:monekin/core/database/services/category/category_service.dart';
import 'package:monekin/core/database/services/transaction/transaction_service.dart';
import 'package:monekin/core/extensions/color.extensions.dart';
import 'package:monekin/core/extensions/lists.extensions.dart';
import 'package:monekin/core/models/category/category.dart';
import 'package:monekin/core/models/date-utils/date_period_state.dart';
import 'package:monekin/core/models/supported-icon/icon_displayer.dart';
Expand All @@ -19,7 20,7 @@ import 'package:monekin/i18n/translations.g.dart';
import '../../../../core/models/transaction/transaction_type.enum.dart';

class TrDistributionChartItem<T> {
T category;
final T category;
List<MoneyTransaction> transactions;
double value;

Expand Down Expand Up @@ -58,10 59,9 @@ class _ChartByCategoriesState extends State<ChartByCategories> {
return widget.filters.copyWith(
status:
TransactionStatus.getStatusThatCountsForStats(widget.filters.status),
transactionTypes: [
if (transactionsType == TransactionType.E) TransactionType.E,
if (transactionsType == TransactionType.I) TransactionType.I
],
transactionTypes: [transactionsType]
.intersectionWithNullable(widget.filters.transactionTypes)
.toList(),
minDate: widget.datePeriodState.startDate,
maxDate: widget.datePeriodState.endDate,
);
Expand All @@ -79,7 79,8 @@ class _ChartByCategoriesState extends State<ChartByCategories> {
.first;

for (final transaction in transactions) {
final trValue = transaction.currentValueInPreferredCurrency.abs();
final trValue = transaction.currentValueInPreferredCurrency *
(transactionsType == TransactionType.E ? -1 : 1);

final categoryToEdit = data.firstWhereOrNull((cat) =>
cat.category.id == transaction.category?.id ||
Expand All @@ -91,22 92,24 @@ class _ChartByCategoriesState extends State<ChartByCategories> {
} else {
data.add(
TrDistributionChartItem(
category: transaction.category!.parentCategoryID == null
? Category.fromDB(transaction.category!, null)
: (await CategoryService.instance
.getCategoryById(transaction.category!.parentCategoryID!)
.first)!,
transactions: [transaction],
value: trValue),
category: transaction.category!.parentCategoryID == null
? Category.fromDB(transaction.category!, null)
: (await CategoryService.instance
.getCategoryById(transaction.category!.parentCategoryID!)
.first)!,
transactions: [transaction],
value: trValue,
),
);
}
}

data.sort((a, b) => b.value.compareTo(a.value));
return data;
return data
.where((element) => element.value > 0)
.sorted((a, b) => b.value.compareTo(a.value));
}

/// Returns a value between 0 and 100
/// Returns a value between 0 and 1
double getElementPercentageInTotal(
double elementValue, List<TrDistributionChartItem> items) {
return (elementValue / items.map((e) => e.value).sum);
Expand Down Expand Up @@ -208,6 211,10 @@ class _ChartByCategoriesState extends State<ChartByCategories> {
super.initState();

transactionsType = widget.initialSelectedType;

if (transactionsType.isTransfer) {
throw 'Can not draw the categories pie chart of transfers';
}
}

@override
Expand Down
4 changes: 3 additions & 1 deletion lib/app/stats/widgets/movements_distribution/tags_stats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 15,9 @@ class TagStats extends StatelessWidget {
final TransactionFilters filters;

TrDistributionChartItem<Tag> getTagInfo(
Tag tag, List<MoneyTransaction> transactions) {
Tag tag,
List<MoneyTransaction> transactions,
) {
transactions = transactions
.where((element) => element.tags.any((elTag) => elTag.id == tag.id))
.toList();
Expand Down
33 changes: 33 additions & 0 deletions lib/core/extensions/lists.extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 5,36 @@ extension RandomListItem<T> on List<T> {
return this[Random().nextInt(length)];
}
}

/// Extension to provide additional list operations such as intersection and union.
extension IntersectionExtension<T> on List<T> {
/// Returns a new list containing the intersection of this list and [other].
/// The intersection is a list of common elements present in both lists.
///
/// Example:
/// ```dart
/// List<int> list1 = [1, 2, 3];
/// List<int> list2 = [2, 3, 4];
/// List<int> result = list1.intersectionWith(list2); // [2, 3]
/// ```
Iterable<T> intersectionWith(List<T> other) {
return where((element) => other.contains(element));
}

/// Returns a new list containing the intersection of this list and [other].
/// If [other] is null, returns the original list unmodified.
///
/// Example:
/// ```dart
/// List<int> list1 = [1, 2, 3];
/// List<int>? list2 = null;
/// List<int> result = list1.intersectionWithNullable(list2); // [1, 2, 3]
/// ```
Iterable<T> intersectionWithNullable(List<T>? other) {
if (other == null) {
return this;
}

return intersectionWith(other);
}
}