Skip to content

Commit 010ad6d

Browse files
committed
update notification to realtime notification
1 parent de1d2fc commit 010ad6d

File tree

2 files changed

+70
-39
lines changed

2 files changed

+70
-39
lines changed

lib/page/notification/notificationPage.dart

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ class _NotificationPageState extends State<NotificationPage> {
3535
var state = Provider.of<NotificationState>(context);
3636
var list = state.notificationList;
3737
if (list == null || list.isEmpty) {
38-
return Padding(
39-
padding: EdgeInsets.symmetric(horizontal: 30),
40-
child:EmptyList(
41-
'No Notification available yet',
42-
subTitle: 'When new notificaion found, they\'ll show up here.',
43-
)
44-
);
38+
return Padding(
39+
padding: EdgeInsets.symmetric(horizontal: 30),
40+
child: EmptyList(
41+
'No Notification available yet',
42+
subTitle: 'When new notificaion found, they\'ll show up here.',
43+
));
4544
}
4645
return ListView.separated(
4746
physics: BouncingScrollPhysics(),
@@ -68,32 +67,34 @@ class _NotificationPageState extends State<NotificationPage> {
6867
? snapshot.data.description.substring(0, 150) + '...'
6968
: snapshot.data.description;
7069
return Container(
71-
padding: EdgeInsets.symmetric(vertical: 10),
72-
color: TwitterColor.white,
73-
child:ListTile(
74-
onTap: () {
75-
var state = Provider.of<FeedState>(context);
76-
state.getpostDetailFromDatabase(null,model:snapshot.data);
77-
Navigator.of(context)
78-
.pushNamed('/FeedPostDetail/' + model.tweetKey);
79-
},
80-
title: _userList(snapshot.data.likeList),
81-
subtitle: Padding(
82-
padding: EdgeInsets.only(left: 60),
83-
child: UrlText(
84-
text: des,
85-
style: TextStyle(
86-
color: AppColor.darkGrey,
87-
fontWeight: FontWeight.w400,
70+
padding: EdgeInsets.symmetric(vertical: 10),
71+
color: TwitterColor.white,
72+
child: ListTile(
73+
onTap: () {
74+
var state = Provider.of<FeedState>(context);
75+
state.getpostDetailFromDatabase(null, model: snapshot.data);
76+
Navigator.of(context)
77+
.pushNamed('/FeedPostDetail/' + model.tweetKey);
78+
},
79+
title: _userList(snapshot.data.likeList),
80+
subtitle: Padding(
81+
padding: EdgeInsets.only(left: 60),
82+
child: UrlText(
83+
text: des,
84+
style: TextStyle(
85+
color: AppColor.darkGrey,
86+
fontWeight: FontWeight.w400,
87+
),
88+
),
8889
),
89-
),
90-
),
91-
)
90+
));
91+
} else if (snapshot.connectionState == ConnectionState.waiting ||
92+
snapshot.connectionState == ConnectionState.active) {
93+
return SizedBox(
94+
height: 4,
95+
child: LinearProgressIndicator(),
9296
);
93-
} else if(snapshot.connectionState == ConnectionState.waiting || snapshot.connectionState == ConnectionState.active){
94-
return LinearProgressIndicator();
95-
}
96-
else{
97+
} else {
9798
return SizedBox();
9899
}
99100
},

lib/state/notificationState.dart

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import 'package:flutter_twitter_clone/model/user.dart';
1010

1111
class NotificationState extends ChangeNotifier {
1212
List<NotificationModel> _notificationList;
13-
List<NotificationModel> get notificationList => _notificationList;
13+
List<NotificationModel> get notificationList => _notificationList;
1414
List<User> userList = [];
1515
dabase.Query query;
1616

@@ -23,6 +23,7 @@ class NotificationState extends ChangeNotifier {
2323
query = _database.reference().child("notification").child(userId);
2424
query.onChildAdded.listen(_onNotificationAdded);
2525
query.onChildChanged.listen(_onNotificationChanged);
26+
query.onChildRemoved.listen(_onNotificationRemoved);
2627
}
2728

2829
return Future.value(true);
@@ -46,8 +47,8 @@ class NotificationState extends ChangeNotifier {
4647
var map = snapshot.value;
4748
if (map != null) {
4849
map.forEach((tweetKey, value) {
49-
var model = NotificationModel.fromJson(tweetKey);
50-
_notificationList.add(model);
50+
var model = NotificationModel.fromJson(tweetKey);
51+
_notificationList.add(model);
5152
});
5253
}
5354
}
@@ -57,11 +58,12 @@ class NotificationState extends ChangeNotifier {
5758
cprint(error);
5859
}
5960
}
61+
6062
/// get notification `Tweet`
6163
Future<FeedModel> getTweetDetail(String tweetId) async {
6264
FeedModel _tweetDetail;
6365
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();
6567
if (snapshot.value != null) {
6668
var map = snapshot.value;
6769
_tweetDetail = FeedModel.fromJson(map);
@@ -71,11 +73,12 @@ class NotificationState extends ChangeNotifier {
7173
return null;
7274
}
7375
}
76+
7477
/// get user who liked your tweet
7578
Future<User> getuserDetail(String userId) async {
7679
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));
7982
}
8083
final databaseReference = FirebaseDatabase.instance.reference();
8184
var snapshot =
@@ -91,11 +94,38 @@ class NotificationState extends ChangeNotifier {
9194
}
9295
}
9396

97+
/// Trigger when somneone like your tweet
9498
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+
}
96106
}
97107

108+
/// Trigger when someone changed his like preference
98109
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+
}
100130
}
101131
}

0 commit comments

Comments
 (0)