Fixed notification pagination for all and unread

This commit is contained in:
llsajjad
2025-09-27 08:54:11 +03:30
parent e432853478
commit 811a1c1385
9 changed files with 371 additions and 189 deletions
@@ -40,6 +40,26 @@ class _DesktopNotificationPageState extends State<DesktopNotificationPage>
Widget build(BuildContext context) {
final viewModel = context.watch<NotificationViewModel>();
final activeIndex = controller.index;
int currentPage = 1;
int totalPages = 1;
bool isLoading = false;
if (activeIndex == 0) {
currentPage = viewModel.currentPageAll;
totalPages = viewModel.totalPagesAll;
isLoading = viewModel.isLoadingAll;
} else if (activeIndex == 1) {
currentPage = viewModel.currentPageUnread;
totalPages = viewModel.totalPagesUnread;
isLoading = viewModel.isLoadingUnread;
} else {
currentPage = viewModel.currentPageImportant;
totalPages = viewModel.totalPagesImportant;
isLoading = viewModel.isLoadingImportant;
}
return SafeArea(
child: Scaffold(
backgroundColor: AppColors.backgroundColor,
@@ -60,6 +80,7 @@ class _DesktopNotificationPageState extends State<DesktopNotificationPage>
NotificationStrings.unread,
NotificationStrings.important,
],
onTap: (_) => setState(() {}),
),
SizedBox(height: 12.0.h()),
Padding(
@@ -93,27 +114,25 @@ class _DesktopNotificationPageState extends State<DesktopNotificationPage>
),
),
SizedBox(height: 12.0.h()),
viewModel.isLoading
? Container()
: Expanded(
child: TabBarView(
controller: controller,
children: const [
NotificationAllTab(),
NotificationUnreadTab(),
NotificationImportantTab(),
],
if (isLoading)
const Expanded(
child: Center(
child: CircularProgressIndicator(
color: AppColors.secondary50,
),
),
if (viewModel.isLoading) ...[
const Spacer(),
const Center(
child: CircularProgressIndicator(
color: AppColors.secondary50,
)
else
Expanded(
child: TabBarView(
controller: controller,
children: const [
NotificationAllTab(),
NotificationUnreadTab(),
NotificationImportantTab(),
],
),
),
const Spacer(),
],
SizedBox(height: 10.0.h()),
Row(
children: [
@@ -131,14 +150,19 @@ class _DesktopNotificationPageState extends State<DesktopNotificationPage>
onTap: () async {
final selectedPage = await showDialog<int>(
context: context,
builder:
(context) => PageSelectionDialog(
totalPages: viewModel.totalPages,
),
builder: (context) => PageSelectionDialog(
totalPages: totalPages,
),
);
if (selectedPage != null) {
viewModel.jumpToPage(selectedPage);
if (activeIndex == 0) {
viewModel.jumpToPageAll(selectedPage);
} else if (activeIndex == 1) {
viewModel.jumpToPageUnread(selectedPage);
} else {
viewModel.jumpToPageImportant(selectedPage);
}
}
},
child: Container(
@@ -153,7 +177,7 @@ class _DesktopNotificationPageState extends State<DesktopNotificationPage>
children: [
SizedBox(width: 5.0.w()),
Text(
viewModel.currentPage.toString(),
currentPage.toString(),
style: TextStyle(
fontSize: 14.0.sp(),
fontWeight: FontWeight.w500,
@@ -181,7 +205,7 @@ class _DesktopNotificationPageState extends State<DesktopNotificationPage>
),
SizedBox(width: 5.0.w()),
Text(
viewModel.totalPages.toString(),
totalPages.toString(),
style: TextStyle(
fontSize: 13.0.sp(),
fontWeight: FontWeight.w400,
@@ -21,22 +21,37 @@ class MobileNotificationPage extends StatefulWidget {
class _MobileNotificationPageState extends State<MobileNotificationPage>
with SingleTickerProviderStateMixin {
late TabController controller;
final ScrollController _scrollController = ScrollController();
final ScrollController _scrollAll = ScrollController();
final ScrollController _scrollUnread = ScrollController();
@override
void initState() {
super.initState();
controller = TabController(length: 3, vsync: this);
_scrollController.addListener(() {
final viewModel = context.read<NotificationViewModel>();
if (_scrollController.position.pixels >=
_scrollController.position.maxScrollExtent - 100 &&
!viewModel.isLoading &&
viewModel.hasMoreData) {
final viewModel = context.read<NotificationViewModel>();
_scrollAll.addListener(() {
if (_scrollAll.position.pixels >=
_scrollAll.position.maxScrollExtent - 100 &&
!viewModel.isLoadingAll &&
viewModel.hasMoreAll) {
viewModel.getNotifications(
page: viewModel.currentPage + 1,
isMobile: true,
page: viewModel.currentPageAll + 1,
isPagination: true,
);
}
});
_scrollUnread.addListener(() {
if (_scrollUnread.position.pixels >=
_scrollUnread.position.maxScrollExtent - 100 &&
!viewModel.isLoadingUnread &&
viewModel.hasMoreUnread) {
viewModel.getUnreadNotifications(
page: viewModel.currentPageUnread + 1,
isPagination: true,
);
}
});
@@ -45,7 +60,8 @@ class _MobileNotificationPageState extends State<MobileNotificationPage>
@override
void dispose() {
controller.dispose();
_scrollController.dispose();
_scrollAll.dispose();
_scrollUnread.dispose();
super.dispose();
}
@@ -98,37 +114,43 @@ class _MobileNotificationPageState extends State<MobileNotificationPage>
),
),
SizedBox(height: 12.0.h()),
viewModel.isLoading
? Container()
: Expanded(
child: TabBarView(
controller: controller,
children: [
NotificationAllTab(scrollController: _scrollController),
NotificationUnreadTab(scrollController: _scrollController),
NotificationImportantTab(
scrollController: _scrollController,
),
],
),
),
if (viewModel.isMoreLoading) ...[
Expanded(
child: TabBarView(
controller: controller,
children: [
NotificationAllTab(scrollController: _scrollAll),
NotificationUnreadTab(scrollController: _scrollUnread),
const NotificationImportantTab(),
],
),
),
if (viewModel.isMoreLoadingAll)
const Padding(
padding: EdgeInsets.symmetric(vertical: 16),
child: Center(
child: CircularProgressIndicator(color: AppColors.secondary50),
),
),
],
if (viewModel.isLoading &&
(viewModel.notificationResponse?.data?.notifications?.isEmpty ??
true)) ...[
const Spacer(),
const Center(
child: CircularProgressIndicator(color: AppColors.secondary50),
if (viewModel.isMoreLoadingUnread)
const Padding(
padding: EdgeInsets.symmetric(vertical: 16),
child: Center(
child: CircularProgressIndicator(color: AppColors.secondary50),
),
),
if ((viewModel.isLoadingAll &&
(viewModel.allNotificationResponse?.data?.notifications?.isEmpty ?? true)) ||
(viewModel.isLoadingUnread &&
(viewModel.unreadNotificationResponse?.data?.notifications?.isEmpty ?? true)))
const Expanded(
child: Center(
child: CircularProgressIndicator(color: AppColors.secondary50),
),
),
const Spacer(),
],
],
),
);
@@ -25,6 +25,7 @@ class TabletNotificationPage extends StatefulWidget {
class _TabletNotificationPageState extends State<TabletNotificationPage>
with SingleTickerProviderStateMixin {
late TabController controller;
final GlobalKey<ScaffoldState> key = GlobalKey();
@override
void initState() {
@@ -41,7 +42,25 @@ class _TabletNotificationPageState extends State<TabletNotificationPage>
@override
Widget build(BuildContext context) {
final viewModel = context.watch<NotificationViewModel>();
final GlobalKey<ScaffoldState> key = GlobalKey();
final activeIndex = controller.index;
int currentPage = 1;
int totalPages = 1;
bool isLoading = false;
if (activeIndex == 0) {
currentPage = viewModel.currentPageAll;
totalPages = viewModel.totalPagesAll;
isLoading = viewModel.isLoadingAll;
} else if (activeIndex == 1) {
currentPage = viewModel.currentPageUnread;
totalPages = viewModel.totalPagesUnread;
isLoading = viewModel.isLoadingUnread;
} else {
currentPage = viewModel.currentPageImportant;
totalPages = viewModel.totalPagesImportant;
isLoading = viewModel.isLoadingImportant;
}
return Scaffold(
key: key,
@@ -63,6 +82,7 @@ class _TabletNotificationPageState extends State<TabletNotificationPage>
NotificationStrings.unread,
NotificationStrings.important,
],
onTap: (_) => setState(() {}),
),
SizedBox(height: 12.0.h()),
Padding(
@@ -96,27 +116,25 @@ class _TabletNotificationPageState extends State<TabletNotificationPage>
),
),
SizedBox(height: 12.0.h()),
viewModel.isLoading
? Container()
: Expanded(
child: TabBarView(
controller: controller,
children: const [
NotificationAllTab(),
NotificationUnreadTab(),
NotificationImportantTab(),
],
if (isLoading)
const Expanded(
child: Center(
child: CircularProgressIndicator(
color: AppColors.secondary50,
),
),
if (viewModel.isLoading) ...[
const Spacer(),
const Center(
child: CircularProgressIndicator(
color: AppColors.secondary50,
)
else
Expanded(
child: TabBarView(
controller: controller,
children: const [
NotificationAllTab(),
NotificationUnreadTab(),
NotificationImportantTab(),
],
),
),
const Spacer(),
],
SizedBox(height: 10.0.h()),
Row(
children: [
@@ -134,14 +152,19 @@ class _TabletNotificationPageState extends State<TabletNotificationPage>
onTap: () async {
final selectedPage = await showDialog<int>(
context: context,
builder:
(context) => PageSelectionDialog(
totalPages: viewModel.totalPages,
),
builder: (context) => PageSelectionDialog(
totalPages: totalPages,
),
);
if (selectedPage != null) {
viewModel.jumpToPage(selectedPage);
if (activeIndex == 0) {
viewModel.jumpToPageAll(selectedPage);
} else if (activeIndex == 1) {
viewModel.jumpToPageUnread(selectedPage);
} else {
viewModel.jumpToPageImportant(selectedPage);
}
}
},
child: Container(
@@ -153,7 +176,7 @@ class _TabletNotificationPageState extends State<TabletNotificationPage>
children: [
SizedBox(width: 5.0.w()),
Text(
viewModel.currentPage.toString(),
currentPage.toString(),
style: TextStyle(
fontSize: 14.0.sp(),
fontWeight: FontWeight.w500,
@@ -184,7 +207,7 @@ class _TabletNotificationPageState extends State<TabletNotificationPage>
),
SizedBox(width: 5.0.w()),
Text(
viewModel.totalPages.toString(),
totalPages.toString(),
style: TextStyle(
fontSize: 13.0.sp(),
fontWeight: FontWeight.w400,