Commit fdf63cc3 authored by Willard's avatar Willard

Add editing dish review functionality

parent ad5aa333
......@@ -38,7 +38,7 @@
android:label="@string/title_activity_stall_view"
android:theme="@style/AppTheme"/>
<activity
android:name=".AddDishReviewActivity"
android:name=".DishReviewActivity"
android:label="@string/title_activity_add_dish_review"
android:theme="@style/AppTheme"/>
<activity android:name=".RegisterActivity">
......
......@@ -21,6 +21,7 @@ import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Path;
import retrofit2.http.Query;
......@@ -57,6 +58,9 @@ public interface CanteeneoApiInterface {
@POST("api/dishes/{id}/reviews")
Call<ResponseBody> newDishReview(@Path("id") int id, @Body DishReview review);
@PUT("api/dishes/{id}/reviews/{review_id}")
Call<ResponseBody> editDishReview(@Path("id") int id, @Path("review_id") int review_id, @Body DishReview review);
@GET("api/dishes/{id}/reviews")
Call<List<DishReview>> getDishReviews(@Path("id") int id);
......
......@@ -2,7 +2,6 @@ package com.testapp;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
......@@ -17,13 +16,16 @@ import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class AddDishReviewActivity extends AppCompatActivity {
public class DishReviewActivity extends AppCompatActivity {
private EditText title;
private EditText body;
private RatingBar rating;
private int id;
private int reviewId;
private boolean editing;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -31,14 +33,23 @@ public class AddDishReviewActivity extends AppCompatActivity {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("Add Dish Review");
editing = getIntent().getBooleanExtra("EDITING", false);
title = (EditText) findViewById(R.id.rv_title);
body = (EditText) findViewById(R.id.rv_body);
rating = (RatingBar) findViewById(R.id.rv_rating);
if(editing) {
setTitle("Edit Dish Review");
reviewId = getIntent().getIntExtra("REVIEW_ID", 0);
title.setText(getIntent().getStringExtra("EDIT_TITLE"));
body.setText(getIntent().getStringExtra("EDIT_BODY"));
rating.setRating(getIntent().getFloatExtra("EDIT_RATING", 0f));
} else {
setTitle("Add Dish Review");
}
id = getIntent().getIntExtra("ID", 0);
Toast.makeText(getApplicationContext(), id + "", Toast.LENGTH_SHORT).show();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
......@@ -46,23 +57,26 @@ public class AddDishReviewActivity extends AppCompatActivity {
DishReview rv = new DishReview();
rv.setTitle(title.getText().toString());
rv.setBody(body.getText().toString());
rv.setRating((int) rating.getRating());
rv.setRating(rating.getRating());
rv.setDishId(id);
rv.setUserId(AppUtils.userId);
sendReview(id, rv);
if(editing) {
editReview(reviewId, rv);
} else {
newReview(rv);
}
}
});
}
private void sendReview(int id, DishReview rv) {
Call<ResponseBody> call = AppUtils.service.newDishReview(id, rv);
public void newReview(DishReview review) {
Call<ResponseBody> call = AppUtils.service.newDishReview(id, review);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
setResult(RESULT_OK);
finish();
}
@Override
......@@ -72,4 +86,19 @@ public class AddDishReviewActivity extends AppCompatActivity {
});
}
public void editReview(int reviewId, DishReview review) {
Call<ResponseBody> call = AppUtils.service.editDishReview(id, reviewId, review);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
setResult(RESULT_OK);
finish();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getApplicationContext(), "Editing review failed", Toast.LENGTH_SHORT);
}
});
}
}
......@@ -33,6 +33,8 @@ public class DishViewActivity extends AppCompatActivity {
private int id;
private DishReview ownedReview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -104,7 +106,7 @@ public class DishViewActivity extends AppCompatActivity {
addReviewBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(DishViewActivity.this, AddDishReviewActivity.class);
Intent i = new Intent(DishViewActivity.this, DishReviewActivity.class);
i.putExtra("ID", id);
startActivityForResult(i, 0);
}
......@@ -126,7 +128,6 @@ public class DishViewActivity extends AppCompatActivity {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
......@@ -142,12 +143,39 @@ public class DishViewActivity extends AppCompatActivity {
List<DishReview> newReviews = response.body();
reviews.clear();
reviews.addAll(newReviews);
ownedReview = null;
for(DishReview review: reviews) {
if(review.getUserId() == AppUtils.userId) {
ownedReview = review;
}
}
Button editReviewBtn = (Button) findViewById(R.id.edit_review_button);
if(ownedReview != null) {
editReviewBtn.setVisibility(View.VISIBLE);
editReviewBtn.setOnClickListener(new View.OnClickListener() {
@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);
}
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<List<DishReview>> call, Throwable t) {
}
});
}
......@@ -156,7 +184,7 @@ public class DishViewActivity extends AppCompatActivity {
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, "Review Added", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Review Posted!", Toast.LENGTH_SHORT).show();
getDishReviews(id);
}
}
......
......@@ -5,14 +5,14 @@ public class DishReview {
int id;
String title;
String body;
int rating;
float rating;
int user_id;
int dish_id;
public DishReview() {
}
public DishReview(int id, String title, String body, int rating, int user_id, int dish_id) {
public DishReview(int id, String title, String body, float rating, int user_id, int dish_id) {
this.id = id;
this.title = title;
this.body = body;
......@@ -33,9 +33,9 @@ public class DishReview {
public void setBody(String body) { this.body = body; }
public int getRating() { return rating; }
public float getRating() { return rating; }
public void setRating(int rating) { this.rating = rating; }
public void setRating(float rating) { this.rating = rating; }
public int getUserId() { return user_id; }
......
......@@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.testapp.AddDishReviewActivity">
tools:context="com.testapp.DishReviewActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
......
......@@ -9,7 +9,7 @@
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.testapp.AddDishReviewActivity"
tools:context="com.testapp.DishReviewActivity"
tools:showIn="@layout/activity_add_review">
<EditText
......
......@@ -150,8 +150,14 @@
<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
android:layout_width="47dp"
android:layout_height="wrap_content"
android:id="@+id/add_review_button"
android:layout_weight="1"
android:text="+" />
</LinearLayout>
......
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