From 967fd2fd7a20141b87e3ce0a3fd60c15027b94e2 Mon Sep 17 00:00:00 2001 From: SurendarSuri30 Date: Fri, 18 Apr 2025 16:45:16 +0530 Subject: [PATCH] notification count added in menu --- .../drawer_routes/custom_drawer_routes.dart | 81 ++++++++++++++++++- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/lib/presentation/routes/drawer_routes/custom_drawer_routes.dart b/lib/presentation/routes/drawer_routes/custom_drawer_routes.dart index ba88bae3..d58abdda 100644 --- a/lib/presentation/routes/drawer_routes/custom_drawer_routes.dart +++ b/lib/presentation/routes/drawer_routes/custom_drawer_routes.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'dart:convert'; import 'dart:math'; +import 'package:http/http.dart' as http; import 'package:iconify_design/iconify_design.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -88,6 +89,10 @@ class _BaseScaffoldState extends ConsumerState { static final _repo = getIt.call(); late final Locale locale; + List> notifications = []; + List userReadedNotiOrNot = []; + int notificationCount = 0; + @override void initState() { super.initState(); @@ -703,6 +708,12 @@ class _BaseScaffoldState extends ConsumerState { String recordId = userId; String collectionId = userDetailsResponse.data['collectionId'] ?? '_pb_users_auth_'; + List userReadedNotification = userDetailsResponse.data['pushed_notification']; + + // Convert to List + userReadedNotiOrNot = List.from(userReadedNotification); + + print('userReadedNotiOrNot: $userReadedNotiOrNot'); if (userAvatar!.isNotEmpty && recordId.isNotEmpty) { _avatarUrl = '$apiUrl/api/files/$collectionId/$recordId/$userAvatar'; @@ -710,6 +721,7 @@ class _BaseScaffoldState extends ConsumerState { _avatarUrl = ''; // Reset to default or empty } }); + fetchNotifications(locale?.languageCode ?? 'en'); // **Set Default Locale in Provider** // if (preferredLanguage == 'ar') { // ref.read(localeProvider.notifier).setLocale(const Locale('ar')); @@ -721,6 +733,70 @@ class _BaseScaffoldState extends ConsumerState { } } + Future fetchNotifications(locale) async { + final baseUrl = apiUrl + '/api/getNotification'; + try { + final response = await http.get(Uri.parse('$baseUrl?language=$locale')); + + if (response.statusCode == 200) { + final Map jsonResponse = + jsonDecode(response.body); // Decode JSON + + if (jsonResponse['message'] == 'Success') { + setState(() { + notifications = List>.from( + jsonResponse['data']); // Assign decoded data + print('notifications $notifications'); + // isLoading = false; + }); + _processNotifications(notifications , userReadedNotiOrNot); + } else { + throw Exception('Failed to load data'); + } + } else { + throw Exception( + 'Failed to load data with status code ${response.statusCode}'); + } + } catch (e) { + print('Error fetching data: $e'); + } + } + + void _processNotifications(List> notification, List push) { + print('inside last function ${notification.length}'); + print('inside last function ${push.length}'); + + int count = 0; + for (var product in notification) { + if (push.contains(product['id'])) { + count++; + } + } + print('count is $count'); + notificationCount = count; + } + + Widget _buildNotificationBadge({required int count}) { + if (count == 0) return SizedBox.shrink(); + + return Container( + padding: EdgeInsets.symmetric(horizontal: 10, vertical: 6), + decoration: BoxDecoration( + color: Colors.red, + borderRadius: BorderRadius.circular(50), + ), + child: Text( + '$count', + style: TextStyle( + color: Colors.white, + fontSize: 12, + fontWeight: FontWeight.bold, + ), + ), + ); + } + + // Future logout() async { // final prefs = await SharedPreferences.getInstance(); // // setState(() { @@ -788,7 +864,7 @@ class _BaseScaffoldState extends ConsumerState { scrolledUnderElevation: 0, titleSpacing: 0, backgroundColor: widget.appbarColor ?? Colors.white, - leadingWidth: widget.showBackButton ? mywidth * 0.20 : null, + leadingWidth: widget.showBackButton ? mywidth * 0.18 : null, bottom: widget.showDivider ? PreferredSize( preferredSize: Size.fromHeight(1), // Height of the line @@ -828,7 +904,7 @@ class _BaseScaffoldState extends ConsumerState { ), ], leading: Padding( - padding: const EdgeInsets.only(top: 7.0, left: 18), + padding: const EdgeInsets.only(top: 7.0, left: 4), child: Row( mainAxisSize: MainAxisSize.min, children: [ @@ -1003,6 +1079,7 @@ class _BaseScaffoldState extends ConsumerState { 'NotoKufi', ), ), ), + trailing: _buildNotificationBadge(count: notificationCount), onTap: () => _navigateTo(context, '/notification'), ), ListTile(