@@ -15,9 +15,10 @@ class NotificationState extends AppState {
15
15
String notificationReciverId, notificationTweetId;
16
16
FeedModel notificationTweetModel;
17
17
NotificationType get notificationType => _notificationType;
18
- set setNotificationType (NotificationType type){
19
- _notificationType = type;
20
- }
18
+ set setNotificationType (NotificationType type) {
19
+ _notificationType = type;
20
+ }
21
+
21
22
// FcmNotificationModel notification;
22
23
String notificationSenderId;
23
24
dabase.Query query;
@@ -49,7 +50,7 @@ class NotificationState extends AppState {
49
50
/// get [Notification list] from firebase realtime database
50
51
void getDataFromDatabase (String userId) {
51
52
try {
52
- loading = true ;
53
+ loading = true ;
53
54
_notificationList = [];
54
55
kDatabase
55
56
.child ('notification' )
@@ -60,13 +61,22 @@ class NotificationState extends AppState {
60
61
var map = snapshot.value;
61
62
if (map != null ) {
62
63
map.forEach ((tweetKey, value) {
63
- var model = NotificationModel .fromJson (tweetKey,value["updatedAt" ],snapshot.value["type" ]);
64
+ var model = NotificationModel .fromJson (
65
+ tweetKey, value["updatedAt" ], snapshot.value["type" ]);
64
66
_notificationList.add (model);
65
67
});
66
- _notificationList.sort ((x,y)=> DateTime .parse (y.updatedAt).compareTo (DateTime .parse (x.updatedAt)));
68
+ _notificationList.sort ((x, y) {
69
+ if (x.updatedAt != null && y.updatedAt != null ) {
70
+ return DateTime .parse (y.updatedAt)
71
+ .compareTo (DateTime .parse (x.updatedAt));
72
+ } else if (x.updatedAt != null ) {
73
+ return 1 ;
74
+ } else
75
+ return 0 ;
76
+ });
67
77
}
68
78
}
69
- loading = false ;
79
+ loading = false ;
70
80
});
71
81
} catch (error) {
72
82
loading = false ;
@@ -94,8 +104,7 @@ class NotificationState extends AppState {
94
104
if (userList.length > 0 && userList.any ((x) => x.userId == userId)) {
95
105
return Future .value (userList.firstWhere ((x) => x.userId == userId));
96
106
}
97
- var snapshot =
98
- await kDatabase.child ('profile' ).child (userId).once ();
107
+ var snapshot = await kDatabase.child ('profile' ).child (userId).once ();
99
108
if (snapshot.value != null ) {
100
109
var map = snapshot.value;
101
110
user = User .fromJson (map);
@@ -106,17 +115,17 @@ class NotificationState extends AppState {
106
115
return null ;
107
116
}
108
117
}
118
+
109
119
/// Remove notification if related Tweet is not found or deleted
110
- void removeNotification (String userId, String tweetkey)async {
111
- kDatabase
112
- .child ('notification' )
113
- .child (userId)
114
- .child (tweetkey).remove ();
120
+ void removeNotification (String userId, String tweetkey) async {
121
+ kDatabase.child ('notification' ).child (userId).child (tweetkey).remove ();
115
122
}
123
+
116
124
/// Trigger when somneone like your tweet
117
125
void _onNotificationAdded (Event event) {
118
126
if (event.snapshot.value != null ) {
119
- var model = NotificationModel .fromJson (event.snapshot.key, event.snapshot.value["updatedAt" ],event.snapshot.value["type" ]);
127
+ var model = NotificationModel .fromJson (event.snapshot.key,
128
+ event.snapshot.value["updatedAt" ], event.snapshot.value["type" ]);
120
129
if (_notificationList == null ) {
121
130
_notificationList = List <NotificationModel >();
122
131
}
@@ -130,7 +139,8 @@ class NotificationState extends AppState {
130
139
/// Trigger when someone changed his like preference
131
140
void _onNotificationChanged (Event event) {
132
141
if (event.snapshot.value != null ) {
133
- var model = NotificationModel .fromJson (event.snapshot.key, event.snapshot.value["updatedAt" ],event.snapshot.value["type" ]);
142
+ var model = NotificationModel .fromJson (event.snapshot.key,
143
+ event.snapshot.value["updatedAt" ], event.snapshot.value["type" ]);
134
144
//update notification list
135
145
_notificationList
136
146
.firstWhere ((x) => x.tweetKey == model.tweetKey)
@@ -143,46 +153,46 @@ class NotificationState extends AppState {
143
153
/// Trigger when someone undo his like on tweet
144
154
void _onNotificationRemoved (Event event) {
145
155
if (event.snapshot.value != null ) {
146
- var model = NotificationModel .fromJson (event.snapshot.key, event.snapshot.value["updatedAt" ],event.snapshot.value["type" ]);
156
+ var model = NotificationModel .fromJson (event.snapshot.key,
157
+ event.snapshot.value["updatedAt" ], event.snapshot.value["type" ]);
147
158
// remove notification from list
148
159
_notificationList.removeWhere ((x) => x.tweetKey == model.tweetKey);
149
160
notifyListeners ();
150
161
print ("Notification Removed" );
151
162
}
152
163
}
164
+
153
165
/// Configure notification services
154
- void initfirebaseService (){
166
+ void initfirebaseService () {
155
167
_firebaseMessaging.configure (
156
168
onMessage: (Map <String , dynamic > message) async {
157
169
// print("onMessage: $message");
158
170
print (message['data' ]);
159
171
notifyListeners ();
160
172
},
161
173
onLaunch: (Map <String , dynamic > message) async {
162
- cprint ("Notification " ,event: "onLaunch" );
174
+ cprint ("Notification " , event: "onLaunch" );
163
175
var data = message['data' ];
164
176
// print(message['data']);
165
177
notificationSenderId = data["senderId" ];
166
178
notificationReciverId = data["receiverId" ];
167
179
notificationReciverId = data["receiverId" ];
168
- if (data["type" ] == "NotificationType.Mention" ){
180
+ if (data["type" ] == "NotificationType.Mention" ) {
169
181
setNotificationType = NotificationType .Mention ;
170
- }
171
- else if (data["type" ] == "NotificationType.Message" ){
182
+ } else if (data["type" ] == "NotificationType.Message" ) {
172
183
setNotificationType = NotificationType .Message ;
173
184
}
174
185
notifyListeners ();
175
186
},
176
187
onResume: (Map <String , dynamic > message) async {
177
- cprint ("Notification " ,event: "onResume" );
188
+ cprint ("Notification " , event: "onResume" );
178
189
var data = message['data' ];
179
190
// print(message['data']);
180
191
notificationSenderId = data["senderId" ];
181
192
notificationReciverId = data["receiverId" ];
182
- if (data["type" ] == "NotificationType.Mention" ){
193
+ if (data["type" ] == "NotificationType.Mention" ) {
183
194
setNotificationType = NotificationType .Mention ;
184
- }
185
- else if (data["type" ] == "NotificationType.Message" ){
195
+ } else if (data["type" ] == "NotificationType.Message" ) {
186
196
setNotificationType = NotificationType .Message ;
187
197
}
188
198
notifyListeners ();
@@ -201,5 +211,4 @@ class NotificationState extends AppState {
201
211
print (token);
202
212
});
203
213
}
204
-
205
214
}
0 commit comments