Commit c05d4184 authored by preetamozarde3's avatar preetamozarde3 Committed by Varun Patil

feat(venter): Allow editing comments (#276)

* Add feature for editing comments

* Use a vector asset for the arrow

* Add back arrow vector asset to drawable
parent f29295e9
package app.insti.adapter;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
......@@ -11,6 +12,8 @@ import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
......@@ -22,9 +25,11 @@ import java.util.List;
import app.insti.R;
import app.insti.Utils;
import app.insti.activity.MainActivity;
import app.insti.api.EmptyCallback;
import app.insti.api.RetrofitInterface;
import app.insti.api.model.Venter;
import app.insti.api.request.CommentCreateRequest;
import app.insti.utils.DateTimeUtil;
import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;
......@@ -38,7 +43,7 @@ public class CommentsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
private static final String TAG = CommentsAdapter.class.getSimpleName();
private Context context;
private Activity activity;
private LayoutInflater inflater;
private String userId;
private Fragment fragment;
......@@ -46,10 +51,10 @@ public class CommentsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
private List<Venter.Comment> commentList = new ArrayList<>();
public CommentsAdapter(Context context, String userId, Fragment fragment) {
this.context = context;
public CommentsAdapter(Activity activity, String userId, Fragment fragment) {
this.activity = activity;
this.userId = userId;
inflater = LayoutInflater.from(context);
inflater = LayoutInflater.from(activity);
this.fragment =fragment;
}
......@@ -60,6 +65,9 @@ public class CommentsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
private TextView textViewName;
private TextView textViewCommentTime;
private TextView textViewComment;
private EditText editTextComment;
private ImageButton send_comment;
private ImageButton back_button;
private final RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
CommentsViewHolder(View itemView) {
......@@ -67,8 +75,11 @@ public class CommentsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
cardView = itemView.findViewById(R.id.cardViewComment);
textViewName = itemView.findViewById(R.id.textViewUserComment);
textViewComment = itemView.findViewById(R.id.textViewComment);
editTextComment = itemView.findViewById(R.id.editTextComment);
textViewCommentTime = itemView.findViewById(R.id.textViewTime);
circleImageView = itemView.findViewById(R.id.circleImageViewUserImage);
send_comment = itemView.findViewById(R.id.send_comment);
back_button = itemView.findViewById(R.id.back_button);
}
private void bindHolder(final int position) {
......@@ -88,7 +99,7 @@ public class CommentsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
cardView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(final View v) {
PopupMenu popupMenu = new PopupMenu(context, cardView);
PopupMenu popupMenu = new PopupMenu(activity, cardView);
if (!(comment.getUser().getUserID().equals(userId))) {
popupMenu.inflate(R.menu.comments_options_secondary_menu);
} else {
......@@ -98,13 +109,43 @@ public class CommentsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.edit_comment_option:
final String temp = textViewComment.getText().toString();
preEditComments(cardView, textViewComment, editTextComment,send_comment, back_button);
back_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textViewComment.setText(temp);
postEditComments(cardView, textViewComment, editTextComment,send_comment, back_button, activity);
}
});
send_comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CommentCreateRequest commentCreateRequest = new CommentCreateRequest(editTextComment.getText().toString());
retrofitInterface.updateComment(Utils.getSessionIDHeader(), comment.getId(), commentCreateRequest).enqueue(new EmptyCallback<Venter.Comment>() {
@Override
public void onResponse(Call<Venter.Comment> call, Response<Venter.Comment> response) {
if (response.isSuccessful()){
textViewComment.setText(editTextComment.getText().toString());
postEditComments(cardView, textViewComment, editTextComment, send_comment, back_button, activity);
} else {
Toast.makeText(activity, "Comment not edited", Toast.LENGTH_SHORT).show();
textViewComment.setText(temp);
postEditComments(cardView, textViewComment, editTextComment, send_comment, back_button, activity);
}
}
});
}
});
break;
case R.id.copy_comment_option:
ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipboardManager clipboardManager = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("Text Copied", textViewComment.getText().toString());
if (clipboardManager != null) {
clipboardManager.setPrimaryClip(clipData);
}
Toast.makeText(context, "Comment Copied", Toast.LENGTH_SHORT).show();
Toast.makeText(activity, "Comment Copied", Toast.LENGTH_SHORT).show();
break;
case R.id.delete_comment_option:
retrofitInterface.deleteComment(Utils.getSessionIDHeader(), comment.getId()).enqueue(new EmptyCallback<String>() {
......@@ -117,19 +158,19 @@ public class CommentsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
notifyItemRangeChanged(position, commentList.size() - position);
textViewCommentLabel.setText("Comments (" + commentList.size() + ")");
} else {
Toast.makeText(context, "You can't delete this comment", Toast.LENGTH_SHORT).show();
Toast.makeText(activity, "You can't delete this comment", Toast.LENGTH_SHORT).show();
}
}
});
break;
default:
clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
clipData = ClipData.newPlainText("Text Copied", textViewComment.getText().toString());
if (clipboardManager != null) {
clipboardManager.setPrimaryClip(clipData);
}
Toast.makeText(context, "Comment Copied", Toast.LENGTH_SHORT).show();
Toast.makeText(activity, "Comment Copied", Toast.LENGTH_SHORT).show();
break;
}
return true;
......@@ -174,4 +215,26 @@ public class CommentsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
this.commentList = commentList;
this.textViewCommentLabel = textViewCommentLabel;
}
private void preEditComments(CardView cardView, TextView textViewComment, EditText editTextComment, ImageButton send_comment, ImageButton back_button) {
cardView.setClickable(false);
cardView.setLongClickable(false);
textViewComment.setVisibility(View.GONE);
editTextComment.setVisibility(View.VISIBLE);
editTextComment.setText(textViewComment.getText().toString());
send_comment.setVisibility(View.VISIBLE);
back_button.setVisibility(View.VISIBLE);
editTextComment.requestFocus();
}
private void postEditComments(CardView cardView, TextView textViewComment, EditText editTextComment, ImageButton send_comment, ImageButton back_button, Activity activity) {
editTextComment.clearFocus();
textViewComment.setVisibility(View.VISIBLE);
send_comment.setVisibility(View.GONE);
back_button.setVisibility(View.GONE);
editTextComment.setVisibility(View.GONE);
cardView.setClickable(true);
cardView.setLongClickable(true);
MainActivity.hideKeyboard(activity);
}
}
\ No newline at end of file
......@@ -106,7 +106,7 @@ public class ComplaintDetailsFragment extends Fragment {
initialiseViews(view);
upVotesList = new ArrayList<>();
commentListAdapter = new CommentsAdapter(getContext(), uId, this);
commentListAdapter = new CommentsAdapter(getActivity(), uId, this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
upVotesAdapter = new UpVotesAdapter(this, getContext());
recyclerViewComments.setLayoutManager(linearLayoutManager);
......
<vector android:height="18dp" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="18dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/>
</vector>
......@@ -56,6 +56,15 @@
</LinearLayout>
<ImageButton
android:id="@+id/back_button"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
style="@style/Widget.AppCompat.Button.Borderless"
android:src="@drawable/ic_arrow_back_black_24dp"
android:visibility="gone"/>
</LinearLayout>
<TextView
......@@ -70,6 +79,28 @@
android:textColor="@android:color/black"
android:textSize="16sp" />
<EditText
android:id="@+id/editTextComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:scrollHorizontally="true"
android:text="Comment"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="@android:color/black"
android:textSize="16sp"
android:visibility="gone"/>
<ImageButton
android:id="@+id/send_comment"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:src="@drawable/baseline_send_black_18"
android:visibility="gone"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/edit_comment_option"
android:title="Edit"/>
<item
android:id="@+id/copy_comment_option"
android:title="Copy"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment