add noti
This commit is contained in:
@@ -1,56 +1,45 @@
|
||||
import 'dart:convert';
|
||||
import 'package:hive_ce/hive.dart';
|
||||
import 'package:worker/core/constants/storage_constants.dart';
|
||||
/// Notification Model
|
||||
///
|
||||
/// Converts JSON data from API to domain entity.
|
||||
library;
|
||||
|
||||
part 'notification_model.g.dart';
|
||||
import 'package:worker/features/notifications/domain/entities/notification.dart';
|
||||
|
||||
@HiveType(typeId: HiveTypeIds.notificationModel)
|
||||
class NotificationModel extends HiveObject {
|
||||
NotificationModel({required this.notificationId, required this.userId, required this.type, required this.title, required this.message, this.data, required this.isRead, required this.isPushed, required this.createdAt, this.readAt});
|
||||
|
||||
@HiveField(0) final String notificationId;
|
||||
@HiveField(1) final String userId;
|
||||
@HiveField(2) final String type;
|
||||
@HiveField(3) final String title;
|
||||
@HiveField(4) final String message;
|
||||
@HiveField(5) final String? data;
|
||||
@HiveField(6) final bool isRead;
|
||||
@HiveField(7) final bool isPushed;
|
||||
@HiveField(8) final DateTime createdAt;
|
||||
@HiveField(9) final DateTime? readAt;
|
||||
/// Notification Model
|
||||
///
|
||||
/// Handles JSON serialization/deserialization.
|
||||
class NotificationModel {
|
||||
/// Convert JSON to Notification entity
|
||||
static Notification fromJson(Map<String, dynamic> json) {
|
||||
return Notification(
|
||||
notificationId: json['notification_id'] as String,
|
||||
userId: json['user_id'] as String,
|
||||
type: json['type'] as String,
|
||||
title: json['title'] as String,
|
||||
message: json['message'] as String,
|
||||
data: json['data'] as Map<String, dynamic>?,
|
||||
isRead: json['is_read'] as bool? ?? false,
|
||||
isPushed: json['is_pushed'] as bool? ?? false,
|
||||
createdAt: DateTime.parse(json['created_at'] as String),
|
||||
readAt: json['read_at'] != null
|
||||
? DateTime.parse(json['read_at'] as String)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
factory NotificationModel.fromJson(Map<String, dynamic> json) => NotificationModel(
|
||||
notificationId: json['notification_id'] as String,
|
||||
userId: json['user_id'] as String,
|
||||
type: json['type'] as String,
|
||||
title: json['title'] as String,
|
||||
message: json['message'] as String,
|
||||
data: json['data'] != null ? jsonEncode(json['data']) : null,
|
||||
isRead: json['is_read'] as bool? ?? false,
|
||||
isPushed: json['is_pushed'] as bool? ?? false,
|
||||
createdAt: DateTime.parse(json['created_at']?.toString() ?? ''),
|
||||
readAt: json['read_at'] != null ? DateTime.parse(json['read_at']?.toString() ?? '') : null,
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'notification_id': notificationId,
|
||||
'user_id': userId,
|
||||
'type': type,
|
||||
'title': title,
|
||||
'message': message,
|
||||
'data': data != null ? jsonDecode(data!) : null,
|
||||
'is_read': isRead,
|
||||
'is_pushed': isPushed,
|
||||
'created_at': createdAt.toIso8601String(),
|
||||
'read_at': readAt?.toIso8601String(),
|
||||
};
|
||||
|
||||
Map<String, dynamic>? get dataMap {
|
||||
if (data == null) return null;
|
||||
try {
|
||||
return jsonDecode(data!) as Map<String, dynamic>;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
/// Convert Notification entity to JSON
|
||||
static Map<String, dynamic> toJson(Notification notification) {
|
||||
return {
|
||||
'notification_id': notification.notificationId,
|
||||
'user_id': notification.userId,
|
||||
'type': notification.type,
|
||||
'title': notification.title,
|
||||
'message': notification.message,
|
||||
'data': notification.data,
|
||||
'is_read': notification.isRead,
|
||||
'is_pushed': notification.isPushed,
|
||||
'created_at': notification.createdAt.toIso8601String(),
|
||||
'read_at': notification.readAt?.toIso8601String(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'notification_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class NotificationModelAdapter extends TypeAdapter<NotificationModel> {
|
||||
@override
|
||||
final typeId = 20;
|
||||
|
||||
@override
|
||||
NotificationModel read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return NotificationModel(
|
||||
notificationId: fields[0] as String,
|
||||
userId: fields[1] as String,
|
||||
type: fields[2] as String,
|
||||
title: fields[3] as String,
|
||||
message: fields[4] as String,
|
||||
data: fields[5] as String?,
|
||||
isRead: fields[6] as bool,
|
||||
isPushed: fields[7] as bool,
|
||||
createdAt: fields[8] as DateTime,
|
||||
readAt: fields[9] as DateTime?,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, NotificationModel obj) {
|
||||
writer
|
||||
..writeByte(10)
|
||||
..writeByte(0)
|
||||
..write(obj.notificationId)
|
||||
..writeByte(1)
|
||||
..write(obj.userId)
|
||||
..writeByte(2)
|
||||
..write(obj.type)
|
||||
..writeByte(3)
|
||||
..write(obj.title)
|
||||
..writeByte(4)
|
||||
..write(obj.message)
|
||||
..writeByte(5)
|
||||
..write(obj.data)
|
||||
..writeByte(6)
|
||||
..write(obj.isRead)
|
||||
..writeByte(7)
|
||||
..write(obj.isPushed)
|
||||
..writeByte(8)
|
||||
..write(obj.createdAt)
|
||||
..writeByte(9)
|
||||
..write(obj.readAt);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is NotificationModelAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
Reference in New Issue
Block a user