@@ -10,7 +10,7 @@ import 'package:flutter_twitter_clone/model/user.dart';
10
10
11
11
class NotificationState extends ChangeNotifier {
12
12
List <NotificationModel > _notificationList;
13
- List <NotificationModel > get notificationList => _notificationList;
13
+ List <NotificationModel > get notificationList => _notificationList;
14
14
List <User > userList = [];
15
15
dabase.Query query;
16
16
@@ -23,6 +23,7 @@ class NotificationState extends ChangeNotifier {
23
23
query = _database.reference ().child ("notification" ).child (userId);
24
24
query.onChildAdded.listen (_onNotificationAdded);
25
25
query.onChildChanged.listen (_onNotificationChanged);
26
+ query.onChildRemoved.listen (_onNotificationRemoved);
26
27
}
27
28
28
29
return Future .value (true );
@@ -46,8 +47,8 @@ class NotificationState extends ChangeNotifier {
46
47
var map = snapshot.value;
47
48
if (map != null ) {
48
49
map.forEach ((tweetKey, value) {
49
- var model = NotificationModel .fromJson (tweetKey);
50
- _notificationList.add (model);
50
+ var model = NotificationModel .fromJson (tweetKey);
51
+ _notificationList.add (model);
51
52
});
52
53
}
53
54
}
@@ -57,11 +58,12 @@ class NotificationState extends ChangeNotifier {
57
58
cprint (error);
58
59
}
59
60
}
61
+
60
62
/// get notification `Tweet`
61
63
Future <FeedModel > getTweetDetail (String tweetId) async {
62
64
FeedModel _tweetDetail;
63
65
final databaseReference = FirebaseDatabase .instance.reference ();
64
- var snapshot = await databaseReference.child ('feed ' ).child (tweetId).once ();
66
+ var snapshot = await databaseReference.child ('tweet ' ).child (tweetId).once ();
65
67
if (snapshot.value != null ) {
66
68
var map = snapshot.value;
67
69
_tweetDetail = FeedModel .fromJson (map);
@@ -71,11 +73,12 @@ class NotificationState extends ChangeNotifier {
71
73
return null ;
72
74
}
73
75
}
76
+
74
77
/// get user who liked your tweet
75
78
Future <User > getuserDetail (String userId) async {
76
79
User user;
77
- if (userList.length > 0 && userList.any ((x)=> x.userId == userId)){
78
- return Future .value (userList.firstWhere ((x)=> x.userId == userId));
80
+ if (userList.length > 0 && userList.any ((x) => x.userId == userId)) {
81
+ return Future .value (userList.firstWhere ((x) => x.userId == userId));
79
82
}
80
83
final databaseReference = FirebaseDatabase .instance.reference ();
81
84
var snapshot =
@@ -91,11 +94,38 @@ class NotificationState extends ChangeNotifier {
91
94
}
92
95
}
93
96
97
+ /// Trigger when somneone like your tweet
94
98
void _onNotificationAdded (Event event) {
95
- print ("Notification added" );
99
+ if (event.snapshot.value != null ) {
100
+ var model = NotificationModel .fromJson (event.snapshot.key);
101
+ _notificationList.add (model);
102
+ // added notification to list
103
+ print ("Notification added" );
104
+ notifyListeners ();
105
+ }
96
106
}
97
107
108
+ /// Trigger when someone changed his like preference
98
109
void _onNotificationChanged (Event event) {
99
- print ("Notification changed" );
110
+ if (event.snapshot.value != null ) {
111
+ var model = NotificationModel .fromJson (event.snapshot.key);
112
+ //update notification list
113
+ _notificationList
114
+ .firstWhere ((x) => x.tweetKey == model.tweetKey)
115
+ .tweetKey = model.tweetKey;
116
+ notifyListeners ();
117
+ print ("Notification changed" );
118
+ }
119
+ }
120
+
121
+ /// Trigger when someone undo his like on tweet
122
+ void _onNotificationRemoved (Event event) {
123
+ if (event.snapshot.value != null ) {
124
+ var model = NotificationModel .fromJson (event.snapshot.key);
125
+ // remove notification from list
126
+ _notificationList.removeWhere ((x) => x.tweetKey == model.tweetKey);
127
+ notifyListeners ();
128
+ print ("Notification Removed" );
129
+ }
100
130
}
101
131
}
0 commit comments