add noti
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
/// Notification Local Data Source
|
||||
///
|
||||
/// Provides mock notification data for development.
|
||||
/// Simulates API responses with JSON data.
|
||||
library;
|
||||
|
||||
/// Notification Local Data Source
|
||||
///
|
||||
/// Returns raw JSON data as if from API.
|
||||
class NotificationLocalDataSource {
|
||||
/// Mock notifications data (as JSON from API)
|
||||
final List<Map<String, dynamic>> _mockNotifications = [
|
||||
// Points earned (General - unread)
|
||||
{
|
||||
'notification_id': 'notif_001',
|
||||
'user_id': 'user_001',
|
||||
'type': 'loyalty_points_earned',
|
||||
'title': 'Chúc mừng! Bạn vừa nhận 500 điểm thưởng',
|
||||
'message':
|
||||
'Hoàn thành đơn hàng DH2023120801 và nhận 500 điểm vào tài khoản.',
|
||||
'data': {'points': 500, 'order_id': 'DH2023120801'},
|
||||
'is_read': false,
|
||||
'is_pushed': true,
|
||||
'created_at': '2025-11-03T05:00:00.000Z',
|
||||
'read_at': null,
|
||||
},
|
||||
|
||||
// Promotion (General - unread)
|
||||
{
|
||||
'notification_id': 'notif_002',
|
||||
'user_id': 'user_001',
|
||||
'type': 'promotion_flash_sale',
|
||||
'title': 'Flash Sale cuối năm - Giảm đến 50%',
|
||||
'message':
|
||||
'Chương trình khuyến mãi đặc biệt chỉ còn 2 ngày. Nhanh tay săn ngay!',
|
||||
'data': {'promotion_id': 'PROMO_FLASH_DEC'},
|
||||
'is_read': false,
|
||||
'is_pushed': true,
|
||||
'created_at': '2025-11-03T02:00:00.000Z',
|
||||
'read_at': null,
|
||||
},
|
||||
|
||||
// Order shipping (Order - unread)
|
||||
{
|
||||
'notification_id': 'notif_003',
|
||||
'user_id': 'user_001',
|
||||
'type': 'order_shipping',
|
||||
'title': 'Đơn hàng đang được giao',
|
||||
'message':
|
||||
'Đơn hàng DH2023120701 đang trên đường giao đến bạn. Dự kiến giao trong hôm nay.',
|
||||
'data': {'order_id': 'DH2023120701'},
|
||||
'is_read': false,
|
||||
'is_pushed': true,
|
||||
'created_at': '2025-11-02T07:00:00.000Z',
|
||||
'read_at': null,
|
||||
},
|
||||
|
||||
// Tier upgrade (General - read)
|
||||
{
|
||||
'notification_id': 'notif_004',
|
||||
'user_id': 'user_001',
|
||||
'type': 'loyalty_tier_upgrade',
|
||||
'title': 'Sắp lên hạng Platinum',
|
||||
'message':
|
||||
'Bạn chỉ còn 2,250 điểm nữa để đạt hạng Platinum với nhiều ưu đãi hấp dẫn.',
|
||||
'data': {
|
||||
'current_tier': 'gold',
|
||||
'next_tier': 'platinum',
|
||||
'points_needed': 2250
|
||||
},
|
||||
'is_read': true,
|
||||
'is_pushed': true,
|
||||
'created_at': '2025-11-01T07:00:00.000Z',
|
||||
'read_at': '2025-11-02T19:00:00.000Z',
|
||||
},
|
||||
|
||||
// Event (General - read)
|
||||
{
|
||||
'notification_id': 'notif_005',
|
||||
'user_id': 'user_001',
|
||||
'type': 'event_invitation',
|
||||
'title': 'Sự kiện VIP sắp diễn ra',
|
||||
'message':
|
||||
'Mời bạn tham gia sự kiện ra mắt bộ sưu tập gạch mới vào 15/12/2023 tại showroom.',
|
||||
'data': {'event_id': 'EVENT_DEC_2023', 'date': '2023-12-15'},
|
||||
'is_read': true,
|
||||
'is_pushed': true,
|
||||
'created_at': '2025-10-31T07:00:00.000Z',
|
||||
'read_at': '2025-11-01T23:00:00.000Z',
|
||||
},
|
||||
|
||||
// Order confirmed (Order - read)
|
||||
{
|
||||
'notification_id': 'notif_006',
|
||||
'user_id': 'user_001',
|
||||
'type': 'order_confirmed',
|
||||
'title': 'Xác nhận đơn hàng thành công',
|
||||
'message':
|
||||
'Đơn hàng DH2023120601 đã được xác nhận. Chúng tôi sẽ sớm chuẩn bị và giao hàng.',
|
||||
'data': {'order_id': 'DH2023120601'},
|
||||
'is_read': true,
|
||||
'is_pushed': true,
|
||||
'created_at': '2025-10-30T07:00:00.000Z',
|
||||
'read_at': '2025-10-31T21:00:00.000Z',
|
||||
},
|
||||
|
||||
// Birthday (General - read)
|
||||
{
|
||||
'notification_id': 'notif_007',
|
||||
'user_id': 'user_001',
|
||||
'type': 'birthday_reward',
|
||||
'title': 'Sinh nhật sắp đến',
|
||||
'message':
|
||||
'Chúc mừng sinh nhật! Bạn sẽ nhận 500 điểm thưởng vào ngày 20/12.',
|
||||
'data': {'birthday_date': '2023-12-20', 'points': 500},
|
||||
'is_read': true,
|
||||
'is_pushed': true,
|
||||
'created_at': '2025-10-27T07:00:00.000Z',
|
||||
'read_at': '2025-10-28T17:00:00.000Z',
|
||||
},
|
||||
];
|
||||
|
||||
/// Get all notifications (returns JSON data)
|
||||
Future<List<Map<String, dynamic>>> getAllNotifications() async {
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
return List.from(_mockNotifications);
|
||||
}
|
||||
|
||||
/// Get notifications by category
|
||||
Future<List<Map<String, dynamic>>> getNotificationsByCategory(
|
||||
String category) async {
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
if (category == 'general') {
|
||||
return _mockNotifications
|
||||
.where((n) =>
|
||||
!(n['type'] as String).toLowerCase().contains('order') ||
|
||||
(n['type'] as String).toLowerCase().contains('loyalty') ||
|
||||
(n['type'] as String).toLowerCase().contains('promotion') ||
|
||||
(n['type'] as String).toLowerCase().contains('event') ||
|
||||
(n['type'] as String).toLowerCase().contains('birthday'))
|
||||
.toList();
|
||||
} else if (category == 'order') {
|
||||
return _mockNotifications
|
||||
.where((n) => (n['type'] as String).toLowerCase().contains('order'))
|
||||
.toList();
|
||||
}
|
||||
|
||||
return List.from(_mockNotifications);
|
||||
}
|
||||
|
||||
/// Get unread count
|
||||
Future<int> getUnreadCount() async {
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
return _mockNotifications.where((n) => !(n['is_read'] as bool)).length;
|
||||
}
|
||||
|
||||
/// Mark notification as read
|
||||
Future<void> markAsRead(String notificationId) async {
|
||||
await Future.delayed(const Duration(milliseconds: 150));
|
||||
|
||||
final index = _mockNotifications
|
||||
.indexWhere((n) => n['notification_id'] == notificationId);
|
||||
if (index != -1) {
|
||||
_mockNotifications[index]['is_read'] = true;
|
||||
_mockNotifications[index]['read_at'] = DateTime.now().toIso8601String();
|
||||
}
|
||||
}
|
||||
|
||||
/// Mark all notifications as read
|
||||
Future<void> markAllAsRead() async {
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
for (var notification in _mockNotifications) {
|
||||
if (!(notification['is_read'] as bool)) {
|
||||
notification['is_read'] = true;
|
||||
notification['read_at'] = DateTime.now().toIso8601String();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/// Notification Repository Implementation
|
||||
///
|
||||
/// Concrete implementation of NotificationRepository.
|
||||
/// Coordinates between local datasource and domain layer.
|
||||
library;
|
||||
|
||||
import 'package:worker/features/notifications/data/datasources/notification_local_datasource.dart';
|
||||
import 'package:worker/features/notifications/data/models/notification_model.dart';
|
||||
import 'package:worker/features/notifications/domain/entities/notification.dart';
|
||||
import 'package:worker/features/notifications/domain/repositories/notification_repository.dart';
|
||||
|
||||
/// Notification Repository Implementation
|
||||
class NotificationRepositoryImpl implements NotificationRepository {
|
||||
/// Local data source
|
||||
final NotificationLocalDataSource localDataSource;
|
||||
|
||||
/// Constructor
|
||||
NotificationRepositoryImpl({required this.localDataSource});
|
||||
|
||||
@override
|
||||
Future<List<Notification>> getAllNotifications() async {
|
||||
final jsonList = await localDataSource.getAllNotifications();
|
||||
return jsonList.map((json) => NotificationModel.fromJson(json)).toList()
|
||||
..sort((a, b) => b.createdAt.compareTo(a.createdAt)); // Sort by newest first
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Notification>> getNotificationsByCategory(String category) async {
|
||||
final jsonList = await localDataSource.getNotificationsByCategory(category);
|
||||
return jsonList.map((json) => NotificationModel.fromJson(json)).toList()
|
||||
..sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> getUnreadCount() async {
|
||||
return await localDataSource.getUnreadCount();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> markAsRead(String notificationId) async {
|
||||
await localDataSource.markAsRead(notificationId);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> markAllAsRead() async {
|
||||
await localDataSource.markAllAsRead();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Notification>> refreshNotifications() async {
|
||||
// For now, just fetch all notifications
|
||||
// In production, this would fetch from server
|
||||
return await getAllNotifications();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user