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 29e2f2b..f851fae 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 @@ -25,10 +25,18 @@ public class Post { private int page_id; private String content; private PostTypes type; + + //Related with visibility private PostVisibilityLevels visibilityLevel; - private ArrayList comments_list; private PostUserAccess user_access_level = PostUserAccess.NO_ACCESS; + //Likes + private int numberLike; + private boolean isLiking; + + //Comments + private ArrayList comments_list; + //Files specific private String file_path_url; @@ -129,6 +137,26 @@ public class Post { return user_access_level; } + + //Set and get the number of likes other the like + void setNumberLike(int numberLike) { + this.numberLike = numberLike; + } + + public int getNumberLike() { + return numberLike; + } + + + //Set and get the liking state over the post + void setLiking(boolean liking) { + isLiking = liking; + } + + public boolean isLiking() { + return isLiking; + } + /** * Check whether the user can delete the post or not * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java index 3c90c3e..f878385 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java @@ -2,6 +2,7 @@ package org.communiquons.android.comunic.client.data.posts; import android.content.Context; import android.support.annotation.Nullable; +import android.util.Log; import org.communiquons.android.comunic.client.api.APIRequest; import org.communiquons.android.comunic.client.api.APIRequestParameters; @@ -22,6 +23,11 @@ import org.json.JSONObject; public class PostsHelper { + /** + * Debug tag + */ + private final static String TAG = "PostsHelper"; + /** * The context of the application */ @@ -287,6 +293,10 @@ public class PostsHelper { post.setUser_access_level(PostUserAccess.NO_ACCESS); } + //Get information about likes + post.setNumberLike(json.getInt("likes")); + post.setLiking(json.getBoolean("userlike")); + //Get file path url (if any) if(json.getString("file_path_url") != null){ post.setFile_path_url(json.getString("file_path_url")); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/UiUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/UiUtils.java index 7f12f84..16e93e2 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/UiUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/UiUtils.java @@ -43,6 +43,13 @@ public class UiUtils { return context.getResources().getDrawable(drawable_id, context.getTheme()); } + /** + * Get a string from ressources + */ + public static String getString(Context context, int res_id){ + return context.getResources().getString(res_id); + } + /** * Create and display a loading dialog * 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 658e404..e945a36 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 @@ -23,6 +23,7 @@ import org.communiquons.android.comunic.client.data.posts.PostsList; import org.communiquons.android.comunic.client.data.utils.UiUtils; import org.communiquons.android.comunic.client.data.utils.Utilities; import org.communiquons.android.comunic.client.ui.views.EditCommentContentView; +import org.communiquons.android.comunic.client.ui.views.LikeButtonView; import java.util.ArrayList; @@ -161,6 +162,11 @@ public class PostsAdapter extends ArrayAdapter{ ImageLoadManager.load(getContext(), post.getFile_path_url(), postImage); } + //Set posts likes + LikeButtonView likeButtonView = convertView.findViewById(R.id.like_button); + likeButtonView.setNumberLikes(post.getNumberLike()); + likeButtonView.setIsLiking(post.isLiking()); + //Process post comments ArrayList comments = post.getComments_list(); LinearLayout commentsView = convertView.findViewById(R.id.comments_list); 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 new file mode 100644 index 0000000..2b6d13f --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/LikeButtonView.java @@ -0,0 +1,154 @@ +package org.communiquons.android.comunic.client.ui.views; + +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; +import android.widget.ImageView; +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; + +/** + * Like button view + * + * @author Pierre HUBERT + * Created by pierre on 4/1/18. + */ + +public class LikeButtonView extends FrameLayout implements View.OnClickListener { + + /** + * Like container + */ + private LinearLayout mContainer; + + /** + * Like image + */ + private ImageView mLikeImage; + + /** + * Like text + */ + private TextView mLikeText; + + /** + * Current user liking status + */ + private boolean mIsLiking = false; + + /** + * Number of likes + */ + private int numberLikes = 0; + + public LikeButtonView(@NonNull Context context) { + super(context); + initView(); + } + + public LikeButtonView(@NonNull Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + initView(); + } + + public LikeButtonView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) { + super(context, attrs, defStyleAttr); + initView(); + } + + + /** + * Initialize view + */ + private void initView(){ + + //Inflate the view + View view = inflate(getContext(), R.layout.view_like_button, null); + + //Append the view + addView(view); + + //Get the views + mContainer = view.findViewById(R.id.like_container); + mLikeImage = view.findViewById(R.id.like_img); + mLikeText = view.findViewById(R.id.like_text); + + //Set the click listener + mContainer.setOnClickListener(this); + + //Refresh the view + refresh(); + } + + /** + * Get the number of likes + * + * @return The number of likes + */ + public int getNumberLikes() { + return numberLikes; + } + + /** + * Set the number of likes + * + * @param numberLikes The number of likes + */ + public void setNumberLikes(int numberLikes) { + this.numberLikes = numberLikes; + refresh(); + } + + /** + * Set the user liking state + * + * @param mIsLiking User liking state + */ + public void setIsLiking(boolean mIsLiking) { + this.mIsLiking = mIsLiking; + refresh(); + } + + /** + * Get user liking state + * + * @return TRUE if the user is liking / FALSE else + */ + public boolean ismIsLiking() { + return mIsLiking; + } + + /** + * Refresh the like view + */ + private void refresh(){ + + //Update the image + mLikeImage.setImageDrawable(UiUtils.getDrawable(getContext(), + mIsLiking ? R.drawable.like_down : R.drawable.like_up)); + + //Update the text + String text = UiUtils.getString(getContext(), mIsLiking ? R.string.like_view_liking : + R.string.like_view_like); + + if(numberLikes > 0) + text += "(" + numberLikes + ")"; + + mLikeText.setText(text); + + } + + @Override + public void onClick(View v) { + + } +} diff --git a/app/src/main/res/drawable/like_down.xml b/app/src/main/res/drawable/like_down.xml new file mode 100644 index 0000000..a1f0e3c --- /dev/null +++ b/app/src/main/res/drawable/like_down.xml @@ -0,0 +1,4 @@ + + diff --git a/app/src/main/res/drawable/like_up.png b/app/src/main/res/drawable/like_up.png new file mode 100644 index 0000000..088120c Binary files /dev/null and b/app/src/main/res/drawable/like_up.png differ diff --git a/app/src/main/res/layout/post_item.xml b/app/src/main/res/layout/post_item.xml index 6a5eac8..61e3314 100644 --- a/app/src/main/res/layout/post_item.xml +++ b/app/src/main/res/layout/post_item.xml @@ -67,8 +67,6 @@ android:contentDescription="@string/post_action_btn_description"/> - - @@ -87,6 +85,13 @@ android:layout_height="wrap_content" tools:text="Post content" /> + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6d55db8..f8fe6b3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -127,4 +127,7 @@ Notifications Friends list Notifications + Like + Like + Liking diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index c4635b3..7a9e3bd 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -76,6 +76,13 @@ 2dp + + +