44 lines
1.0 KiB
Dart
44 lines
1.0 KiB
Dart
class Habit {
|
|
const Habit({
|
|
required this.id,
|
|
required this.title,
|
|
required this.createdAt,
|
|
required this.completedDates,
|
|
});
|
|
|
|
final String id;
|
|
final String title;
|
|
final DateTime createdAt;
|
|
final Set<String> completedDates;
|
|
|
|
Habit copyWith({String? title, Set<String>? completedDates}) {
|
|
return Habit(
|
|
id: id,
|
|
title: title ?? this.title,
|
|
createdAt: createdAt,
|
|
completedDates: completedDates ?? this.completedDates,
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'id': id,
|
|
'title': title,
|
|
'createdAt': createdAt.toIso8601String(),
|
|
'completedDates': completedDates.toList()..sort(),
|
|
};
|
|
}
|
|
|
|
static Habit fromJson(Map<String, dynamic> json) {
|
|
return Habit(
|
|
id: json['id'] as String,
|
|
title: json['title'] as String,
|
|
createdAt: DateTime.parse(json['createdAt'] as String),
|
|
completedDates:
|
|
((json['completedDates'] as List?) ?? const [])
|
|
.map((value) => value.toString())
|
|
.toSet(),
|
|
);
|
|
}
|
|
}
|