Commit 5791a30c authored by preetamozarde3's avatar preetamozarde3 Committed by Varun Patil

fix(venter): Use boolean fields to check user status on complaint (#275)

* Add boolean fields for upvotes, subscription and fix minor issues

* Minor codacy changes

* Add feature for marking upvoted complaints, Remove unused code and Fix a minor bug

* Refactor a color attribute

* Change upvote indicator color to colorPrimary

* Revert change to call API when onStart() is called
parent b2a1f667
...@@ -5,6 +5,7 @@ import android.content.DialogInterface; ...@@ -5,6 +5,7 @@ import android.content.DialogInterface;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView; import android.support.v7.widget.CardView;
...@@ -25,7 +26,6 @@ import app.insti.R; ...@@ -25,7 +26,6 @@ import app.insti.R;
import app.insti.Utils; import app.insti.Utils;
import app.insti.api.EmptyCallback; import app.insti.api.EmptyCallback;
import app.insti.api.RetrofitInterface; import app.insti.api.RetrofitInterface;
import app.insti.api.model.User;
import app.insti.api.model.Venter; import app.insti.api.model.Venter;
import app.insti.fragment.ComplaintFragment; import app.insti.fragment.ComplaintFragment;
import app.insti.utils.DateTimeUtil; import app.insti.utils.DateTimeUtil;
...@@ -97,7 +97,7 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol ...@@ -97,7 +97,7 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
}); });
try { try {
populateViews(pos, notificationson, notificationsoff, textViewVotes, textViewComments); populateViews(pos, notificationson, notificationsoff, textViewVotes, textViewComments, buttonVotes);
buttonComments.setOnClickListener(new View.OnClickListener() { buttonComments.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
...@@ -109,7 +109,7 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol ...@@ -109,7 +109,7 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
buttonVotes.setOnClickListener(new View.OnClickListener() { buttonVotes.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (complaintList.get(position).getVoteCount() == 0) { if (!complaintList.get(position).isComplaintUpvoted()) {
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface(); RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.upVote(Utils.getSessionIDHeader(), complaintList.get(pos).getComplaintID(), 1).enqueue(new Callback<Venter.Complaint>() { retrofitInterface.upVote(Utils.getSessionIDHeader(), complaintList.get(pos).getComplaintID(), 1).enqueue(new Callback<Venter.Complaint>() {
@Override @Override
...@@ -119,7 +119,8 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol ...@@ -119,7 +119,8 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
if (complaint != null) { if (complaint != null) {
textViewVotes.setText(String.valueOf(complaint.getUsersUpVoted().size())); textViewVotes.setText(String.valueOf(complaint.getUsersUpVoted().size()));
} }
complaintList.get(position).setVoteCount(1); complaintList.get(position).setComplaintUpvoted(true);
buttonVotes.setColorFilter(ResourcesCompat.getColor(context.getResources(), R.color.colorPrimary, null));
} }
} }
...@@ -128,7 +129,7 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol ...@@ -128,7 +129,7 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
Log.i(TAG, "failure in up vote: " + t.toString()); Log.i(TAG, "failure in up vote: " + t.toString());
} }
}); });
} else if (complaintList.get(position).getVoteCount() == 1) { } else if (complaintList.get(position).isComplaintUpvoted()) {
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface(); RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.upVote(Utils.getSessionIDHeader(), complaintList.get(pos).getComplaintID(), 0).enqueue(new Callback<Venter.Complaint>() { retrofitInterface.upVote(Utils.getSessionIDHeader(), complaintList.get(pos).getComplaintID(), 0).enqueue(new Callback<Venter.Complaint>() {
@Override @Override
...@@ -138,7 +139,8 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol ...@@ -138,7 +139,8 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
if (complaint != null) { if (complaint != null) {
textViewVotes.setText(String.valueOf(complaint.getUsersUpVoted().size())); textViewVotes.setText(String.valueOf(complaint.getUsersUpVoted().size()));
} }
complaintList.get(position).setVoteCount(0); complaintList.get(position).setComplaintUpvoted(false);
buttonVotes.clearColorFilter();
} }
} }
@Override @Override
...@@ -169,7 +171,7 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol ...@@ -169,7 +171,7 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
} }
} }
private void populateViews(int pos, ImageButton notificationson, ImageButton notificationsoff, TextView textViewVotes, TextView textViewComments) { private void populateViews(int pos, ImageButton notificationson, ImageButton notificationsoff, TextView textViewVotes, TextView textViewComments, ImageButton buttonVotes) {
try { try {
textViewLocation.setText(complaintList.get(pos).getLocationDescription()); textViewLocation.setText(complaintList.get(pos).getLocationDescription());
textViewUserName.setText(complaintList.get(pos).getComplaintCreatedBy().getUserName()); textViewUserName.setText(complaintList.get(pos).getComplaintCreatedBy().getUserName());
...@@ -184,13 +186,17 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol ...@@ -184,13 +186,17 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
textViewStatus.setBackgroundTintList(ColorStateList.valueOf(context.getResources().getColor(R.color.colorGreen))); textViewStatus.setBackgroundTintList(ColorStateList.valueOf(context.getResources().getColor(R.color.colorGreen)));
textViewStatus.setTextColor(context.getResources().getColor(R.color.secondaryTextColor)); textViewStatus.setTextColor(context.getResources().getColor(R.color.secondaryTextColor));
} }
if (complaintList.get(pos).isComplaintUpvoted()){
buttonVotes.setColorFilter(ResourcesCompat.getColor(context.getResources(), R.color.colorPrimary, null));
} else
buttonVotes.clearColorFilter();
String time = DateTimeUtil.getDate(complaintList.get(pos).getComplaintReportDate()); String time = DateTimeUtil.getDate(complaintList.get(pos).getComplaintReportDate());
textViewReportDate.setText(time); textViewReportDate.setText(time);
textViewDescription.setText(complaintList.get(pos).getDescription()); textViewDescription.setText(complaintList.get(pos).getDescription());
if (complaintList.get(pos).getComplaintsubscribed() == 1){ if (complaintList.get(pos).isComplaintSubscribed()){
notificationson.setVisibility(View.VISIBLE); notificationson.setVisibility(View.VISIBLE);
notificationsoff.setVisibility(View.GONE); notificationsoff.setVisibility(View.GONE);
}else if (complaintList.get(pos).getComplaintsubscribed() == 0){ }else if (!complaintList.get(pos).isComplaintSubscribed()){
notificationson.setVisibility(View.GONE); notificationson.setVisibility(View.GONE);
notificationsoff.setVisibility(View.VISIBLE); notificationsoff.setVisibility(View.VISIBLE);
} }
...@@ -222,7 +228,7 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol ...@@ -222,7 +228,7 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
private void subscribeToComplaint(final Venter.Complaint detailedComplaint, final ImageButton notificationsoff, final ImageButton notificationson) { private void subscribeToComplaint(final Venter.Complaint detailedComplaint, final ImageButton notificationsoff, final ImageButton notificationson) {
final RetrofitInterface retrofitInterface = Utils.getRetrofitInterface(); final RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
if (detailedComplaint.getComplaintsubscribed() == 1) { if (detailedComplaint.isComplaintSubscribed()) {
AlertDialog.Builder unsubscribe = new AlertDialog.Builder(context); AlertDialog.Builder unsubscribe = new AlertDialog.Builder(context);
unsubscribe.setMessage("Are you sure you want to unsubscribe to this complaint?"); unsubscribe.setMessage("Are you sure you want to unsubscribe to this complaint?");
unsubscribe.setCancelable(true); unsubscribe.setCancelable(true);
...@@ -237,7 +243,7 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol ...@@ -237,7 +243,7 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
if (response.isSuccessful()) { if (response.isSuccessful()) {
notificationson.setVisibility(View.GONE); notificationson.setVisibility(View.GONE);
notificationsoff.setVisibility(View.VISIBLE); notificationsoff.setVisibility(View.VISIBLE);
detailedComplaint.setComplaintsubscribed(0); detailedComplaint.setComplaintSubscribed(false);
Toast.makeText(context, "You have been unsubscribed from this complaint!", Toast.makeText(context, "You have been unsubscribed from this complaint!",
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
} }
...@@ -261,14 +267,14 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol ...@@ -261,14 +267,14 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
AlertDialog alert11 = unsubscribe.create(); AlertDialog alert11 = unsubscribe.create();
alert11.show(); alert11.show();
} else if (detailedComplaint.getComplaintsubscribed() == 0){ } else if (!detailedComplaint.isComplaintSubscribed()){
retrofitInterface.subscribetoComplaint(Utils.getSessionIDHeader(), detailedComplaint.getComplaintID(), 1).enqueue(new EmptyCallback<Venter.Complaint>() { retrofitInterface.subscribetoComplaint(Utils.getSessionIDHeader(), detailedComplaint.getComplaintID(), 1).enqueue(new EmptyCallback<Venter.Complaint>() {
@Override @Override
public void onResponse(Call<Venter.Complaint> call, Response<Venter.Complaint> response) { public void onResponse(Call<Venter.Complaint> call, Response<Venter.Complaint> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
notificationson.setVisibility(View.VISIBLE); notificationson.setVisibility(View.VISIBLE);
notificationsoff.setVisibility(View.GONE); notificationsoff.setVisibility(View.GONE);
detailedComplaint.setComplaintsubscribed(1); detailedComplaint.setComplaintSubscribed(true);
Toast.makeText(context, "You have been subscribed to this complaint!", Toast.makeText(context, "You have been subscribed to this complaint!",
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
} }
...@@ -294,13 +300,6 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol ...@@ -294,13 +300,6 @@ public class ComplaintsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
@Override @Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) { public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
List<User> userList = complaintList.get(position).getUsersUpVoted();
for (User user : userList) {
if (user.getUserID().equals(userID))
complaintList.get(position).setVoteCount(1);
else
complaintList.get(position).setVoteCount(0);
}
if (viewHolder instanceof ComplaintsViewHolder) { if (viewHolder instanceof ComplaintsViewHolder) {
((ComplaintsViewHolder) viewHolder).bindHolder(position); ((ComplaintsViewHolder) viewHolder).bindHolder(position);
} }
......
...@@ -37,12 +37,14 @@ public class Venter { ...@@ -37,12 +37,14 @@ public class Venter {
private List<TagUri> tags; private List<TagUri> tags;
@SerializedName("users_up_voted") @SerializedName("users_up_voted")
private List<User> usersUpVoted; private List<User> usersUpVoted;
@SerializedName("is_subscribed")
private boolean complaintSubscribed;
@SerializedName("upvoted")
private boolean complaintUpvoted;
@SerializedName("images") @SerializedName("images")
private List<String> images; private List<String> images;
@SerializedName("comments") @SerializedName("comments")
private List<Comment> comment; private List<Comment> comment;
private int voteCount;
private int complaintsubscribed;
@NonNull @NonNull
public String getComplaintID() { public String getComplaintID() {
...@@ -157,20 +159,20 @@ public class Venter { ...@@ -157,20 +159,20 @@ public class Venter {
public void setComment(List<Comment> comment) { public void setComment(List<Comment> comment) {
this.comment = comment; this.comment = comment;
} }
public boolean isComplaintUpvoted() {
public int getVoteCount() { return complaintUpvoted;
return voteCount;
} }
public void setVoteCount(int voteCount) { public void setComplaintUpvoted(boolean complaintUpvoted) {
this.voteCount = voteCount; this.complaintUpvoted = complaintUpvoted;
} }
public int getComplaintsubscribed() {
return complaintsubscribed; public boolean isComplaintSubscribed() {
return complaintSubscribed;
} }
public void setComplaintsubscribed(int complaintsubscribed) { public void setComplaintSubscribed(boolean complaintSubscribed) {
this.complaintsubscribed = complaintsubscribed; this.complaintSubscribed = complaintSubscribed;
} }
} }
......
...@@ -219,10 +219,10 @@ public class ComplaintDetailsFragment extends Fragment { ...@@ -219,10 +219,10 @@ public class ComplaintDetailsFragment extends Fragment {
addCommentsToView(detailedComplaint); addCommentsToView(detailedComplaint);
initViewPagerForImages(detailedComplaint); initViewPagerForImages(detailedComplaint);
addTagsToView(detailedComplaint); addTagsToView(detailedComplaint);
if (detailedComplaint.getComplaintsubscribed() == 1){ if (detailedComplaint.isComplaintSubscribed()){
notificationson.setVisibility(View.VISIBLE); notificationson.setVisibility(View.VISIBLE);
notificationsoff.setVisibility(View.GONE); notificationsoff.setVisibility(View.GONE);
} else if (detailedComplaint.getComplaintsubscribed() == 0){ } else if (!detailedComplaint.isComplaintSubscribed()){
notificationson.setVisibility(View.GONE); notificationson.setVisibility(View.GONE);
notificationsoff.setVisibility(View.VISIBLE); notificationsoff.setVisibility(View.VISIBLE);
} }
...@@ -303,13 +303,13 @@ public class ComplaintDetailsFragment extends Fragment { ...@@ -303,13 +303,13 @@ public class ComplaintDetailsFragment extends Fragment {
private void upVote(final Venter.Complaint detailedComplaint) { private void upVote(final Venter.Complaint detailedComplaint) {
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface(); RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
if (detailedComplaint.getVoteCount() == 0) { if (!detailedComplaint.isComplaintUpvoted()) {
retrofitInterface.upVote(Utils.getSessionIDHeader(), cId, 1).enqueue(new Callback<Venter.Complaint>() { retrofitInterface.upVote(Utils.getSessionIDHeader(), cId, 1).enqueue(new Callback<Venter.Complaint>() {
@Override @Override
public void onResponse(Call<Venter.Complaint> call, Response<Venter.Complaint> response) { public void onResponse(Call<Venter.Complaint> call, Response<Venter.Complaint> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
Venter.Complaint complaint = response.body(); Venter.Complaint complaint = response.body();
detailedComplaint.setVoteCount(1); detailedComplaint.setComplaintUpvoted(true);
addVotesToView(complaint); addVotesToView(complaint);
} }
} }
...@@ -319,13 +319,13 @@ public class ComplaintDetailsFragment extends Fragment { ...@@ -319,13 +319,13 @@ public class ComplaintDetailsFragment extends Fragment {
Log.i(TAG, "failure in up vote: " + t.toString()); Log.i(TAG, "failure in up vote: " + t.toString());
} }
}); });
} else if (detailedComplaint.getVoteCount() ==1){ } else if (detailedComplaint.isComplaintUpvoted()){
retrofitInterface.upVote(Utils.getSessionIDHeader(), cId, 0).enqueue(new Callback<Venter.Complaint>() { retrofitInterface.upVote(Utils.getSessionIDHeader(), cId, 0).enqueue(new Callback<Venter.Complaint>() {
@Override @Override
public void onResponse(Call<Venter.Complaint> call, Response<Venter.Complaint> response) { public void onResponse(Call<Venter.Complaint> call, Response<Venter.Complaint> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
Venter.Complaint complaint = response.body(); Venter.Complaint complaint = response.body();
detailedComplaint.setVoteCount(0); detailedComplaint.setComplaintUpvoted(false);
addVotesToView(complaint); addVotesToView(complaint);
} }
} }
...@@ -340,7 +340,7 @@ public class ComplaintDetailsFragment extends Fragment { ...@@ -340,7 +340,7 @@ public class ComplaintDetailsFragment extends Fragment {
private void subscribeToComplaint(final Venter.Complaint detailedComplaint){ private void subscribeToComplaint(final Venter.Complaint detailedComplaint){
final RetrofitInterface retrofitInterface = Utils.getRetrofitInterface(); final RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
if (detailedComplaint.getComplaintsubscribed() == 1) { if (detailedComplaint.isComplaintSubscribed()) {
AlertDialog.Builder unsubscribe = new AlertDialog.Builder(getActivity()); AlertDialog.Builder unsubscribe = new AlertDialog.Builder(getActivity());
unsubscribe.setMessage("Are you sure you want to unsubscribe to this complaint?"); unsubscribe.setMessage("Are you sure you want to unsubscribe to this complaint?");
unsubscribe.setCancelable(true); unsubscribe.setCancelable(true);
...@@ -353,7 +353,7 @@ public class ComplaintDetailsFragment extends Fragment { ...@@ -353,7 +353,7 @@ public class ComplaintDetailsFragment extends Fragment {
@Override @Override
public void onResponse(Call<Venter.Complaint> call, Response<Venter.Complaint> response) { public void onResponse(Call<Venter.Complaint> call, Response<Venter.Complaint> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
detailedComplaint.setComplaintsubscribed(0); detailedComplaint.setComplaintSubscribed(false);
notificationson.setVisibility(View.GONE); notificationson.setVisibility(View.GONE);
notificationsoff.setVisibility(View.VISIBLE); notificationsoff.setVisibility(View.VISIBLE);
} }
...@@ -377,12 +377,12 @@ public class ComplaintDetailsFragment extends Fragment { ...@@ -377,12 +377,12 @@ public class ComplaintDetailsFragment extends Fragment {
AlertDialog alert11 = unsubscribe.create(); AlertDialog alert11 = unsubscribe.create();
alert11.show(); alert11.show();
} else if (detailedComplaint.getComplaintsubscribed() == 0){ } else if (!detailedComplaint.isComplaintSubscribed()){
retrofitInterface.subscribetoComplaint(Utils.getSessionIDHeader(), detailedComplaint.getComplaintID(), 1).enqueue(new Callback<Venter.Complaint>() { retrofitInterface.subscribetoComplaint(Utils.getSessionIDHeader(), detailedComplaint.getComplaintID(), 1).enqueue(new Callback<Venter.Complaint>() {
@Override @Override
public void onResponse(Call<Venter.Complaint> call, Response<Venter.Complaint> response) { public void onResponse(Call<Venter.Complaint> call, Response<Venter.Complaint> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
detailedComplaint.setComplaintsubscribed(1); detailedComplaint.setComplaintSubscribed(true);
notificationsoff.setVisibility(View.GONE); notificationsoff.setVisibility(View.GONE);
notificationson.setVisibility(View.VISIBLE); notificationson.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(), "You are subscribed to this complaint!", Toast.makeText(getActivity(), "You are subscribed to this complaint!",
......
...@@ -9,7 +9,6 @@ import android.support.design.widget.TabLayout; ...@@ -9,7 +9,6 @@ import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
...@@ -21,10 +20,8 @@ import app.insti.Utils; ...@@ -21,10 +20,8 @@ import app.insti.Utils;
import app.insti.adapter.ComplaintDetailsPagerAdapter; import app.insti.adapter.ComplaintDetailsPagerAdapter;
import app.insti.api.EmptyCallback; import app.insti.api.EmptyCallback;
import app.insti.api.RetrofitInterface; import app.insti.api.RetrofitInterface;
import app.insti.api.model.User;
import app.insti.api.model.Venter; import app.insti.api.model.Venter;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
public class ComplaintFragment extends Fragment { public class ComplaintFragment extends Fragment {
...@@ -67,13 +64,6 @@ public class ComplaintFragment extends Fragment { ...@@ -67,13 +64,6 @@ public class ComplaintFragment extends Fragment {
if (getActivity() == null || getView() == null) return; if (getActivity() == null || getView() == null) return;
if (response.body() != null) { if (response.body() != null) {
Venter.Complaint complaint = response.body(); Venter.Complaint complaint = response.body();
for (User currentUser : complaint.getUsersUpVoted()) {
if (currentUser.getUserID().equals(userId)) {
complaint.setVoteCount(1);
} else {
complaint.setVoteCount(0);
}
}
initTabViews(complaint); initTabViews(complaint);
//Make progress circle gone After loading //Make progress circle gone After loading
getActivity().findViewById(R.id.loadingPanel).setVisibility(View.GONE); getActivity().findViewById(R.id.loadingPanel).setVisibility(View.GONE);
......
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