-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostDetailsActivity.java
More file actions
581 lines (491 loc) · 21.6 KB
/
Copy pathPostDetailsActivity.java
File metadata and controls
581 lines (491 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package com.codewithharry.firebase;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import com.codewithharry.firebase.adapters.AdapterComments;
import com.codewithharry.firebase.models.ModelComment;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
public class PostDetailsActivity extends AppCompatActivity {
//to get details of user and post
String hisUid, myUid, myEmail, myName, myDp,
postId, pImage, pLikes, hisDp, hisName;
boolean mProcessComment = false;
boolean mProcessLike = false;
//progress dialog
ProgressDialog pd;
//views
ImageView uPictureIv, pImageIv;
TextView uNameTv, pTimeTiv, pTitleTv, pDescriptionTv, pLikesTv, pCommentsTv;
ImageButton moreBtn;
Button likeBtn, shareBtn;
LinearLayout profileLayout;
RecyclerView recyclerView;
ArrayList<ModelComment> commentList;
AdapterComments adapterComments;
FirebaseAuth firebaseAuth;
//add comments views
EditText commentEt;
ImageButton sendBtn;
ImageView cAvatarIv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_details);
//Actionbar and its properties
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Post Details");
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
//get id of post using intent
Intent intent = getIntent();
postId = intent.getStringExtra("postId");
//init views
uPictureIv = findViewById(R.id.uPictureIv);
pImageIv = findViewById(R.id.pImageIv);
uNameTv = findViewById(R.id.uNameTv);
pTimeTiv = findViewById(R.id.pTimeTv);
pTitleTv = findViewById(R.id.pTitleTv);
pDescriptionTv = findViewById(R.id.pDescriptionTv);
pLikesTv = findViewById(R.id.pLikesTv);
pCommentsTv = findViewById(R.id.pCommentsTv);
moreBtn = findViewById(R.id.moreBtn);
likeBtn = findViewById(R.id.likeBtn);
shareBtn = findViewById(R.id.shareBtn);
profileLayout = findViewById(R.id.profileLayout);
recyclerView = findViewById(R.id.recyclerView);
commentEt = findViewById(R.id.commentEt);
sendBtn = findViewById(R.id.sendBtn);
cAvatarIv = findViewById(R.id.cAvtarIv);
//init FIrebase
firebaseAuth = FirebaseAuth.getInstance();
loadPostInfo();
checkUserStatus();
loadUserInfo();
setLikes();
//set subtitle of action
actionBar.setSubtitle("Signed in as " + myEmail);
loadComments();
//send comment button click
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
postComment();
}
});
//like button handler
likeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
likePost();
}
});
moreBtn.setOnClickListener(view -> showMoreOptions());
}
private void loadComments() {
//layout for recyclerView
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
//set layout to recyclerView
recyclerView.setLayoutManager(layoutManager);
//init comments list
commentList = new ArrayList<>();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Posts").child(postId).child("Comments");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
commentList.clear();
for(DataSnapshot ds: snapshot.getChildren()) {
ModelComment modelComment = ds.getValue(ModelComment.class);
commentList.add(modelComment);
//set up adapter
adapterComments = new AdapterComments(getApplicationContext(), commentList);
//set adapter
recyclerView.setAdapter(adapterComments);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void showMoreOptions() {
//creating pop up menu currently having option Delete, we wil add more options later
PopupMenu popupMenu = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
popupMenu = new PopupMenu(this, moreBtn, Gravity.END);
}
//show delete option in only post(s) of currently signed in user
if (hisUid.equals(myUid)) {
//add items in menu
popupMenu.getMenu().add(Menu.NONE, 0, 0, "Delete");
popupMenu.getMenu().add(Menu.NONE, 1, 0, "Edit");
} else {
moreBtn.setVisibility(View.GONE);
}
//item click listener
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == 0) {
//delete option is clicked
beginDelete();
} else if (id == 1) {
//Edit is clicked
//start AddPostActivity with key "editPost" and the id of the post clicked
Intent intent = new Intent(PostDetailsActivity.this, AddPostActivity.class);
intent.putExtra("key", "editPost");
intent.putExtra("editPostId", postId);
startActivity(intent);
}
return false;
}
});
//show menu
popupMenu.show();
}
private void beginDelete() {
//post can be with or without image
if (pImage.equals("noImage")) {
//post is without image
deleteWithoutImage();
} else {
//post is with image
deleteWithImage();
}
}
private void deleteWithImage() {
//progress bar
ProgressDialog pd = new ProgressDialog(this);
pd.setMessage("Deleting post..");
/*Steps:
* Delete image using url
* Delete from database using post id*/
StorageReference picRef = FirebaseStorage.getInstance().getReferenceFromUrl(pImage);
picRef.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
//image deleted, now delete database
Query fquery = FirebaseDatabase.getInstance().getReference("Posts")
.orderByChild("pId").equalTo(postId);
fquery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()) {
ds.getRef().removeValue();
//remove values from firebase where pId matches
}
//deleted
Toast.makeText(PostDetailsActivity.this, "Deleted successfully", Toast.LENGTH_SHORT).show();
pd.dismiss();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//failed, can't go further
pd.dismiss();
Toast.makeText(PostDetailsActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void deleteWithoutImage() {
ProgressDialog pd = new ProgressDialog(this);
pd.setMessage("Deleting post..");
Query fquery = FirebaseDatabase.getInstance().getReference("Posts")
.orderByChild("pId").equalTo(postId);
fquery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()) {
ds.getRef().removeValue();
//remove values from firebase where pId matches
}
//deleted
Toast.makeText(PostDetailsActivity.this, "Deleted successfully", Toast.LENGTH_SHORT).show();
pd.dismiss();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void setLikes() {
final DatabaseReference likesRef = FirebaseDatabase.getInstance().getReference().child("Likes");
likesRef.addValueEventListener(new ValueEventListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.child(postId).hasChild(myUid)) {
//user has liked this post
/*To indicate that the post is liked by this(SignedIn) user
* Change drawable left icon of like button
* Change text of like button fro "Like" to "Liked"*/
likeBtn.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_liked, 0, 0, 0);
likeBtn.setText("Liked");
} else {
//user has not liked this post
/*To indicate that the post is not liked by this(SignedIn) user
* Change drawable left icon of like button
* Change text of like button fro "Liked" to "Like"*/
likeBtn.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_like_black, 0, 0, 0);
likeBtn.setText("Like");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void likePost() {
//get total number of likes for the post, whose like button is clicked
//if currently signed in user has not liked it before
//increase value by 1, otherwise decrease value by 1
mProcessLike = true;
//get id of the post clicked
final DatabaseReference likesRef = FirebaseDatabase.getInstance().getReference().child("Likes");
final DatabaseReference postsRef = FirebaseDatabase.getInstance().getReference().child("Posts");
likesRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (mProcessLike) {
if (snapshot.child(postId).hasChild(myUid)) {
//already liked, so remove like
postsRef.child(postId).child("pLikes").setValue("" + (Integer.parseInt(pLikes) - 1));
likesRef.child(postId).child(myUid).removeValue();
mProcessLike = false;
} else {
//not liked, like
postsRef.child(postId).child("pLikes").setValue("" + (Integer.parseInt(pLikes) + 1));
likesRef.child(postId).child(myUid).setValue("Liked"); //set any value
mProcessLike = false;
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void postComment() {
pd = new ProgressDialog(this);
pd.setMessage("Adding comment..");
//get data from comment edit text
String comment = commentEt.getText().toString().trim();
//validate
if (TextUtils.isEmpty(comment)) {
//no value is entered
Toast.makeText(this, "Comment section is empty.", Toast.LENGTH_SHORT).show();
return;
}
String timeStamp = String.valueOf(System.currentTimeMillis());
//each post will have child comments that will contain comments of that post
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Posts").child(postId).child("Comments");
HashMap<String, Object> hashMap = new HashMap<>();
//put info in hashMap
hashMap.put("cId",timeStamp);
hashMap.put("comment", comment);
hashMap.put("timeStamp", timeStamp);
hashMap.put("uid", myUid);
hashMap.put("uEmail", myEmail);
hashMap.put("uDp", myDp);
hashMap.put("uName", myName);
//put this data in db
ref.child(timeStamp).setValue(hashMap)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
//added
pd.dismiss();
Toast.makeText(PostDetailsActivity.this, "Comment Added..", Toast.LENGTH_SHORT).show();
commentEt.setText("");
updateCommentCount();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//failed, not added
pd.dismiss();
Toast.makeText(PostDetailsActivity.this, "Failed because "+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void updateCommentCount() {
//when ever user adds a comment increase the comment count
mProcessComment = true;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Posts").child(postId);
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (mProcessComment) {
String comments = "" + snapshot.child("pComments").getValue();
int newCommentVal = Integer.parseInt(comments) + 1;
ref.child("pComments").setValue(""+newCommentVal);
mProcessComment = false;
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void loadUserInfo() {
//get current user info
Query myRef = FirebaseDatabase.getInstance().getReference("Users");
myRef.orderByChild("uid").equalTo(myUid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()) {
myName = "" + ds.child("name").getValue();
myDp = "" + ds.child("image").getValue();
//set data
try {
//if image is received
Picasso.get().load(myDp).placeholder(R.drawable.ic_default_image_blue).into(cAvatarIv);
} catch (Exception e) {
Picasso.get().load(R.drawable.ic_default_image_blue).into(cAvatarIv);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void loadPostInfo() {
//get post using the id of the post
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Posts");
Query query = ref.orderByChild("pId").equalTo(postId);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
//keep checking the posts until you get the required post
for (DataSnapshot ds : snapshot.getChildren()) {
//get data
String pTitle = "" + ds.child("pTitle").getValue();
String pDescr = "" + ds.child("pDescr").getValue();
pLikes = "" + ds.child("pLikes").getValue();
String pTimeStamp = "" + ds.child("pTime").getValue();
pImage = "" + ds.child("pImage").getValue();
hisDp = "" + ds.child("uDp").getValue();
hisUid = "" + ds.child("uid").getValue();
String pEmail = "" + ds.child("uEmail").getValue();
String hisName = "" + ds.child("uName").getValue();
String commentCount = "" + ds.child("pComments").getValue();
//convert timeStamp to dd/mm/yyyy hh:mm am/pm
Calendar calendar = Calendar.getInstance(Locale.getDefault());
calendar.setTimeInMillis(Long.parseLong(pTimeStamp));
String pTime = DateFormat.format("dd/MM/yyyy hh:mm aa", calendar).toString();
//set data
pTitleTv.setText(pTitle);
pDescriptionTv.setText(pDescr);
pLikesTv.setText(pLikes + " Likes");
pTimeTiv.setText(pTime);
pCommentsTv.setText(commentCount + " Comments");
uNameTv.setText(hisName);
//set image of the user who posted
//set post image
//if there is no image then hide image view
if (pImage.equals("noImage")) {
//hide imageview
pImageIv.setVisibility(View.GONE);
} else {
//show imageview
pImageIv.setVisibility(View.VISIBLE);
try {
Picasso.get().load(pImage).into(pImageIv);
} catch (Exception e) {
}
}
//set user image in comment part
try {
Picasso.get().load(hisDp).placeholder(R.drawable.ic_default_image_blue).into(uPictureIv);
} catch (Exception e) {
Picasso.get().load(R.drawable.ic_default_image_blue).into(cAvatarIv);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void checkUserStatus() {
//Get current user
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
//User is signed in stay here
myEmail = user.getEmail();
myUid = user.getUid();
} else {
//User not signed in go to main activity
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return super.onSupportNavigateUp();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
menu.findItem(R.id.action_add_post).setVisible(false);
menu.findItem(R.id.action_search).setVisible(false);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
//get item id
int id = item.getItemId();
if (id == R.id.action_logout) {
FirebaseAuth.getInstance().signOut();
checkUserStatus();
}
return super.onOptionsItemSelected(item);
}
}