fix: show current balance on homepage

This commit is contained in:
Matyáš Caras 2024-01-22 15:15:10 +01:00
parent 96e672f1d5
commit 91fc0bb607
Signed by untrusted user who does not match committer: hernik
GPG key ID: 2A3175F98820C5C6
5 changed files with 285 additions and 163 deletions

View file

@ -160,6 +160,28 @@ class Wallet {
await WalletManager.saveWallet(this);
}
/// Returns the current balance
///
/// Basically just takes *starterBalance* and adds all the entries to it
double calculateCurrentBalance() {
var toAdd = 0.0;
for (final e in entries) {
toAdd += (e.type == EntryType.income) ? e.data.amount : -e.data.amount;
}
return starterBalance + toAdd;
}
/// Returns the amount that was made/lost during a month
double calculateMonthStatus(int month, int year) {
var f = 0.0;
for (final e in entries.where(
(element) => element.date.year == year && element.date.month == month,
)) {
f += (e.type == EntryType.income) ? e.data.amount : -e.data.amount;
}
return f;
}
/// Empty wallet used for placeholders
static final Wallet empty = Wallet(
name: "Empty",

View file

@ -87,5 +87,8 @@
"dayCounter":"{count, plural, =1{den} few{dny} many{dnů} other{dnů} }",
"yearCounter":"{count, plural, =1{rok} few{rok} many{let} other{let} }",
"recurEvery":"{count, plural, =1{Opakovat každý} few{Opakovat každé} many{Opakovat každých} other{Opakovat každých}}",
"startingWithDate": "počínaje datem"
"startingWithDate": "počínaje datem",
"evenMoney":"Váš stav je stejný jako minulý měsíc.",
"balanceStatusA": "Váš stav je ",
"balanceStatusB": " oproti minulému měsíci."
}

View file

@ -203,5 +203,8 @@
"startingWithDate": "starting",
"@startingWithDate":{
"description": "Shown after 'Recur every X Y', e.g. 'Recur every 2 month starting 20th June 2023'"
}
},
"evenMoney":"You're on the same balance as last month.",
"balanceStatusA": "Your balance is ",
"balanceStatusB": " compared to last month."
}

View file

@ -150,10 +150,10 @@ class ExpensesLineChart extends StatelessWidget {
isStrokeCapRound: true,
dotData: const FlDotData(show: false),
belowBarData: BarAreaData(),
color:
(MediaQuery.of(context).platformBrightness == Brightness.dark)
color: ((MediaQuery.of(context).platformBrightness ==
Brightness.dark)
? Colors.green.shade300
: Colors.green
: Colors.green)
.harmonizeWith(Theme.of(context).colorScheme.primary),
spots: List.generate(
yearly ? 12 : date.lastDay,
@ -167,10 +167,10 @@ class ExpensesLineChart extends StatelessWidget {
isStrokeCapRound: true,
dotData: const FlDotData(show: false),
belowBarData: BarAreaData(),
color:
(MediaQuery.of(context).platformBrightness == Brightness.dark)
color: ((MediaQuery.of(context).platformBrightness ==
Brightness.dark)
? Colors.red.shade300
: Colors.red
: Colors.red)
.harmonizeWith(Theme.of(context).colorScheme.primary),
spots: List.generate(
yearly

View file

@ -222,7 +222,86 @@ class _HomeViewState extends State<HomeView> {
),
],
)
: GroupedListView(
: Column(
children: [
Text(
NumberFormat.compactCurrency(
locale: locale,
symbol: selectedWallet!.currency.symbol,
).format(selectedWallet!.calculateCurrentBalance()),
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 22,
),
),
const SizedBox(
height: 5,
),
if (selectedWallet!.calculateMonthStatus(
DateTime.now().month,
DateTime.now().year,
) ==
0)
Text(AppLocalizations.of(context).evenMoney)
else
RichText(
text: TextSpan(
children: [
TextSpan(
text: AppLocalizations.of(context)
.balanceStatusA,
),
TextSpan(
style: TextStyle(
color: (selectedWallet!
.calculateMonthStatus(
DateTime.now().month,
DateTime.now().year,
) >
0
? ((MediaQuery.of(context)
.platformBrightness ==
Brightness.dark)
? Colors.green.shade300
: Colors.green)
.harmonizeWith(
Theme.of(context)
.colorScheme
.primary,
)
: ((MediaQuery.of(context)
.platformBrightness ==
Brightness.dark)
? Colors.red.shade300
: Colors.red)
.harmonizeWith(
Theme.of(context)
.colorScheme
.primary,
)),
),
text: NumberFormat.compactCurrency(
locale: locale,
symbol: selectedWallet!.currency.symbol,
).format(
selectedWallet!.calculateMonthStatus(
DateTime.now().month,
DateTime.now().year,
),
),
),
TextSpan(
text: AppLocalizations.of(context)
.balanceStatusB,
),
],
),
),
const SizedBox(
height: 10,
),
Expanded(
child: GroupedListView(
groupHeaderBuilder: (element) => Text(
DateFormat.yMMMM(locale).format(element.date),
style: TextStyle(
@ -231,7 +310,8 @@ class _HomeViewState extends State<HomeView> {
),
elements: selectedWallet!.entries,
itemComparator: (a, b) => b.date.compareTo(a.date),
groupBy: (e) => DateFormat.yMMMM(locale).format(e.date),
groupBy: (e) =>
DateFormat.yMMMM(locale).format(e.date),
groupComparator: (a, b) {
// TODO: better sorting algorithm lol
final yearA = RegExp(r'\d+').firstMatch(a);
@ -273,17 +353,22 @@ class _HomeViewState extends State<HomeView> {
.then(
(editedEntry) {
if (editedEntry == null) return;
selectedWallet!.entries.remove(element);
selectedWallet!.entries.add(editedEntry);
WalletManager.saveWallet(selectedWallet!);
selectedWallet!.entries
.remove(element);
selectedWallet!.entries
.add(editedEntry);
WalletManager.saveWallet(
selectedWallet!,
);
setState(() {});
},
);
},
backgroundColor:
Theme.of(context).colorScheme.secondary,
foregroundColor:
Theme.of(context).colorScheme.onSecondary,
foregroundColor: Theme.of(context)
.colorScheme
.onSecondary,
icon: Icons.edit,
),
SlidableAction(
@ -296,16 +381,19 @@ class _HomeViewState extends State<HomeView> {
showDialog(
context: context,
builder: (cx) => PlatformDialog(
title:
AppLocalizations.of(context).sureDialog,
title: AppLocalizations.of(context)
.sureDialog,
content: Text(
AppLocalizations.of(context).deleteSure,
AppLocalizations.of(context)
.deleteSure,
),
actions: [
PlatformButton(
text: AppLocalizations.of(context).yes,
text: AppLocalizations.of(context)
.yes,
onPressed: () {
selectedWallet?.entries.removeWhere(
selectedWallet?.entries
.removeWhere(
(e) => e.id == element.id,
);
@ -317,7 +405,8 @@ class _HomeViewState extends State<HomeView> {
},
),
PlatformButton(
text: AppLocalizations.of(context).no,
text: AppLocalizations.of(context)
.no,
onPressed: () {
Navigator.of(cx).pop();
},
@ -339,8 +428,8 @@ class _HomeViewState extends State<HomeView> {
padding: const EdgeInsets.all(8),
child: Icon(
element.category.icon,
color:
element.category.color.calculateTextColor(),
color: element.category.color
.calculateTextColor(),
),
),
),
@ -350,10 +439,12 @@ class _HomeViewState extends State<HomeView> {
children: [
TextSpan(
text: NumberFormat.currency(
symbol: selectedWallet!.currency.symbol,
symbol:
selectedWallet!.currency.symbol,
).format(element.data.amount),
style: TextStyle(
color: (element.type == EntryType.income)
color: (element.type ==
EntryType.income)
? (MediaQuery.of(context)
.platformBrightness ==
Brightness.dark)
@ -391,6 +482,9 @@ class _HomeViewState extends State<HomeView> {
),
),
),
],
),
),
),
);
}