diff --git a/lib/data/repositories/notification_repository.dart b/lib/data/repositories/notification_repository.dart index e80f732..fc0075c 100644 --- a/lib/data/repositories/notification_repository.dart +++ b/lib/data/repositories/notification_repository.dart @@ -4,7 +4,7 @@ import 'package:tm_app/data/services/notification_service.dart'; class NotificationsRepository { NotificationsRepository({required NotificationsService notificationsService}) - : _notificationsService = notificationsService; + : _notificationsService = notificationsService; final NotificationsService _notificationsService; @@ -12,14 +12,20 @@ class NotificationsRepository { required int limit, required int offset, }) async { - return _notificationsService.getNotifications( + return _notificationsService.getNotifications(limit: limit, offset: offset); + } + + Future>> markAllAsRead() async { + return _notificationsService.markAllAsRead(); + } + + Future> getUnreadNotifications({ + required int limit, + required int offset, + }) { + return _notificationsService.getUnreadNotifications( limit: limit, offset: offset, ); } - -Future>> markAllAsRead() async { - return _notificationsService.markAllAsRead(); -} - } diff --git a/lib/data/services/api/notification_api.dart b/lib/data/services/api/notification_api.dart index e0b988a..a2d0af3 100644 --- a/lib/data/services/api/notification_api.dart +++ b/lib/data/services/api/notification_api.dart @@ -10,7 +10,8 @@ class NotificationApi { static const String markAllAsRead = '/api/v1/notifications/mark'; static const String markAsRead = '/api/v1/notifications/mark-as-read'; static const String tenderApprovalsTender = '/api/v1/notifications'; - static String getTenderApprovalById({required String tenderId}) { - return '$tenderApprovalsTender/$tenderId'; + static const String unreadNotifications = '/api/v1/notifications?seen=false'; + static String getUnreadNotifications({required int limit, required int offset}) { + return '$unreadNotifications?limit=$limit&offset=$offset'; } } diff --git a/lib/data/services/notification_service.dart b/lib/data/services/notification_service.dart index 5d8e809..90f415a 100644 --- a/lib/data/services/notification_service.dart +++ b/lib/data/services/notification_service.dart @@ -1,14 +1,15 @@ // notification_service.dart import 'dart:convert'; + +import 'package:tm_app/core/utils/result.dart'; import 'package:tm_app/data/services/api/notification_api.dart'; import 'package:tm_app/data/services/model/notification__response/notification_response_model.dart'; -import 'package:tm_app/core/utils/result.dart'; import '../../core/network/network_manager.dart'; class NotificationsService { NotificationsService({required NetworkManager networkManager}) - : _networkManager = networkManager; + : _networkManager = networkManager; final NetworkManager _networkManager; Future> getNotifications({ @@ -24,15 +25,14 @@ class NotificationsService { return result; } -Future>> markAllAsRead() async { - final result = await _networkManager.makeRequest( - NotificationApi.markAllAsRead, - method: 'GET', - (json) => json, - ); - return result; -} - + Future>> markAllAsRead() async { + final result = await _networkManager.makeRequest( + NotificationApi.markAllAsRead, + method: 'GET', + (json) => json, + ); + return result; + } Future> markAsRead({ required String notificationId, @@ -46,4 +46,16 @@ Future>> markAllAsRead() async { ); return result; } + + Future> getUnreadNotifications({ + required int limit, + required int offset, + }) async { + final result = await _networkManager.makeRequest( + NotificationApi.unreadNotifications, + method: 'GET', + (json) => NotificationResponseModel.fromJson(json), + ); + return result; + } } diff --git a/lib/view_models/notification_view_model.dart b/lib/view_models/notification_view_model.dart index 47a647f..21bf51b 100644 --- a/lib/view_models/notification_view_model.dart +++ b/lib/view_models/notification_view_model.dart @@ -11,6 +11,7 @@ class NotificationViewModel with ChangeNotifier { : _repository = repositoryViewModel { WidgetsBinding.instance.addPostFrameCallback((_) { getNotifications(); + getUnreadNotifications(); }); } @@ -30,6 +31,13 @@ class NotificationViewModel with ChangeNotifier { bool get hasMoreData => _hasMoreData; int get totalPages => _totalPages; + NotificationResponseModel? _unreadNotificationResponse; + bool _isUnreadLoading = false; + + NotificationResponseModel? get unreadNotificationResponse => + _unreadNotificationResponse; + bool get isUnreadLoading => _isUnreadLoading; + /// Fetch notifications Future getNotifications({int page = 1, bool isMobile = false}) async { _isLoading = true; @@ -110,7 +118,9 @@ class NotificationViewModel with ChangeNotifier { ); } - await getNotifications(page: 1); + await getNotifications(page: currentPage, ); + await getUnreadNotifications(); + // toast success AppToast.success(context, 'All notifications marked as read.'); @@ -147,4 +157,28 @@ class NotificationViewModel with ChangeNotifier { return '${difference.inDays} days ago'; } } + + Future getUnreadNotifications({int page = 1}) async { + _isUnreadLoading = true; + notifyListeners(); + + final int offset = _pageSize * page; + + final result = await _repository.getUnreadNotifications( + limit: _pageSize, + offset: offset, + ); + + switch (result) { + case Ok(): + _unreadNotificationResponse = result.value; + break; + case Error(): + _errorMessage = result.error.toString(); + break; + } + + _isUnreadLoading = false; + notifyListeners(); + } } diff --git a/lib/views/notification/widgets/notification_unread_tab.dart b/lib/views/notification/widgets/notification_unread_tab.dart index b5275d3..efb980e 100644 --- a/lib/views/notification/widgets/notification_unread_tab.dart +++ b/lib/views/notification/widgets/notification_unread_tab.dart @@ -12,9 +12,20 @@ class NotificationUnreadTab extends StatelessWidget { @override Widget build(BuildContext context) { final viewModel = context.watch(); + + if (viewModel.isUnreadLoading) { + return const Center(child: CircularProgressIndicator()); + } + final notifications = - viewModel.notificationResponse?.data?.notifications ?? []; + viewModel.unreadNotificationResponse?.data?.notifications ?? []; + + if (notifications.isEmpty) { + return const Center(child: Text('No unread notifications')); + } + return ListView.builder( + controller: scrollController, itemCount: notifications.length, itemBuilder: (context, index) { final notification = notifications[index];