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); 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 /// Empty wallet used for placeholders
static final Wallet empty = Wallet( static final Wallet empty = Wallet(
name: "Empty", name: "Empty",

View file

@ -87,5 +87,8 @@
"dayCounter":"{count, plural, =1{den} few{dny} many{dnů} other{dnů} }", "dayCounter":"{count, plural, =1{den} few{dny} many{dnů} other{dnů} }",
"yearCounter":"{count, plural, =1{rok} few{rok} many{let} other{let} }", "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}}", "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": "starting",
"@startingWithDate":{ "@startingWithDate":{
"description": "Shown after 'Recur every X Y', e.g. 'Recur every 2 month starting 20th June 2023'" "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,11 +150,11 @@ class ExpensesLineChart extends StatelessWidget {
isStrokeCapRound: true, isStrokeCapRound: true,
dotData: const FlDotData(show: false), dotData: const FlDotData(show: false),
belowBarData: BarAreaData(), belowBarData: BarAreaData(),
color: color: ((MediaQuery.of(context).platformBrightness ==
(MediaQuery.of(context).platformBrightness == Brightness.dark) Brightness.dark)
? Colors.green.shade300 ? Colors.green.shade300
: Colors.green : Colors.green)
.harmonizeWith(Theme.of(context).colorScheme.primary), .harmonizeWith(Theme.of(context).colorScheme.primary),
spots: List.generate( spots: List.generate(
yearly ? 12 : date.lastDay, yearly ? 12 : date.lastDay,
(index) => FlSpot(index.toDouble(), incomeData[index]), (index) => FlSpot(index.toDouble(), incomeData[index]),
@ -167,11 +167,11 @@ class ExpensesLineChart extends StatelessWidget {
isStrokeCapRound: true, isStrokeCapRound: true,
dotData: const FlDotData(show: false), dotData: const FlDotData(show: false),
belowBarData: BarAreaData(), belowBarData: BarAreaData(),
color: color: ((MediaQuery.of(context).platformBrightness ==
(MediaQuery.of(context).platformBrightness == Brightness.dark) Brightness.dark)
? Colors.red.shade300 ? Colors.red.shade300
: Colors.red : Colors.red)
.harmonizeWith(Theme.of(context).colorScheme.primary), .harmonizeWith(Theme.of(context).colorScheme.primary),
spots: List.generate( spots: List.generate(
yearly yearly
? 12 ? 12

View file

@ -222,173 +222,267 @@ class _HomeViewState extends State<HomeView> {
), ),
], ],
) )
: GroupedListView( : Column(
groupHeaderBuilder: (element) => Text( children: [
DateFormat.yMMMM(locale).format(element.date), Text(
style: TextStyle( NumberFormat.compactCurrency(
color: Theme.of(context).colorScheme.primary, locale: locale,
), symbol: selectedWallet!.currency.symbol,
), ).format(selectedWallet!.calculateCurrentBalance()),
elements: selectedWallet!.entries, style: const TextStyle(
itemComparator: (a, b) => b.date.compareTo(a.date), fontWeight: FontWeight.bold,
groupBy: (e) => DateFormat.yMMMM(locale).format(e.date), fontSize: 22,
groupComparator: (a, b) {
// TODO: better sorting algorithm lol
final yearA = RegExp(r'\d+').firstMatch(a);
if (yearA == null) return 0;
final yearB = RegExp(r'\d+').firstMatch(b);
if (yearB == null) return 0;
final compareYears = int.parse(yearB.group(0)!)
.compareTo(int.parse(yearA.group(0)!));
if (compareYears != 0) return compareYears;
final months = List<String>.generate(
12,
(index) => DateFormat.MMMM(locale).format(
DateTime(2023, index + 1),
), ),
);
final monthA = RegExp('[^0-9 ]+').firstMatch(a);
if (monthA == null) return 0;
final monthB = RegExp('[^0-9 ]+').firstMatch(b);
if (monthB == null) return 0;
return months.indexOf(monthB.group(0)!).compareTo(
months.indexOf(monthA.group(0)!),
);
},
itemBuilder: (context, element) => Slidable(
endActionPane: ActionPane(
motion: const ScrollMotion(),
children: [
SlidableAction(
onPressed: (c) {
Navigator.of(context)
.push<WalletSingleEntry>(
MaterialPageRoute(
builder: (c) => CreateSingleEntryView(
w: selectedWallet!,
editEntry: element,
),
),
)
.then(
(editedEntry) {
if (editedEntry == null) return;
selectedWallet!.entries.remove(element);
selectedWallet!.entries.add(editedEntry);
WalletManager.saveWallet(selectedWallet!);
setState(() {});
},
);
},
backgroundColor:
Theme.of(context).colorScheme.secondary,
foregroundColor:
Theme.of(context).colorScheme.onSecondary,
icon: Icons.edit,
),
SlidableAction(
backgroundColor:
Theme.of(context).colorScheme.error,
foregroundColor:
Theme.of(context).colorScheme.onError,
icon: Icons.delete,
onPressed: (c) {
showDialog(
context: context,
builder: (cx) => PlatformDialog(
title:
AppLocalizations.of(context).sureDialog,
content: Text(
AppLocalizations.of(context).deleteSure,
),
actions: [
PlatformButton(
text: AppLocalizations.of(context).yes,
onPressed: () {
selectedWallet?.entries.removeWhere(
(e) => e.id == element.id,
);
WalletManager.saveWallet(
selectedWallet!,
);
Navigator.of(cx).pop();
setState(() {});
},
),
PlatformButton(
text: AppLocalizations.of(context).no,
onPressed: () {
Navigator.of(cx).pop();
},
),
],
),
);
},
),
],
), ),
child: ListTile( const SizedBox(
leading: Container( height: 5,
decoration: BoxDecoration( ),
borderRadius: BorderRadius.circular(16), if (selectedWallet!.calculateMonthStatus(
color: element.category.color, DateTime.now().month,
), DateTime.now().year,
child: Padding( ) ==
padding: const EdgeInsets.all(8), 0)
child: Icon( Text(AppLocalizations.of(context).evenMoney)
element.category.icon, else
color: RichText(
element.category.color.calculateTextColor(),
),
),
),
title: Text(element.data.name),
subtitle: RichText(
text: TextSpan( text: TextSpan(
children: [ children: [
TextSpan( TextSpan(
text: NumberFormat.currency( text: AppLocalizations.of(context)
symbol: selectedWallet!.currency.symbol, .balanceStatusA,
).format(element.data.amount), ),
TextSpan(
style: TextStyle( style: TextStyle(
color: (element.type == EntryType.income) color: (selectedWallet!
? (MediaQuery.of(context) .calculateMonthStatus(
.platformBrightness == DateTime.now().month,
Brightness.dark) DateTime.now().year,
? Colors.green.shade300 ) >
: Colors.green.harmonizeWith( 0
Theme.of(context) ? ((MediaQuery.of(context)
.colorScheme .platformBrightness ==
.primary, Brightness.dark)
) ? Colors.green.shade300
: (MediaQuery.of(context) : Colors.green)
.platformBrightness == .harmonizeWith(
Brightness.dark) Theme.of(context)
? Colors.red.shade300 .colorScheme
: Colors.red.harmonizeWith( .primary,
Theme.of(context) )
.colorScheme : ((MediaQuery.of(context)
.primary, .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( TextSpan(
text: text: AppLocalizations.of(context)
" | ${DateFormat.MMMd(locale).format(element.date)}", .balanceStatusB,
style: TextStyle(
color: Theme.of(context)
.colorScheme
.background
.calculateTextColor(),
),
), ),
], ],
), ),
), ),
const SizedBox(
height: 10,
), ),
), Expanded(
child: GroupedListView(
groupHeaderBuilder: (element) => Text(
DateFormat.yMMMM(locale).format(element.date),
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
),
),
elements: selectedWallet!.entries,
itemComparator: (a, b) => b.date.compareTo(a.date),
groupBy: (e) =>
DateFormat.yMMMM(locale).format(e.date),
groupComparator: (a, b) {
// TODO: better sorting algorithm lol
final yearA = RegExp(r'\d+').firstMatch(a);
if (yearA == null) return 0;
final yearB = RegExp(r'\d+').firstMatch(b);
if (yearB == null) return 0;
final compareYears = int.parse(yearB.group(0)!)
.compareTo(int.parse(yearA.group(0)!));
if (compareYears != 0) return compareYears;
final months = List<String>.generate(
12,
(index) => DateFormat.MMMM(locale).format(
DateTime(2023, index + 1),
),
);
final monthA = RegExp('[^0-9 ]+').firstMatch(a);
if (monthA == null) return 0;
final monthB = RegExp('[^0-9 ]+').firstMatch(b);
if (monthB == null) return 0;
return months.indexOf(monthB.group(0)!).compareTo(
months.indexOf(monthA.group(0)!),
);
},
itemBuilder: (context, element) => Slidable(
endActionPane: ActionPane(
motion: const ScrollMotion(),
children: [
SlidableAction(
onPressed: (c) {
Navigator.of(context)
.push<WalletSingleEntry>(
MaterialPageRoute(
builder: (c) => CreateSingleEntryView(
w: selectedWallet!,
editEntry: element,
),
),
)
.then(
(editedEntry) {
if (editedEntry == null) return;
selectedWallet!.entries
.remove(element);
selectedWallet!.entries
.add(editedEntry);
WalletManager.saveWallet(
selectedWallet!,
);
setState(() {});
},
);
},
backgroundColor:
Theme.of(context).colorScheme.secondary,
foregroundColor: Theme.of(context)
.colorScheme
.onSecondary,
icon: Icons.edit,
),
SlidableAction(
backgroundColor:
Theme.of(context).colorScheme.error,
foregroundColor:
Theme.of(context).colorScheme.onError,
icon: Icons.delete,
onPressed: (c) {
showDialog(
context: context,
builder: (cx) => PlatformDialog(
title: AppLocalizations.of(context)
.sureDialog,
content: Text(
AppLocalizations.of(context)
.deleteSure,
),
actions: [
PlatformButton(
text: AppLocalizations.of(context)
.yes,
onPressed: () {
selectedWallet?.entries
.removeWhere(
(e) => e.id == element.id,
);
WalletManager.saveWallet(
selectedWallet!,
);
Navigator.of(cx).pop();
setState(() {});
},
),
PlatformButton(
text: AppLocalizations.of(context)
.no,
onPressed: () {
Navigator.of(cx).pop();
},
),
],
),
);
},
),
],
),
child: ListTile(
leading: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: element.category.color,
),
child: Padding(
padding: const EdgeInsets.all(8),
child: Icon(
element.category.icon,
color: element.category.color
.calculateTextColor(),
),
),
),
title: Text(element.data.name),
subtitle: RichText(
text: TextSpan(
children: [
TextSpan(
text: NumberFormat.currency(
symbol:
selectedWallet!.currency.symbol,
).format(element.data.amount),
style: TextStyle(
color: (element.type ==
EntryType.income)
? (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,
),
),
),
TextSpan(
text:
" | ${DateFormat.MMMd(locale).format(element.date)}",
style: TextStyle(
color: Theme.of(context)
.colorScheme
.background
.calculateTextColor(),
),
),
],
),
),
),
),
),
),
],
), ),
), ),
), ),