diff --git a/app/src/main/java/com/dowerx/quack/fragment/PostRecyclerViewAdapter.java b/app/src/main/java/com/dowerx/quack/fragment/PostRecyclerViewAdapter.java index 27a679e..2606e31 100644 --- a/app/src/main/java/com/dowerx/quack/fragment/PostRecyclerViewAdapter.java +++ b/app/src/main/java/com/dowerx/quack/fragment/PostRecyclerViewAdapter.java @@ -4,6 +4,7 @@ import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import android.annotation.SuppressLint; +import android.graphics.Color; import android.view.LayoutInflater; import android.view.ViewGroup; import android.widget.Button; @@ -35,7 +36,26 @@ public class PostRecyclerViewAdapter extends RecyclerView.Adapter { + 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())) { holder.remove.setAlpha(0.5f); holder.remove.setClickable(false); diff --git a/app/src/main/java/com/dowerx/quack/service/PostService.java b/app/src/main/java/com/dowerx/quack/service/PostService.java index 0402360..6e03497 100644 --- a/app/src/main/java/com/dowerx/quack/service/PostService.java +++ b/app/src/main/java/com/dowerx/quack/service/PostService.java @@ -18,6 +18,7 @@ import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Tasks; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.firestore.CollectionReference; +import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.Filter; import com.google.firebase.firestore.FirebaseFirestore; @@ -52,12 +53,24 @@ public class PostService { String username = user.get("username", String.class); String email = user.get("email", 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( new User(username, email, id), post.get("content", String.class), post.get("image", String.class), - 0, - false, + likes.size(), + liked, post.getId() )); } @@ -96,12 +109,24 @@ public class PostService { String username = user.get("username", String.class); String email = user.get("email", 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( new User(username, email, id), post.get("content", String.class), post.get("image", String.class), - 0, - false, + likes.size(), + liked, post.getId() )); } @@ -200,4 +225,85 @@ public class PostService { } } } + + // post_id, user_id + public static class ToggleLike extends AsyncTask { + + private static final Integer LIKED = 0; + private static final Integer DISLIKED = 1; + private static final Integer ERROR = -1; + + private final Callable callback_liked; + private final Callable callback_disliked; + + private final Context context; + + public ToggleLike(Callable callback_liked, Callable 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 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(); + } + + } + } }