378 lines
12 KiB
Dart
378 lines
12 KiB
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/recurrent.dart';
|
|
import 'package:okane/database/collections/template.dart';
|
|
import 'package:okane/database/database.dart';
|
|
import 'package:okane/i18n/strings.g.dart';
|
|
import 'package:okane/ui/state/core.dart';
|
|
import 'package:okane/ui/transaction.dart';
|
|
import 'package:okane/ui/utils.dart';
|
|
import 'package:okane/ui/widgets/add_expense_category.dart';
|
|
import 'package:searchfield/searchfield.dart';
|
|
|
|
enum Period { days, weeks, months, years }
|
|
|
|
class AddTransactionTemplateWidget extends StatefulWidget {
|
|
final VoidCallback onAdd;
|
|
|
|
final Account activeAccountItem;
|
|
|
|
const AddTransactionTemplateWidget({
|
|
super.key,
|
|
required this.activeAccountItem,
|
|
required this.onAdd,
|
|
});
|
|
|
|
@override
|
|
State<AddTransactionTemplateWidget> createState() =>
|
|
_AddTransactionTemplateWidgetState();
|
|
}
|
|
|
|
class _AddTransactionTemplateWidgetState
|
|
extends State<AddTransactionTemplateWidget> {
|
|
final TextEditingController _beneficiaryTextController =
|
|
TextEditingController();
|
|
final TextEditingController _amountTextController = TextEditingController();
|
|
final TextEditingController _templateNameController = TextEditingController();
|
|
|
|
SearchFieldListItem<Beneficiary>? _selectedBeneficiary;
|
|
|
|
TransactionDirection _selectedDirection = TransactionDirection.send;
|
|
|
|
ExpenseCategory? _expenseCategory;
|
|
|
|
bool _isRecurring = false;
|
|
|
|
Period _selectedPeriod = Period.weeks;
|
|
int _periodSize = 1;
|
|
|
|
String getBeneficiaryName(Beneficiary item) {
|
|
return switch (item.type) {
|
|
BeneficiaryType.account => t.common.beneficiary.nameWithAccount(
|
|
name: item.name,
|
|
),
|
|
BeneficiaryType.other => item.name,
|
|
};
|
|
}
|
|
|
|
Future<void> _submit(BuildContext context) async {
|
|
final beneficiaryName = _beneficiaryTextController.text;
|
|
if (_selectedBeneficiary == null && beneficiaryName.isEmpty) {
|
|
return;
|
|
}
|
|
if (_templateNameController.text.isEmpty) {
|
|
return;
|
|
}
|
|
|
|
Beneficiary? beneficiary = _selectedBeneficiary?.item;
|
|
if (beneficiary == null ||
|
|
getBeneficiaryName(beneficiary) != beneficiaryName) {
|
|
// Add a new beneficiary, if none was selected
|
|
final result = await showDialog<bool>(
|
|
context: context,
|
|
builder:
|
|
(context) => AlertDialog(
|
|
title: Text(t.common.beneficiary.addBeneficiary.title),
|
|
content: Text(
|
|
t.common.beneficiary.addBeneficiary.body(name: beneficiaryName),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
style: TextButton.styleFrom(
|
|
textStyle: Theme.of(context).textTheme.labelLarge,
|
|
),
|
|
child: Text(t.modals.add),
|
|
onPressed: () => Navigator.of(context).pop(true),
|
|
),
|
|
TextButton(
|
|
style: TextButton.styleFrom(
|
|
textStyle: Theme.of(context).textTheme.labelLarge,
|
|
),
|
|
child: Text(t.modals.cancel),
|
|
onPressed: () => Navigator.of(context).pop(false),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
if (result == null || !result) {
|
|
return;
|
|
}
|
|
|
|
beneficiary =
|
|
Beneficiary()
|
|
..name = beneficiaryName
|
|
..type = BeneficiaryType.other;
|
|
await upsertBeneficiary(beneficiary);
|
|
}
|
|
|
|
final factor = switch (_selectedDirection) {
|
|
TransactionDirection.send => -1,
|
|
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);
|
|
|
|
if (_isRecurring) {
|
|
final days = switch (_selectedPeriod) {
|
|
Period.days => _periodSize,
|
|
Period.weeks => _periodSize * 7,
|
|
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);
|
|
}
|
|
widget.onAdd();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 16),
|
|
child: ListView(
|
|
shrinkWrap: true,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
child: TextField(
|
|
controller: _templateNameController,
|
|
decoration: InputDecoration(label: Text(t.common.templateName)),
|
|
),
|
|
),
|
|
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
child: SegmentedButton<TransactionDirection>(
|
|
segments: [
|
|
ButtonSegment(
|
|
value: TransactionDirection.send,
|
|
label: Text(t.common.transaction.directionSend),
|
|
icon: Icon(Icons.remove),
|
|
),
|
|
ButtonSegment(
|
|
value: TransactionDirection.receive,
|
|
label: Text(t.common.transaction.directionReceive),
|
|
icon: Icon(Icons.add),
|
|
),
|
|
],
|
|
selected: <TransactionDirection>{_selectedDirection},
|
|
multiSelectionEnabled: false,
|
|
onSelectionChanged: (selection) {
|
|
setState(() => _selectedDirection = selection.first);
|
|
},
|
|
),
|
|
),
|
|
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
child: BlocBuilder<CoreCubit, CoreState>(
|
|
builder:
|
|
(context, state) => SearchField<Beneficiary>(
|
|
suggestions:
|
|
state.beneficiaries
|
|
.where((el) {
|
|
final bloc = GetIt.I.get<CoreCubit>();
|
|
if (el.type == BeneficiaryType.account) {
|
|
return el.account.value?.id !=
|
|
bloc.activeAccount?.id;
|
|
}
|
|
return true;
|
|
})
|
|
.map((el) {
|
|
return SearchFieldListItem(
|
|
getBeneficiaryName(el),
|
|
item: el,
|
|
);
|
|
})
|
|
.toList(),
|
|
hint: switch (_selectedDirection) {
|
|
TransactionDirection.send =>
|
|
t.common.transaction.beneficiaryTextfieldHintSend,
|
|
TransactionDirection.receive =>
|
|
t.common.transaction.beneficiaryTextfieldHintReceive,
|
|
},
|
|
controller: _beneficiaryTextController,
|
|
selectedValue: _selectedBeneficiary,
|
|
onSuggestionTap: (beneficiary) {
|
|
setState(() => _selectedBeneficiary = beneficiary);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
child: TextField(
|
|
controller: _amountTextController,
|
|
keyboardType: TextInputType.numberWithOptions(
|
|
signed: false,
|
|
decimal: false,
|
|
),
|
|
decoration: InputDecoration(
|
|
hintText: t.common.amount,
|
|
icon: Icon(Icons.euro),
|
|
),
|
|
),
|
|
),
|
|
|
|
Row(
|
|
children: [
|
|
Text(t.common.expenseCategory.name),
|
|
|
|
Padding(
|
|
padding: EdgeInsets.only(left: 16),
|
|
child: OutlinedButton(
|
|
onPressed: () async {
|
|
final category = await showDialogOrModal(
|
|
context: context,
|
|
builder: (_) => AddExpenseCategory(),
|
|
);
|
|
if (category == null) {
|
|
return;
|
|
}
|
|
|
|
setState(() => _expenseCategory = category);
|
|
},
|
|
child: Text(
|
|
_expenseCategory?.name ?? t.common.expenseCategory.none,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
|
|
Row(
|
|
children: [
|
|
Text(t.pages.templates.addTemplate.isRecurring),
|
|
Padding(
|
|
padding: EdgeInsets.only(left: 16),
|
|
child: Switch(
|
|
value: _isRecurring,
|
|
onChanged: (value) {
|
|
setState(() {
|
|
_isRecurring = value;
|
|
});
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
Padding(
|
|
padding: EdgeInsets.only(left: 16, right: 16, top: 16),
|
|
child: SegmentedButton<Period>(
|
|
segments: [
|
|
ButtonSegment(
|
|
value: Period.days,
|
|
label: Text(t.common.period.days),
|
|
),
|
|
ButtonSegment(
|
|
value: Period.weeks,
|
|
label: Text(t.common.period.weeks),
|
|
),
|
|
ButtonSegment(
|
|
value: Period.months,
|
|
label: Text(t.common.period.months),
|
|
),
|
|
ButtonSegment(
|
|
value: Period.years,
|
|
label: Text(t.common.period.years),
|
|
),
|
|
],
|
|
selected: <Period>{_selectedPeriod},
|
|
multiSelectionEnabled: false,
|
|
onSelectionChanged:
|
|
_isRecurring
|
|
? (selection) {
|
|
setState(() => _selectedPeriod = selection.first);
|
|
}
|
|
: null,
|
|
),
|
|
),
|
|
|
|
Row(
|
|
children: [
|
|
IconButton(
|
|
icon: Icon(Icons.remove),
|
|
onPressed:
|
|
_isRecurring
|
|
? () {
|
|
if (_periodSize <= 1) {
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_periodSize--;
|
|
});
|
|
}
|
|
: null,
|
|
),
|
|
|
|
SizedBox(
|
|
width: 100,
|
|
child: Center(
|
|
child: Text(
|
|
switch (_selectedPeriod) {
|
|
Period.days => t.common.period.daysNumber(
|
|
number: _periodSize,
|
|
),
|
|
Period.weeks => t.common.period.weeksNumber(
|
|
number: _periodSize,
|
|
),
|
|
Period.months => t.common.period.monthsNumber(
|
|
number: _periodSize,
|
|
),
|
|
Period.years => t.common.period.yearsNumber(
|
|
number: _periodSize,
|
|
),
|
|
},
|
|
style: TextStyle(
|
|
color: _isRecurring ? Colors.black : Colors.grey,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
|
|
IconButton(
|
|
icon: Icon(Icons.add),
|
|
onPressed:
|
|
_isRecurring
|
|
? () {
|
|
setState(() {
|
|
_periodSize++;
|
|
});
|
|
}
|
|
: null,
|
|
),
|
|
],
|
|
),
|
|
|
|
Align(
|
|
alignment: Alignment.centerRight,
|
|
child: OutlinedButton(
|
|
onPressed: () => _submit(context),
|
|
child: Text(t.modals.add),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|