Merge pull request 'added_unread_allread_notification' (#157) from added_unread_allread_notification into main

Reviewed-on: https://repo.ravanertebat.com/TM/tm_app/pulls/157
This commit is contained in:
a.ghabeli
2025-09-24 08:43:05 +03:30
8 changed files with 227 additions and 105 deletions
+99 -47
View File
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:tm_app/core/utils/app_toast.dart';
import 'package:tm_app/core/utils/result.dart';
import 'package:tm_app/data/repositories/notification_repository.dart';
import 'package:tm_app/data/services/model/notification__response/notification_response_model.dart';
@@ -10,6 +11,7 @@ class NotificationViewModel with ChangeNotifier {
: _repository = repositoryViewModel {
WidgetsBinding.instance.addPostFrameCallback((_) {
getNotifications();
getUnreadNotifications();
});
}
@@ -29,50 +31,57 @@ 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<void> getNotifications({int page = 1, bool isMobile = false}) async {
_isLoading = true;
notifyListeners();
Future<void> getNotifications({int page = 1, bool isMobile = false}) async {
_isLoading = true;
notifyListeners();
final int offset = _pageSize * page;
final int offset = _pageSize * page;
final result = await _repository.getNotifications(
limit: _pageSize,
offset: offset,
);
final result = await _repository.getNotifications(
limit: _pageSize,
offset: offset,
);
switch (result) {
case Ok<NotificationResponseModel>():
if (isMobile && page > 1) {
final oldNotifications =
_notificationResponseModel?.data?.notifications ?? [];
switch (result) {
case Ok<NotificationResponseModel>():
if (isMobile && page > 1) {
final oldNotifications =
_notificationResponseModel?.data?.notifications ?? [];
final newNotifications = result.value.data?.notifications ?? [];
final newNotifications = result.value.data?.notifications ?? [];
_notificationResponseModel = result.value.copyWith(
data: result.value.data?.copyWith(
notifications: [...oldNotifications, ...newNotifications],
),
);
} else {
_notificationResponseModel = result.value;
}
_notificationResponseModel = result.value.copyWith(
data: result.value.data?.copyWith(
notifications: [...oldNotifications, ...newNotifications],
),
);
} else {
_notificationResponseModel = result.value;
}
currentPage = page;
_hasMoreData =
(result.value.data?.notifications?.length ?? 0) >= _pageSize;
_totalPages = result.value.data?.meta?.pages ?? 1;
break;
currentPage = page;
_hasMoreData =
(result.value.data?.notifications?.length ?? 0) >= _pageSize;
_totalPages = result.value.data?.meta?.pages ?? 1;
break;
case Error<NotificationResponseModel>():
_errorMessage = result.error.toString();
break;
case Error<NotificationResponseModel>():
_errorMessage = result.error.toString();
break;
}
_isLoading = false;
notifyListeners();
}
_isLoading = false;
notifyListeners();
}
/// Jump to page
Future<void> jumpToPage(int page) async {
if (_totalPages == 0 || page < 1 || page > _totalPages) {
@@ -82,33 +91,52 @@ Future<void> getNotifications({int page = 1, bool isMobile = false}) async {
}
/// Mark all as read
Future<void> markAllAsRead() async {
Future<void> markAllAsRead(BuildContext context) async {
_isLoading = true;
_errorMessage = null;
notifyListeners();
final result = await _repository.markAllAsRead();
switch (result) {
case Ok<NotificationResponseModel>():
if (_notificationResponseModel?.data?.notifications != null) {
final updatedNotifications =
_notificationResponseModel!.data!.notifications!
.map((n) => n.copyWith(seen: true))
.toList();
case Ok<Map<String, dynamic>>():
final response = result.value;
final success = response['success'] as bool? ?? false;
final message = response['message'] as String? ?? 'Unknown response';
_notificationResponseModel = _notificationResponseModel!.copyWith(
data: _notificationResponseModel!.data!.copyWith(
notifications: updatedNotifications,
),
);
if (success) {
if (_notificationResponseModel?.data?.notifications != null) {
final updatedNotifications =
_notificationResponseModel!.data!.notifications!
.map((n) => n.copyWith(seen: true))
.toList();
_notificationResponseModel = _notificationResponseModel!.copyWith(
data: _notificationResponseModel!.data!.copyWith(
notifications: updatedNotifications,
),
);
}
await getNotifications(page: currentPage, );
await getUnreadNotifications();
// toast success
AppToast.success(context, 'All notifications marked as read.');
} else {
_errorMessage = message;
AppToast.error(context, message);
}
break;
case Error<NotificationResponseModel>():
case Error<Map<String, dynamic>>():
_errorMessage = result.error.toString();
AppToast.error(context, _errorMessage!);
break;
}
_isLoading = false;
notifyListeners();
}
@@ -129,4 +157,28 @@ Future<void> getNotifications({int page = 1, bool isMobile = false}) async {
return '${difference.inDays} days ago';
}
}
Future<void> 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<NotificationResponseModel>():
_unreadNotificationResponse = result.value;
break;
case Error<NotificationResponseModel>():
_errorMessage = result.error.toString();
break;
}
_isUnreadLoading = false;
notifyListeners();
}
}