task clearable nullables

This commit is contained in:
Paul Nettleton 2022-09-07 15:59:28 -05:00
parent 250a0e7553
commit 67acef1ba9
4 changed files with 26 additions and 14 deletions

View File

@ -138,8 +138,11 @@ class Task {
DateTime? created, DateTime? created,
DateTime? updated, DateTime? updated,
DateTime? dueDate, DateTime? dueDate,
bool? clearDueDate,
DateTime? startDate, DateTime? startDate,
bool? clearStartDate,
DateTime? endDate, DateTime? endDate,
bool? clearEndDate,
List<DateTime>? reminderDates, List<DateTime>? reminderDates,
String? title, String? title,
String? description, String? description,
@ -150,6 +153,7 @@ class Task {
double? kanbanPosition, double? kanbanPosition,
User? createdBy, User? createdBy,
Duration? repeatAfter, Duration? repeatAfter,
bool? clearRepeatAfter,
List<Task>? subtasks, List<Task>? subtasks,
List<Label>? labels, List<Label>? labels,
List<TaskAttachment>? attachments, List<TaskAttachment>? attachments,
@ -162,9 +166,9 @@ class Task {
bucketId: bucketId ?? this.bucketId, bucketId: bucketId ?? this.bucketId,
created: created ?? this.created, created: created ?? this.created,
updated: updated ?? this.updated, updated: updated ?? this.updated,
dueDate: dueDate ?? this.dueDate, dueDate: (clearDueDate ?? false) ? null : (dueDate ?? this.dueDate),
startDate: startDate ?? this.startDate, startDate: (clearStartDate ?? false) ? null : (startDate ?? this.startDate),
endDate: endDate ?? this.endDate, endDate: (clearEndDate ?? false) ? null : (endDate ?? this.endDate),
reminderDates: reminderDates ?? this.reminderDates, reminderDates: reminderDates ?? this.reminderDates,
title: title ?? this.title, title: title ?? this.title,
description: description ?? this.description, description: description ?? this.description,
@ -173,7 +177,7 @@ class Task {
color: (resetColor ?? false) ? null : (color ?? this.color), color: (resetColor ?? false) ? null : (color ?? this.color),
kanbanPosition: kanbanPosition ?? this.kanbanPosition, kanbanPosition: kanbanPosition ?? this.kanbanPosition,
createdBy: createdBy ?? this.createdBy, createdBy: createdBy ?? this.createdBy,
repeatAfter: repeatAfter ?? this.repeatAfter, repeatAfter: (clearRepeatAfter ?? false) ? null : (repeatAfter ?? this.repeatAfter),
subtasks: subtasks ?? this.subtasks, subtasks: subtasks ?? this.subtasks,
labels: labels ?? this.labels, labels: labels ?? this.labels,
attachments: attachments ?? this.attachments, attachments: attachments ?? this.attachments,

View File

@ -370,8 +370,11 @@ class _TaskEditPageState extends State<TaskEditPage> {
description: _description, description: _description,
reminderDates: _reminderDates, reminderDates: _reminderDates,
dueDate: _dueDate, dueDate: _dueDate,
clearDueDate: _dueDate == null,
startDate: _startDate, startDate: _startDate,
clearStartDate: _startDate == null,
endDate: _endDate, endDate: _endDate,
clearEndDate: _endDate == null,
priority: _priority, priority: _priority,
repeatAfter: _repeatAfter, repeatAfter: _repeatAfter,
labels: _labels, labels: _labels,

View File

@ -1,4 +1,4 @@
getRepeatAfterTypeFromDuration(Duration? repeatAfter) { String? getRepeatAfterTypeFromDuration(Duration? repeatAfter) {
if (repeatAfter == null || repeatAfter.inSeconds == 0) { if (repeatAfter == null || repeatAfter.inSeconds == 0) {
return null; return null;
} }
@ -18,7 +18,7 @@ getRepeatAfterTypeFromDuration(Duration? repeatAfter) {
return 'Hours'; return 'Hours';
} }
getRepeatAfterValueFromDuration(Duration? repeatAfter) { int? getRepeatAfterValueFromDuration(Duration? repeatAfter) {
if (repeatAfter == null || repeatAfter.inSeconds == 0) { if (repeatAfter == null || repeatAfter.inSeconds == 0) {
return null; return null;
} }
@ -43,13 +43,16 @@ getRepeatAfterValueFromDuration(Duration? repeatAfter) {
return repeatAfter.inHours; return repeatAfter.inHours;
} }
getDurationFromType(String? value, String? type) { Duration? getDurationFromType(String? value, String? type) {
// Return an empty duration if either of the values is not set // Return an empty duration if either of the values is not set
if (value == null || value == '' || type == null || type == '') { if (value == null || value == '' || type == null || type == '') {
return Duration(); return Duration();
} }
int val = int.parse(value); int? val = int.tryParse(value);
if (val == null) {
return null;
}
switch (type) { switch (type) {
case 'Hours': case 'Hours':
@ -63,4 +66,6 @@ getDurationFromType(String? value, String? type) {
case 'Years': case 'Years':
return Duration(days: val * 365); return Duration(days: val * 365);
} }
return null;
} }

View File

@ -45,32 +45,32 @@ void main() {
}); });
test('Hours to duration', () { test('Hours to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Hours'); Duration? parsedDuration = getDurationFromType('6', 'Hours');
expect(parsedDuration, Duration(hours: 6)); expect(parsedDuration, Duration(hours: 6));
}); });
test('Days to duration', () { test('Days to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Days'); Duration? parsedDuration = getDurationFromType('6', 'Days');
expect(parsedDuration, Duration(days: 6)); expect(parsedDuration, Duration(days: 6));
}); });
test('Weeks to duration', () { test('Weeks to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Weeks'); Duration? parsedDuration = getDurationFromType('6', 'Weeks');
expect(parsedDuration, Duration(days: 6 * 7)); expect(parsedDuration, Duration(days: 6 * 7));
}); });
test('Months to duration', () { test('Months to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Months'); Duration? parsedDuration = getDurationFromType('6', 'Months');
expect(parsedDuration, Duration(days: 6 * 30)); expect(parsedDuration, Duration(days: 6 * 30));
}); });
test('Years to duration', () { test('Years to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Years'); Duration? parsedDuration = getDurationFromType('6', 'Years');
expect(parsedDuration, Duration(days: 6 * 365)); expect(parsedDuration, Duration(days: 6 * 365));
}); });
test('null to duration', () { test('null to duration', () {
Duration parsedDuration = getDurationFromType(null, null); Duration? parsedDuration = getDurationFromType(null, null);
expect(parsedDuration, Duration()); expect(parsedDuration, Duration());
}); });
} }