diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/likes/LikesHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/likes/LikesHelper.java new file mode 100644 index 0000000..0683cf4 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/likes/LikesHelper.java @@ -0,0 +1,71 @@ +package org.communiquons.android.comunic.client.data.likes; + +import android.content.Context; + +import org.communiquons.android.comunic.client.api.APIRequest; +import org.communiquons.android.comunic.client.api.APIRequestParameters; +import org.communiquons.android.comunic.client.api.APIResponse; + +/** + * Likes Helper + * + * @author Pierre HUBERT + * Created by pierre on 4/7/18. + */ + +public class LikesHelper { + + /** + * The context of the application + */ + private Context mContext; + + /** + * Public constructor of the likes helper class + * + * @param context The context of the application + */ + public LikesHelper(Context context){ + + //Save the context + mContext = context.getApplicationContext(); + + } + + /** + * Update the like other a content + * + * @param type The type of the element + * @param id The ID of the target element + * @param liking New liking status + * @return TRUE for a success / FALSE for a failure + */ + public boolean update(LikesType type, int id, boolean liking){ + + //Perform an API request + APIRequestParameters params = new APIRequestParameters(mContext, "likes/update"); + params.addInt("id", id); + params.addBoolean("like", liking); + + //Put the kind of element + switch (type){ + case POST: + params.addString("type", "post"); + break; + + default: + throw new RuntimeException("Unrecognized kind of post !"); + } + + //Intend to perform the request + try { + APIResponse response = new APIRequest().exec(params); + + return response.getResponse_code() == 200; + + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/likes/LikesType.java b/app/src/main/java/org/communiquons/android/comunic/client/data/likes/LikesType.java new file mode 100644 index 0000000..2f54dd1 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/likes/LikesType.java @@ -0,0 +1,17 @@ +package org.communiquons.android.comunic.client.data.likes; + +/** + * Likes types + * + * @author Pierre HUBERT + * Created by pierre on 4/7/18. + */ + +public enum LikesType { + + /** + * For the posts + */ + POST + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java index f851fae..9d3358b 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java @@ -139,7 +139,7 @@ public class Post { //Set and get the number of likes other the like - void setNumberLike(int numberLike) { + public void setNumberLike(int numberLike) { this.numberLike = numberLike; } @@ -149,7 +149,7 @@ public class Post { //Set and get the liking state over the post - void setLiking(boolean liking) { + public void setLiking(boolean liking) { isLiking = liking; } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java index e945a36..f1ff4a4 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java @@ -166,6 +166,13 @@ public class PostsAdapter extends ArrayAdapter{ LikeButtonView likeButtonView = convertView.findViewById(R.id.like_button); likeButtonView.setNumberLikes(post.getNumberLike()); likeButtonView.setIsLiking(post.isLiking()); + likeButtonView.setUpdateListener(new LikeButtonView.OnLikeUpdateListener() { + @Override + public void OnLikeUpdate(boolean isLiking) { + //Call listener + mListener.onPostLikeUpdate(post, isLiking); + } + }); //Process post comments ArrayList comments = post.getComments_list(); @@ -282,6 +289,14 @@ public class PostsAdapter extends ArrayAdapter{ */ void showPostActions(View button, int pos, Post post); + /** + * Handles the update of the likes of a post + * + * @param post The target post + * @param is_liking New liking status + */ + void onPostLikeUpdate(Post post, boolean is_liking); + /** * Handles the deletion process of a post * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsListFragment.java index 11a82b2..9143472 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsListFragment.java @@ -24,6 +24,8 @@ import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper; import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; import org.communiquons.android.comunic.client.data.comments.Comment; import org.communiquons.android.comunic.client.data.comments.CommentsHelper; +import org.communiquons.android.comunic.client.data.likes.LikesHelper; +import org.communiquons.android.comunic.client.data.likes.LikesType; import org.communiquons.android.comunic.client.data.posts.Post; import org.communiquons.android.comunic.client.data.posts.PostsHelper; import org.communiquons.android.comunic.client.data.posts.PostsList; @@ -111,6 +113,11 @@ public class PostsListFragment extends Fragment */ GetUsersHelper mUserHelper; + /** + * Likes helper + */ + LikesHelper mLikesHelper; + /** * Set the list of posts of the fragment * @@ -129,6 +136,23 @@ public class PostsListFragment extends Fragment this.mUsersInfo = list; } + @Override + public void onStart() { + super.onStart(); + + //Create post helper + mPostsHelper = new PostsHelper(getActivity()); + + //Create comment helper + mCommentsHelper = new CommentsHelper(getActivity()); + + //Create user helper + mUserHelper = new GetUsersHelper(getActivity(), DatabaseHelper.getInstance(getActivity())); + + //Create likes helper + mLikesHelper = new LikesHelper(getActivity()); + } + @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { @@ -151,15 +175,6 @@ public class PostsListFragment extends Fragment */ public void show(){ - //Create post helper - mPostsHelper = new PostsHelper(getActivity()); - - //Create comment helper - mCommentsHelper = new CommentsHelper(getActivity()); - - //Create user helper - mUserHelper = new GetUsersHelper(getActivity(), DatabaseHelper.getInstance(getActivity())); - //Check if the list of posts is not null if(mPostsList == null && mUsersInfo == null) return; @@ -292,6 +307,24 @@ public class PostsListFragment extends Fragment button.showContextMenu(); } + @Override + public void onPostLikeUpdate(final Post post, final boolean is_liking) { + + //Save new post information + post.setNumberLike(post.getNumberLike() + (is_liking ? 1 : -1)); + post.setLiking(is_liking); + + //Perform the update in the background + new AsyncTask(){ + + @Override + protected Boolean doInBackground(Void... params) { + return mLikesHelper.update(LikesType.POST, post.getId(), is_liking); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + + } + @Override public void deletePost(final int pos) { diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/LikeButtonView.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/LikeButtonView.java index 7090263..2651488 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/LikeButtonView.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/LikeButtonView.java @@ -4,7 +4,6 @@ import android.content.Context; import android.support.annotation.AttrRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.annotation.StyleRes; import android.util.AttributeSet; import android.view.View; import android.widget.FrameLayout; @@ -13,7 +12,6 @@ import android.widget.LinearLayout; import android.widget.TextView; import org.communiquons.android.comunic.client.R; -import org.communiquons.android.comunic.client.data.utils.StringsUtils; import org.communiquons.android.comunic.client.data.utils.UiUtils; /** @@ -50,6 +48,11 @@ public class LikeButtonView extends FrameLayout implements View.OnClickListener */ private int numberLikes = 0; + /** + * Like Update listener + */ + private OnLikeUpdateListener mUpdateListener = null; + public LikeButtonView(@NonNull Context context) { super(context); initView(); @@ -127,6 +130,15 @@ public class LikeButtonView extends FrameLayout implements View.OnClickListener return mIsLiking; } + /** + * Set like update listener + * + * @param updateListener The listener for the like update + */ + public void setUpdateListener(OnLikeUpdateListener updateListener) { + this.mUpdateListener = updateListener; + } + /** * Refresh the like view */ @@ -163,5 +175,22 @@ public class LikeButtonView extends FrameLayout implements View.OnClickListener //Refresh display refresh(); + //Call listener (if any) + if(mUpdateListener != null) + mUpdateListener.OnLikeUpdate(mIsLiking); + } + + /** + * Likes update listener interface + */ + public interface OnLikeUpdateListener { + + /** + * This method is called when an update is done on a like + * + * @param isLiking New liking status + */ + void OnLikeUpdate(boolean isLiking); + } }