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

View File

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

View File

@ -1,4 +1,4 @@
getRepeatAfterTypeFromDuration(Duration? repeatAfter) {
String? getRepeatAfterTypeFromDuration(Duration? repeatAfter) {
if (repeatAfter == null || repeatAfter.inSeconds == 0) {
return null;
}
@ -18,7 +18,7 @@ getRepeatAfterTypeFromDuration(Duration? repeatAfter) {
return 'Hours';
}
getRepeatAfterValueFromDuration(Duration? repeatAfter) {
int? getRepeatAfterValueFromDuration(Duration? repeatAfter) {
if (repeatAfter == null || repeatAfter.inSeconds == 0) {
return null;
}
@ -43,13 +43,16 @@ getRepeatAfterValueFromDuration(Duration? repeatAfter) {
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
if (value == null || value == '' || type == null || type == '') {
return Duration();
}
int val = int.parse(value);
int? val = int.tryParse(value);
if (val == null) {
return null;
}
switch (type) {
case 'Hours':
@ -63,4 +66,6 @@ getDurationFromType(String? value, String? type) {
case 'Years':
return Duration(days: val * 365);
}
return null;
}

View File

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