Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add functionality to pin notifications by swiping #5

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## [1.2.0] - 17 April 2024

* Add functionality to pin notifications by swiping

## [1.1.0] - 15 April 2024

* Initial Release
* Add option for snackbar and dialog popups

## [1.0.0] - 14 April 2024

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,56 @@ class CustomNotificationWidget extends StatelessWidget {
: Dismissible(
key: Key(notification.id),
onDismissed: (direction) async {
await dismissNotification(notificationService, notification);
if (direction == DismissDirection.endToStart) {
await dismissNotification(notificationService, notification);
} else if (direction == DismissDirection.startToEnd) {
await pinNotification(
notificationService, notification, context);
}
},
background: Container(
color: Colors.red,
color: const Color.fromRGBO(59, 213, 111, 1),
alignment: Alignment.centerLeft,
child: const Padding(
padding: EdgeInsets.only(left: 16.0),
child: Icon(
Icons.push_pin,
color: Colors.white,
),
),
),
secondaryBackground: Container(
color: const Color.fromRGBO(255, 131, 131, 1),
alignment: Alignment.centerRight,
child: const Icon(
Icons.delete,
color: Colors.white,
child: const Padding(
padding: EdgeInsets.only(right: 16.0),
child: Icon(
Icons.delete,
color: Colors.white,
),
),
),
child: GestureDetector(
onTap: () async =>
_navigateToNotificationDetail(context, notification),
onTap: () async => _navigateToNotificationDetail(
context,
notification,
),
child: ListTile(
leading: Icon(
notification.icon,
color: style.leadingIconColor,
color: Colors.grey,
),
title: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Text(
notification.title,
style: style.titleTextStyle,
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.w400,
fontSize: 16,
),
),
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class NotificationModel {
final OcurringInterval? occuringInterval;

/// Indicates if the notification is pinned.
final bool isPinned;
bool isPinned;

/// Indicates if the notification has been read.
bool isRead;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,42 @@ class NotificationCenterState extends State<NotificationCenter> {
: Dismissible(
key: Key(notification.id),
onDismissed: (direction) async {
await dismissNotification(
widget.config.service,
notification,
context);
if (direction ==
DismissDirection.endToStart) {
await dismissNotification(
widget.config.service,
notification,
context);
} else if (direction ==
DismissDirection.startToEnd) {
await pinNotification(
widget.config.service,
notification,
context);
}
},
background: Container(
color: Colors.red,
color:
const Color.fromRGBO(59, 213, 111, 1),
alignment: Alignment.centerLeft,
child: const Padding(
padding: EdgeInsets.only(left: 16.0),
child: Icon(
Icons.push_pin,
color: Colors.white,
),
),
),
secondaryBackground: Container(
color:
const Color.fromRGBO(255, 131, 131, 1),
alignment: Alignment.centerRight,
child: const Icon(
Icons.delete,
color: Colors.white,
child: const Padding(
padding: EdgeInsets.only(right: 16.0),
child: Icon(
Icons.delete,
color: Colors.white,
),
),
),
child: GestureDetector(
Expand Down Expand Up @@ -238,6 +263,21 @@ Future<void> dismissNotification(
}
}

Future<void> pinNotification(
NotificationService notificationService,
NotificationModel notification,
BuildContext context,
) async {
await notificationService.pinActiveNotification(notification);
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Notification pinned"),
),
);
}
}

Future<void> markNotificationAsRead(
NotificationService notificationService,
NotificationModel notification,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ abstract class NotificationService with ChangeNotifier {
/// Dismisses an active notification.
Future dismissActiveNotification(NotificationModel notification);

/// Pin an active notification.
Future pinActiveNotification(NotificationModel notification);

/// Marks a notification as read.
Future markNotificationAsRead(NotificationModel notification);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ class FirebaseNotificationService
}
}

@override
Future<void> pinActiveNotification(
NotificationModel notificationModel) async {
try {
DocumentReference documentReference = FirebaseFirestore.instance
.collection(FirebaseCollectionNames.activeNotifications)
.doc(notificationModel.id);
await documentReference.update({'isPinned': true});
notificationModel.isPinned = true;
notifyListeners();
} catch (e) {
debugPrint('Error updating document: $e');
}
}

@override
Future<void> markNotificationAsRead(
NotificationModel notificationModel) async {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: "A new Flutter project."
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.1.0
version: 1.2.0

environment:
sdk: '>=3.3.2 <4.0.0'
Expand Down