Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null safety fixes #17

Merged
merged 4 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/models/bucket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Bucket {
final List<Task> tasks;

Bucket({
this.id = -1,
this.id = 0,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to 0 as discussed in #16.

required this.listId,
required this.title,
this.position,
Expand Down Expand Up @@ -47,7 +47,7 @@ class Bucket {
.toList();

toJSON() => {
'id': id != -1 ? id : null,
'id': id,
'list_id': listId,
'title': title,
'position': position,
Expand Down
4 changes: 2 additions & 2 deletions lib/models/label.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Label {
late final Color textColor = color != null && color!.computeLuminance() <= 0.5 ? vLabelLight : vLabelDark;

Label({
this.id = -1,
this.id = 0,
required this.title,
this.description = '',
this.color,
Expand All @@ -35,7 +35,7 @@ class Label {
createdBy = User.fromJson(json['created_by']);

toJSON() => {
'id': id != -1 ? id : null,
'id': id,
'title': title,
'description': description,
'hex_color':
Expand Down
4 changes: 2 additions & 2 deletions lib/models/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TaskList {
final bool isFavorite;

TaskList({
this.id = -1,
this.id = 0,
required this.title,
required this.namespaceId,
this.description = '',
Expand Down Expand Up @@ -39,7 +39,7 @@ class TaskList {

toJSON() {
return {
'id': id != -1 ? id : null,
'id': id,
'title': title,
'description': description,
'owner': owner.toJSON(),
Expand Down
4 changes: 2 additions & 2 deletions lib/models/namespace.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Namespace {
final User owner;

Namespace({
this.id = -1,
this.id = 0,
DateTime? created,
DateTime? updated,
required this.title,
Expand All @@ -25,7 +25,7 @@ class Namespace {
owner = User.fromJson(json['owner']);

Map<String, dynamic> toJSON() => {
'id': id != -1 ? id : null,
'id': id,
'created': created.toUtc().toIso8601String(),
'updated': updated.toUtc().toIso8601String(),
'title': title,
Expand Down
23 changes: 14 additions & 9 deletions lib/models/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,25 @@ class Task {
final int? parentTaskId, priority, bucketId;
final int listId;
final DateTime created, updated;
final DateTime? dueDate, startDate, endDate;
DateTime? dueDate, startDate, endDate;
final List<DateTime> reminderDates;
final String identifier;
final String title, description;
final bool done;
final Color? color;
Color? color;
final double? kanbanPosition;
final User createdBy;
final Duration? repeatAfter;
Duration? repeatAfter;
final List<Task> subtasks;
final List<Label> labels;
final List<TaskAttachment> attachments;
// TODO: add position(?)

late final checkboxStatistics = getCheckboxStatistics(description);
late final hasCheckboxes = checkboxStatistics.total != 0;
late final textColor = (color != null && color!.computeLuminance() > 0.5) ? Colors.black : Colors.white;
late final hasDueDate = dueDate?.year != 1;

Task({
this.id = -1,
this.id = 0,
this.identifier = '',
this.title = '',
this.description = '',
Expand All @@ -59,6 +57,14 @@ class Task {

bool loading = false;

Color get textColor {
if (color != null && color!.computeLuminance() > 0.5) {
return Colors.black;
}
return Colors.white;
}
bool get hasDueDate => dueDate?.year != 1;

Task.fromJson(Map<String, dynamic> json)
: id = json['id'],
title = json['title'],
Expand Down Expand Up @@ -104,7 +110,7 @@ class Task {
createdBy = User.fromJson(json['created_by']);

toJSON() => {
'id': id != -1 ? id : null,
'id': id,
'title': title,
'description': description,
'identifier': identifier.isNotEmpty ? identifier : null,
Expand Down Expand Up @@ -146,7 +152,6 @@ class Task {
String? identifier,
bool? done,
Color? color,
bool? resetColor,
double? kanbanPosition,
User? createdBy,
Duration? repeatAfter,
Expand All @@ -170,7 +175,7 @@ class Task {
description: description ?? this.description,
identifier: identifier ?? this.identifier,
done: done ?? this.done,
color: (resetColor ?? false) ? null : (color ?? this.color),
color: color ?? this.color,
kanbanPosition: kanbanPosition ?? this.kanbanPosition,
createdBy: createdBy ?? this.createdBy,
repeatAfter: repeatAfter ?? this.repeatAfter,
Expand Down
4 changes: 2 additions & 2 deletions lib/models/taskAttachment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TaskAttachment {
// TODO: add file

TaskAttachment({
this.id = -1,
this.id = 0,
required this.taskId,
DateTime? created,
required this.createdBy,
Expand All @@ -23,7 +23,7 @@ class TaskAttachment {
createdBy = User.fromJson(json['created_by']);

toJSON() => {
'id': id != -1 ? id : null,
'id': id,
'task_id': taskId,
'created': created.toUtc().toIso8601String(),
'created_by': createdBy.toJSON(),
Expand Down
4 changes: 2 additions & 2 deletions lib/models/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class User {
final DateTime created, updated;

User({
this.id = -1,
this.id = 0,
this.name = '',
required this.username,
DateTime? created,
Expand All @@ -23,7 +23,7 @@ class User {
updated = DateTime.parse(json['updated']);

toJSON() => {
'id': id != -1 ? id : null,
'id': id,
'name': name,
'username': username,
'created': created.toUtc().toIso8601String(),
Expand Down
15 changes: 7 additions & 8 deletions lib/pages/list/task_edit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -365,19 +365,18 @@ class _TaskEditPageState extends State<TaskEditPage> {
// Removes all reminders with no value set.
_reminderDates.removeWhere((d) => d == DateTime(0));

Task updatedTask = widget.task.copyWith(
final updatedTask = widget.task.copyWith(
title: _title,
description: _description,
reminderDates: _reminderDates,
dueDate: _dueDate,
startDate: _startDate,
endDate: _endDate,
priority: _priority,
repeatAfter: _repeatAfter,
labels: _labels,
color: _resetColor ? null : (_color ?? widget.task.color),
resetColor: _resetColor,
);
)
..dueDate = _dueDate
..startDate = _startDate
..endDate = _endDate
..color = _resetColor ? null : (_color ?? widget.task.color)
..repeatAfter = _repeatAfter;

// update the labels
await VikunjaGlobal.of(context)
Expand Down
13 changes: 9 additions & 4 deletions lib/utils/repeat_after_parse.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
getRepeatAfterTypeFromDuration(Duration? repeatAfter) {
String? getRepeatAfterTypeFromDuration(Duration? repeatAfter) {
if (repeatAfter == null || repeatAfter.inSeconds == 0) {
return null;
}
Expand All @@ -18,7 +18,7 @@ getRepeatAfterTypeFromDuration(Duration? repeatAfter) {
return 'Hours';
}

getRepeatAfterValueFromDuration(Duration? repeatAfter) {
int? getRepeatAfterValueFromDuration(Duration? repeatAfter) {
if (repeatAfter == null || repeatAfter.inSeconds == 0) {
return null;
}
Expand All @@ -43,13 +43,16 @@ getRepeatAfterValueFromDuration(Duration? repeatAfter) {
return repeatAfter.inHours;
}

getDurationFromType(String? value, String? type) {
Duration? getDurationFromType(String? value, String? type) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added return types to make it less ambiguous.

// 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':
Expand All @@ -63,4 +66,6 @@ getDurationFromType(String? value, String? type) {
case 'Years':
return Duration(days: val * 365);
}

return null;
}
35 changes: 14 additions & 21 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.2"
version: "2.9.0"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -70,14 +70,7 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
version: "1.2.1"
checked_yaml:
dependency: transitive
description:
Expand All @@ -98,7 +91,7 @@ packages:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
collection:
dependency: transitive
description:
Expand Down Expand Up @@ -168,7 +161,7 @@ packages:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.3.1"
ffi:
dependency: transitive
description:
Expand Down Expand Up @@ -400,21 +393,21 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.11"
version: "0.12.12"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.4"
version: "0.1.5"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0"
mime:
dependency: transitive
description:
Expand Down Expand Up @@ -449,7 +442,7 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
path_drawing:
dependency: transitive
description:
Expand Down Expand Up @@ -594,7 +587,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.2"
version: "1.9.0"
stack_trace:
dependency: transitive
description:
Expand All @@ -615,35 +608,35 @@ packages:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.2.1"
test:
dependency: "direct dev"
description:
name: test
url: "https://pub.dartlang.org"
source: hosted
version: "1.21.1"
version: "1.21.4"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.9"
version: "0.4.12"
test_core:
dependency: transitive
description:
name: test_core
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.13"
version: "0.4.16"
timezone:
dependency: transitive
description:
Expand Down
Loading