Files
worker/lib/features/notifications/data/datasources/notification_local_datasource.dart
2025-11-07 11:52:06 +07:00

186 lines
6.1 KiB
Dart

/// 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();
}
}
}
}