Skip to content

Commit dea30a7

Browse files
committed
Tweet delete functionality improved
1 parent 40c2471 commit dea30a7

File tree

2 files changed

+111
-70
lines changed

2 files changed

+111
-70
lines changed

lib/page/feed/feedPostDetail.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class _FeedPostDetailState extends State<FeedPostDetail> {
4242
trailing: customInkWell(
4343
radius: BorderRadius.circular(20),
4444
context: context,
45-
onPressed: (){openbottomSheet(TweetType.Reply,model.key);},
45+
onPressed: (){openbottomSheet(TweetType.Reply,model);},
4646
child:Container(
4747
width: 25,
4848
height: 25,
@@ -66,7 +66,7 @@ class _FeedPostDetailState extends State<FeedPostDetail> {
6666
trailing: customInkWell(
6767
radius: BorderRadius.circular(20),
6868
context: context,
69-
onPressed: (){openbottomSheet(TweetType.Tweet,model.key);},
69+
onPressed: (){openbottomSheet(TweetType.Detail,model);},
7070
child:Container(
7171
width: 25,
7272
height: 25,
@@ -84,10 +84,10 @@ class _FeedPostDetailState extends State<FeedPostDetail> {
8484
);
8585
}
8686

87-
void openbottomSheet(TweetType type,String tweetId) async {
88-
var state = Provider.of<FeedState>(context,);
87+
void openbottomSheet(TweetType type,FeedModel model) async {
88+
// var state = Provider.of<FeedState>(context,);
8989
var authState = Provider.of<AuthState>(context,);
90-
bool isMyTweet = authState.userId == state.tweetDetailModel.last.userId;
90+
bool isMyTweet = authState.userId == model.userId;
9191
await showModalBottomSheet(
9292
backgroundColor: Colors.transparent,
9393
context: context,
@@ -111,12 +111,12 @@ class _FeedPostDetailState extends State<FeedPostDetail> {
111111
borderRadius: BorderRadius.all(Radius.circular(10)))
112112
),
113113
widgetBottomSheetRow(AppIcon.link,text:'Copy link to tweet'),
114-
isMyTweet ? widgetBottomSheetRow(AppIcon.unFollow,text:'Pin to profile') : widgetBottomSheetRow(AppIcon.unFollow,text:'Unfollow ${state.tweetDetailModel.last.user.userName}'),
115-
isMyTweet ? widgetBottomSheetRow(AppIcon.delete,text:'Delete Tweet', onPressed: (){deleteTweet(type,tweetId);},isEnable:true) : widgetBottomSheetRow(AppIcon.unFollow,text:'Unfollow ${state.tweetDetailModel.last.user.userName}'),
116-
isMyTweet ? Container() : widgetBottomSheetRow(AppIcon.mute,text:'Mute ${state.tweetDetailModel.last.user.userName}'),
114+
isMyTweet ? widgetBottomSheetRow(AppIcon.unFollow,text:'Pin to profile') : widgetBottomSheetRow(AppIcon.unFollow,text:'Unfollow ${model.user.userName}'),
115+
isMyTweet ? widgetBottomSheetRow(AppIcon.delete,text:'Delete Tweet', onPressed: (){deleteTweet(type,model.key, parentkey: model.parentkey);},isEnable:true) : widgetBottomSheetRow(AppIcon.unFollow,text:'Unfollow ${model.user.userName}'),
116+
isMyTweet ? Container() : widgetBottomSheetRow(AppIcon.mute,text:'Mute ${model.user.userName}'),
117117
widgetBottomSheetRow(AppIcon.mute,text:'Mute this convertion'),
118118
widgetBottomSheetRow(AppIcon.viewHidden,text:'View hidden replies'),
119-
isMyTweet ? Container() : widgetBottomSheetRow(AppIcon.block,text:'Block ${state.tweetDetailModel.last.user.userName}'),
119+
isMyTweet ? Container() : widgetBottomSheetRow(AppIcon.block,text:'Block ${model.user.userName}'),
120120
isMyTweet ? Container() : widgetBottomSheetRow(AppIcon.report,text:'Report Tweet'),
121121
],
122122
),
@@ -156,11 +156,11 @@ class _FeedPostDetailState extends State<FeedPostDetail> {
156156
void openImage() async {
157157
Navigator.pushNamed(context, '/ImageViewPge');
158158
}
159-
void deleteTweet(TweetType type,String tweetId){
159+
void deleteTweet(TweetType type,String tweetId, {String parentkey}){
160160
var state = Provider.of<FeedState>(context,);
161-
state.deleteTweet(tweetId,type);
161+
state.deleteTweet(tweetId,type,parentkey:parentkey);
162162
Navigator.of(context).pop();
163-
if(type == TweetType.Tweet)
163+
if(type == TweetType.Detail)
164164
Navigator.of(context).pop();
165165
}
166166
@override

lib/state/feedState.dart

Lines changed: 99 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,31 @@ class FeedState extends AuthState {
2828
/// [Skip if any duplicate tweet already present]
2929
if (_tweetDetailModel.length == 0 ||
3030
_tweetDetailModel.length > 0 &&
31-
!_tweetDetailModel.any((x) => x.key == model.key)) {
32-
_tweetDetailModel.add(model);
33-
notifyListeners();
34-
}
31+
!_tweetDetailModel.any((x) => x.key == model.key)) {
32+
_tweetDetailModel.add(model);
33+
notifyListeners();
34+
}
3535
}
36+
3637
/// remove last tweet available from tweet detail page stack
3738
void removeLastTweetDetail(String tweetKey) {
3839
if (_tweetDetailModel != null && _tweetDetailModel.length > 0) {
3940
_tweetDetailModel.removeWhere((x) => x.key == tweetKey);
4041
tweetReplyMap.removeWhere((key, value) => key == tweetKey);
4142
}
4243
}
44+
4345
/// [clear all tweets] if any tweet present in tweet detail page or comment tweet
44-
void clearAllDetailAndReplyTweetStack(){
45-
if(_tweetDetailModel != null){
46+
void clearAllDetailAndReplyTweetStack() {
47+
if (_tweetDetailModel != null) {
4648
_tweetDetailModel.clear();
4749
}
48-
if(tweetReplyMap != null){
49-
tweetReplyMap.clear();
50-
}
50+
if (tweetReplyMap != null) {
51+
tweetReplyMap.clear();
52+
}
5153
cprint('Empty tweets from stack');
5254
}
55+
5356
/// contain tweet list for home page
5457
List<FeedModel> get feedlist {
5558
if (_feedlist == null) {
@@ -66,6 +69,7 @@ class FeedState extends AuthState {
6669
return List.from(_commentlist);
6770
}
6871
}
72+
6973
/// [Intitilise firebase Database]
7074
Future<bool> databaseInit() {
7175
try {
@@ -81,7 +85,8 @@ class FeedState extends AuthState {
8185
return Future.value(false);
8286
}
8387
}
84-
/// get [Tweet list] from firebase realtime database
88+
89+
/// get [Tweet list] from firebase realtime database
8590
void getDataFromDatabase() {
8691
try {
8792
isBusy = true;
@@ -108,6 +113,7 @@ class FeedState extends AuthState {
108113
cprint(error);
109114
}
110115
}
116+
111117
/// get [Tweet Detail] from firebase realtime database
112118
void getpostDetailFromDatabase(String postID) {
113119
try {
@@ -127,7 +133,7 @@ class FeedState extends AuthState {
127133
// _tweetDetailModel = null;
128134
}
129135
}).then((value) {
130-
_commentlist = List<FeedModel>();
136+
_commentlist = List<FeedModel>();
131137
if (_tweetDetail.replyTweetKeyList != null &&
132138
_tweetDetail.replyTweetKeyList.length > 0) {
133139
_tweetDetail.replyTweetKeyList.forEach((x) {
@@ -148,25 +154,23 @@ class FeedState extends AuthState {
148154
if (!_commentlist.any((x) => x.key == key)) {
149155
_commentlist.add(commentmodel);
150156
}
151-
} else {
152-
153-
}
157+
} else {}
154158
if (x == _tweetDetail.replyTweetKeyList.last) {
155159
tweetReplyMap.putIfAbsent(postID, () => _commentlist);
156160
notifyListeners();
157161
}
158162
});
159163
});
160-
}
161-
else{
164+
} else {
162165
tweetReplyMap.putIfAbsent(postID, () => _commentlist);
163-
notifyListeners();
166+
notifyListeners();
164167
}
165168
});
166169
} catch (error) {
167170
cprint(error);
168171
}
169172
}
173+
170174
/// create [New Tweet]
171175
createTweet(FeedModel model) {
172176
/// Create feed in [Firebase database]
@@ -180,30 +184,65 @@ class FeedState extends AuthState {
180184
isBusy = false;
181185
notifyListeners();
182186
}
187+
183188
/// [Delete tweet]
184-
deleteTweet(String tweetId, TweetType type) {
189+
deleteTweet(String tweetId, TweetType type, {String parentkey}) {
185190
/// Delete feed in [Firebase database]
186191
try {
187-
_database.reference().child('feed').child(tweetId).remove().then((_) {
188-
if (_feedlist.any((x) => x.key == tweetId)) {
189-
if (_tweetDetailModel.last.imagePath != null &&
190-
_tweetDetailModel.last.imagePath.length > 0) {
191-
deleteFile(_tweetDetailModel.last.imagePath, 'feeds');
192-
}
193-
_feedlist.removeWhere((x) => x.key == tweetId);
194-
if (_feedlist.length == 0) {
195-
_feedlist = null;
196-
}
197-
notifyListeners();
198-
cprint('Tweet deleted');
192+
_database.reference().child('feed').child(tweetId).remove().then((_) {
193+
FeedModel deletedTweet;
194+
if (_feedlist.any((x) => x.key == tweetId)) {
195+
/// Delete tweet if it is in home page tweet.
196+
deletedTweet = _feedlist.firstWhere((x) => x.key == tweetId);
197+
_feedlist.remove(deletedTweet);
198+
if (_feedlist.length == 0) {
199+
_feedlist = null;
199200
}
200-
});
201-
_database.reference().child('comment').child(tweetId).remove();
202-
201+
cprint('Tweet deleted from home page tweet');
202+
}
203+
204+
/// Delete tweet if it is in nested tweet detail page
205+
if (type == TweetType.Detail &&
206+
_tweetDetailModel != null &&
207+
_tweetDetailModel.length > 0) {
208+
deletedTweet = _tweetDetailModel.firstWhere((x) => x.key == tweetId);
209+
_tweetDetailModel.remove(_tweetDetailModel);
210+
if (_tweetDetailModel.length == 0) {
211+
_tweetDetailModel = null;
212+
}
213+
214+
cprint('Tweet deleted from nested tweet detail page tweet');
215+
}
216+
217+
/// Delete tweet if it is in nested tweet detail comment section page
218+
if (parentkey != null &&
219+
parentkey.isNotEmpty &&
220+
tweetReplyMap != null &&
221+
tweetReplyMap.length > 0 &&
222+
tweetReplyMap.keys.any((x) => x == parentkey)) {
223+
// (type == TweetType.Reply || tweetReplyMap.length > 1) &&
224+
deletedTweet =
225+
tweetReplyMap[parentkey].firstWhere((x) => x.key == tweetId);
226+
tweetReplyMap[parentkey].remove(deletedTweet);
227+
if (tweetReplyMap[parentkey].length == 0) {
228+
tweetReplyMap[parentkey] = null;
229+
}
230+
cprint(
231+
'Tweet deleted from nested ttweet detail comment section page');
232+
}
233+
234+
/// Delete tweet image from firebase if exist.
235+
if (deletedTweet.imagePath != null &&
236+
deletedTweet.imagePath.length > 0) {
237+
deleteFile(deletedTweet.imagePath, 'feeds');
238+
}
239+
notifyListeners();
240+
});
203241
} catch (error) {
204242
cprint(error);
205243
}
206244
}
245+
207246
/// upload [file] to firebase storage and return its path url
208247
Future<String> uploadFile(File file) async {
209248
try {
@@ -225,7 +264,8 @@ class FeedState extends AuthState {
225264
return null;
226265
}
227266
}
228-
/// [Delete file] from firebase storage
267+
268+
/// [Delete file] from firebase storage
229269
Future<void> deleteFile(String url, String baseUrl) async {
230270
try {
231271
String filePath = url.replaceAll(
@@ -246,6 +286,7 @@ class FeedState extends AuthState {
246286
cprint(error);
247287
}
248288
}
289+
249290
/// add [Like] to tweet
250291
addLikeToComment({String postId, FeedModel commentModel, String userId}) {
251292
try {
@@ -278,6 +319,7 @@ class FeedState extends AuthState {
278319
cprint(error);
279320
}
280321
}
322+
281323
/// [postId] is tweet id, [userId] is user's id
282324
addLikeToTweet(FeedModel tweet, String userId) {
283325
try {
@@ -307,17 +349,17 @@ class FeedState extends AuthState {
307349
cprint("[addLikeToTweet] $error");
308350
}
309351
}
352+
310353
/// add [new comment tweet] to any tweet
311354
addcommentToPost(String postId, FeedModel replyTweet) {
312355
try {
313356
isBusy = true;
314357
notifyListeners();
315358
if (postId != null) {
316359
FeedModel tweet = _feedlist.firstWhere((x) => x.key == postId);
317-
360+
318361
var json = replyTweet.toJson();
319362
_database.reference().child('feed').push().set(json).then((value) {
320-
321363
tweet.replyTweetKeyList.add(_feedlist.last.key);
322364
_database.reference().child('feed').child(postId).set(tweet.toJson());
323365
});
@@ -341,25 +383,25 @@ class FeedState extends AuthState {
341383

342384
if (_tweetDetailModel != null && _tweetDetailModel.length > 0) {
343385
if (_tweetDetailModel.any((x) => x.key == model.key)) {
344-
345386
var oldEntry = _tweetDetailModel.singleWhere((entry) {
346-
return entry.key == event.snapshot.key;
387+
return entry.key == event.snapshot.key;
347388
});
348389
_tweetDetailModel[_tweetDetailModel.indexOf(oldEntry)] = model;
349390
}
350-
if(tweetReplyMap != null && tweetReplyMap.length > 0){
351-
if(true){
352-
var list = tweetReplyMap[model.parentkey];
353-
// var list = tweetReplyMap.values.firstWhere((x) => x.any((y) => y.key == model.key));
354-
if(list != null && list.length > 0){
355-
list.firstWhere((x) => x.key == model.key).likeCount = model.likeCount;
356-
list.firstWhere((x) => x.key == model.key).likeList = model.likeList;
357-
}
358-
else{
359-
list = [];
360-
list.add(model);
361-
}
391+
if (tweetReplyMap != null && tweetReplyMap.length > 0) {
392+
if (true) {
393+
var list = tweetReplyMap[model.parentkey];
394+
// var list = tweetReplyMap.values.firstWhere((x) => x.any((y) => y.key == model.key));
395+
if (list != null && list.length > 0) {
396+
list.firstWhere((x) => x.key == model.key).likeCount =
397+
model.likeCount;
398+
list.firstWhere((x) => x.key == model.key).likeList =
399+
model.likeList;
400+
} else {
401+
list = [];
402+
list.add(model);
362403
}
404+
}
363405
}
364406
}
365407
if (event.snapshot != null) {
@@ -386,16 +428,15 @@ class FeedState extends AuthState {
386428
}
387429

388430
_onCommentAdded(FeedModel tweet) {
389-
/// add [new tweet] comment to comment list
390-
if(tweetReplyMap != null && tweetReplyMap.length > 0){
391-
var list = tweetReplyMap[tweet.parentkey];
392-
if(list != null){
393-
list.add(tweet);
394-
}
431+
/// add [new tweet] comment to comment list
432+
if (tweetReplyMap != null && tweetReplyMap.length > 0) {
433+
var list = tweetReplyMap[tweet.parentkey];
434+
if (list != null) {
435+
list.add(tweet);
436+
}
395437
}
396-
cprint('Comment created');
438+
cprint('Comment created');
397439
isBusy = false;
398440
notifyListeners();
399441
}
400-
401442
}

0 commit comments

Comments
 (0)