Allow deleting templates
This commit is contained in:
@@ -1,26 +1,24 @@
|
||||
import 'dart:math';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:okane/database/collections/recurrent.dart';
|
||||
import 'package:okane/database/database.dart';
|
||||
import 'package:okane/ui/state/core.dart';
|
||||
import 'package:okane/ui/utils.dart';
|
||||
import 'package:okane/ui/widgets/add_transaction.dart';
|
||||
|
||||
class UpcomingTransactionsCard extends StatelessWidget {
|
||||
const UpcomingTransactionsCard({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final bloc = GetIt.I.get<CoreCubit>();
|
||||
return BlocBuilder<CoreCubit, CoreState>(
|
||||
builder: (context, state) {
|
||||
final today = DateTime.now();
|
||||
final upcomingRaw =
|
||||
state.recurringTransactions.where((t) {
|
||||
if (t.lastExecution == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return today.difference(t.lastExecution!).inDays <=
|
||||
(t.days * 1.5).toInt();
|
||||
}).toList();
|
||||
state.recurringTransactions.where((t) => t.isDue(today)).toList();
|
||||
final List<RecurringTransaction> upcoming =
|
||||
upcomingRaw.isEmpty
|
||||
? List.empty()
|
||||
@@ -53,7 +51,23 @@ class UpcomingTransactionsCard extends StatelessWidget {
|
||||
),
|
||||
trailing: IconButton(
|
||||
icon: Icon(Icons.play_arrow),
|
||||
onPressed: () {},
|
||||
onPressed: () {
|
||||
showDialogOrModal(
|
||||
context: context,
|
||||
builder:
|
||||
(context) => AddTransactionWidget(
|
||||
activeAccountItem: bloc.activeAccount!,
|
||||
template: t.template.value!,
|
||||
onAdd: (transaction) async {
|
||||
// Update the recurring template
|
||||
print(transaction.date);
|
||||
t.lastExecution = transaction.date;
|
||||
await upsertRecurringTransaction(t);
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:grouped_list/grouped_list.dart';
|
||||
import 'package:okane/database/database.dart';
|
||||
import 'package:okane/ui/state/core.dart';
|
||||
import 'package:okane/ui/utils.dart';
|
||||
import 'package:okane/ui/widgets/add_template.dart';
|
||||
@@ -30,7 +31,25 @@ class TemplateListState extends State<TemplateListPage> {
|
||||
itemCount: nonRecurringTemplates.length,
|
||||
itemBuilder: (context, index) {
|
||||
final template = nonRecurringTemplates[index];
|
||||
return ListTile(title: Text(template.name));
|
||||
return ListTile(
|
||||
title: Text(template.name),
|
||||
trailing: IconButton(
|
||||
icon: Icon(Icons.delete),
|
||||
color: Colors.red,
|
||||
onPressed: () async {
|
||||
final result = await confirm(
|
||||
context,
|
||||
"Remove Template",
|
||||
"Are you sure you want to remove the template '${template.name}'",
|
||||
);
|
||||
if (!result) {
|
||||
return;
|
||||
}
|
||||
|
||||
await deleteTransactionTemplate(template);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
SliverToBoxAdapter(child: Text("Recurring")),
|
||||
@@ -38,31 +57,32 @@ class TemplateListState extends State<TemplateListPage> {
|
||||
itemCount: state.recurringTransactions.length,
|
||||
itemBuilder: (context, index) {
|
||||
final template = state.recurringTransactions[index];
|
||||
return ListTile(title: Text(template.template.value!.name));
|
||||
return ListTile(
|
||||
title: Text(template.template.value!.name),
|
||||
trailing: IconButton(
|
||||
icon: Icon(Icons.delete, color: Colors.red),
|
||||
onPressed: () async {
|
||||
final result = await confirm(
|
||||
context,
|
||||
"Remove Template",
|
||||
"Are you sure you want to remove the template '${template.template.value!.name}'",
|
||||
);
|
||||
if (!result) {
|
||||
return;
|
||||
}
|
||||
|
||||
await deleteRecurringTransactionTemplate(template);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
/*Padding(
|
||||
padding: EdgeInsets.only(top: 16),
|
||||
child: ListView.builder(
|
||||
itemCount: state.recurringTransactions.length,
|
||||
shrinkWrap: true,
|
||||
itemBuilder: (ctx, idx) {
|
||||
print(idx);
|
||||
return ListTile(
|
||||
title: Text(
|
||||
state.recurringTransactions[idx].template.value!.name,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),*/
|
||||
Positioned(
|
||||
right: 16,
|
||||
bottom: 16,
|
||||
child: FloatingActionButton(
|
||||
child: Icon(Icons.add),
|
||||
onPressed:
|
||||
account == null
|
||||
? () {}
|
||||
@@ -80,6 +100,7 @@ class TemplateListState extends State<TemplateListPage> {
|
||||
showDragHandle: true,
|
||||
);
|
||||
},
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -83,58 +83,10 @@ class TransactionListState extends State<TransactionListPage> {
|
||||
),
|
||||
],
|
||||
),
|
||||
/*Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: EdgeInsets.only(top: 16),
|
||||
child: GroupedListView(
|
||||
elements: state.transactions,
|
||||
reverse: true,
|
||||
groupBy:
|
||||
(Transaction item) => formatDateTime(item.date),
|
||||
groupHeaderBuilder:
|
||||
(item) => Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black.withAlpha(170),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(4),
|
||||
child: Text(
|
||||
formatDateTime(item.date),
|
||||
style: TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
shrinkWrap: true,
|
||||
indexedItemBuilder:
|
||||
(ctx, item, idx) => TransactionCard(
|
||||
transaction: item,
|
||||
onTap: () {
|
||||
GetIt.I.get<CoreCubit>().setActiveTransaction(
|
||||
item,
|
||||
);
|
||||
if (getScreenSize(ctx) == ScreenSize.small) {
|
||||
Navigator.of(
|
||||
context,
|
||||
).pushNamed("/transactions/details");
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),*/
|
||||
Positioned(
|
||||
right: 16,
|
||||
bottom: 16,
|
||||
child: FloatingActionButton(
|
||||
child: Icon(Icons.add),
|
||||
onPressed:
|
||||
account == null
|
||||
? () {}
|
||||
@@ -144,7 +96,7 @@ class TransactionListState extends State<TransactionListPage> {
|
||||
builder:
|
||||
(ctx) => AddTransactionWidget(
|
||||
activeAccountItem: account,
|
||||
onAdd: () {
|
||||
onAdd: (_) {
|
||||
setState(() {});
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
@@ -152,6 +104,7 @@ class TransactionListState extends State<TransactionListPage> {
|
||||
showDragHandle: true,
|
||||
);
|
||||
},
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user