Commit c20d33d3 authored by Willard's avatar Willard

Add deleting reviews

parent b63e073a
...@@ -17,6 +17,7 @@ import okhttp3.ResponseBody; ...@@ -17,6 +17,7 @@ import okhttp3.ResponseBody;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.http.Body; import retrofit2.http.Body;
import retrofit2.http.DELETE;
import retrofit2.http.Field; import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded; import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET; import retrofit2.http.GET;
...@@ -61,6 +62,9 @@ public interface CanteeneoApiInterface { ...@@ -61,6 +62,9 @@ public interface CanteeneoApiInterface {
@PUT("api/dishes/{id}/reviews/{review_id}") @PUT("api/dishes/{id}/reviews/{review_id}")
Call<ResponseBody> editDishReview(@Path("id") int id, @Path("review_id") int review_id, @Body DishReview review); Call<ResponseBody> editDishReview(@Path("id") int id, @Path("review_id") int review_id, @Body DishReview review);
@DELETE("api/dishes/{id}/reviews/{review_id}")
Call<ResponseBody> deleteDishReview(@Path("id") int id, @Path("review_id") int review_id);
@GET("api/dishes/{id}/reviews") @GET("api/dishes/{id}/reviews")
Call<List<DishReview>> getDishReviews(@Path("id") int id); Call<List<DishReview>> getDishReviews(@Path("id") int id);
......
package com.testapp; package com.testapp;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.BaseAdapter; import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RatingBar; import android.widget.RatingBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.testapp.entities.DishReview; import com.testapp.entities.DishReview;
import com.testapp.entities.StallReview;
import java.util.ArrayList; import java.util.ArrayList;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class DishReviewAdapter extends BaseAdapter { public class DishReviewAdapter extends BaseAdapter {
private Activity context; private Activity context;
ArrayList<DishReview> dishReviews; ArrayList<DishReview> dishReviews;
...@@ -45,13 +53,66 @@ public class DishReviewAdapter extends BaseAdapter { ...@@ -45,13 +53,66 @@ public class DishReviewAdapter extends BaseAdapter {
TextView body = (TextView) v.findViewById(R.id.body); TextView body = (TextView) v.findViewById(R.id.body);
TextView username = (TextView) v.findViewById(R.id.user); TextView username = (TextView) v.findViewById(R.id.user);
DishReview dr = dishReviews.get(position); final DishReview dr = dishReviews.get(position);
title.setText(dr.getTitle()); title.setText(dr.getTitle());
rating.setRating(dr.getRating()); rating.setRating(dr.getRating());
body.setText(dr.getBody()); body.setText(dr.getBody());
username.setText(dr.getUser());
Button editButton = (Button)v.findViewById(R.id.edit_button);
Button deleteButton = (Button)v.findViewById(R.id.delete_button);
if(dr.getUserId() == AppUtils.userId) {
editButton.setVisibility(View.VISIBLE);
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity context = DishReviewAdapter.this.context;
Intent i = new Intent(context, DishReviewActivity.class);
i.putExtra("ID", dr.getUserId());
i.putExtra("REVIEW_ID", dr.getId());
i.putExtra("EDITING", true);
i.putExtra("EDIT_TITLE", dr.getTitle());
i.putExtra("EDIT_BODY", dr.getBody());
i.putExtra("EDIT_RATING", dr.getRating());
context.startActivityForResult(i, 0);
}
});
deleteButton.setVisibility(View.VISIBLE);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final DishViewActivity context = (DishViewActivity)DishReviewAdapter.this.context;
new AlertDialog.Builder(context)
.setTitle("Confirm Delete")
.setMessage("Are you sure you want to delete your review? This cannot be undone.")
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Call<ResponseBody> call = AppUtils.service.deleteDishReview(dr.getDishId(), dr.getId());
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Toast.makeText(context, "Review deleted", Toast.LENGTH_SHORT).show();
context.getDishReviews(dr.getDishId());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
}
})
.setNegativeButton(R.string.no, null)
.show();
}
});
} else {
editButton.setVisibility(View.INVISIBLE);
deleteButton.setVisibility(View.INVISIBLE);
}
Toast.makeText(v.getContext(), dr.getBody(), Toast.LENGTH_SHORT).show(); Toast.makeText(v.getContext(), dr.getBody(), Toast.LENGTH_SHORT).show();
// TODO set username
return v; return v;
} }
} }
...@@ -33,8 +33,6 @@ public class DishViewActivity extends AppCompatActivity { ...@@ -33,8 +33,6 @@ public class DishViewActivity extends AppCompatActivity {
private int id; private int id;
private DishReview ownedReview;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
...@@ -135,42 +133,26 @@ public class DishViewActivity extends AppCompatActivity { ...@@ -135,42 +133,26 @@ public class DishViewActivity extends AppCompatActivity {
} }
} }
private void getDishReviews(int id) { public void getDishReviews(int id) {
Call<List<DishReview>> call = AppUtils.service.getDishReviews(id); Call<List<DishReview>> call = AppUtils.service.getDishReviews(id);
call.enqueue(new Callback<List<DishReview>>() { call.enqueue(new Callback<List<DishReview>>() {
@Override @Override
public void onResponse(Call<List<DishReview>> call, Response<List<DishReview>> response) { public void onResponse(Call<List<DishReview>> call, Response<List<DishReview>> response) {
List<DishReview> newReviews = response.body(); List<DishReview> newReviews = response.body();
reviews.clear(); DishReview ownedReview = null;
reviews.addAll(newReviews); for(DishReview review: newReviews) {
ownedReview = null;
for(DishReview review: reviews) {
if(review.getUserId() == AppUtils.userId) { if(review.getUserId() == AppUtils.userId) {
ownedReview = review; ownedReview = review;
break;
} }
} }
reviews.clear();
Button editReviewBtn = (Button) findViewById(R.id.edit_review_button);
if(ownedReview != null) { if(ownedReview != null) {
editReviewBtn.setVisibility(View.VISIBLE); newReviews.remove(ownedReview);
editReviewBtn.setOnClickListener(new View.OnClickListener() { reviews.add(ownedReview);
@Override
public void onClick(View v) {
Intent i = new Intent(DishViewActivity.this, DishReviewActivity.class);
i.putExtra("ID", DishViewActivity.this.id);
i.putExtra("REVIEW_ID", ownedReview.getId());
i.putExtra("EDITING", true);
i.putExtra("EDIT_TITLE", ownedReview.getTitle());
i.putExtra("EDIT_BODY", ownedReview.getBody());
i.putExtra("EDIT_RATING", ownedReview.getRating());
startActivityForResult(i, 0);
}
});
} else {
editReviewBtn.setVisibility(View.INVISIBLE);
} }
reviews.addAll(newReviews);
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
} }
......
...@@ -147,13 +147,6 @@ ...@@ -147,13 +147,6 @@
android:textSize="18sp" android:textSize="18sp"
android:layout_weight="1"/> android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_review_button"
android:layout_marginRight="16dp"
android:text="Edit" />
<Button <Button
android:layout_width="47dp" android:layout_width="47dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
......
...@@ -42,4 +42,34 @@ ...@@ -42,4 +42,34 @@
android:layout_below="@+id/user" android:layout_below="@+id/user"
android:layout_alignParentLeft="true" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/> android:layout_alignParentStart="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/body"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/delete_button"
android:backgroundTint="@color/deleteTint"
android:background="@drawable/ic_clear_black_24dp"
android:textAllCaps="false"
android:elevation="0dp"
style="@style/Widget.AppCompat.Button.Borderless"
android:minHeight="@dimen/button_size"
android:minWidth="@dimen/button_size"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_button"
android:background="@drawable/ic_mode_edit_black_24dp"
android:textAllCaps="false"
android:elevation="0dp"
style="@style/Widget.AppCompat.Button.Borderless"
android:minHeight="@dimen/button_size"
android:minWidth="@dimen/button_size"
android:layout_alignBaseline="@+id/delete_button"
android:layout_alignBottom="@+id/delete_button"
android:layout_toLeftOf="@+id/delete_button"
android:layout_toStartOf="@+id/delete_button"/>
</RelativeLayout> </RelativeLayout>
\ No newline at end of file
...@@ -3,4 +3,5 @@ ...@@ -3,4 +3,5 @@
<color name="colorPrimary">#3f51b5</color> <color name="colorPrimary">#3f51b5</color>
<color name="colorPrimaryDark">#303F9F</color> <color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color> <color name="colorAccent">#FF4081</color>
<color name="deleteTint">#FF0000</color>
</resources> </resources>
...@@ -6,4 +6,5 @@ ...@@ -6,4 +6,5 @@
<dimen name="nav_header_vertical_spacing">16dp</dimen> <dimen name="nav_header_vertical_spacing">16dp</dimen>
<dimen name="nav_header_height">160dp</dimen> <dimen name="nav_header_height">160dp</dimen>
<dimen name="fab_margin">16dp</dimen> <dimen name="fab_margin">16dp</dimen>
<dimen name="button_size">24dp</dimen>
</resources> </resources>
...@@ -42,4 +42,6 @@ ...@@ -42,4 +42,6 @@
<string name="title_activity_blahblah">blahblah</string> <string name="title_activity_blahblah">blahblah</string>
<string name="title_activity_stall_view">StallViewActivity</string> <string name="title_activity_stall_view">StallViewActivity</string>
<string name="title_activity_add_dish_review">AddDishReviewActivity</string> <string name="title_activity_add_dish_review">AddDishReviewActivity</string>
<string name="yes">Yes</string>
<string name="no">No</string>
</resources> </resources>
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