update notification tab data when clicking on navigation bars

This commit is contained in:
amirrezaghabeli
2025-09-27 10:47:13 +03:30
parent b6a61df904
commit ae348c3ed9
6 changed files with 139 additions and 90 deletions
+80 -40
View File
@@ -5,19 +5,38 @@ import 'package:tm_app/data/repositories/notification_repository.dart';
import 'package:tm_app/data/services/model/notification__response/notification_response_model.dart';
import 'package:tm_app/views/notification/strings/notification_strings.dart';
class NotificationViewModel with ChangeNotifier {
final NotificationsRepository _repository;
NotificationViewModel({required NotificationsRepository repositoryViewModel})
: _repository = repositoryViewModel {
: _repository = repositoryViewModel {
WidgetsBinding.instance.addPostFrameCallback((_) {
getNotifications();
getUnreadNotifications();
getImportantNotifications();
init();
});
}
void init() async {
clearData();
await getNotifications();
await getUnreadNotifications();
await getImportantNotifications();
}
void clearData() {
allNotificationResponse = null;
unreadNotificationResponse = null;
importantNotificationResponse = null;
_errorMessage = null;
currentPageAll = 1;
totalPagesAll = 0;
currentPageUnread = 1;
totalPagesUnread = 0;
currentPageImportant = 1;
totalPagesImportant = 0;
notifyListeners();
}
static const int _pageSize = 10;
String? _errorMessage;
@@ -47,8 +66,10 @@ class NotificationViewModel with ChangeNotifier {
int currentPageImportant = 1;
int totalPagesImportant = 0;
Future<void> getNotifications({int page = 1, bool isPagination = false}) async {
Future<void> getNotifications({
int page = 1,
bool isPagination = false,
}) async {
if (isPagination) {
isMoreLoadingAll = true;
} else {
@@ -57,7 +78,10 @@ class NotificationViewModel with ChangeNotifier {
notifyListeners();
final int offset = (page - 1) * _pageSize;
final result = await _repository.getNotifications(limit: _pageSize, offset: offset);
final result = await _repository.getNotifications(
limit: _pageSize,
offset: offset,
);
switch (result) {
case Ok<NotificationResponseModel>():
@@ -66,9 +90,7 @@ class NotificationViewModel with ChangeNotifier {
final old = allNotificationResponse?.data?.notifications ?? [];
final fresh = data.data?.notifications ?? [];
allNotificationResponse = data.copyWith(
data: data.data?.copyWith(
notifications: [...old, ...fresh],
),
data: data.data?.copyWith(notifications: [...old, ...fresh]),
);
} else {
allNotificationResponse = data;
@@ -91,7 +113,10 @@ class NotificationViewModel with ChangeNotifier {
notifyListeners();
}
Future<void> getUnreadNotifications({int page = 1, bool isPagination = false}) async {
Future<void> getUnreadNotifications({
int page = 1,
bool isPagination = false,
}) async {
if (isPagination) {
isMoreLoadingUnread = true;
} else {
@@ -100,7 +125,10 @@ class NotificationViewModel with ChangeNotifier {
notifyListeners();
final int offset = (page - 1) * _pageSize;
final result = await _repository.getUnreadNotifications(limit: _pageSize, offset: offset);
final result = await _repository.getUnreadNotifications(
limit: _pageSize,
offset: offset,
);
switch (result) {
case Ok<NotificationResponseModel>():
@@ -109,9 +137,7 @@ class NotificationViewModel with ChangeNotifier {
final old = unreadNotificationResponse?.data?.notifications ?? [];
final fresh = data.data?.notifications ?? [];
unreadNotificationResponse = data.copyWith(
data: data.data?.copyWith(
notifications: [...old, ...fresh],
),
data: data.data?.copyWith(notifications: [...old, ...fresh]),
);
} else {
unreadNotificationResponse = data;
@@ -132,7 +158,10 @@ class NotificationViewModel with ChangeNotifier {
notifyListeners();
}
Future<void> getImportantNotifications({int page = 1, bool isPagination = false}) async {
Future<void> getImportantNotifications({
int page = 1,
bool isPagination = false,
}) async {
if (isPagination) {
isMoreLoadingImportant = true;
} else {
@@ -142,7 +171,10 @@ class NotificationViewModel with ChangeNotifier {
final int offset = (page - 1) * _pageSize;
final result = await _repository.getUnreadNotifications(limit: _pageSize, offset: offset);
final result = await _repository.getUnreadNotifications(
limit: _pageSize,
offset: offset,
);
switch (result) {
case Ok<NotificationResponseModel>():
@@ -151,9 +183,7 @@ class NotificationViewModel with ChangeNotifier {
final old = importantNotificationResponse?.data?.notifications ?? [];
final fresh = data.data?.notifications ?? [];
importantNotificationResponse = data.copyWith(
data: data.data?.copyWith(
notifications: [...old, ...fresh],
),
data: data.data?.copyWith(notifications: [...old, ...fresh]),
);
} else {
importantNotificationResponse = data;
@@ -165,7 +195,6 @@ class NotificationViewModel with ChangeNotifier {
break;
case Error<NotificationResponseModel>():
_errorMessage = result.error.toString();
_updateImportantFromAll();
@@ -179,10 +208,11 @@ class NotificationViewModel with ChangeNotifier {
void _updateImportantFromAll() {
final all = allNotificationResponse?.data?.notifications ?? [];
final filtered = (all.where((n) {
final p = n.priority ?? '';
return p.toLowerCase() == 'important';
}).toList());
final filtered =
(all.where((n) {
final p = n.priority ?? '';
return p.toLowerCase() == 'important';
}).toList());
if (allNotificationResponse != null) {
importantNotificationResponse = allNotificationResponse!.copyWith(
@@ -229,13 +259,16 @@ class NotificationViewModel with ChangeNotifier {
case Ok<Map<String, dynamic>>():
final response = result.value;
final success = response['success'] as bool? ?? false;
final message = response['message'] as String? ?? NotificationStrings.unknownResponse;
final message =
response['message'] as String? ??
NotificationStrings.unknownResponse;
if (success) {
if (allNotificationResponse?.data?.notifications != null) {
final updatedNotifications = allNotificationResponse!.data!.notifications!
.map((n) => n.copyWith(seen: true))
.toList();
final updatedNotifications =
allNotificationResponse!.data!.notifications!
.map((n) => n.copyWith(seen: true))
.toList();
allNotificationResponse = allNotificationResponse!.copyWith(
data: allNotificationResponse!.data!.copyWith(
@@ -244,24 +277,31 @@ class NotificationViewModel with ChangeNotifier {
);
unreadNotificationResponse = unreadNotificationResponse?.copyWith(
data: unreadNotificationResponse?.data?.copyWith(notifications: []),
data: unreadNotificationResponse?.data?.copyWith(
notifications: [],
),
);
if (importantNotificationResponse?.data?.notifications != null) {
final updatedImportant = importantNotificationResponse!.data!.notifications!
.map((n) => n.copyWith(seen: true))
.toList();
final updatedImportant =
importantNotificationResponse!.data!.notifications!
.map((n) => n.copyWith(seen: true))
.toList();
importantNotificationResponse = importantNotificationResponse!.copyWith(
data: importantNotificationResponse!.data!.copyWith(
notifications: updatedImportant,
),
);
importantNotificationResponse = importantNotificationResponse!
.copyWith(
data: importantNotificationResponse!.data!.copyWith(
notifications: updatedImportant,
),
);
}
}
if (context.mounted) {
AppToast.success(context, NotificationStrings.allNotificationMarkedAsRead);
AppToast.success(
context,
NotificationStrings.allNotificationMarkedAsRead,
);
}
} else {
_errorMessage = message;