Skip to content

Commit

Permalink
Merge pull request #153 from K-w-e/feature/currency_selector
Browse files Browse the repository at this point in the history
CurrencySelector #70
  • Loading branch information
lucaantonelli committed Feb 25, 2024
2 parents 830e80c + 57facf8 commit 894dec8
Show file tree
Hide file tree
Showing 21 changed files with 262 additions and 109 deletions.
16 changes: 10 additions & 6 deletions lib/custom_widgets/account_modal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ import 'dart:ui';

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../constants/functions.dart';
import '../providers/currency_provider.dart';
import 'line_chart.dart';

class AccountDialog extends StatelessWidget with Functions {
class AccountDialog extends ConsumerWidget with Functions {
final String accountName;
final num amount;

const AccountDialog({super.key, required this.accountName, required this.amount});

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
final currencyState = ref.watch(currencyStateNotifier);

return SingleChildScrollView(
child: Column(
children: [
Expand Down Expand Up @@ -108,7 +112,7 @@ class AccountDialog extends StatelessWidget with Functions {
TextSpan(
text: "-285,99", style: Theme.of(context).textTheme.labelSmall),
TextSpan(
text: "€",
text: currencyState.selectedCurrency.symbol,
style: Theme.of(context)
.textTheme
.headlineSmall
Expand Down Expand Up @@ -154,7 +158,7 @@ class AccountDialog extends StatelessWidget with Functions {
text: "-280,00",
style: Theme.of(context).textTheme.labelSmall),
TextSpan(
text: "€",
text: currencyState.selectedCurrency.symbol,
style: Theme.of(context).textTheme.headlineSmall?.apply(
fontFeatures: [const FontFeature.subscripts()],
),
Expand Down Expand Up @@ -228,7 +232,7 @@ class AccountDialog extends StatelessWidget with Functions {
style: Theme.of(context).textTheme.labelSmall,
),
TextSpan(
text: "€",
text: currencyState.selectedCurrency.symbol,
style: Theme.of(context).textTheme.headlineSmall?.apply(
fontFeatures: [const FontFeature.subscripts()],
),
Expand Down Expand Up @@ -303,7 +307,7 @@ class AccountDialog extends StatelessWidget with Functions {
style: Theme.of(context).textTheme.labelSmall,
),
TextSpan(
text: "€",
text: currencyState.selectedCurrency.symbol,
style: Theme.of(context).textTheme.headlineSmall?.apply(
fontFeatures: [const FontFeature.subscripts()],
),
Expand Down
17 changes: 12 additions & 5 deletions lib/custom_widgets/accounts_sum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import '../model/bank_account.dart';
import '../constants/functions.dart';
import '../constants/style.dart';
import '../../../providers/accounts_provider.dart';
import '../providers/currency_provider.dart';

/// This class shows account summaries in the dashboard
class AccountsSum extends ConsumerWidget with Functions {
Expand All @@ -19,8 +20,9 @@ class AccountsSum extends ConsumerWidget with Functions {

@override
Widget build(BuildContext context, WidgetRef ref) {
final currencyState = ref.watch(currencyStateNotifier);

return Container(
width: 160.0,
margin: const EdgeInsets.fromLTRB(0, 4, 16, 6),
decoration: BoxDecoration(
color: white,
Expand All @@ -40,7 +42,8 @@ class AccountsSum extends ConsumerWidget with Functions {
await ref
.read(accountsProvider.notifier)
.selectedAccount(account)
.whenComplete(() => Navigator.of(context).pushNamed('/account'));
.whenComplete(
() => Navigator.of(context).pushNamed('/account'));
},
child: Container(
padding: const EdgeInsets.fromLTRB(12, 8, 12, 8),
Expand All @@ -67,7 +70,10 @@ class AccountsSum extends ConsumerWidget with Functions {
children: [
Text(
account.name,
style: Theme.of(context).textTheme.bodyLarge!.copyWith(color: darkBlue7),
style: Theme.of(context)
.textTheme
.bodyLarge!
.copyWith(color: darkBlue7),
),
RichText(
text: TextSpan(
Expand All @@ -80,8 +86,9 @@ class AccountsSum extends ConsumerWidget with Functions {
.copyWith(color: darkBlue7),
),
TextSpan(
text: "€",
style: Theme.of(context).textTheme.bodyMedium?.apply(
text: currencyState.selectedCurrency.symbol,
style:
Theme.of(context).textTheme.bodySmall?.apply(
fontFeatures: [const FontFeature.subscripts()],
).copyWith(color: darkBlue7),
),
Expand Down
10 changes: 7 additions & 3 deletions lib/custom_widgets/budget_circular_indicator.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:percent_indicator/circular_percent_indicator.dart';
import '../constants/functions.dart';
import '../providers/currency_provider.dart';

/// This class shows account summaries in dashboard
class BudgetCircularIndicator extends StatelessWidget with Functions {
class BudgetCircularIndicator extends ConsumerWidget with Functions {
final String title;
final num amount;
final double perc;
Expand All @@ -20,7 +22,9 @@ class BudgetCircularIndicator extends StatelessWidget with Functions {
});

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
final currencyState = ref.watch(currencyStateNotifier);

return Column(
children: [
CircularPercentIndicator(
Expand All @@ -43,7 +47,7 @@ class BudgetCircularIndicator extends StatelessWidget with Functions {
.copyWith(color: Theme.of(context).colorScheme.primary),
),
TextSpan(
text: "€",
text: currencyState.selectedCurrency.symbol,
style: Theme.of(context)
.textTheme
.labelMedium!
Expand Down
12 changes: 8 additions & 4 deletions lib/custom_widgets/transactions_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../constants/constants.dart';
import '../constants/functions.dart';
import '../constants/style.dart';
import '../model/transaction.dart';
import '../providers/currency_provider.dart';
import '../providers/transactions_provider.dart';
import '../utils/date_helper.dart';
import 'default_container.dart';
Expand Down Expand Up @@ -99,7 +100,7 @@ class _TransactionsListState extends State<TransactionsList> with Functions {
}
}

class TransactionTitle extends StatelessWidget with Functions {
class TransactionTitle extends ConsumerWidget with Functions {
final DateTime date;
final num total;
final bool first;
Expand All @@ -112,7 +113,8 @@ class TransactionTitle extends StatelessWidget with Functions {
});

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
final currencyState = ref.watch(currencyStateNotifier);
final color = total < 0 ? red : (total > 0 ? green : blue3);
return Padding(
padding: EdgeInsets.only(top: first ? 0 : 24),
Expand All @@ -136,7 +138,7 @@ class TransactionTitle extends StatelessWidget with Functions {
style: Theme.of(context).textTheme.bodyLarge!.copyWith(color: color),
),
TextSpan(
text: "€",
text: currencyState.selectedCurrency.symbol,
style: Theme.of(context)
.textTheme
.labelMedium!
Expand All @@ -163,6 +165,8 @@ class TransactionRow extends ConsumerWidget with Functions {

@override
Widget build(BuildContext context, WidgetRef ref) {
final currencyState = ref.watch(currencyStateNotifier);

return Column(
children: [
Material(
Expand Down Expand Up @@ -233,7 +237,7 @@ class TransactionRow extends ConsumerWidget with Functions {
.copyWith(color: typeToColor(transaction.type)),
),
TextSpan(
text: "€",
text: currencyState.selectedCurrency.symbol,
style: Theme.of(context)
.textTheme
.labelSmall!
Expand Down
35 changes: 32 additions & 3 deletions lib/model/currency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,37 @@ class Currency extends BaseEntity {
};
}

class BudgetMethods extends SossoldiDatabase {
class CurrencyMethods extends SossoldiDatabase {
Future<Currency> getSelectedCurrency() async {
final db = await database;

final maps = await db.query(
currencyTable,
columns: CurrencyFields.allFields,
where: '${CurrencyFields.mainCurrency} = ?',
whereArgs: [1],
);

if (maps.isNotEmpty) {
return Currency.fromJson(maps.first);
} else {
//fallback
return const Currency(
id: 2,
symbol: '\$',
code: 'USD',
name: "United States Dollar",
mainCurrency: true
);
}
}

Future<Currency> insert(Currency item) async {
final db = await database;
final id = await db.insert(currencyTable, item.toJson());
return item.copy(id: id);
}


Future<Currency> selectById(int id) async {
final db = await database;

Expand Down Expand Up @@ -100,7 +123,6 @@ class BudgetMethods extends SossoldiDatabase {
Future<int> updateItem(Currency item) async {
final db = await database;

// You can use `rawUpdate` to write the query in SQL
return db.update(
currencyTable,
item.toJson(),
Expand All @@ -119,4 +141,11 @@ class BudgetMethods extends SossoldiDatabase {
whereArgs: [id]);
}

void changeMainCurrency(int id) async {
final db = await database;

db.rawUpdate("UPDATE currency SET mainCurrency = 0");
db.rawUpdate("UPDATE currency SET mainCurrency = 1 WHERE id = $id");
}

}
4 changes: 3 additions & 1 deletion lib/pages/accounts/add_account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import '../../providers/accounts_provider.dart';
import '../../constants/constants.dart';
import '../../constants/functions.dart';
import '../../constants/style.dart';
import '../../providers/currency_provider.dart';

final showAccountIconsProvider = StateProvider.autoDispose<bool>((ref) => false);

Expand Down Expand Up @@ -42,6 +43,7 @@ class _AddAccountState extends ConsumerState<AddAccount> with Functions {
final showAccountIcons = ref.watch(showAccountIconsProvider);
final accountMainSwitch = ref.watch(accountMainSwitchProvider);
final countNetWorth = ref.watch(countNetWorthSwitchProvider);
final currencyState = ref.watch(currencyStateNotifier);

return Scaffold(
backgroundColor: Theme.of(context).colorScheme.background,
Expand Down Expand Up @@ -255,7 +257,7 @@ class _AddAccountState extends ConsumerState<AddAccount> with Functions {
controller: startingValueController,
decoration: InputDecoration(
hintText: "Initial balance",
suffixText: "€",
suffixText: currencyState.selectedCurrency.symbol,
hintStyle: Theme.of(context)
.textTheme
.titleLarge!
Expand Down
4 changes: 3 additions & 1 deletion lib/pages/add_page/widgets/amount_section.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "package:flutter_riverpod/flutter_riverpod.dart";
import '../../../constants/functions.dart';
import "../../../constants/style.dart";
import '../../../model/transaction.dart';
import '../../../providers/currency_provider.dart';
import '../../../providers/transactions_provider.dart';
import 'account_selector.dart';
import 'type_tab.dart';
Expand Down Expand Up @@ -42,6 +43,7 @@ class _AmountSectionState extends ConsumerState<AmountSection> with Functions {
Widget build(BuildContext context) {
final trsncTypeList = ref.watch(transactionTypeList);
final selectedType = ref.watch(transactionTypeProvider);
final currencyState = ref.watch(currencyStateNotifier);

return Container(
color: Theme.of(context).colorScheme.surface,
Expand Down Expand Up @@ -274,7 +276,7 @@ class _AmountSectionState extends ConsumerState<AmountSection> with Functions {
hintText: "0",
border: InputBorder.none,
prefixText: ' ', // set to center the amount
suffixText: '€',
suffixText: currencyState.selectedCurrency.symbol,
suffixStyle: Theme.of(context)
.textTheme
.headlineMedium!
Expand Down
Loading

0 comments on commit 894dec8

Please sign in to comment.