Skip to content

Commit

Permalink
Fix recurring info + new end repetition selector
Browse files Browse the repository at this point in the history
  • Loading branch information
K-w-e committed May 10, 2024
1 parent 763aab4 commit fcdd3fc
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 37 deletions.
2 changes: 1 addition & 1 deletion lib/model/recurring_transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class RecurringTransaction extends BaseEntity {
RecurringTransaction(
id: id ?? this.id,
fromDate: fromDate ?? this.fromDate,
toDate: toDate ?? this.toDate,
toDate: toDate,
amount: amount ?? this.amount,
note: note ?? this.note,
recurrency: recurrency ?? this.recurrency,
Expand Down
105 changes: 75 additions & 30 deletions lib/pages/add_page/widgets/recurrence_list_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class RecurrenceListTile extends ConsumerWidget with Functions {
@override
Widget build(BuildContext context, WidgetRef ref) {
final isRecurring = ref.watch(selectedRecurringPayProvider);
final endDate = ref.watch(endDateProvider);

return Column(
children: [
Expand Down Expand Up @@ -102,35 +103,13 @@ class RecurrenceListTile extends ConsumerWidget with Functions {
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4)),
),
onPressed: () async {
FocusManager.instance.primaryFocus?.unfocus();
if (Platform.isIOS) {
showCupertinoModalPopup(
context: context,
builder: (_) => Container(
height: 300,
color: white,
child: CupertinoDatePicker(
initialDateTime: ref.watch(endDateProvider),
minimumYear: 2015,
maximumYear: 2050,
mode: CupertinoDatePickerMode.date,
onDateTimeChanged: (date) => ref.read(endDateProvider.notifier).state = date,
),
),
);
} else if (Platform.isAndroid) {
final DateTime? pickedDate = await showDatePicker(
context: context,
initialDate: ref.watch(endDateProvider),
firstDate: DateTime(2015),
lastDate: DateTime(2050),
);
if (pickedDate != null) {
ref.read(endDateProvider.notifier).state = pickedDate;
}
}
},
onPressed: () => showModalBottomSheet(
context: context,
elevation: 10,
builder: (BuildContext context) {
return const EndDateSelector();
},
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Expand All @@ -143,7 +122,7 @@ class RecurrenceListTile extends ConsumerWidget with Functions {
),
const Spacer(),
Text(
dateToString(ref.read(endDateProvider.notifier).state),
endDate != null ? dateToString(endDate) : "Never",
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
color: Theme.of(context).colorScheme.secondary),
),
Expand All @@ -161,3 +140,69 @@ class RecurrenceListTile extends ConsumerWidget with Functions {
);
}
}

class EndDateSelector extends ConsumerWidget with Functions {
const EndDateSelector({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
return Scaffold(
appBar: AppBar(),
body: ListView(
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: [
ListTile(
visualDensity: const VisualDensity(vertical: -3),
trailing: ref.watch(endDateProvider) != null
? null
: const Icon(Icons.check),
title: const Text(
"Never",
),
onTap: () => {
ref.read(endDateProvider.notifier).state = null,
Navigator.pop(context)
},
),
ListTile(
visualDensity: const VisualDensity(vertical: -3),
title: const Text("On a date"),
trailing: ref.watch(endDateProvider) != null
? const Icon(Icons.check)
: null,
subtitle: Text(ref.read(endDateProvider) != null ? dateToString(ref.read(endDateProvider.notifier).state!) : ''),
onTap: () async {
FocusManager.instance.primaryFocus?.unfocus();
if (Platform.isIOS) {
showCupertinoModalPopup(
context: context,
builder: (_) => Container(
height: 300,
color: white,
child: CupertinoDatePicker(
initialDateTime: ref.watch(endDateProvider),
minimumYear: 2015,
maximumYear: 2050,
mode: CupertinoDatePickerMode.date,
onDateTimeChanged: (date) =>
ref.read(endDateProvider.notifier).state = date,
),
),
);
} else if (Platform.isAndroid) {
final DateTime? pickedDate = await showDatePicker(
context: context,
initialDate: ref.watch(endDateProvider),
firstDate: DateTime(2015),
lastDate: DateTime(2050),
);
if (pickedDate != null) {
ref.read(endDateProvider.notifier).state = pickedDate;
}
}
})
],
));
}
}
6 changes: 5 additions & 1 deletion lib/pages/planning_page/widget/recurring_payments_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ class _RecurringPaymentSectionState extends ConsumerState<RecurringPaymentSectio
color: Theme.of(context).colorScheme.secondary,
),
onPressed: () => {
ref.read(selectedTransactionUpdateProvider.notifier).state = null,
ref.read(selectedRecurringPayProvider.notifier).state = true,
ref.read(intervalProvider.notifier).state = Recurrence.weekly,
ref.read(bankAccountProvider.notifier).state = null,
ref.read(categoryProvider.notifier).state = null,
ref.read(intervalProvider.notifier).state = Recurrence.monthly,
ref.read(endDateProvider.notifier).state = null,
Navigator.of(context).pushNamed("/add-page").then((value) => setState(() { _refreshData(); }))
},
label: Text(
Expand Down
8 changes: 3 additions & 5 deletions lib/providers/transactions_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final categoryProvider = StateProvider<CategoryTransaction?>((ref) => null);
// Recurring Payment
final selectedRecurringPayProvider = StateProvider<bool>((ref) => false);
final intervalProvider = StateProvider<Recurrence>((ref) => Recurrence.monthly);
final endDateProvider = StateProvider<DateTime>((ref) => DateTime.now().add(const Duration(days: 1)));
final endDateProvider = StateProvider<DateTime?>((ref) => null);

// Set when a transaction is selected for update
final selectedTransactionUpdateProvider = StateProvider<Transaction?>((ref) => null);
Expand Down Expand Up @@ -179,7 +179,7 @@ class AsyncTransactionsNotifier extends AutoDisposeAsyncNotifier<List<Transactio
RecurringTransaction transaction =
ref.read(selectedRecurringTransactionUpdateProvider)!.copy(
fromDate: ref.read(dateProvider),
toDate: ref.read(dateProvider).add(const Duration(days: 10)),
toDate: ref.read(endDateProvider),
recurrency: recurrency.name.toUpperCase(),
amount: amount,
note: label,
Expand Down Expand Up @@ -221,9 +221,7 @@ class AsyncTransactionsNotifier extends AutoDisposeAsyncNotifier<List<Transactio
ref.read(categoryProvider.notifier).state = await CategoryTransactionMethods().selectById(transaction.idCategory);
ref.read(bankAccountProvider.notifier).state = ref.watch(accountsProvider).value!.firstWhere((element) => element.id == transaction.idBankAccount);
ref.read(intervalProvider.notifier).state = parseRecurrence(transaction.recurrency);
if(transaction.toDate != null) {
ref.read(endDateProvider.notifier).state = transaction.toDate!;
}
ref.read(endDateProvider.notifier).state = transaction.toDate;
}
}

Expand Down

0 comments on commit fcdd3fc

Please sign in to comment.