From d050aab728c404d1d10774955f294b7ecb2e2bba Mon Sep 17 00:00:00 2001 From: amirrezaghabeli Date: Sun, 5 Oct 2025 10:23:29 +0330 Subject: [PATCH] Refactor loading and error handling in DesktopHomePage; improve notification click handling in service worker --- lib/views/home/pages/d_home_page.dart | 95 ++++++++++--------- .../pages/m_notification_page.dart | 30 ++++++ web/firebase-messaging-sw.js | 50 +++++++--- 3 files changed, 117 insertions(+), 58 deletions(-) diff --git a/lib/views/home/pages/d_home_page.dart b/lib/views/home/pages/d_home_page.dart index 09c2a33..1c4aaba 100644 --- a/lib/views/home/pages/d_home_page.dart +++ b/lib/views/home/pages/d_home_page.dart @@ -21,62 +21,67 @@ class DesktopHomePage extends StatelessWidget { const DesktopNavigationWidget( currentIndex: 0, // Home index ), - Expanded( - child: Consumer( - builder: (context, homeViewModel, child) { - if (homeViewModel.isLoading) { - return const Center( + Consumer( + builder: (context, homeViewModel, child) { + if (homeViewModel.isLoading) { + return const Expanded( + child: Center( child: CircularProgressIndicator( color: AppColors.secondary50, ), - ); - } + ), + ); + } - if (homeViewModel.errorMessage != null) { - return Center(child: Text(homeViewModel.errorMessage!)); - } + if (homeViewModel.errorMessage != null) { + return Expanded( + child: Center(child: Text(homeViewModel.errorMessage!)), + ); + } - if (homeViewModel.tenderApprovalsStateResponse != null && - homeViewModel.data != null && - homeViewModel.feedbackStats != null) { - return SafeArea( - child: SingleChildScrollView( - child: SizedBox( - width: 780, - child: Column( - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - const SizedBox(height: 55), - // SizedBox(width: 780, child: NotificationCard()), - const SizedBox(height: 40.0), - _progressBarsRow(homeViewModel), - SizedBox(height: 32.0.h()), - Padding( - padding: EdgeInsets.symmetric( - horizontal: 15.0.w(), + if (homeViewModel.tenderApprovalsStateResponse != null && + homeViewModel.data != null && + homeViewModel.feedbackStats != null) { + return Expanded( + child: SingleChildScrollView( + child: SizedBox( + width: double.infinity, + child: Center( + child: SizedBox( + width: 780, + child: Column( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + const SizedBox(height: 55), + // SizedBox(width: 780, child: NotificationCard()), + const SizedBox(height: 40.0), + _progressBarsRow(homeViewModel), + SizedBox(height: 32.0.h()), + Padding( + padding: EdgeInsets.symmetric( + horizontal: 15.0.w(), + ), + child: _firstTenderCardsRow( + context, + homeViewModel, + ), ), - child: _firstTenderCardsRow( - context, - homeViewModel, - ), - ), - SizedBox(height: 32.0.h()), - _yourTenderText(homeViewModel), - SizedBox(height: 8.0.h()), - _bottomListView(homeViewModel), - ], + SizedBox(height: 32.0.h()), + _yourTenderText(homeViewModel), + SizedBox(height: 8.0.h()), + _bottomListView(homeViewModel), + ], + ), ), ), ), - ); - } - return const Center( - child: CircularProgressIndicator( - color: AppColors.secondary50, ), ); - }, - ), + } + return const Center( + child: CircularProgressIndicator(color: AppColors.secondary50), + ); + }, ), ], ), diff --git a/lib/views/notification/pages/m_notification_page.dart b/lib/views/notification/pages/m_notification_page.dart index 4d40c77..4fd4d0e 100644 --- a/lib/views/notification/pages/m_notification_page.dart +++ b/lib/views/notification/pages/m_notification_page.dart @@ -97,6 +97,36 @@ class _MobileNotificationPageState extends State ], ), 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, + ), + ), + ), + ), + ), + ), + ), Expanded( child: TabBarView( controller: controller, diff --git a/web/firebase-messaging-sw.js b/web/firebase-messaging-sw.js index 8f56201..96ecdbb 100644 --- a/web/firebase-messaging-sw.js +++ b/web/firebase-messaging-sw.js @@ -14,25 +14,49 @@ firebase.initializeApp({ const messaging = firebase.messaging(); // Handle background messages -messaging.onBackgroundMessage(async (payload) => { +// messaging.onBackgroundMessage(async (payload) => { - const notificationTitle = payload.notification.title; - const notificationOptions = { - body: payload.notification.body, - badge: '/icons/Icon-192.svg', - icon: '/icons/notification.svg', - data: payload.data, - click_action: payload.notification.click_action, - }; +// const notificationTitle = payload.notification.title; +// const notificationOptions = { +// body: payload.notification.body, +// badge: '/icons/Icon-192.svg', +// icon: '/icons/notification.svg', +// data: payload.data, +// click_action: payload.notification.click_action, +// }; - self.registration.showNotification(notificationTitle, notificationOptions); -}); +// self.registration.showNotification(notificationTitle, notificationOptions); +// }); // Open or focus the app when a notification is clicked self.addEventListener('notificationclick', (event) => { event.notification.close(); - const targetUrl = 'https://app.opplens.com/notification'; + + // Get the URL from the data payload, defaulting to the notification page + const urlToOpen = event.notification.data?.url || '/notification'; + const baseUrl = 'https://app.opplens.com'; + const targetUrl = urlToOpen.startsWith('http') ? urlToOpen : `${baseUrl}${urlToOpen}`; + + console.log('Opening URL:', targetUrl); + event.waitUntil( - self.clients.openWindow(targetUrl) + clients.matchAll({ + type: 'window', + includeUncontrolled: true, + }).then((clientList) => { + // Find an open client (any tab/window belonging to your site) + const focusedClient = clientList.find(client => { + return new URL(client.url).origin === location.origin; + }); + + if (focusedClient) { + // If a client is open, navigate it to the new URL and focus it + focusedClient.navigate(urlToOpen); + return focusedClient.focus(); + } else { + // Otherwise, open a new window + return clients.openWindow(targetUrl); + } + }) ); }); \ No newline at end of file