From 066805df27efbe93ed71345e42b26242442543e7 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 18 Mar 2018 14:24:16 +0100 Subject: [PATCH] Work progress on comments update. --- .../comunic/client/data/comments/Comment.java | 12 +++++ .../client/ui/adapters/CommentsAdapter.java | 48 ++++++++++++++++--- .../client/ui/adapters/PostsAdapter.java | 4 ++ app/src/main/res/layout/comment_item.xml | 9 ++++ .../main/res/menu/menu_comments_actions.xml | 10 ++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/values/styles.xml | 10 ++++ 7 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 app/src/main/res/menu/menu_comments_actions.xml diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/comments/Comment.java b/app/src/main/java/org/communiquons/android/comunic/client/data/comments/Comment.java index 32a338e..969aced 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/comments/Comment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/comments/Comment.java @@ -22,6 +22,9 @@ public class Comment { private int likes; private boolean user_like; + //This field is used to indicate the corresponding comment has been deleted + private boolean deleted = false; + //Get and set comment ID public int getId() { @@ -111,4 +114,13 @@ public class Comment { public void setUser_like(boolean user_like) { this.user_like = user_like; } + + //Get and set the current deleted state of the comment + public boolean isDeleted() { + return deleted; + } + + public void setDeleted(boolean deleted) { + this.deleted = deleted; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/CommentsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/CommentsAdapter.java index 311b99e..30bd46e 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/CommentsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/CommentsAdapter.java @@ -2,17 +2,22 @@ package org.communiquons.android.comunic.client.ui.adapters; import android.content.Context; import android.support.annotation.Nullable; +import android.view.ContextMenu; import android.view.LayoutInflater; +import android.view.MenuInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; +import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.Account.AccountUtils; import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadManager; 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.utils.UiUtils; import java.util.ArrayList; @@ -26,6 +31,11 @@ import java.util.ArrayList; public class CommentsAdapter extends ArrayAdapter { + /** + * Comment helper + */ + private static CommentsHelper mCHelper = null; + /** * Class constructor * @@ -48,6 +58,11 @@ public class CommentsAdapter extends ArrayAdapter { static View getInflatedView(Context context, Comment comment, @Nullable UserInfo user, ViewGroup viewGroup){ + //Create comment helper if required + if(mCHelper == null){ + mCHelper = new CommentsHelper(context.getApplicationContext()); + } + //Inflate a view View v = LayoutInflater.from(context).inflate(R.layout.comment_item, viewGroup, false); @@ -65,28 +80,49 @@ public class CommentsAdapter extends ArrayAdapter { * @param user Information about the user (NULL for none) * @return Updated view */ - private static View fillView(Context context, View view, Comment comment, - @Nullable UserInfo user){ + private static View fillView(final Context context, View view, Comment comment, + @Nullable UserInfo user) { + + //Check wether the current user is the owner of the comment or not + final boolean isOwner = AccountUtils.getID(context) == comment.getUserID(); //Update user name and account image ImageView accountImage = view.findViewById(R.id.user_account_image); TextView accountName = view.findViewById(R.id.user_account_name); - if(user == null){ + if (user == null) { accountImage.setImageDrawable(UiUtils.getDrawable(context, R.drawable.default_account_image)); accountName.setText(""); - } - else { + } else { ImageLoadManager.load(context, user.getAcountImageURL(), accountImage); accountName.setText(user.getDisplayFullName()); } //Update comment content - ((TextView)view.findViewById(R.id.comment_text)).setText(comment.getContent()); + ((TextView) view.findViewById(R.id.comment_text)).setText(comment.getContent()); + + //Update comment actions + ImageButton actions = view.findViewById(R.id.comment_actions_btn); + + //Create context menu + actions.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { + + //Inflate the menu + new MenuInflater(context).inflate(R.menu.menu_comments_actions, menu); + + //Disable some entries accordingly to ownership status + menu.findItem(R.id.action_delete).setEnabled(isOwner); + + } + }); + //Return view return view; + } } 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 e4075ce..a7ac3d3 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 @@ -180,6 +180,10 @@ public class PostsAdapter extends ArrayAdapter{ for (Comment comment : comments) { + //Check if the comment has been deleted + if(comment.isDeleted()) + continue; //Skip comment + //Try to find information about the user UserInfo commentUser = mUsersInfos.containsKey(comment.getUserID()) ? mUsersInfos.get(comment.getUserID()) : null; diff --git a/app/src/main/res/layout/comment_item.xml b/app/src/main/res/layout/comment_item.xml index 02076a2..9c23b7d 100644 --- a/app/src/main/res/layout/comment_item.xml +++ b/app/src/main/res/layout/comment_item.xml @@ -8,6 +8,7 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_comments_actions.xml b/app/src/main/res/menu/menu_comments_actions.xml new file mode 100644 index 0000000..a186228 --- /dev/null +++ b/app/src/main/res/menu/menu_comments_actions.xml @@ -0,0 +1,10 @@ + + + + + + + \ 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 48fd616..e391663 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -105,4 +105,5 @@ Specified comment content seems to be invalid… An error occurred while trying to create comment! Send + Delete diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 3d4d882..077561a 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -75,6 +75,11 @@ 2dp + + + + + +