flutter_notification_center/lib/src/models/notification.dart

147 lines
4.5 KiB
Dart
Raw Normal View History

2024-04-10 10:55:26 +02:00
import 'package:flutter/material.dart';
2024-04-09 09:59:58 +02:00
/// Enum representing the interval at which notifications occur.
2024-04-04 13:10:51 +02:00
enum OcurringInterval {
2024-04-09 09:59:58 +02:00
/// Notifications occur daily.
2024-04-03 14:38:40 +02:00
daily,
2024-04-09 09:59:58 +02:00
/// Notifications occur weekly.
2024-04-03 14:38:40 +02:00
weekly,
2024-04-09 09:59:58 +02:00
/// Notifications occur monthly.
2024-04-03 14:38:40 +02:00
monthly,
2024-04-09 09:59:58 +02:00
/// Debug option for testing purposes.
debug,
2024-04-03 14:38:40 +02:00
}
2024-04-09 09:59:58 +02:00
/// Model class representing a notification.
2024-04-03 14:38:40 +02:00
class NotificationModel {
2024-04-09 09:59:58 +02:00
/// Unique identifier for the notification.
final String id;
/// Title of the notification.
final String title;
/// Body content of the notification.
final String body;
/// Date and time when the notification was pushed.
DateTime? dateTimePushed;
/// Date and time when the notification is scheduled for.
DateTime? scheduledFor;
/// Indicates if the notification is recurring.
final bool recurring;
/// Interval at which the notification occurs, applicable if it's recurring.
final OcurringInterval? occuringInterval;
/// Indicates if the notification is pinned.
final bool isPinned;
/// Indicates if the notification has been read.
final bool isRead;
2024-04-10 10:55:26 +02:00
/// Icon to be displayed with the notification.
final IconData icon;
2024-04-09 09:59:58 +02:00
/// Constructs a new NotificationModel instance.
///
/// [id]: Unique identifier for the notification.
/// [title]: Title of the notification.
/// [body]: Body content of the notification.
/// [dateTimePushed]: Date and time when the notification was pushed.
/// [scheduledFor]: Date and time when the notification is scheduled for.
/// [recurring]: Indicates if the notification is recurring.
/// [occuringInterval]: Interval at which the notification occurs, applicable if it's recurring.
/// [isPinned]: Indicates if the notification is pinned.
/// [isRead]: Indicates if the notification has been read.
2024-04-03 14:38:40 +02:00
NotificationModel({
2024-04-04 13:10:51 +02:00
required this.id,
2024-04-03 14:38:40 +02:00
required this.title,
required this.body,
2024-04-04 13:10:51 +02:00
this.dateTimePushed,
2024-04-03 14:38:40 +02:00
this.scheduledFor,
2024-04-04 13:10:51 +02:00
this.recurring = false,
this.occuringInterval,
2024-04-09 09:59:58 +02:00
this.isPinned = false,
this.isRead = false,
2024-04-10 10:55:26 +02:00
this.icon = Icons.notifications,
2024-04-03 14:38:40 +02:00
});
2024-04-09 09:59:58 +02:00
/// Override toString() to provide custom string representation
2024-04-04 13:10:51 +02:00
@override
String toString() {
2024-04-10 10:55:26 +02:00
return 'NotificationModel{id: $id, title: $title, body: $body, dateTimePushed: $dateTimePushed, scheduledFor: $scheduledFor, recurring: $recurring, occuringInterval: $occuringInterval, isPinned: $isPinned, icon: $icon}';
2024-04-09 09:59:58 +02:00
}
/// Method to create a NotificationModel object from JSON data
static NotificationModel fromJson(Map<String, dynamic> json) {
return NotificationModel(
id: json['id'],
title: json['title'],
body: json['body'],
dateTimePushed: json['dateTimePushed'] != null
? DateTime.parse(json['dateTimePushed'])
: null,
scheduledFor: json['scheduledFor'] != null
? DateTime.parse(json['scheduledFor'])
: null,
recurring: json['recurring'] ?? false,
occuringInterval: json['occuringInterval'] != null
? OcurringInterval.values[json['occuringInterval']]
: null,
isPinned: json['isPinned'] ?? false,
isRead: json['isRead'] ?? false,
2024-04-10 10:55:26 +02:00
icon: json['icon'] != null
? IconData(json['icon'], fontFamily: Icons.notifications.fontFamily)
: Icons.notifications,
2024-04-09 09:59:58 +02:00
);
}
/// Convert the NotificationModel object to a Map.
Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'body': body,
'dateTimePushed': dateTimePushed?.toIso8601String(),
'scheduledFor': scheduledFor?.toIso8601String(),
'recurring': recurring,
'occuringInterval': occuringInterval?.index,
'isPinned': isPinned,
'isRead': isRead,
2024-04-10 10:55:26 +02:00
'icon': icon.codePoint,
2024-04-09 09:59:58 +02:00
};
}
/// Create a copy of the NotificationModel with some fields replaced.
NotificationModel copyWith({
String? id,
String? title,
String? body,
DateTime? dateTimePushed,
DateTime? scheduledFor,
bool? recurring,
OcurringInterval? occuringInterval,
bool? isPinned,
bool? isRead,
2024-04-10 10:55:26 +02:00
IconData? icon,
2024-04-09 09:59:58 +02:00
}) {
return NotificationModel(
id: id ?? this.id,
title: title ?? this.title,
body: body ?? this.body,
dateTimePushed: dateTimePushed ?? this.dateTimePushed,
scheduledFor: scheduledFor ?? this.scheduledFor,
recurring: recurring ?? this.recurring,
occuringInterval: occuringInterval ?? this.occuringInterval,
isPinned: isPinned ?? this.isPinned,
isRead: isRead ?? this.isRead,
2024-04-10 10:55:26 +02:00
icon: icon ?? this.icon,
2024-04-09 09:59:58 +02:00
);
2024-04-04 13:10:51 +02:00
}
2024-04-03 14:38:40 +02:00
}