Skip to content

Commit

Permalink
chore: rename .money => .formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
sadespresso committed Nov 23, 2024
1 parent cc66565 commit 7bf981f
Show file tree
Hide file tree
Showing 16 changed files with 477 additions and 244 deletions.
67 changes: 67 additions & 0 deletions lib/data/money.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "dart:developer";

import "package:flow/data/currencies.dart";
import "package:flow/data/exchange_rates.dart";
import "package:intl/intl.dart";

class Money {
final double amount;
Expand Down Expand Up @@ -138,6 +139,72 @@ class Money {
toString() {
return "Money($currency $amount)";
}

String formatMoney({
bool includeCurrency = true,
bool useCurrencySymbol = true,
bool compact = false,
bool takeAbsoluteValue = false,
int? decimalDigits,
}) {
final num amountToFormat = takeAbsoluteValue ? amount.abs() : amount;
final String currencyToFormat = !includeCurrency ? "" : currency;
useCurrencySymbol = useCurrencySymbol && includeCurrency;

final String? symbol = useCurrencySymbol
? NumberFormat.simpleCurrency(
locale: Intl.defaultLocale,
name: currencyToFormat,
).currencySymbol
: null;

if (compact) {
return NumberFormat.compactCurrency(
locale: Intl.defaultLocale,
name: currencyToFormat,
symbol: symbol,
decimalDigits: decimalDigits,
).format(amountToFormat);
}
return NumberFormat.currency(
locale: Intl.defaultLocale,
name: currencyToFormat,
symbol: symbol,
decimalDigits: decimalDigits,
).format(amountToFormat);
}

/// Returns money-formatted string in the primary currency
/// in the default locale
///
/// e.g., $420.69
String get formatted => formatMoney();

/// Returns compact money-formatted string in the primary
/// currency in the default locale
///
/// e.g., $1.2M
String get formattedCompact => formatMoney(compact: true);

/// Returns money-formatted string (in the default locale)
///
/// e.g., 467,000
String get formattedNoMarker => formatMoney(includeCurrency: false);

/// Returns money-formatted string (in the default locale)
///
/// e.g., 1.2M
String get formattedNoMarkerCompact => formatMoney(
includeCurrency: false,
compact: true,
);

String toSemanticLabel() {
final String currencyName =
iso4217CurrenciesGrouped[currency]?.name ?? currency;

return "$formattedNoMarker $currencyName";
}
}

class MoneyException implements Exception {
Expand Down
63 changes: 0 additions & 63 deletions lib/l10n/extensions.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import "package:flow/data/money.dart";
import "package:flow/l10n/flow_localizations.dart";
import "package:flutter/widgets.dart";
import "package:intl/intl.dart";

extension L10nHelper on BuildContext {
FlowLocalizations get l => FlowLocalizations.of(this);
Expand Down Expand Up @@ -44,64 +42,3 @@ extension L10nStringHelper on String {
String tr([dynamic replace]) =>
FlowLocalizations.getTransalation(this, replace: replace);
}

extension MoneyFormatters on Money {
String formatMoney({
bool includeCurrency = true,
bool useCurrencySymbol = true,
bool compact = false,
bool takeAbsoluteValue = false,
int? decimalDigits,
}) {
final num amountToFormat = takeAbsoluteValue ? amount.abs() : amount;
final String currencyToFormat = !includeCurrency ? "" : currency;
useCurrencySymbol = useCurrencySymbol && includeCurrency;

final String? symbol = useCurrencySymbol
? NumberFormat.simpleCurrency(
locale: Intl.defaultLocale,
name: currencyToFormat,
).currencySymbol
: null;

if (compact) {
return NumberFormat.compactCurrency(
locale: Intl.defaultLocale,
name: currencyToFormat,
symbol: symbol,
decimalDigits: decimalDigits,
).format(amountToFormat);
}
return NumberFormat.currency(
locale: Intl.defaultLocale,
name: currencyToFormat,
symbol: symbol,
decimalDigits: decimalDigits,
).format(amountToFormat);
}

/// Returns money-formatted string in the primary currency
/// in the default locale
///
/// e.g., $420.69
String get money => formatMoney();

/// Returns compact money-formatted string in the primary
/// currency in the default locale
///
/// e.g., $1.2M
String get moneyCompact => formatMoney(compact: true);

/// Returns money-formatted string (in the default locale)
///
/// e.g., 467,000
String get moneyNoMarker => formatMoney(includeCurrency: false);

/// Returns money-formatted string (in the default locale)
///
/// e.g., 1.2M
String get moneyNoMarkerCompact => formatMoney(
includeCurrency: false,
compact: true,
);
}
3 changes: 2 additions & 1 deletion lib/routes/account_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,11 @@ class _AccountPageState extends State<AccountPage> {
listPadding: widget.listPadding,
headerPadding: widget.headerPadding,
firstHeaderTopPadding: firstHeaderTopPadding,
headerBuilder: (range, rangeTransactions) =>
headerBuilder: (pendingGroup, range, rangeTransactions) =>
TransactionListDateHeader(
transactions: rangeTransactions,
date: range.from,
pendingGroup: pendingGroup,
),
)
},
Expand Down
3 changes: 2 additions & 1 deletion lib/routes/category_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,11 @@ class _CategoryPageState extends State<CategoryPage> {
listPadding: widget.listPadding,
headerPadding: widget.headerPadding,
firstHeaderTopPadding: firstHeaderTopPadding,
headerBuilder: (range, rangeTransactions) =>
headerBuilder: (pendingGroup, range, rangeTransactions) =>
TransactionListDateHeader(
transactions: rangeTransactions,
date: range.from,
pendingGroup: pendingGroup,
),
)
},
Expand Down
7 changes: 4 additions & 3 deletions lib/routes/home/home_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,14 @@ class _HomeTabState extends State<HomeTab> with AutomaticKeepAliveClientMixin {
bottom: 80.0,
),
headerBuilder: (
TimeRange range,
List<Transaction> transactions,
pendingGroup,
range,
transactions,
) =>
TransactionListDateHeader(
transactions: transactions,
date: range.from,
future: !range.from.isPast,
pendingGroup: pendingGroup == true,
),
);
}
Expand Down
29 changes: 23 additions & 6 deletions lib/routes/transactions_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "package:flow/objectbox.dart";
import "package:flow/objectbox/actions.dart";
import "package:flow/objectbox/objectbox.g.dart";
import "package:flow/widgets/general/spinner.dart";
import "package:flow/widgets/general/wavy_divider.dart";
import "package:flow/widgets/grouped_transaction_list.dart";
import "package:flow/widgets/home/transactions_date_header.dart";
import "package:flutter/material.dart";
Expand Down Expand Up @@ -91,25 +92,41 @@ class _TransactionsPageState extends State<TransactionsPage> {
title: widget.title == null ? null : Text(widget.title!),
),
body: SafeArea(
child: StreamBuilder<Map<TimeRange, List<Transaction>>>(
child: StreamBuilder<List<Transaction>>(
stream: widget.query
.watch(triggerImmediately: true)
.map((event) => event.find().groupByDate()),
.map((event) => event.find()),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Spinner.center();
}

final Map<TimeRange, List<Transaction>> grouped =
snapshot.requireData;
final DateTime now = DateTime.now().startOfNextMinute();

final Map<TimeRange, List<Transaction>> transactions = snapshot
.requireData
.where((transaction) =>
!transaction.transactionDate.isAfter(now) &&
transaction.isPending != true)
.groupByDate();
final Map<TimeRange, List<Transaction>> pendingTransactions =
snapshot
.requireData
.where((transaction) =>
transaction.transactionDate.isAfter(now) ||
transaction.isPending == true)
.groupByDate();

return GroupedTransactionList(
transactions: grouped,
headerBuilder: (range, transactions) =>
transactions: transactions,
pendingTransactions: pendingTransactions,
headerBuilder: (pendingGroup, range, transactions) =>
TransactionListDateHeader(
pendingGroup: pendingGroup,
transactions: transactions,
date: range.from,
),
pendingDivider: WavyDivider(),
header: widget.header,
);
},
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/category/transactions_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TransactionsInfo extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
flow.money,
flow.formatted,
style: context.textTheme.displaySmall,
),
Text(
Expand Down
3 changes: 1 addition & 2 deletions lib/widgets/category_card.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "package:flow/data/money.dart";
import "package:flow/entity/category.dart";
import "package:flow/l10n/extensions.dart";
import "package:flow/objectbox/actions.dart";
import "package:flow/prefs.dart";
import "package:flow/theme/theme.dart";
Expand Down Expand Up @@ -61,7 +60,7 @@ class CategoryCard extends StatelessWidget {
Text(
Money(category.transactions.sumWithoutCurrency,
primaryCurrency)
.money,
.formatted,
style: context.textTheme.bodyMedium?.semi(context),
),
],
Expand Down
3 changes: 1 addition & 2 deletions lib/widgets/flow_card.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "package:auto_size_text/auto_size_text.dart";
import "package:flow/data/money.dart";
import "package:flow/entity/transaction.dart";
import "package:flow/l10n/extensions.dart";
import "package:flow/theme/theme.dart";
import "package:flow/widgets/general/surface.dart";
import "package:flutter/material.dart";
Expand Down Expand Up @@ -32,7 +31,7 @@ class FlowCard extends StatelessWidget {
alignment: Alignment.centerLeft,
padding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 16.0),
child: AutoSizeText(
flow.abs().money,
flow.abs().formatted,
style: context.textTheme.displaySmall?.copyWith(
color: type.color(context),
),
Expand Down
Loading

0 comments on commit 7bf981f

Please sign in to comment.