Skip to content

Commit

Permalink
#137 : Implemented notification feature for user follow actions (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alpha17-2 committed May 26, 2024
2 parents 0fa8340 + 187e6bc commit 061fb25
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
11 changes: 9 additions & 2 deletions lib/models/notification_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Notification {
bool seen;
String forUser;
// Following notification details

String? userWhoFollowed;
// Comment notification details
String? postId; // Common for all , likes, comment , and replies
String? commentId;
Expand All @@ -29,6 +29,7 @@ class Notification {
required this.createdAt,
required this.seen,
required this.forUser,
this.userWhoFollowed,
this.postId,
this.commentId,
this.userWhoCommented,
Expand All @@ -47,6 +48,7 @@ class Notification {
DateTime? createdAt,
bool? seen,
String? forUser,
String? userWhoFollowed,
String? postId,
String? commentId,
String? userWhoCommented,
Expand All @@ -64,6 +66,7 @@ class Notification {
createdAt: createdAt ?? this.createdAt,
seen: seen ?? this.seen,
forUser: forUser ?? this.forUser,
userWhoFollowed: userWhoFollowed ?? this.userWhoFollowed,
postId: postId ?? this.postId,
commentId: commentId ?? this.commentId,
userWhoCommented: userWhoCommented ?? this.userWhoCommented,
Expand All @@ -84,6 +87,7 @@ class Notification {
'createdAt': createdAt.millisecondsSinceEpoch,
'seen': seen,
'forUser': forUser,
'userWhoFollowed': userWhoFollowed,
'postId': postId,
'commentId': commentId,
'userWhoCommented': userWhoCommented,
Expand All @@ -104,6 +108,7 @@ class Notification {
createdAt: DateTime.fromMillisecondsSinceEpoch(map['createdAt'] as int),
seen: map['seen'] as bool,
forUser: map['forUser'] as String,
userWhoFollowed: map['userWhoFollowed'] != null ? map['userWhoFollowed'] as String : null,
postId: map['postId'] != null ? map['postId'] as String : null,
commentId: map['commentId'] != null ? map['commentId'] as String : null,
userWhoCommented: map['userWhoCommented'] != null ? map['userWhoCommented'] as String : null,
Expand All @@ -124,7 +129,7 @@ class Notification {

@override
String toString() {
return 'Notification(type: $type, id: $id, createdAt: $createdAt, seen: $seen, forUser: $forUser, postId: $postId, commentId: $commentId, userWhoCommented: $userWhoCommented, comment: $comment, replyId: $replyId, reply: $reply, userWhoReplied: $userWhoReplied, likeId: $likeId, userWhoLiked: $userWhoLiked, userWhoMentioned: $userWhoMentioned)';
return 'Notification(type: $type, id: $id, createdAt: $createdAt, seen: $seen, forUser: $forUser, userWhoFollowed: $userWhoFollowed, postId: $postId, commentId: $commentId, userWhoCommented: $userWhoCommented, comment: $comment, replyId: $replyId, reply: $reply, userWhoReplied: $userWhoReplied, likeId: $likeId, userWhoLiked: $userWhoLiked, userWhoMentioned: $userWhoMentioned)';
}

@override
Expand All @@ -137,6 +142,7 @@ class Notification {
other.createdAt == createdAt &&
other.seen == seen &&
other.forUser == forUser &&
other.userWhoFollowed == userWhoFollowed &&
other.postId == postId &&
other.commentId == commentId &&
other.userWhoCommented == userWhoCommented &&
Expand All @@ -156,6 +162,7 @@ class Notification {
createdAt.hashCode ^
seen.hashCode ^
forUser.hashCode ^
userWhoFollowed.hashCode ^
postId.hashCode ^
commentId.hashCode ^
userWhoCommented.hashCode ^
Expand Down
75 changes: 74 additions & 1 deletion lib/view/reusable_components/notification_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,80 @@ class NotificationBox extends StatelessWidget {
case 'mention':
return Container();
case 'follow':
return Container();
return FutureBuilder<User>(
future: UserData.getUser(userID: notification.userWhoFollowed!),
builder: (BuildContext context, AsyncSnapshot<User> userSnap) {
if (userSnap.connectionState == ConnectionState.done &&
userSnap.hasData) {
final timeOfComment = calculateTimeAgo(notification.createdAt);
return ListTile(
leading: CircleAvatar(
backgroundImage: (userSnap.data!.displayPicture != null &&
userSnap.data!.displayPicture!.isNotEmpty)
? CachedNetworkImageProvider(
userSnap.data!.displayPicture!)
: null,
radius: 20,
child: userSnap.data!.displayPicture == null ||
userSnap.data!.displayPicture!.isEmpty
? const Icon(Icons.person)
: null,
),
title: RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(
text: userSnap.data!.name, // The name
style: TextStyle(
fontWeight: FontWeight.bold,
fontFamily: CustomFont.poppins,
color: Colors.blue, // Change to your desired color
fontSize: 13,
),
),
TextSpan(
text: ' started following you', // The rest of the text
style: TextStyle(
color: Colors.black,
fontFamily: CustomFont.poppins,
fontSize: 13,
),
),
],
),
),
subtitle: Padding(
padding: const EdgeInsets.only(top: 6.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Say Hi to your new follower !",
maxLines: 3,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontFamily: CustomFont.poppins,
fontSize: 12,
color: Colors.grey.shade800),
),
const SizedBox(
height: 2,
),
Text(
'${timeago.format(timeOfComment, locale: 'en_short')} ago',
style: TextStyle(
fontFamily: CustomFont.poppins,
fontSize: 10,
color: Colors.red.shade700)),
],
),
),
);
}
return const SizedBox();
},
);
case 'reply':
return Container();
default:
Expand Down
20 changes: 19 additions & 1 deletion lib/view/screens/ProfileScreen/other_profile.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:wave/controllers/Authentication/user_controller.dart';
import 'package:wave/data/notification_data.dart';
import 'package:wave/models/response_model.dart';
import 'package:wave/models/user_model.dart';
import 'package:wave/services/notification_service.dart';
import 'package:wave/utils/constants/custom_colors.dart';
import 'package:wave/utils/constants/custom_fonts.dart';
import 'package:wave/utils/constants/custom_icons.dart';
Expand All @@ -11,8 +14,8 @@ import 'package:wave/utils/device_size.dart';
import 'package:get/get.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:wave/utils/routing.dart';
import 'package:wave/models/notification_model.dart' as not;
import 'package:wave/view/screens/ProfileScreen/more_options_other_profile.dart';
import 'package:wave/view/screens/ProfileScreen/more_options_self.dart';

class OtherProfile extends StatefulWidget {
final String otherUserId;
Expand Down Expand Up @@ -116,6 +119,21 @@ class _OtherProfileState extends State<OtherProfile> {
CustomResponse? customResponse =
await userDataController
.followUser(widget.otherUserId);
not.Notification notification =
not.Notification(
createdAt: DateTime.now(),
userWhoFollowed: userDataController.user!.id,
forUser: widget.otherUserId,
id: "",
seen: false,
type: 'follow',
);

NotificationData.createNotification(
notification: notification);

// TODO : Send push notification
// NotificationService.sendPushNotification(fcmToken: fcmToken, title: title, message: message)
}
},
shape: RoundedRectangleBorder(
Expand Down

0 comments on commit 061fb25

Please sign in to comment.