-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdapterPost.java
More file actions
403 lines (350 loc) · 16.2 KB
/
Copy pathAdapterPost.java
File metadata and controls
403 lines (350 loc) · 16.2 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
package com.codewithharry.firebase.adapters;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.format.DateFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
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 androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;
import com.codewithharry.firebase.AddPostActivity;
import com.codewithharry.firebase.PostDetailsActivity;
import com.codewithharry.firebase.R;
import com.codewithharry.firebase.TheirProfileActivity;
import com.codewithharry.firebase.models.ModelPost;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
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.Locale;
public class AdapterPost extends RecyclerView.Adapter<AdapterPost.MyHolder> {
Context context;
ArrayList<ModelPost> postList;
String myUid;
boolean mProcessLike = false;
private final DatabaseReference likesRef; //for likes database node
private final DatabaseReference postsRef; //reference of posts
public AdapterPost(Context context, ArrayList<ModelPost> postList) {
this.context = context;
this.postList = postList;
myUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
likesRef = FirebaseDatabase.getInstance().getReference().child("Likes");
postsRef = FirebaseDatabase.getInstance().getReference().child("Posts");
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
//inflate layout row_posts.xml
View view = LayoutInflater.from(context).inflate(R.layout.row_posts, viewGroup, false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {
//get data
final String uid = postList.get(position).getUid();
String uEmail = postList.get(position).getuEmail();
String uName = postList.get(position).getuName();
String uDp = postList.get(position).getuDp();
final String pId = postList.get(position).getpId();
String pTitle = postList.get(position).getpTitle();
String pDescription = postList.get(position).getpDescr();
final String pImage = postList.get(position).getpImage();
String pTimeStamp = postList.get(position).getpTime();
String pLikes = postList.get(position).getpLikes();
String pComments = postList.get(position).getpComments();
//contains total number of likes for the post
//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
holder.uNameTv.setText(uName);
holder.pTimeTv.setText(pTime);
holder.pTitleTv.setText(pTitle);
holder.pDescriptionTv.setText(pDescription);
holder.pLikesTv.setText(pLikes + " Likes");
holder.pCommentsTv.setText(pComments + " Comments");
//set likes for post
setLikes(holder, pId);
//set user dp
try {
Picasso.get().load(uDp).placeholder(R.drawable.ic_default_image_white).into(holder.uPictureIv);
} catch (Exception e) {
}
//set post image
//if there is no image then hide image view
if (pImage.equals("noImage")) {
//hide imageview
holder.pImageIv.setVisibility(View.GONE);
} else {
//show imageview
holder.pImageIv.setVisibility(View.VISIBLE);
try {
Picasso.get().load(pImage).into(holder.pImageIv);
} catch (Exception e) {
}
}
//handle button clicks
holder.moreBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showMoreOptions(holder.moreBtn, uid, myUid, pId, pImage);
}
});
holder.likeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//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
int pLikes = Integer.parseInt(postList.get(position).getpLikes());
mProcessLike = true;
//get id of the post clicked
String postIde = postList.get(position).getpId();
likesRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (mProcessLike) {
if (snapshot.child(postIde).hasChild(myUid)) {
//already liked, so remove like
postsRef.child(postIde).child("pLikes").setValue("" + (pLikes - 1));
likesRef.child(postIde).child(myUid).removeValue();
mProcessLike = false;
} else {
//not liked, like
postsRef.child(postIde).child("pLikes").setValue("" + (pLikes + 1));
likesRef.child(postIde).child(myUid).setValue("Liked"); //set any value
mProcessLike = false;
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
});
holder.commentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//start postDetailActivity
Intent intent = new Intent(context, PostDetailsActivity.class);
intent.putExtra("postId", pId);
context.startActivity(intent);
}
});
holder.shareBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//will implement later
Toast.makeText(context, "Share", Toast.LENGTH_SHORT).show();
}
});
holder.profileLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*Click to go to TheirProfileActivity with UID,
this UID is clicked of the clicked user
* which will be used to show user specific data/posts*/
Intent intent = new Intent(context, TheirProfileActivity.class);
intent.putExtra("uid", uid);
context.startActivity(intent);
}
});
}
//
// @Override
// public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
// }
private void setLikes(MyHolder holder, String postKey) {
likesRef.addValueEventListener(new ValueEventListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.child(postKey).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"*/
holder.likeBtn.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_liked, 0, 0, 0);
holder.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"*/
holder.likeBtn.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_like_black, 0, 0, 0);
holder.likeBtn.setText("Like");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void showMoreOptions(ImageButton moreBtn, String uid, String myUid, String pId, String pImage) {
//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(context, moreBtn, Gravity.END);
}
//show delete option in only post(s) of currently signed in user
if (uid.equals(myUid)) {
//add items in menu
popupMenu.getMenu().add(Menu.NONE, 0, 0, "Delete");
popupMenu.getMenu().add(Menu.NONE, 1, 0, "Edit");
}
popupMenu.getMenu().add(Menu.NONE, 2, 0, "View Details");
//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(pId, pImage);
} else if (id == 1) {
//Edit is clicked
//start AddPostActivity with key "editPost" and the id of the post clicked
Intent intent = new Intent(context, AddPostActivity.class);
intent.putExtra("key", "editPost");
intent.putExtra("editPostId", pId);
context.startActivity(intent);
} else if (id == 2) {
//start postdetailactivity
Intent intent = new Intent(context, PostDetailsActivity.class);
intent.putExtra("postId", pId);
context.startActivity(intent);
}
return false;
}
});
//show menu
popupMenu.show();
}
private void beginDelete(String pId, String pImage) {
//post can be with or without image
if (pImage.equals("noImage")) {
//post is without image
deleteWithoutImage(pId);
} else {
//post is with image
deleteWithImage(pId, pImage);
}
}
private void deleteWithImage(String pId, String pImage) {
//progress bar
ProgressDialog pd = new ProgressDialog(context);
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(pId);
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(context, "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(context, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void deleteWithoutImage(String pId) {
ProgressDialog pd = new ProgressDialog(context);
pd.setMessage("Deleting post..");
Query fquery = FirebaseDatabase.getInstance().getReference("Posts")
.orderByChild("pId").equalTo(pId);
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(context, "Deleted successfully", Toast.LENGTH_SHORT).show();
pd.dismiss();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public int getItemCount() {
return postList.size();
}
//view holder class
class MyHolder extends RecyclerView.ViewHolder {
//views from row_post.xml
ImageView uPictureIv, pImageIv;
TextView uNameTv, pTimeTv, pTitleTv, pDescriptionTv, pLikesTv, pCommentsTv;
ImageButton moreBtn;
Button likeBtn, commentBtn, shareBtn;
LinearLayout profileLayout;
public MyHolder(@NonNull View itemView) {
super(itemView);
//init views
uPictureIv = itemView.findViewById(R.id.uPictureIv);
pImageIv = itemView.findViewById(R.id.pImageIv);
uNameTv = itemView.findViewById(R.id.uNameTv);
pTimeTv = itemView.findViewById(R.id.pTimeTv);
pTitleTv = itemView.findViewById(R.id.pTitleTv);
pDescriptionTv = itemView.findViewById(R.id.pDescriptionTv);
pLikesTv = itemView.findViewById(R.id.pLikesTv);
pCommentsTv = itemView.findViewById(R.id.pCommentsTv);
moreBtn = itemView.findViewById(R.id.moreBtn);
commentBtn = itemView.findViewById(R.id.commentBtn);
shareBtn = itemView.findViewById(R.id.shareBtn);
likeBtn = itemView.findViewById(R.id.likeBtn);
profileLayout = itemView.findViewById(R.id.profileLayout);
}
}
}