Files
tm_app/lib/view_models/notification_view_model.dart
T
2025-09-27 08:54:11 +03:30

304 lines
9.3 KiB
Dart

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';
import 'package:tm_app/views/notification/strings/notification_strings.dart';
class NotificationViewModel with ChangeNotifier {
final NotificationsRepository _repository;
NotificationViewModel({required NotificationsRepository repositoryViewModel})
: _repository = repositoryViewModel {
WidgetsBinding.instance.addPostFrameCallback((_) {
getNotifications();
getUnreadNotifications();
getImportantNotifications();
});
}
static const int _pageSize = 10;
String? _errorMessage;
String? get errorMessage => _errorMessage;
// All
NotificationResponseModel? allNotificationResponse;
bool isLoadingAll = false;
bool isMoreLoadingAll = false;
bool hasMoreAll = true;
int currentPageAll = 1;
int totalPagesAll = 0;
// Unread
NotificationResponseModel? unreadNotificationResponse;
bool isLoadingUnread = false;
bool isMoreLoadingUnread = false;
bool hasMoreUnread = true;
int currentPageUnread = 1;
int totalPagesUnread = 0;
// Important
NotificationResponseModel? importantNotificationResponse;
bool isLoadingImportant = false;
bool isMoreLoadingImportant = false;
bool hasMoreImportant = true;
int currentPageImportant = 1;
int totalPagesImportant = 0;
Future<void> getNotifications({int page = 1, bool isPagination = false}) async {
if (isPagination) {
isMoreLoadingAll = true;
} else {
isLoadingAll = true;
}
notifyListeners();
final int offset = (page - 1) * _pageSize;
final result = await _repository.getNotifications(limit: _pageSize, offset: offset);
switch (result) {
case Ok<NotificationResponseModel>():
final data = result.value;
if (isPagination && allNotificationResponse != null) {
final old = allNotificationResponse?.data?.notifications ?? [];
final fresh = data.data?.notifications ?? [];
allNotificationResponse = data.copyWith(
data: data.data?.copyWith(
notifications: [...old, ...fresh],
),
);
} else {
allNotificationResponse = data;
}
_updateImportantFromAll();
currentPageAll = page;
totalPagesAll = data.data?.meta?.pages ?? 1;
hasMoreAll = (data.data?.notifications?.length ?? 0) >= _pageSize;
break;
case Error<NotificationResponseModel>():
_errorMessage = result.error.toString();
break;
}
isLoadingAll = false;
isMoreLoadingAll = false;
notifyListeners();
}
Future<void> getUnreadNotifications({int page = 1, bool isPagination = false}) async {
if (isPagination) {
isMoreLoadingUnread = true;
} else {
isLoadingUnread = true;
}
notifyListeners();
final int offset = (page - 1) * _pageSize;
final result = await _repository.getUnreadNotifications(limit: _pageSize, offset: offset);
switch (result) {
case Ok<NotificationResponseModel>():
final data = result.value;
if (isPagination && unreadNotificationResponse != null) {
final old = unreadNotificationResponse?.data?.notifications ?? [];
final fresh = data.data?.notifications ?? [];
unreadNotificationResponse = data.copyWith(
data: data.data?.copyWith(
notifications: [...old, ...fresh],
),
);
} else {
unreadNotificationResponse = data;
}
currentPageUnread = page;
totalPagesUnread = data.data?.meta?.pages ?? 1;
hasMoreUnread = (data.data?.notifications?.length ?? 0) >= _pageSize;
break;
case Error<NotificationResponseModel>():
_errorMessage = result.error.toString();
break;
}
isLoadingUnread = false;
isMoreLoadingUnread = false;
notifyListeners();
}
Future<void> getImportantNotifications({int page = 1, bool isPagination = false}) async {
if (isPagination) {
isMoreLoadingImportant = true;
} else {
isLoadingImportant = true;
}
notifyListeners();
final int offset = (page - 1) * _pageSize;
final result = await _repository.getUnreadNotifications(limit: _pageSize, offset: offset);
switch (result) {
case Ok<NotificationResponseModel>():
final data = result.value;
if (isPagination && importantNotificationResponse != null) {
final old = importantNotificationResponse?.data?.notifications ?? [];
final fresh = data.data?.notifications ?? [];
importantNotificationResponse = data.copyWith(
data: data.data?.copyWith(
notifications: [...old, ...fresh],
),
);
} else {
importantNotificationResponse = data;
}
currentPageImportant = page;
totalPagesImportant = data.data?.meta?.pages ?? 1;
hasMoreImportant = (data.data?.notifications?.length ?? 0) >= _pageSize;
break;
case Error<NotificationResponseModel>():
_errorMessage = result.error.toString();
_updateImportantFromAll();
break;
}
isLoadingImportant = false;
isMoreLoadingImportant = false;
notifyListeners();
}
void _updateImportantFromAll() {
final all = allNotificationResponse?.data?.notifications ?? [];
final filtered = (all.where((n) {
final p = n.priority ?? '';
return p.toLowerCase() == 'important';
}).toList());
if (allNotificationResponse != null) {
importantNotificationResponse = allNotificationResponse!.copyWith(
data: allNotificationResponse!.data!.copyWith(notifications: filtered),
);
} else {
importantNotificationResponse = null;
}
}
Future<void> jumpToPageAll(int page) async {
if (totalPagesAll == 0 || page < 1 || page > totalPagesAll) {
return;
}
await getNotifications(page: page);
}
Future<void> jumpToPageUnread(int page) async {
if (totalPagesUnread == 0 || page < 1 || page > totalPagesUnread) {
return;
}
await getUnreadNotifications(page: page);
}
Future<void> jumpToPageImportant(int page) async {
if (totalPagesImportant == 0 || page < 1 || page > totalPagesImportant) {
return;
}
await getImportantNotifications(page: page);
}
Future<void> markAllAsRead(BuildContext context) async {
isLoadingAll = true;
_errorMessage = null;
notifyListeners();
final result = await _repository.markAllAsRead();
if (!context.mounted) {
return;
}
switch (result) {
case Ok<Map<String, dynamic>>():
final response = result.value;
final success = response['success'] as bool? ?? false;
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();
allNotificationResponse = allNotificationResponse!.copyWith(
data: allNotificationResponse!.data!.copyWith(
notifications: updatedNotifications,
),
);
unreadNotificationResponse = unreadNotificationResponse?.copyWith(
data: unreadNotificationResponse?.data?.copyWith(notifications: []),
);
if (importantNotificationResponse?.data?.notifications != null) {
final updatedImportant = importantNotificationResponse!.data!.notifications!
.map((n) => n.copyWith(seen: true))
.toList();
importantNotificationResponse = importantNotificationResponse!.copyWith(
data: importantNotificationResponse!.data!.copyWith(
notifications: updatedImportant,
),
);
}
}
if (context.mounted) {
AppToast.success(context, NotificationStrings.allNotificationMarkedAsRead);
}
} else {
_errorMessage = message;
if (context.mounted) {
AppToast.error(context, message);
}
}
break;
case Error<Map<String, dynamic>>():
_errorMessage = result.error.toString();
if (context.mounted) {
AppToast.error(context, _errorMessage!);
}
break;
}
isLoadingAll = false;
notifyListeners();
}
String timeAgo(String dateTimeString) {
final createdAt = DateTime.parse(dateTimeString);
final now = DateTime.now();
final difference = now.difference(createdAt);
if (difference.inMinutes < 1) {
return NotificationStrings.now;
} else if (difference.inMinutes < 60) {
return '${difference.inMinutes} ${NotificationStrings.min} ';
} else if (difference.inHours < 24) {
return '${difference.inHours} ${NotificationStrings.hour} ';
} else if (difference.inDays == 1) {
return NotificationStrings.yesterday;
} else {
return '${difference.inDays} ${NotificationStrings.daysAgo}';
}
}
}