From beeb07e9034f9cbc07d4fb2f01460c6d532300f4 Mon Sep 17 00:00:00 2001 From: pratikbaid3 Date: Fri, 30 Jul 2021 13:48:41 +0530 Subject: [PATCH 1/6] feat: Created popup for notification section --- lib/Components/popup_dialogue_container.dart | 182 +++++++++++++++++++ lib/Pages/home_screen.dart | 13 +- 2 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 lib/Components/popup_dialogue_container.dart diff --git a/lib/Components/popup_dialogue_container.dart b/lib/Components/popup_dialogue_container.dart new file mode 100644 index 00000000..d079b28e --- /dev/null +++ b/lib/Components/popup_dialogue_container.dart @@ -0,0 +1,182 @@ +import 'package:flood_mobile/Constants/app_color.dart'; +import 'package:flutter/material.dart'; +import 'package:font_awesome_flutter/font_awesome_flutter.dart'; + +Widget popupDialogueContainer() { + List recentActions = [ + ListTile( + title: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + "Finished Downloading", + textAlign: TextAlign.left, + style: TextStyle( + fontSize: 16, + color: AppColor.blueAccentColor, + fontWeight: FontWeight.bold), + ), + SizedBox( + height: 4, + ), + Text( + "Jan 24, 2021, 1:18PM", + textAlign: TextAlign.left, + style: TextStyle(fontSize: 12, color: Colors.white38), + ), + SizedBox( + height: 4, + ), + Text( + "LibreOffice_7.1.3_MacOS_x86-54.dmg", + textAlign: TextAlign.left, + style: TextStyle( + fontSize: 14, + color: Colors.white, + ), + ), + ], + ), + ), + ListTile( + title: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + "Finished Downloading", + textAlign: TextAlign.left, + style: TextStyle( + fontSize: 16, + color: AppColor.blueAccentColor, + fontWeight: FontWeight.bold), + ), + SizedBox( + height: 4, + ), + Text( + "Jan 24, 2021, 1:18PM", + textAlign: TextAlign.left, + style: TextStyle(fontSize: 12, color: Colors.white38), + ), + SizedBox( + height: 4, + ), + Text( + "LibreOffice_7.1.3_MacOS", + textAlign: TextAlign.left, + style: TextStyle( + fontSize: 14, + color: Colors.white, + ), + ), + ], + ), + ), + ListTile( + title: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + "Finished Downloading", + textAlign: TextAlign.left, + style: TextStyle( + fontSize: 16, + color: AppColor.blueAccentColor, + fontWeight: FontWeight.bold), + ), + SizedBox( + height: 4, + ), + Text( + "Jan 24, 2021, 1:18PM", + textAlign: TextAlign.left, + style: TextStyle(fontSize: 12, color: Colors.white38), + ), + SizedBox( + height: 4, + ), + Text( + "LibreOffice_7.1.3_MacOS", + textAlign: TextAlign.left, + style: TextStyle( + fontSize: 14, + color: Colors.white, + ), + ), + ], + ), + ), + ListTile( + title: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + "Finished Downloading", + textAlign: TextAlign.left, + style: TextStyle( + fontSize: 16, + color: AppColor.blueAccentColor, + fontWeight: FontWeight.bold), + ), + SizedBox( + height: 4, + ), + Text( + "Jan 24, 2021, 1:18PM", + textAlign: TextAlign.left, + style: TextStyle(fontSize: 12, color: Colors.white38), + ), + SizedBox( + height: 4, + ), + Text( + "LibreOffice_7.1.3_MacOS", + textAlign: TextAlign.left, + style: TextStyle( + fontSize: 14, + color: Colors.white, + ), + ), + ], + ), + ), + ]; + return Container( + color: AppColor.secondaryColor, + width: 300.0, // Change as per your requirement + child: ListView.builder( + shrinkWrap: true, + itemCount: recentActions.length, + itemBuilder: (BuildContext context, int index) { + if (index == 3) { + return Column( + children: [ + recentActions[index], + SizedBox( + height: 10, + ), + Divider(), + TextButton( + onPressed: () {}, + child: Text( + 'Clear All', + style: TextStyle( + color: Colors.white60, + ), + ), + ) + ], + ); + } + return Column( + children: [ + recentActions[index], + SizedBox( + height: 10, + ) + ], + ); + }, + ), + ); +} diff --git a/lib/Pages/home_screen.dart b/lib/Pages/home_screen.dart index 0b623048..6cf75c14 100644 --- a/lib/Pages/home_screen.dart +++ b/lib/Pages/home_screen.dart @@ -1,5 +1,6 @@ import 'package:flood_mobile/Api/client_api.dart'; import 'package:flood_mobile/Components/nav_drawer_list_tile.dart'; +import 'package:flood_mobile/Components/popup_dialogue_container.dart'; import 'package:flood_mobile/Constants/app_color.dart'; import 'package:flood_mobile/Pages/about_screen.dart'; import 'package:flood_mobile/Pages/settings_screen.dart'; @@ -10,6 +11,7 @@ import 'package:flood_mobile/Route/routes.dart'; import 'package:flutter/material.dart'; import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart'; import 'package:flutter_svg/svg.dart'; +import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:hidden_drawer_menu/controllers/simple_hidden_drawer_controller.dart'; import 'package:hidden_drawer_menu/simple_hidden_drawer/simple_hidden_drawer.dart'; import 'package:provider/provider.dart'; @@ -88,7 +90,16 @@ class _HomeScreenState extends State { Icons.notifications, color: Colors.white, ), - onPressed: () {}, + onPressed: () { + showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + backgroundColor: AppColor.secondaryColor, + content: popupDialogueContainer(), + ); + }); + }, ), ], ), From ea4942272f2ba882f0de4a0cf91ca15583a7ab24 Mon Sep 17 00:00:00 2001 From: pratikbaid3 Date: Wed, 4 Aug 2021 16:02:44 +0530 Subject: [PATCH 2/6] feat: Created notification popup, api and model --- lib/Api/notifications_api.dart | 38 ++++++++++++++++++++ lib/Components/popup_dialogue_container.dart | 1 + lib/Model/notification_model.dart | 0 lib/Pages/home_screen.dart | 2 ++ lib/Provider/api_provider.dart | 2 +- 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 lib/Api/notifications_api.dart create mode 100644 lib/Model/notification_model.dart diff --git a/lib/Api/notifications_api.dart b/lib/Api/notifications_api.dart new file mode 100644 index 00000000..c2fb1440 --- /dev/null +++ b/lib/Api/notifications_api.dart @@ -0,0 +1,38 @@ +import 'dart:convert'; + +import 'package:dio/dio.dart'; +import 'package:flood_mobile/Provider/api_provider.dart'; +import 'package:flood_mobile/Provider/user_detail_provider.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:provider/provider.dart'; + +class NotificationApi { + static Future getNotifications({@required BuildContext context}) async { + try { + String url = Provider.of(context, listen: false).baseUrl + + ApiProvider.notifications; + print('---GET NOTIFICATIONS---'); + print(url); + Response response; + Dio dio = new Dio(); + //Headers + dio.options.headers['Accept'] = "application/json"; + dio.options.headers['Content-Type'] = "application/json"; + dio.options.headers['Connection'] = "keep-alive"; + dio.options.headers['Cookie'] = + Provider.of(context, listen: false).token; + response = await dio.get(url, queryParameters: { + 'id': 'notification-tooltip', + 'limit': 5, + 'start': 0 + }); + if (response.statusCode == 200) { + print('---NOTIFICATIONS---'); + print(response.data); + } else {} + } catch (e) { + print('--ERROR--'); + print(e.toString()); + } + } +} diff --git a/lib/Components/popup_dialogue_container.dart b/lib/Components/popup_dialogue_container.dart index d079b28e..76e5c964 100644 --- a/lib/Components/popup_dialogue_container.dart +++ b/lib/Components/popup_dialogue_container.dart @@ -158,6 +158,7 @@ Widget popupDialogueContainer() { Divider(), TextButton( onPressed: () {}, + style: TextButton.styleFrom(fixedSize: Size(200, 50)), child: Text( 'Clear All', style: TextStyle( diff --git a/lib/Model/notification_model.dart b/lib/Model/notification_model.dart new file mode 100644 index 00000000..e69de29b diff --git a/lib/Pages/home_screen.dart b/lib/Pages/home_screen.dart index 6cf75c14..de6279e6 100644 --- a/lib/Pages/home_screen.dart +++ b/lib/Pages/home_screen.dart @@ -1,4 +1,5 @@ import 'package:flood_mobile/Api/client_api.dart'; +import 'package:flood_mobile/Api/notifications_api.dart'; import 'package:flood_mobile/Components/nav_drawer_list_tile.dart'; import 'package:flood_mobile/Components/popup_dialogue_container.dart'; import 'package:flood_mobile/Constants/app_color.dart'; @@ -33,6 +34,7 @@ class _HomeScreenState extends State { //Initialize the ap Provider.of(context, listen: false).listenToSSE(context); ClientApi.getClientSettings(context); + NotificationApi.getNotifications(context: context); super.didChangeDependencies(); } diff --git a/lib/Provider/api_provider.dart b/lib/Provider/api_provider.dart index 2bdae665..a1432e32 100644 --- a/lib/Provider/api_provider.dart +++ b/lib/Provider/api_provider.dart @@ -27,7 +27,7 @@ class ApiProvider extends ChangeNotifier { static String setTorrentContentPriorityUrl = '/api/torrents/'; //api/notifications?id=notification-tooltip&limit=10&start=0 - static String notifications = 'api/notifications'; + static String notifications = '/api/notifications'; Future setBaseUrl(String url) async { baseUrl = url; From d110e602941f7abf3b9c66691c22c4a0a9b53098 Mon Sep 17 00:00:00 2001 From: pratikbaid3 Date: Thu, 5 Aug 2021 13:30:15 +0530 Subject: [PATCH 3/6] feat: Added live data and badge for notification --- lib/Api/notifications_api.dart | 8 +- ...notification_popup_dialogue_container.dart | 106 ++++++++++ lib/Components/popup_dialogue_container.dart | 183 ------------------ lib/Model/notification_model.dart | 48 +++++ lib/Pages/home_screen.dart | 45 +++-- lib/Provider/home_provider.dart | 14 ++ pubspec.lock | 7 + pubspec.yaml | 1 + 8 files changed, 213 insertions(+), 199 deletions(-) create mode 100644 lib/Components/notification_popup_dialogue_container.dart delete mode 100644 lib/Components/popup_dialogue_container.dart diff --git a/lib/Api/notifications_api.dart b/lib/Api/notifications_api.dart index c2fb1440..0ab79dea 100644 --- a/lib/Api/notifications_api.dart +++ b/lib/Api/notifications_api.dart @@ -1,7 +1,9 @@ import 'dart:convert'; import 'package:dio/dio.dart'; +import 'package:flood_mobile/Model/notification_model.dart'; import 'package:flood_mobile/Provider/api_provider.dart'; +import 'package:flood_mobile/Provider/home_provider.dart'; import 'package:flood_mobile/Provider/user_detail_provider.dart'; import 'package:flutter/cupertino.dart'; import 'package:provider/provider.dart'; @@ -28,7 +30,11 @@ class NotificationApi { }); if (response.statusCode == 200) { print('---NOTIFICATIONS---'); - print(response.data); + NotificationModel model = NotificationModel.fromJson(response.data); + Provider.of(context, listen: false) + .setNotificationModel(model); + Provider.of(context, listen: false) + .setUnreadNotifications(model.unread); } else {} } catch (e) { print('--ERROR--'); diff --git a/lib/Components/notification_popup_dialogue_container.dart b/lib/Components/notification_popup_dialogue_container.dart new file mode 100644 index 00000000..a9ccd7e6 --- /dev/null +++ b/lib/Components/notification_popup_dialogue_container.dart @@ -0,0 +1,106 @@ +import 'package:flood_mobile/Constants/app_color.dart'; +import 'package:flood_mobile/Model/notification_model.dart'; +import 'package:flood_mobile/Provider/home_provider.dart'; +import 'package:flood_mobile/Services/date_converter.dart'; +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; + +Widget notificationPopupDialogueContainer({@required BuildContext context}) { + return Container( + color: AppColor.secondaryColor, + width: 300.0, // Change as per your requirement + child: ListView.builder( + shrinkWrap: true, + itemCount: Provider.of(context) + .notificationModel + .notifications + .length, + itemBuilder: (BuildContext context, int index) { + if (index == + Provider.of(context) + .notificationModel + .notifications + .length - + 1) { + return Column( + children: [ + NotificationListTile( + model: Provider.of(context) + .notificationModel + .notifications[index]), + SizedBox( + height: 10, + ), + Divider(), + TextButton( + onPressed: () {}, + style: TextButton.styleFrom(fixedSize: Size(200, 50)), + child: Text( + 'Clear All', + style: TextStyle( + color: Colors.white60, + ), + ), + ) + ], + ); + } + return Column( + children: [ + NotificationListTile( + model: Provider.of(context) + .notificationModel + .notifications[index]), + SizedBox( + height: 10, + ) + ], + ); + }, + ), + ); +} + +class NotificationListTile extends StatelessWidget { + NotificationContentModel model; + + NotificationListTile({@required this.model}); + + @override + Widget build(BuildContext context) { + return ListTile( + title: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + model.status, + textAlign: TextAlign.left, + style: TextStyle( + fontSize: 16, + color: AppColor.blueAccentColor, + fontWeight: FontWeight.bold), + ), + SizedBox( + height: 4, + ), + Text( + DateTime.fromMillisecondsSinceEpoch(model.ts).toString(), + textAlign: TextAlign.left, + style: TextStyle(fontSize: 12, color: Colors.white38), + ), + SizedBox( + height: 4, + ), + Text( + model.name, + textAlign: TextAlign.left, + style: TextStyle( + fontSize: 14, + color: Colors.white, + ), + ), + ], + ), + ); + } +} diff --git a/lib/Components/popup_dialogue_container.dart b/lib/Components/popup_dialogue_container.dart deleted file mode 100644 index 76e5c964..00000000 --- a/lib/Components/popup_dialogue_container.dart +++ /dev/null @@ -1,183 +0,0 @@ -import 'package:flood_mobile/Constants/app_color.dart'; -import 'package:flutter/material.dart'; -import 'package:font_awesome_flutter/font_awesome_flutter.dart'; - -Widget popupDialogueContainer() { - List recentActions = [ - ListTile( - title: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - "Finished Downloading", - textAlign: TextAlign.left, - style: TextStyle( - fontSize: 16, - color: AppColor.blueAccentColor, - fontWeight: FontWeight.bold), - ), - SizedBox( - height: 4, - ), - Text( - "Jan 24, 2021, 1:18PM", - textAlign: TextAlign.left, - style: TextStyle(fontSize: 12, color: Colors.white38), - ), - SizedBox( - height: 4, - ), - Text( - "LibreOffice_7.1.3_MacOS_x86-54.dmg", - textAlign: TextAlign.left, - style: TextStyle( - fontSize: 14, - color: Colors.white, - ), - ), - ], - ), - ), - ListTile( - title: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - "Finished Downloading", - textAlign: TextAlign.left, - style: TextStyle( - fontSize: 16, - color: AppColor.blueAccentColor, - fontWeight: FontWeight.bold), - ), - SizedBox( - height: 4, - ), - Text( - "Jan 24, 2021, 1:18PM", - textAlign: TextAlign.left, - style: TextStyle(fontSize: 12, color: Colors.white38), - ), - SizedBox( - height: 4, - ), - Text( - "LibreOffice_7.1.3_MacOS", - textAlign: TextAlign.left, - style: TextStyle( - fontSize: 14, - color: Colors.white, - ), - ), - ], - ), - ), - ListTile( - title: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - "Finished Downloading", - textAlign: TextAlign.left, - style: TextStyle( - fontSize: 16, - color: AppColor.blueAccentColor, - fontWeight: FontWeight.bold), - ), - SizedBox( - height: 4, - ), - Text( - "Jan 24, 2021, 1:18PM", - textAlign: TextAlign.left, - style: TextStyle(fontSize: 12, color: Colors.white38), - ), - SizedBox( - height: 4, - ), - Text( - "LibreOffice_7.1.3_MacOS", - textAlign: TextAlign.left, - style: TextStyle( - fontSize: 14, - color: Colors.white, - ), - ), - ], - ), - ), - ListTile( - title: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - "Finished Downloading", - textAlign: TextAlign.left, - style: TextStyle( - fontSize: 16, - color: AppColor.blueAccentColor, - fontWeight: FontWeight.bold), - ), - SizedBox( - height: 4, - ), - Text( - "Jan 24, 2021, 1:18PM", - textAlign: TextAlign.left, - style: TextStyle(fontSize: 12, color: Colors.white38), - ), - SizedBox( - height: 4, - ), - Text( - "LibreOffice_7.1.3_MacOS", - textAlign: TextAlign.left, - style: TextStyle( - fontSize: 14, - color: Colors.white, - ), - ), - ], - ), - ), - ]; - return Container( - color: AppColor.secondaryColor, - width: 300.0, // Change as per your requirement - child: ListView.builder( - shrinkWrap: true, - itemCount: recentActions.length, - itemBuilder: (BuildContext context, int index) { - if (index == 3) { - return Column( - children: [ - recentActions[index], - SizedBox( - height: 10, - ), - Divider(), - TextButton( - onPressed: () {}, - style: TextButton.styleFrom(fixedSize: Size(200, 50)), - child: Text( - 'Clear All', - style: TextStyle( - color: Colors.white60, - ), - ), - ) - ], - ); - } - return Column( - children: [ - recentActions[index], - SizedBox( - height: 10, - ) - ], - ); - }, - ), - ); -} diff --git a/lib/Model/notification_model.dart b/lib/Model/notification_model.dart index e69de29b..846d526b 100644 --- a/lib/Model/notification_model.dart +++ b/lib/Model/notification_model.dart @@ -0,0 +1,48 @@ +class NotificationModel { + List notifications = []; + int read; + int total; + int unread; + + NotificationModel({this.read, this.notifications, this.total, this.unread}); + + NotificationModel.fromJson(Map json) { + read = json['count']['read']; + total = json['count']['total']; + unread = json['count']['unread']; + dynamic notificationsList = json['notifications'].toList(); + for (var data in notificationsList) { + notifications.add(NotificationContentModel.fromJson(data)); + } + } +} + +class NotificationContentModel { + int ts; + String name; + String id; + bool read; + String identification; + String status; + + NotificationContentModel( + {this.identification, + this.id, + this.name, + this.read, + this.ts, + this.status}); + + NotificationContentModel.fromJson(Map json) { + ts = json['ts']; + name = json['data']['name']; + id = json['id']; + read = json['read']; + identification = json['_id']; + if (id == 'notification.torrent.finished') { + status = 'Finished Download'; + } else { + status = 'Notification'; + } + } +} diff --git a/lib/Pages/home_screen.dart b/lib/Pages/home_screen.dart index de6279e6..dda55ae6 100644 --- a/lib/Pages/home_screen.dart +++ b/lib/Pages/home_screen.dart @@ -1,11 +1,13 @@ +import 'package:badges/badges.dart'; import 'package:flood_mobile/Api/client_api.dart'; import 'package:flood_mobile/Api/notifications_api.dart'; import 'package:flood_mobile/Components/nav_drawer_list_tile.dart'; -import 'package:flood_mobile/Components/popup_dialogue_container.dart'; +import 'package:flood_mobile/Components/notification_popup_dialogue_container.dart'; import 'package:flood_mobile/Constants/app_color.dart'; import 'package:flood_mobile/Pages/about_screen.dart'; import 'package:flood_mobile/Pages/settings_screen.dart'; import 'package:flood_mobile/Pages/torrent_screen.dart'; +import 'package:flood_mobile/Provider/home_provider.dart'; import 'package:flood_mobile/Provider/sse_provider.dart'; import 'package:flood_mobile/Provider/user_detail_provider.dart'; import 'package:flood_mobile/Route/routes.dart'; @@ -87,21 +89,34 @@ class _HomeScreenState extends State { backgroundColor: AppColor.primaryColor, elevation: 0, actions: [ - IconButton( - icon: Icon( - Icons.notifications, - color: Colors.white, + Badge( + badgeColor: AppColor.blueAccentColor, + badgeContent: Center( + child: Text( + Provider.of(context) + .unreadNotifications + .toString(), + style: TextStyle(color: Colors.white), + ), + ), + position: BadgePosition(top: 0, end: 3), + child: IconButton( + icon: Icon( + Icons.notifications, + color: Colors.white, + ), + onPressed: () { + showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + backgroundColor: AppColor.secondaryColor, + content: notificationPopupDialogueContainer( + context: context), + ); + }); + }, ), - onPressed: () { - showDialog( - context: context, - builder: (BuildContext context) { - return AlertDialog( - backgroundColor: AppColor.secondaryColor, - content: popupDialogueContainer(), - ); - }); - }, ), ], ), diff --git a/lib/Provider/home_provider.dart b/lib/Provider/home_provider.dart index 8ac05054..1a407e61 100644 --- a/lib/Provider/home_provider.dart +++ b/lib/Provider/home_provider.dart @@ -1,12 +1,26 @@ +import 'package:flood_mobile/Model/notification_model.dart'; import 'package:flood_mobile/Model/torrent_model.dart'; import 'package:flutter/cupertino.dart'; class HomeProvider extends ChangeNotifier { List torrentList = []; Map torrentListJson = {}; + int unreadNotifications = 0; + NotificationModel notificationModel; String upSpeed = '0 KB/s'; String downSpeed = '0 KB/s'; + + void setUnreadNotifications(int count) { + unreadNotifications = count; + notifyListeners(); + } + + void setNotificationModel(NotificationModel newModel) { + notificationModel = newModel; + notifyListeners(); + } + void setSpeed(String up, String down) { upSpeed = up + '/s'; downSpeed = down + '/s'; diff --git a/pubspec.lock b/pubspec.lock index b00ea128..6a43e4cd 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -29,6 +29,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.6.1" + badges: + dependency: "direct main" + description: + name: badges + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" boolean_selector: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 2d1beaa1..c5af82ed 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -53,6 +53,7 @@ dependencies: pull_to_reveal: ^0.0.3-nullsafety.0 shared_preferences: ^2.0.5 video_player: ^2.1.6 + badges: ^2.0.1 flutter_client_sse: # git: # url: https://github.com/pratikbaid3/flutter_client_sse From 1fc1eda5663bf67ec03e1f25c90c6d6e068e170b Mon Sep 17 00:00:00 2001 From: pratikbaid3 Date: Thu, 5 Aug 2021 13:47:38 +0530 Subject: [PATCH 4/6] fix: Fixed widget rebuilding issue --- ...notification_popup_dialogue_container.dart | 2 +- lib/Pages/home_screen.dart | 101 +++++++++--------- 2 files changed, 51 insertions(+), 52 deletions(-) diff --git a/lib/Components/notification_popup_dialogue_container.dart b/lib/Components/notification_popup_dialogue_container.dart index a9ccd7e6..a5427fc9 100644 --- a/lib/Components/notification_popup_dialogue_container.dart +++ b/lib/Components/notification_popup_dialogue_container.dart @@ -25,7 +25,7 @@ Widget notificationPopupDialogueContainer({@required BuildContext context}) { return Column( children: [ NotificationListTile( - model: Provider.of(context) + model: Provider.of(context, listen: false) .notificationModel .notifications[index]), SizedBox( diff --git a/lib/Pages/home_screen.dart b/lib/Pages/home_screen.dart index dda55ae6..b8e951f1 100644 --- a/lib/Pages/home_screen.dart +++ b/lib/Pages/home_screen.dart @@ -66,62 +66,61 @@ class _HomeScreenState extends State { screenCurrent = AboutScreen(); break; } - - return Scaffold( - appBar: AppBar( - leading: IconButton( - icon: Icon( - Icons.menu, - color: Colors.white, + return Consumer(builder: (context, homeModel, child) { + return Scaffold( + appBar: AppBar( + leading: IconButton( + icon: Icon( + Icons.menu, + color: Colors.white, + ), + onPressed: () { + controller.toggle(); + }, ), - onPressed: () { - controller.toggle(); - }, - ), - title: Image( - image: AssetImage( - 'assets/images/icon.png', + title: Image( + image: AssetImage( + 'assets/images/icon.png', + ), + width: 60, + height: 60, ), - width: 60, - height: 60, - ), - centerTitle: true, - backgroundColor: AppColor.primaryColor, - elevation: 0, - actions: [ - Badge( - badgeColor: AppColor.blueAccentColor, - badgeContent: Center( - child: Text( - Provider.of(context) - .unreadNotifications - .toString(), - style: TextStyle(color: Colors.white), + centerTitle: true, + backgroundColor: AppColor.primaryColor, + elevation: 0, + actions: [ + Badge( + badgeColor: AppColor.blueAccentColor, + badgeContent: Center( + child: Text( + homeModel.unreadNotifications.toString(), + style: TextStyle(color: Colors.white), + ), ), - ), - position: BadgePosition(top: 0, end: 3), - child: IconButton( - icon: Icon( - Icons.notifications, - color: Colors.white, + position: BadgePosition(top: 0, end: 3), + child: IconButton( + icon: Icon( + Icons.notifications, + color: Colors.white, + ), + onPressed: () { + showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + backgroundColor: AppColor.secondaryColor, + content: notificationPopupDialogueContainer( + context: context), + ); + }); + }, ), - onPressed: () { - showDialog( - context: context, - builder: (BuildContext context) { - return AlertDialog( - backgroundColor: AppColor.secondaryColor, - content: notificationPopupDialogueContainer( - context: context), - ); - }); - }, ), - ), - ], - ), - body: screenCurrent, - ); + ], + ), + body: screenCurrent, + ); + }); }, ), ); From 2f02d3d48abd884d5f2594c0c4e34bf184b6223b Mon Sep 17 00:00:00 2001 From: pratikbaid3 Date: Sat, 7 Aug 2021 16:20:12 +0530 Subject: [PATCH 5/6] feat: Completed notification popup --- lib/Api/event_handler_api.dart | 11 ++ lib/Api/notifications_api.dart | 31 ++++- ...notification_popup_dialogue_container.dart | 122 ++++++++++-------- lib/Constants/event_names.dart | 1 + lib/Provider/sse_provider.dart | 4 + 5 files changed, 115 insertions(+), 54 deletions(-) diff --git a/lib/Api/event_handler_api.dart b/lib/Api/event_handler_api.dart index 23523d7a..aa7b8387 100644 --- a/lib/Api/event_handler_api.dart +++ b/lib/Api/event_handler_api.dart @@ -55,6 +55,17 @@ class EventHandlerApi { } } + //Setting notification count + static void setNotificationCount({SSEModel model, BuildContext context}) { + Map data = json.decode(model.data); + if (data.isNotEmpty) { + print('---SET UNREAD NOTIFICATION COUNT---'); + //Set unread notification count + Provider.of(context, listen: false) + .setUnreadNotifications(data['unread']); + } + } + //Updating the full list of torrent static void updateFullTorrentList({SSEModel model, BuildContext context}) { Map oldTorrentList = diff --git a/lib/Api/notifications_api.dart b/lib/Api/notifications_api.dart index 0ab79dea..85bb04dd 100644 --- a/lib/Api/notifications_api.dart +++ b/lib/Api/notifications_api.dart @@ -33,12 +33,39 @@ class NotificationApi { NotificationModel model = NotificationModel.fromJson(response.data); Provider.of(context, listen: false) .setNotificationModel(model); - Provider.of(context, listen: false) - .setUnreadNotifications(model.unread); } else {} } catch (e) { print('--ERROR--'); print(e.toString()); } } + + static Future clearNotification( + {@required BuildContext context}) async { + try { + String url = Provider.of(context, listen: false).baseUrl + + ApiProvider.notifications; + print('---CLEAR NOTIFICATIONS---'); + print(url); + Response response; + Dio dio = new Dio(); + //Headers + dio.options.headers['Accept'] = "application/json"; + dio.options.headers['Content-Type'] = "application/json"; + dio.options.headers['Connection'] = "keep-alive"; + dio.options.headers['Cookie'] = + Provider.of(context, listen: false).token; + response = await dio.delete(url); + if (response.statusCode == 200) { + print('---NOTIFICATIONS CLEARED---'); + getNotifications(context: context); + } else { + print('---ERROR---'); + print(response); + } + } catch (e) { + print('--ERROR--'); + print(e.toString()); + } + } } diff --git a/lib/Components/notification_popup_dialogue_container.dart b/lib/Components/notification_popup_dialogue_container.dart index a5427fc9..8b23d22f 100644 --- a/lib/Components/notification_popup_dialogue_container.dart +++ b/lib/Components/notification_popup_dialogue_container.dart @@ -1,3 +1,4 @@ +import 'package:flood_mobile/Api/notifications_api.dart'; import 'package:flood_mobile/Constants/app_color.dart'; import 'package:flood_mobile/Model/notification_model.dart'; import 'package:flood_mobile/Provider/home_provider.dart'; @@ -6,59 +7,76 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; Widget notificationPopupDialogueContainer({@required BuildContext context}) { - return Container( - color: AppColor.secondaryColor, - width: 300.0, // Change as per your requirement - child: ListView.builder( - shrinkWrap: true, - itemCount: Provider.of(context) - .notificationModel - .notifications - .length, - itemBuilder: (BuildContext context, int index) { - if (index == - Provider.of(context) - .notificationModel - .notifications - .length - - 1) { - return Column( - children: [ - NotificationListTile( - model: Provider.of(context, listen: false) - .notificationModel - .notifications[index]), - SizedBox( - height: 10, - ), - Divider(), - TextButton( - onPressed: () {}, - style: TextButton.styleFrom(fixedSize: Size(200, 50)), - child: Text( - 'Clear All', - style: TextStyle( - color: Colors.white60, - ), - ), - ) - ], - ); - } - return Column( - children: [ - NotificationListTile( - model: Provider.of(context) - .notificationModel - .notifications[index]), - SizedBox( - height: 10, - ) - ], + return (Provider.of(context) + .notificationModel + .notifications + .length == + 0) + ? Container( + color: AppColor.secondaryColor, + width: 300, + child: Text( + 'No notifications to display', + style: TextStyle( + color: AppColor.textColor, + ), + ), + ) + : Container( + color: AppColor.secondaryColor, + width: 300.0, // Change as per your requirement + child: ListView.builder( + shrinkWrap: true, + itemCount: Provider.of(context) + .notificationModel + .notifications + .length, + itemBuilder: (BuildContext context, int index) { + if (index == + Provider.of(context) + .notificationModel + .notifications + .length - + 1) { + return Column( + children: [ + NotificationListTile( + model: Provider.of(context, listen: false) + .notificationModel + .notifications[index]), + SizedBox( + height: 10, + ), + Divider(), + TextButton( + onPressed: () { + NotificationApi.clearNotification(context: context); + }, + style: TextButton.styleFrom(fixedSize: Size(200, 50)), + child: Text( + 'Clear All', + style: TextStyle( + color: Colors.white60, + ), + ), + ) + ], + ); + } + return Column( + children: [ + NotificationListTile( + model: Provider.of(context) + .notificationModel + .notifications[index]), + SizedBox( + height: 10, + ) + ], + ); + }, + ), ); - }, - ), - ); } class NotificationListTile extends StatelessWidget { diff --git a/lib/Constants/event_names.dart b/lib/Constants/event_names.dart index ae40b93a..afd8fdb9 100644 --- a/lib/Constants/event_names.dart +++ b/lib/Constants/event_names.dart @@ -6,4 +6,5 @@ class Events { static const String TORRENT_LIST_FULL_UPDATE = 'TORRENT_LIST_FULL_UPDATE'; //Change in the torrent list static const String TORRENT_LIST_DIFF_CHANGE = 'TORRENT_LIST_DIFF_CHANGE'; + static const String NOTIFICATION_COUNT_CHANGE='NOTIFICATION_COUNT_CHANGE'; } diff --git a/lib/Provider/sse_provider.dart b/lib/Provider/sse_provider.dart index 36d6f779..5ed2a799 100644 --- a/lib/Provider/sse_provider.dart +++ b/lib/Provider/sse_provider.dart @@ -36,6 +36,10 @@ class SSEProvider extends ChangeNotifier { EventHandlerApi.updateFullTorrentList( model: event, context: context); break; + case Events.NOTIFICATION_COUNT_CHANGE: + EventHandlerApi.setNotificationCount( + model: event, context: context); + break; } notifyListeners(); } From 9aad8f92f2cd032c86b547dad2eb5e0126241b17 Mon Sep 17 00:00:00 2001 From: pratikbaid3 Date: Sat, 7 Aug 2021 16:29:28 +0530 Subject: [PATCH 6/6] feat: Added notification pop access in all the pages --- lib/Components/base_app_bar.dart | 65 ++++++++++++++++++++++---------- lib/Constants/event_names.dart | 6 ++- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/lib/Components/base_app_bar.dart b/lib/Components/base_app_bar.dart index 70d67d30..7b5cb4f5 100644 --- a/lib/Components/base_app_bar.dart +++ b/lib/Components/base_app_bar.dart @@ -1,5 +1,10 @@ +import 'package:badges/badges.dart'; import 'package:flood_mobile/Constants/app_color.dart'; +import 'package:flood_mobile/Provider/home_provider.dart'; import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; + +import 'notification_popup_dialogue_container.dart'; class BaseAppBar extends StatelessWidget implements PreferredSizeWidget { final AppBar appBar; @@ -8,27 +13,49 @@ class BaseAppBar extends StatelessWidget implements PreferredSizeWidget { @override Widget build(BuildContext context) { - return AppBar( - title: Image( - image: AssetImage( - 'assets/images/icon.png', - ), - width: 60, - height: 60, - ), - centerTitle: true, - backgroundColor: AppColor.primaryColor, - elevation: 0, - actions: [ - IconButton( - icon: Icon( - Icons.notifications, - color: Colors.white, + return Consumer(builder: (context, homeModel, child) { + return AppBar( + title: Image( + image: AssetImage( + 'assets/images/icon.png', ), - onPressed: () {}, + width: 60, + height: 60, ), - ], - ); + centerTitle: true, + backgroundColor: AppColor.primaryColor, + elevation: 0, + actions: [ + Badge( + badgeColor: AppColor.blueAccentColor, + badgeContent: Center( + child: Text( + homeModel.unreadNotifications.toString(), + style: TextStyle(color: Colors.white), + ), + ), + position: BadgePosition(top: 0, end: 3), + child: IconButton( + icon: Icon( + Icons.notifications, + color: Colors.white, + ), + onPressed: () { + showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + backgroundColor: AppColor.secondaryColor, + content: notificationPopupDialogueContainer( + context: context), + ); + }); + }, + ), + ), + ], + ); + }); } @override diff --git a/lib/Constants/event_names.dart b/lib/Constants/event_names.dart index afd8fdb9..e8f9f910 100644 --- a/lib/Constants/event_names.dart +++ b/lib/Constants/event_names.dart @@ -2,9 +2,13 @@ class Events { //Transfer rate static const String TRANSFER_SUMMARY_DIFF_CHANGE = 'TRANSFER_SUMMARY_DIFF_CHANGE'; + //Full list of torrents static const String TORRENT_LIST_FULL_UPDATE = 'TORRENT_LIST_FULL_UPDATE'; + //Change in the torrent list static const String TORRENT_LIST_DIFF_CHANGE = 'TORRENT_LIST_DIFF_CHANGE'; - static const String NOTIFICATION_COUNT_CHANGE='NOTIFICATION_COUNT_CHANGE'; + + //Notifications count changed + static const String NOTIFICATION_COUNT_CHANGE = 'NOTIFICATION_COUNT_CHANGE'; }