Files
tasks/lib/model/task.dart
T
2026-06-12 12:22:04 +02:00

112 lines
2.9 KiB
Dart

import 'location.dart';
class Task {
final String id;
final String title;
final String description;
final DateTime? start;
final DateTime? due;
final bool isCompleted;
final String category;
final List<Task> subtasks;
final List<DateTime> alarms;
final Location? location;
final String url;
Task({
required this.id,
required this.title,
this.description = '',
this.start,
this.due,
this.isCompleted = false,
this.category = '',
this.subtasks = const [],
this.alarms = const [],
this.location,
this.url = '',
});
Task copyWith({
String? id,
String? title,
String? description,
DateTime? start,
DateTime? due,
bool? isCompleted,
String? category,
List<Task>? subtasks,
List<DateTime>? alarms,
Location? location,
String? url,
}) {
return Task(
id: id ?? this.id,
title: title ?? this.title,
description: description ?? this.description,
start: start ?? this.start,
due: due ?? this.due,
isCompleted: isCompleted ?? this.isCompleted,
category: category ?? this.category,
subtasks: subtasks ?? this.subtasks,
alarms: alarms ?? this.alarms,
location: location ?? this.location,
url: url ?? this.url,
);
}
factory Task.fromJson(Map<String, dynamic> json) {
return Task(
id: json['id'] as String,
title: json['title'] as String,
description: json['description'] as String? ?? '',
start: json['start'] != null
? DateTime.parse(json['start'] as String)
: null,
due: json['due'] != null ? DateTime.parse(json['due'] as String) : null,
isCompleted: json['isCompleted'] as bool? ?? false,
category: json['category'] as String? ?? '',
subtasks:
(json['subtasks'] as List<dynamic>?)
?.map((e) => Task.fromJson(e as Map<String, dynamic>))
.toList() ??
[],
alarms:
(json['alarms'] as List<dynamic>?)
?.map((e) => DateTime.parse(e as String))
.toList() ??
[],
location: json['location'] != null
? Location.fromJson(json['location'] as Map<String, dynamic>)
: null,
url: json['url'] as String? ?? '',
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'title': title,
'description': description,
'start': start?.toIso8601String(),
'due': due?.toIso8601String(),
'isCompleted': isCompleted,
'category': category,
'subtasks': subtasks.map((e) => e.toJson()).toList(),
'alarms': alarms.map((e) => e.toIso8601String()).toList(),
'location': location?.toJson(),
'url': url,
};
}
@override
bool operator ==(Object other) {
if (other is! Task) return false;
return hashCode == other.hashCode;
}
@override
int get hashCode => id.hashCode;
}