likes
This commit is contained in:
parent
2ffc8db200
commit
093883d40e
@ -4,6 +4,7 @@ import androidx.annotation.NonNull;
|
|||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
@ -35,7 +36,26 @@ public class PostRecyclerViewAdapter extends RecyclerView.Adapter<PostRecyclerVi
|
|||||||
holder.mItem = mValues.get(position);
|
holder.mItem = mValues.get(position);
|
||||||
holder.username.setText(holder.mItem.getUser().getUsername());
|
holder.username.setText(holder.mItem.getUser().getUsername());
|
||||||
holder.content.setText(holder.mItem.getContent());
|
holder.content.setText(holder.mItem.getContent());
|
||||||
holder.like.setText("Likes " + holder.mItem.getLikes());
|
holder.like.setText((holder.mItem.isLiked() ? "Liked " : "Likes ") + holder.mItem.getLikes());
|
||||||
|
holder.like.setBackgroundColor(holder.mItem.isLiked() ? Color.rgb(36, 166, 71) : Color.rgb(173, 33, 61));
|
||||||
|
holder.like.setOnClickListener(v -> {
|
||||||
|
PostService.ToggleLike task = new PostService.ToggleLike(
|
||||||
|
()->{
|
||||||
|
holder.mItem.setLikes(holder.mItem.getLikes()+1);
|
||||||
|
holder.like.setText("Liked " + holder.mItem.getLikes());
|
||||||
|
holder.like.setBackgroundColor(Color.rgb(36, 166, 71));
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
()->{
|
||||||
|
holder.mItem.setLikes(holder.mItem.getLikes()-1);
|
||||||
|
holder.like.setText("Likes " + holder.mItem.getLikes());
|
||||||
|
holder.like.setBackgroundColor(Color.rgb(173, 33, 61));
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
v.getContext());
|
||||||
|
task.execute(holder.mItem.getId(), AuthService.getId());
|
||||||
|
});
|
||||||
|
|
||||||
if (!AuthService.getId().equals(holder.mItem.getUser().getId())) {
|
if (!AuthService.getId().equals(holder.mItem.getUser().getId())) {
|
||||||
holder.remove.setAlpha(0.5f);
|
holder.remove.setAlpha(0.5f);
|
||||||
holder.remove.setClickable(false);
|
holder.remove.setClickable(false);
|
||||||
|
@ -18,6 +18,7 @@ import com.google.android.gms.tasks.Task;
|
|||||||
import com.google.android.gms.tasks.Tasks;
|
import com.google.android.gms.tasks.Tasks;
|
||||||
import com.google.firebase.auth.FirebaseAuth;
|
import com.google.firebase.auth.FirebaseAuth;
|
||||||
import com.google.firebase.firestore.CollectionReference;
|
import com.google.firebase.firestore.CollectionReference;
|
||||||
|
import com.google.firebase.firestore.DocumentReference;
|
||||||
import com.google.firebase.firestore.DocumentSnapshot;
|
import com.google.firebase.firestore.DocumentSnapshot;
|
||||||
import com.google.firebase.firestore.Filter;
|
import com.google.firebase.firestore.Filter;
|
||||||
import com.google.firebase.firestore.FirebaseFirestore;
|
import com.google.firebase.firestore.FirebaseFirestore;
|
||||||
@ -52,12 +53,24 @@ public class PostService {
|
|||||||
String username = user.get("username", String.class);
|
String username = user.get("username", String.class);
|
||||||
String email = user.get("email", String.class);
|
String email = user.get("email", String.class);
|
||||||
String id = user.get("id", String.class);
|
String id = user.get("id", String.class);
|
||||||
|
|
||||||
|
QuerySnapshot likes = Tasks.await(
|
||||||
|
db.collection("like")
|
||||||
|
.where(Filter.equalTo("post", post.getReference()))
|
||||||
|
.get());
|
||||||
|
|
||||||
|
boolean liked = false;
|
||||||
|
for (DocumentSnapshot like : likes) {
|
||||||
|
if (Tasks.await(like.getDocumentReference("user").get()).getString("id").equals(AuthService.getId()))
|
||||||
|
liked = true;
|
||||||
|
}
|
||||||
|
|
||||||
posts.add(new Post(
|
posts.add(new Post(
|
||||||
new User(username, email, id),
|
new User(username, email, id),
|
||||||
post.get("content", String.class),
|
post.get("content", String.class),
|
||||||
post.get("image", String.class),
|
post.get("image", String.class),
|
||||||
0,
|
likes.size(),
|
||||||
false,
|
liked,
|
||||||
post.getId()
|
post.getId()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -96,12 +109,24 @@ public class PostService {
|
|||||||
String username = user.get("username", String.class);
|
String username = user.get("username", String.class);
|
||||||
String email = user.get("email", String.class);
|
String email = user.get("email", String.class);
|
||||||
String id = user.get("id", String.class);
|
String id = user.get("id", String.class);
|
||||||
|
|
||||||
|
QuerySnapshot likes = Tasks.await(
|
||||||
|
db.collection("like")
|
||||||
|
.where(Filter.equalTo("post", post.getReference()))
|
||||||
|
.get());
|
||||||
|
|
||||||
|
boolean liked = false;
|
||||||
|
for (DocumentSnapshot like : likes) {
|
||||||
|
if (Tasks.await(like.getDocumentReference("user").get()).getString("id").equals(AuthService.getId()))
|
||||||
|
liked = true;
|
||||||
|
}
|
||||||
|
|
||||||
posts.add(new Post(
|
posts.add(new Post(
|
||||||
new User(username, email, id),
|
new User(username, email, id),
|
||||||
post.get("content", String.class),
|
post.get("content", String.class),
|
||||||
post.get("image", String.class),
|
post.get("image", String.class),
|
||||||
0,
|
likes.size(),
|
||||||
false,
|
liked,
|
||||||
post.getId()
|
post.getId()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -200,4 +225,85 @@ public class PostService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// post_id, user_id
|
||||||
|
public static class ToggleLike extends AsyncTask<String, Void, Integer> {
|
||||||
|
|
||||||
|
private static final Integer LIKED = 0;
|
||||||
|
private static final Integer DISLIKED = 1;
|
||||||
|
private static final Integer ERROR = -1;
|
||||||
|
|
||||||
|
private final Callable<Void> callback_liked;
|
||||||
|
private final Callable<Void> callback_disliked;
|
||||||
|
|
||||||
|
private final Context context;
|
||||||
|
|
||||||
|
public ToggleLike(Callable<Void> callback_liked, Callable<Void> callback_disliked, Context context) {
|
||||||
|
this.callback_liked = callback_liked;
|
||||||
|
this.callback_disliked = callback_disliked;
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Integer doInBackground(String... strings) {
|
||||||
|
FirebaseFirestore db = FirebaseFirestore.getInstance();
|
||||||
|
|
||||||
|
try {
|
||||||
|
DocumentReference post = db.document("/post/" + strings[0]);
|
||||||
|
DocumentReference user = Tasks.await(
|
||||||
|
db.collection("user")
|
||||||
|
.where(Filter.equalTo("id", strings[1]))
|
||||||
|
.get())
|
||||||
|
.getDocuments().get(0).getReference();
|
||||||
|
|
||||||
|
QuerySnapshot result = Tasks.await(
|
||||||
|
db.collection("like")
|
||||||
|
.where(Filter.equalTo("post", post))
|
||||||
|
.where(Filter.equalTo("user", user))
|
||||||
|
.get());
|
||||||
|
|
||||||
|
// not liked yet, like it
|
||||||
|
if (result.isEmpty()) {
|
||||||
|
HashMap<String, Object> data = new HashMap<>();
|
||||||
|
data.put("post", post);
|
||||||
|
data.put("user", user);
|
||||||
|
Tasks.await(db.collection("like").add(data));
|
||||||
|
return LIKED;
|
||||||
|
}
|
||||||
|
// already liked, remove the like
|
||||||
|
else {
|
||||||
|
for (DocumentSnapshot doc : result) {
|
||||||
|
Tasks.await(doc.getReference().delete());
|
||||||
|
}
|
||||||
|
return DISLIKED;
|
||||||
|
}
|
||||||
|
} catch (ExecutionException | InterruptedException e) {
|
||||||
|
return ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(Integer result) {
|
||||||
|
try {
|
||||||
|
if (result == LIKED)
|
||||||
|
this.callback_liked.call();
|
||||||
|
else if (result == DISLIKED)
|
||||||
|
this.callback_disliked.call();
|
||||||
|
else {
|
||||||
|
Log.e(this.getClass().getName(), "failed to toggle like");
|
||||||
|
Toast toast = new Toast(context);
|
||||||
|
toast.setText("failed to toggle likes");
|
||||||
|
toast.setDuration(Toast.LENGTH_LONG);
|
||||||
|
toast.show();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(this.getClass().getName(), "failed in callbacks while attempting to toggle like");
|
||||||
|
Toast toast = new Toast(context);
|
||||||
|
toast.setText("failed to toggle likes");
|
||||||
|
toast.setDuration(Toast.LENGTH_LONG);
|
||||||
|
toast.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user