Martin Erlic
Martin Erlic

Reputation: 5667

Boolean logic for "likeCount" incrementer

I created this incrementLike method that increments an integer in my database. Unfortunately I can't figure out how to stop the user from continually incrementing that value every time they click the "like" image.

I suppose the likeAlready = true statement is not reachable because it always seems to be false. What is wrong with my logic/implementation?

private void incrementLike(int position) {
        ParseQuery<ParseObject> query = new ParseQuery<>("Comment");
        query.whereEqualTo(ParseConstants.KEY_OBJECT_ID, getItem(position).getObjectId());
        query.findInBackground((comment, e) -> {
            if (e == null) {
                // Iterate over all messages and delete them
                for (ParseObject commentObject : comment)
                {
                    boolean likedAlready = false;
                    if (likedAlready == false) {
                        commentObject.increment("likeCount");
                        commentObject.saveInBackground();
                        Toast.makeText(getContext(), "Post liked", Toast.LENGTH_SHORT).show();
                        likedAlready = true;
                    } else {
                        Toast.makeText(getContext(), "You already liked this post", Toast.LENGTH_SHORT).show();
                    }

                }
            } else {
                Log.e("Error", e.getMessage());
            }
        });
    }

Update:

I created a new Class table called "Like" with two pointer columns, i.e. senderId which the User's objectId and commentObjectId which is the related Comment's objectId.

When the "like" image is pressed, I create a new object in the "Like" table, with the senderId and commentObjectId. The last step is to determine if the senderId has already been sent for that specific Comment object. This is what I have so far:

private void incrementLike(int position) {

        ParseQuery<ParseObject> query = new ParseQuery<>(ParseConstants.CLASS_COMMENT);
        query.whereEqualTo(ParseConstants.KEY_OBJECT_ID, getItem(position).getObjectId());
        query.findInBackground((comment, e) -> {
            if (e == null) {
                // Iterate over all messages
                for (ParseObject commentObject : comment)
                {

                    ParseObject newLike = new ParseObject(ParseConstants.CLASS_LIKE);
                    newLike.put(ParseConstants.KEY_SENDER_ID, ParseUser.getCurrentUser());
                    newLike.put(ParseConstants.KEY_COMMENT_OBJECT_ID, commentObject);
                    newLike.saveInBackground();
                    Toast.makeText(getContext(), "Yeet liked", Toast.LENGTH_SHORT).show();

                    ParseQuery<ParseObject> query2 = new ParseQuery<>(ParseConstants.CLASS_LIKE);
                    query2.whereEqualTo(ParseConstants.KEY_COMMENT_OBJECT_ID, commentObject.getObjectId());
                    query2.findInBackground((comment2, e2) -> {
                        if (e2 == null) {

                            // I now have a list of Like objects with the commentObjectId associated with this Comment
                            // Only increment if the User objectId of the current ParseUser does not exist in this list
                            commentObject.increment("likeCount");
                            commentObject.saveInBackground();
                            /*this.adapter.addAll(mYeets);*/
                            notifyDataSetChanged();


                        } else {
                            Log.e("Error", e2.getMessage());
                        }
                    });

                }
            } else {
                Log.e("Error", e.getMessage());
            }
        });
    }

I'm having trouble thinking through the very last step. Any suggestions?

Upvotes: 1

Views: 60

Answers (3)

Chris Panella
Chris Panella

Reputation: 212

You should be storing who "liked" it (As in who tapped on the icon). You can use a loop to check through something like a Dictionary where you have saved the user's names as key-value pairs. Another reference that could help is this Shared Preferences.

Upvotes: 1

user6556461
user6556461

Reputation: 377

The Problem is every time you run incrementLike() method, it first initialize your boolean to false. so its one of the solution can be you initialize your boolean as global variable. But the problem with this solution will be that every time user run app the value of boolean will be lost. So if you want to store it permanently, you have to store it in your db about who liked, then you can retrieve from db either the person like already or not.

Upvotes: 1

Marcin Orlowski
Marcin Orlowski

Reputation: 75645

Unfortunately I can't figure out how to stop the user from continually incrementing that value every time they click the "like" image.

Simple boolean logic in your current code is not doable really, because aside from incrementing the db stored counter you must also store information who (i.e. user id) incremented it (also in DB). You can check that in advance then and show "like" button only if such user did not yet do so.

Upvotes: 2

Related Questions