Merge pull request 'notification_important_api' (#175) from notification_important_api into main

Reviewed-on: https://repo.ravanertebat.com/TM/tm_app/pulls/175
This commit is contained in:
a.ghabeli
2025-09-27 15:49:25 +03:30
9 changed files with 92 additions and 77 deletions
@@ -1,3 +1,4 @@
// notification_repository.dart
import 'package:tm_app/core/utils/result.dart'; import 'package:tm_app/core/utils/result.dart';
import 'package:tm_app/data/services/model/notification__response/notification_response_model.dart'; import 'package:tm_app/data/services/model/notification__response/notification_response_model.dart';
import 'package:tm_app/data/services/notification_service.dart'; import 'package:tm_app/data/services/notification_service.dart';
@@ -28,4 +29,14 @@ class NotificationsRepository {
offset: offset, offset: offset,
); );
} }
Future<Result<NotificationResponseModel>> getImportantNotifications({
required int limit,
required int offset,
}) {
return _notificationsService.getImportantNotifications(
limit: limit,
offset: offset,
);
}
} }
+9 -7
View File
@@ -1,17 +1,19 @@
// notification_api.dart
class NotificationApi { class NotificationApi {
static const String notifications = '/api/v1/notifications'; static const String notifications = '/api/v1/notifications';
static String getNotifications({required int limit, required int offset}) { static String getNotifications({required int limit, required int offset}) {
return '$notifications?limit=$limit&offset=$offset'; return '$notifications?limit=$limit&offset=$offset';
} }
static const String feedback = '/api/v1/feedback';
static const String markAllAsRead = '/api/v1/notifications/mark'; static const String markAllAsRead = '/api/v1/notifications/mark';
static const String markAsRead = '/api/v1/notifications/mark-as-read'; static const String markAsRead = '/api/v1/notifications/mark-as-read';
static const String tenderApprovalsTender = '/api/v1/notifications';
static const String unreadNotifications = '/api/v1/notifications?seen=false'; static const String unreadNotifications = '/api/v1/notifications?seen=false';
static String getUnreadNotifications({required int limit, required int offset}) { static String getUnreadNotifications({required int limit, required int offset}) {
return '$unreadNotifications?limit=$limit&offset=$offset'; return '/api/v1/notifications?seen=false&limit=$limit&offset=$offset';
} }
}
static String getImportantNotifications({required int limit, required int offset}) {
return '/api/v1/notifications?priority=important&limit=$limit&offset=$offset';
}
}
+13 -4
View File
@@ -1,10 +1,8 @@
// notification_service.dart // notification_service.dart
import 'dart:convert'; import 'dart:convert';
import 'package:tm_app/core/utils/result.dart'; import 'package:tm_app/core/utils/result.dart';
import 'package:tm_app/data/services/api/notification_api.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/data/services/model/notification__response/notification_response_model.dart';
import '../../core/network/network_manager.dart'; import '../../core/network/network_manager.dart';
class NotificationsService { class NotificationsService {
@@ -18,7 +16,6 @@ class NotificationsService {
}) async { }) async {
final result = await _networkManager.makeRequest( final result = await _networkManager.makeRequest(
NotificationApi.getNotifications(limit: limit, offset: offset), NotificationApi.getNotifications(limit: limit, offset: offset),
//NotificationApi.notifications,
method: 'GET', method: 'GET',
(json) => NotificationResponseModel.fromJson(json), (json) => NotificationResponseModel.fromJson(json),
); );
@@ -52,7 +49,19 @@ class NotificationsService {
required int offset, required int offset,
}) async { }) async {
final result = await _networkManager.makeRequest( final result = await _networkManager.makeRequest(
NotificationApi.unreadNotifications, NotificationApi.getUnreadNotifications(limit: limit, offset: offset),
method: 'GET',
(json) => NotificationResponseModel.fromJson(json),
);
return result;
}
Future<Result<NotificationResponseModel>> getImportantNotifications({
required int limit,
required int offset,
}) async {
final result = await _networkManager.makeRequest(
NotificationApi.getImportantNotifications(limit: limit, offset: offset),
method: 'GET', method: 'GET',
(json) => NotificationResponseModel.fromJson(json), (json) => NotificationResponseModel.fromJson(json),
); );
+13 -6
View File
@@ -1,3 +1,4 @@
// notification_view_model.dart
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:tm_app/core/utils/app_toast.dart'; import 'package:tm_app/core/utils/app_toast.dart';
@@ -79,7 +80,8 @@ class NotificationViewModel with ChangeNotifier {
} }
notifyListeners(); notifyListeners();
final int offset = (page - 1) * _pageSize; final int offset = page * _pageSize;
final result = await _repository.getNotifications( final result = await _repository.getNotifications(
limit: _pageSize, limit: _pageSize,
offset: offset, offset: offset,
@@ -88,10 +90,15 @@ class NotificationViewModel with ChangeNotifier {
switch (result) { switch (result) {
case Ok<NotificationResponseModel>(): case Ok<NotificationResponseModel>():
final data = result.value; final data = result.value;
if (isPagination && allNotificationResponse != null) { if (isPagination && allNotificationResponse != null) {
final old = allNotificationResponse?.data ?? []; final old = allNotificationResponse?.data ?? [];
final fresh = data.data ?? []; final fresh = data.data ?? [];
allNotificationResponse = data.copyWith(data: [...old, ...fresh]);
allNotificationResponse = allNotificationResponse!.copyWith(
data: [...old, ...fresh],
meta: data.meta,
);
} else { } else {
allNotificationResponse = data; allNotificationResponse = data;
} }
@@ -100,7 +107,7 @@ class NotificationViewModel with ChangeNotifier {
currentPageAll = page; currentPageAll = page;
totalPagesAll = data.meta?.pages ?? 1; totalPagesAll = data.meta?.pages ?? 1;
hasMoreAll = (data.data?.length ?? 0) >= _pageSize; hasMoreAll = currentPageAll < totalPagesAll;
break; break;
case Error<NotificationResponseModel>(): case Error<NotificationResponseModel>():
@@ -124,7 +131,7 @@ class NotificationViewModel with ChangeNotifier {
} }
notifyListeners(); notifyListeners();
final int offset = (page - 1) * _pageSize; final int offset = page * _pageSize;
final result = await _repository.getUnreadNotifications( final result = await _repository.getUnreadNotifications(
limit: _pageSize, limit: _pageSize,
offset: offset, offset: offset,
@@ -167,9 +174,9 @@ class NotificationViewModel with ChangeNotifier {
} }
notifyListeners(); notifyListeners();
final int offset = (page - 1) * _pageSize; final int offset = page * _pageSize;
final result = await _repository.getUnreadNotifications( final result = await _repository.getImportantNotifications(
limit: _pageSize, limit: _pageSize,
offset: offset, offset: offset,
); );
@@ -24,6 +24,7 @@ class _MobileNotificationPageState extends State<MobileNotificationPage>
final ScrollController _scrollAll = ScrollController(); final ScrollController _scrollAll = ScrollController();
final ScrollController _scrollUnread = ScrollController(); final ScrollController _scrollUnread = ScrollController();
final ScrollController _scrollImportant = ScrollController();
@override @override
void initState() { void initState() {
@@ -55,6 +56,18 @@ class _MobileNotificationPageState extends State<MobileNotificationPage>
); );
} }
}); });
_scrollImportant.addListener(() {
if (_scrollImportant.position.pixels >=
_scrollImportant.position.maxScrollExtent - 100 &&
!viewModel.isLoadingImportant &&
viewModel.hasMoreImportant) {
viewModel.getImportantNotifications(
page: viewModel.currentPageImportant + 1,
isPagination: true,
);
}
});
} }
@override @override
@@ -62,6 +75,7 @@ class _MobileNotificationPageState extends State<MobileNotificationPage>
controller.dispose(); controller.dispose();
_scrollAll.dispose(); _scrollAll.dispose();
_scrollUnread.dispose(); _scrollUnread.dispose();
_scrollImportant.dispose();
super.dispose(); super.dispose();
} }
@@ -83,45 +97,13 @@ class _MobileNotificationPageState extends State<MobileNotificationPage>
], ],
), ),
SizedBox(height: 12.0.h()), SizedBox(height: 12.0.h()),
Padding(
padding: EdgeInsetsDirectional.only(end: 24.0.w()),
child: Align(
alignment: Alignment.centerRight,
child: InkWell(
onTap: () {
viewModel.markAllAsRead(context);
},
borderRadius: BorderRadius.circular(99),
child: Container(
width: 132.0.w(),
height: 32.0.h(),
decoration: BoxDecoration(
color: AppColors.primary20,
borderRadius: BorderRadius.circular(99),
),
child: Center(
child: Text(
NotificationStrings.markAllAsReadButton,
style: TextStyle(
fontSize: 14.0.sp(),
fontWeight: FontWeight.w500,
color: AppColors.mainBlue,
),
),
),
),
),
),
),
SizedBox(height: 12.0.h()),
Expanded( Expanded(
child: TabBarView( child: TabBarView(
controller: controller, controller: controller,
children: [ children: [
NotificationAllTab(scrollController: _scrollAll), NotificationAllTab(scrollController: _scrollAll),
NotificationUnreadTab(scrollController: _scrollUnread), NotificationUnreadTab(scrollController: _scrollUnread),
const NotificationImportantTab(), NotificationImportantTab(scrollController: _scrollImportant),
], ],
), ),
), ),
@@ -142,11 +124,9 @@ class _MobileNotificationPageState extends State<MobileNotificationPage>
), ),
), ),
if ((viewModel.isLoadingAll && if (viewModel.isMoreLoadingImportant)
(viewModel.allNotificationResponse?.data?.isEmpty ?? true)) || const Padding(
(viewModel.isLoadingUnread && padding: EdgeInsets.symmetric(vertical: 16),
(viewModel.unreadNotificationResponse?.data?.isEmpty ?? true)))
const Expanded(
child: Center( child: Center(
child: CircularProgressIndicator(color: AppColors.secondary50), child: CircularProgressIndicator(color: AppColors.secondary50),
), ),
@@ -24,4 +24,5 @@ class NotificationStrings {
static const String noUnreadNotifications = 'No unread notifications'; static const String noUnreadNotifications = 'No unread notifications';
static const String noImportantNotifications = 'No important notifications'; static const String noImportantNotifications = 'No important notifications';
static const String moreTextButton = 'More'; static const String moreTextButton = 'More';
static const String noNotifications = 'No notifications';
} }
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:tm_app/view_models/notification_view_model.dart'; import 'package:tm_app/view_models/notification_view_model.dart';
import 'package:tm_app/views/notification/strings/notification_strings.dart';
import 'notification_card.dart'; import 'notification_card.dart';
@@ -12,8 +13,17 @@ class NotificationAllTab extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final viewModel = context.watch<NotificationViewModel>(); final viewModel = context.watch<NotificationViewModel>();
final notifications =
viewModel.allNotificationResponse?.data ?? []; if (viewModel.isLoadingAll &&
(viewModel.allNotificationResponse?.data?.isEmpty ?? true)) {
return const Center(child: CircularProgressIndicator());
}
final notifications = viewModel.allNotificationResponse?.data ?? [];
if (notifications.isEmpty) {
return const Center(child: Text(NotificationStrings.noNotifications));
}
return ListView.builder( return ListView.builder(
controller: scrollController, controller: scrollController,
@@ -28,3 +38,4 @@ class NotificationAllTab extends StatelessWidget {
); );
} }
} }
@@ -13,22 +13,18 @@ class NotificationImportantTab extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final viewModel = context.watch<NotificationViewModel>(); final viewModel = context.watch<NotificationViewModel>();
if (viewModel.isLoadingImportant) { if (viewModel.isLoadingImportant &&
(viewModel.importantNotificationResponse?.data?.isEmpty ?? true)) {
return const Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());
} }
final importantFromApi = viewModel.importantNotificationResponse?.data ?? []; final importantNotifications =
viewModel.importantNotificationResponse?.data ?? [];
final allNotifications = viewModel.allNotificationResponse?.data ?? [];
final source = importantFromApi.isNotEmpty ? importantFromApi : allNotifications;
final importantNotifications = source
.where((n) => (n.priority ?? '').toLowerCase() == 'important')
.toList();
if (importantNotifications.isEmpty) { if (importantNotifications.isEmpty) {
return const Center(child: Text(NotificationStrings.noImportantNotifications)); return const Center(
child: Text(NotificationStrings.noImportantNotifications),
);
} }
return ListView.builder( return ListView.builder(
@@ -38,9 +34,7 @@ class NotificationImportantTab extends StatelessWidget {
final notification = importantNotifications[index]; final notification = importantNotifications[index];
return Padding( return Padding(
padding: const EdgeInsets.only(bottom: 16.0), padding: const EdgeInsets.only(bottom: 16.0),
child: NotificationCard( child: NotificationCard(notification: notification),
notification: notification,
),
); );
}, },
); );
@@ -3,7 +3,6 @@ import 'package:provider/provider.dart';
import 'package:tm_app/core/utils/size_config.dart'; import 'package:tm_app/core/utils/size_config.dart';
import 'package:tm_app/view_models/notification_view_model.dart'; import 'package:tm_app/view_models/notification_view_model.dart';
import 'package:tm_app/views/notification/strings/notification_strings.dart'; import 'package:tm_app/views/notification/strings/notification_strings.dart';
import 'notification_card.dart'; import 'notification_card.dart';
class NotificationUnreadTab extends StatelessWidget { class NotificationUnreadTab extends StatelessWidget {
@@ -13,16 +12,17 @@ class NotificationUnreadTab extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final viewModel = context.watch<NotificationViewModel>(); final viewModel = context.watch<NotificationViewModel>();
if (viewModel.isLoadingUnread &&
if (viewModel.isLoadingUnread){ (viewModel.unreadNotificationResponse?.data?.isEmpty ?? true)) {
return const Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());
} }
final notifications = final notifications = viewModel.unreadNotificationResponse?.data ?? [];
viewModel.unreadNotificationResponse?.data ?? [];
if (notifications.isEmpty) { if (notifications.isEmpty) {
return const Center(child: Text(NotificationStrings.noUnreadNotifications,)); return const Center(
child: Text(NotificationStrings.noUnreadNotifications),
);
} }
return ListView.builder( return ListView.builder(