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