Start migration to sqlite using drift
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:okane/database/collections/expense_category.dart';
|
||||
import 'package:okane/database/database.dart';
|
||||
import 'package:okane/i18n/strings.g.dart';
|
||||
import 'package:okane/ui/state/core.dart';
|
||||
|
||||
@@ -50,12 +48,14 @@ class AddExpenseCategoryState extends State<AddExpenseCategory> {
|
||||
Spacer(),
|
||||
OutlinedButton(
|
||||
onPressed: () async {
|
||||
// TODO
|
||||
/*
|
||||
final category =
|
||||
ExpenseCategory()
|
||||
..name = _categoryNameController.text;
|
||||
await upsertExpenseCategory(category);
|
||||
_categoryNameController.text = "";
|
||||
Navigator.of(context).pop(category);
|
||||
Navigator.of(context).pop(category);*/
|
||||
},
|
||||
child: Text(t.modals.add),
|
||||
),
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
import 'package:drift/drift.dart' show Value;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:okane/database/collections/account.dart';
|
||||
import 'package:okane/database/collections/beneficiary.dart';
|
||||
import 'package:okane/database/collections/expense_category.dart';
|
||||
import 'package:okane/database/collections/recurrent.dart';
|
||||
import 'package:okane/database/collections/template.dart';
|
||||
import 'package:okane/database/database.dart';
|
||||
import 'package:okane/database/sqlite.dart';
|
||||
import 'package:okane/i18n/strings.g.dart';
|
||||
import 'package:okane/ui/state/core.dart';
|
||||
import 'package:okane/ui/transaction.dart';
|
||||
@@ -68,6 +64,7 @@ class _AddTransactionTemplateWidgetState
|
||||
return;
|
||||
}
|
||||
|
||||
final db = GetIt.I.get<OkaneDatabase>();
|
||||
Beneficiary? beneficiary = _selectedBeneficiary?.item;
|
||||
if (beneficiary == null ||
|
||||
getBeneficiaryName(beneficiary) != beneficiaryName) {
|
||||
@@ -101,12 +98,12 @@ class _AddTransactionTemplateWidgetState
|
||||
if (result == null || !result) {
|
||||
return;
|
||||
}
|
||||
|
||||
beneficiary =
|
||||
Beneficiary()
|
||||
..name = beneficiaryName
|
||||
..type = BeneficiaryType.other;
|
||||
await upsertBeneficiary(beneficiary);
|
||||
beneficiary = await db.beneficiariesDao.upsertBeneficiary(
|
||||
BeneficiariesCompanion(
|
||||
name: Value(beneficiaryName),
|
||||
type: Value(BeneficiaryType.other),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final factor = switch (_selectedDirection) {
|
||||
@@ -114,15 +111,16 @@ class _AddTransactionTemplateWidgetState
|
||||
TransactionDirection.receive => 1,
|
||||
};
|
||||
final amount = factor * double.parse(_amountTextController.text).abs();
|
||||
final template =
|
||||
TransactionTemplate()
|
||||
..name = _templateNameController.text
|
||||
..account.value = widget.activeAccountItem
|
||||
..beneficiary.value = beneficiary
|
||||
..expenseCategory.value = _expenseCategory
|
||||
..recurring = _isRecurring
|
||||
..amount = amount;
|
||||
await upsertTransactionTemplate(template);
|
||||
final template = await db.transactionTemplatesDao.upsertTemplate(
|
||||
TransactionTemplatesCompanion(
|
||||
name: Value(_templateNameController.text),
|
||||
accountId: Value(widget.activeAccountItem.id),
|
||||
beneficiaryId: Value(beneficiary.id),
|
||||
expenseCategoryId: Value(_expenseCategory?.id),
|
||||
recurring: Value(_isRecurring),
|
||||
amount: Value(amount),
|
||||
),
|
||||
);
|
||||
|
||||
if (_isRecurring) {
|
||||
final days = switch (_selectedPeriod) {
|
||||
@@ -131,13 +129,14 @@ class _AddTransactionTemplateWidgetState
|
||||
Period.months => _periodSize * 31,
|
||||
Period.years => _periodSize * 365,
|
||||
};
|
||||
final recurringTransaction =
|
||||
RecurringTransaction()
|
||||
..account.value = widget.activeAccountItem
|
||||
..template.value = template
|
||||
..lastExecution = null
|
||||
..days = days;
|
||||
await upsertRecurringTransaction(recurringTransaction);
|
||||
await db.recurringTransactionsDao.upsertRecurringTransaction(
|
||||
RecurringTransactionsCompanion(
|
||||
accountId: Value(widget.activeAccountItem.id),
|
||||
templateId: Value(template.id),
|
||||
lastExecution: Value(null),
|
||||
days: Value(days),
|
||||
),
|
||||
);
|
||||
}
|
||||
widget.onAdd();
|
||||
}
|
||||
@@ -190,8 +189,7 @@ class _AddTransactionTemplateWidgetState
|
||||
.where((el) {
|
||||
final bloc = GetIt.I.get<CoreCubit>();
|
||||
if (el.type == BeneficiaryType.account) {
|
||||
return el.account.value?.id !=
|
||||
bloc.activeAccount?.id;
|
||||
return el.accountId != bloc.activeAccount?.id;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:okane/database/collections/account.dart';
|
||||
import 'package:okane/database/collections/beneficiary.dart';
|
||||
import 'package:okane/database/collections/expense_category.dart';
|
||||
import 'package:okane/database/collections/template.dart';
|
||||
import 'package:okane/database/collections/transaction.dart';
|
||||
import 'package:okane/database/database.dart';
|
||||
import 'package:okane/database/sqlite.dart';
|
||||
import 'package:okane/i18n/strings.g.dart';
|
||||
import 'package:okane/ui/state/core.dart';
|
||||
import 'package:okane/ui/transaction.dart';
|
||||
@@ -14,14 +10,14 @@ import 'package:okane/ui/utils.dart';
|
||||
import 'package:okane/ui/widgets/add_expense_category.dart';
|
||||
import 'package:searchfield/searchfield.dart';
|
||||
|
||||
typedef AddTransactionCallback = void Function(Transaction);
|
||||
typedef AddTransactionCallback = void Function(TransactionDto);
|
||||
|
||||
class AddTransactionWidget extends StatefulWidget {
|
||||
final AddTransactionCallback onAdd;
|
||||
|
||||
final Account activeAccountItem;
|
||||
|
||||
final TransactionTemplate? template;
|
||||
final TransactionTemplateDto? template;
|
||||
|
||||
const AddTransactionWidget({
|
||||
super.key,
|
||||
@@ -51,6 +47,8 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
// TODO
|
||||
/*
|
||||
if (widget.template != null) {
|
||||
_selectedDirection =
|
||||
widget.template!.amount > 0
|
||||
@@ -58,12 +56,12 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
|
||||
: TransactionDirection.send;
|
||||
_amountTextController.text = widget.template!.amount.toString();
|
||||
_beneficiaryTextController.text =
|
||||
widget.template!.beneficiary.value!.name;
|
||||
widget.template!;
|
||||
_selectedBeneficiary = SearchFieldListItem(
|
||||
getBeneficiaryName(widget.template!.beneficiary.value!),
|
||||
item: widget.template!.beneficiary.value!,
|
||||
);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
String getBeneficiaryName(Beneficiary item) {
|
||||
@@ -81,6 +79,7 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
|
||||
return;
|
||||
}
|
||||
|
||||
final db = GetIt.I.get<OkaneDatabase>();
|
||||
Beneficiary? beneficiary = _selectedBeneficiary?.item;
|
||||
if (beneficiary == null ||
|
||||
getBeneficiaryName(beneficiary) != beneficiaryName) {
|
||||
@@ -115,11 +114,12 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
|
||||
return;
|
||||
}
|
||||
|
||||
beneficiary =
|
||||
Beneficiary()
|
||||
..name = beneficiaryName
|
||||
..type = BeneficiaryType.other;
|
||||
await upsertBeneficiary(beneficiary);
|
||||
beneficiary = await db.beneficiariesDao.upsertBeneficiary(
|
||||
BeneficiariesCompanion(
|
||||
name: Value(beneficiaryName),
|
||||
type: Value(BeneficiaryType.other),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final factor = switch (_selectedDirection) {
|
||||
@@ -127,30 +127,38 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
|
||||
TransactionDirection.receive => 1,
|
||||
};
|
||||
final amount = factor * double.parse(_amountTextController.text).abs();
|
||||
final transaction =
|
||||
Transaction()
|
||||
..account.value = widget.activeAccountItem
|
||||
..beneficiary.value = beneficiary
|
||||
..amount = amount
|
||||
..tags = []
|
||||
..expenseCategory.value = _expenseCategory
|
||||
..date = _selectedDate;
|
||||
await upsertTransaction(transaction);
|
||||
final rawTransaction = TransactionsCompanion(
|
||||
accountId: Value(widget.activeAccountItem.id),
|
||||
beneficiaryId: Value(beneficiary.id),
|
||||
amount: Value(amount),
|
||||
// tags: [],
|
||||
expenseCategoryId: Value(_expenseCategory?.id),
|
||||
date: Value(_selectedDate),
|
||||
);
|
||||
final transaction = await db.transactionsDao.upsertTransaction(
|
||||
rawTransaction,
|
||||
);
|
||||
|
||||
if (beneficiary.type == BeneficiaryType.account) {
|
||||
final otherTransaction =
|
||||
Transaction()
|
||||
..account.value = beneficiary.account.value!
|
||||
..beneficiary.value = await getAccountBeneficiary(
|
||||
widget.activeAccountItem,
|
||||
)
|
||||
..date = _selectedDate
|
||||
..expenseCategory.value = _expenseCategory
|
||||
..amount = -1 * amount;
|
||||
await upsertTransaction(otherTransaction);
|
||||
final otherTransaction = rawTransaction.copyWith(
|
||||
accountId: Value(beneficiary.accountId!),
|
||||
beneficiaryId: Value(
|
||||
(await db.beneficiariesDao.getAccountBeneficiary(
|
||||
widget.activeAccountItem,
|
||||
)).id,
|
||||
),
|
||||
amount: Value(-1 * rawTransaction.amount.value),
|
||||
);
|
||||
await db.transactionsDao.upsertTransaction(otherTransaction);
|
||||
}
|
||||
|
||||
widget.onAdd(transaction);
|
||||
widget.onAdd(
|
||||
TransactionDto(
|
||||
transaction: transaction,
|
||||
beneficiary: beneficiary,
|
||||
expenseCategory: _expenseCategory,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -167,17 +175,17 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
|
||||
return;
|
||||
}
|
||||
|
||||
_amountTextController.text = template.amount.toString();
|
||||
_amountTextController.text = template.template.amount.toString();
|
||||
_selectedDirection =
|
||||
template.amount > 0
|
||||
template.template.amount > 0
|
||||
? TransactionDirection.receive
|
||||
: TransactionDirection.send;
|
||||
_selectedBeneficiary = SearchFieldListItem(
|
||||
getBeneficiaryName(template.beneficiary.value!),
|
||||
item: template.beneficiary.value!,
|
||||
getBeneficiaryName(template.beneficiary),
|
||||
item: template.beneficiary,
|
||||
);
|
||||
_beneficiaryTextController.text = getBeneficiaryName(
|
||||
template.beneficiary.value!,
|
||||
template.beneficiary,
|
||||
);
|
||||
},
|
||||
child: Text(t.pages.transactions.addTransaction.useTemplate),
|
||||
@@ -216,7 +224,7 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
|
||||
.where((el) {
|
||||
final bloc = GetIt.I.get<CoreCubit>();
|
||||
if (el.type == BeneficiaryType.account) {
|
||||
return el.account.value?.id.toInt() ==
|
||||
return el.accountId ==
|
||||
bloc.activeAccount?.id.toInt();
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:okane/database/collections/transaction.dart';
|
||||
import 'package:okane/database/sqlite.dart';
|
||||
import 'package:okane/ui/utils.dart';
|
||||
import 'package:okane/ui/widgets/image_wrapper.dart';
|
||||
|
||||
@@ -13,7 +13,7 @@ class TransactionCard extends StatelessWidget {
|
||||
this.subtitle,
|
||||
});
|
||||
|
||||
final Transaction transaction;
|
||||
final TransactionDto transaction;
|
||||
|
||||
final VoidCallback onTap;
|
||||
|
||||
@@ -24,19 +24,22 @@ class TransactionCard extends StatelessWidget {
|
||||
child: ListTile(
|
||||
onTap: onTap,
|
||||
leading: ImageWrapper(
|
||||
title: transaction.beneficiary.value!.name,
|
||||
path: transaction.beneficiary.value!.imagePath,
|
||||
title: transaction.beneficiary.name,
|
||||
path: transaction.beneficiary.imagePath,
|
||||
),
|
||||
trailing: Text(formatDateTime(transaction.date)),
|
||||
title: Text(transaction.beneficiary.value!.name),
|
||||
trailing: Text(formatDateTime(transaction.transaction.date)),
|
||||
title: Text(transaction.beneficiary.name),
|
||||
subtitle: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
formatCurrency(transaction.amount),
|
||||
formatCurrency(transaction.transaction.amount),
|
||||
style: TextStyle(
|
||||
color: transaction.amount < 0 ? Colors.red : Colors.green,
|
||||
color:
|
||||
transaction.transaction.amount < 0
|
||||
? Colors.red
|
||||
: Colors.green,
|
||||
),
|
||||
),
|
||||
if (subtitle != null) subtitle!,
|
||||
|
||||
Reference in New Issue
Block a user