Commit 08af01f0 authored by Willard's avatar Willard

Add dish favorites

parent cd1ca47a
......@@ -43,6 +43,8 @@
android:theme="@style/AppTheme"/>
<activity android:name=".RegisterActivity">
</activity>
<activity android:name=".AddStallReviewActivity">
</activity>
</application>
</manifest>
\ No newline at end of file
......@@ -7,7 +7,7 @@ import com.testapp.entities.DishType;
import com.testapp.entities.Location;
import com.testapp.entities.NewUserResult;
import com.testapp.entities.Stall;
import com.testapp.entities.StallFavoriteInfo;
import com.testapp.entities.FavoriteInfo;
import com.testapp.entities.StallReview;
import com.testapp.entities.Token;
......@@ -34,6 +34,10 @@ public interface CanteeneoApiInterface {
@Query("min_price") int min_price,
@Query("max_price") int max_price);
@FormUrlEncoded
@POST("api/users/new")
Call<NewUserResult> newUser(@Field("email") String email, @Field("username") String username, @Field("password") String password);
@GET("api/locations")
Call<List<Location>> getLocations();
......@@ -43,6 +47,16 @@ public interface CanteeneoApiInterface {
@GET("api/cuisines")
Call<List<Cuisine>> getCuisines();
@FormUrlEncoded
@POST("api/dishes/{id}/favorites")
Call<FavoriteInfo> setDishFavorite(@Path("id") int id, @Field("favorited") boolean favorited);
@GET("api/dishes/{id}/favorites")
Call<FavoriteInfo> getDishFavorite(@Path("id") int id);
@POST("api/dishes/{id}/reviews")
Call<ResponseBody> newDishReview(@Path("id") int id, @Body DishReview review);
@GET("api/dishes/{id}/reviews")
Call<List<DishReview>> getDishReviews(@Path("id") int id);
......@@ -54,21 +68,17 @@ public interface CanteeneoApiInterface {
@FormUrlEncoded
@POST("api/stalls/{id}/favorites")
Call<StallFavoriteInfo> setStallFavorite(@Path("id") int id, @Field("favorited") boolean favorited);
Call<FavoriteInfo> setStallFavorite(@Path("id") int id, @Field("favorited") boolean favorited);
@GET("api/stalls/{id}/favorites")
Call<StallFavoriteInfo> getStallFavorite(@Path("id") int id);
Call<FavoriteInfo> getStallFavorite(@Path("id") int id);
@POST("api/stalls/{id}/reviews")
Call<ResponseBody> newStallReview(@Path("id") int id, @Body StallReview review);
@GET("api/stalls/{id}/dishes")
Call<List<Dish>> getDishesByStall(@Path("id") int id);
@POST("api/dishes/{id}/reviews")
Call<ResponseBody> newDishReview(@Path("id") int id, @Body DishReview review);
@FormUrlEncoded
@POST("api/users/new")
Call<NewUserResult> newUser(@Field("email") String email, @Field("username") String username, @Field("password") String password);
@GET("api/token")
Call<Token> getToken();
}
......@@ -7,6 +7,7 @@ import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
......@@ -14,6 +15,7 @@ import android.widget.Toast;
import com.squareup.picasso.Picasso;
import com.testapp.entities.DishReview;
import com.testapp.entities.FavoriteInfo;
import java.util.ArrayList;
import java.util.List;
......@@ -27,6 +29,7 @@ public class DishViewActivity extends AppCompatActivity {
private TextView dishStall;
private ArrayList<DishReview> reviews = new ArrayList<>();
private DishReviewAdapter adapter;
private CheckBox favoriteCheckbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -41,6 +44,7 @@ public class DishViewActivity extends AppCompatActivity {
setTitle("Dish Pls");
Intent i = getIntent();
final int id = i.getIntExtra("ID", 0);
TextView dishName = (TextView) findViewById(R.id.dish_name);
dishName.setText(i.getStringExtra("NAME"));
TextView dishPrice = (TextView) findViewById(R.id.dish_price);
......@@ -61,6 +65,39 @@ public class DishViewActivity extends AppCompatActivity {
});
}
favoriteCheckbox = (CheckBox)findViewById(R.id.favorite_checkbox);
favoriteCheckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Call<FavoriteInfo> favoriteCall = AppUtils.service.setDishFavorite(id, favoriteCheckbox.isChecked());
favoriteCall.enqueue(new Callback<FavoriteInfo>() {
@Override
public void onResponse(Call<FavoriteInfo> call, Response<FavoriteInfo> response) {
favoriteCheckbox.setText("Favorite (" + response.body().getCount() + ")");
}
@Override
public void onFailure(Call<FavoriteInfo> call, Throwable t) {
}
});
}
});
Call<FavoriteInfo> countCall = AppUtils.service.getDishFavorite(id);
countCall.enqueue(new Callback<FavoriteInfo>() {
@Override
public void onResponse(Call<FavoriteInfo> call, Response<FavoriteInfo> response) {
favoriteCheckbox.setChecked(response.body().isFavorited());
favoriteCheckbox.setText("Favorite (" + response.body().getCount() + ")");
}
@Override
public void onFailure(Call<FavoriteInfo> call, Throwable t) {
favoriteCheckbox.setText("Favorite (-1)");
}
});
Button addReviewBtn = (Button) findViewById(R.id.add_review_button);
addReviewBtn.setOnClickListener(new View.OnClickListener() {
@Override
......
......@@ -4,7 +4,6 @@ import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
......@@ -12,12 +11,11 @@ import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import com.testapp.entities.Dish;
import com.testapp.entities.Stall;
import com.testapp.entities.StallFavoriteInfo;
import com.testapp.entities.FavoriteInfo;
import com.testapp.entities.StallReview;
import java.util.ArrayList;
......@@ -81,31 +79,31 @@ public class StallViewActivity extends AppCompatActivity {
favoriteCheckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Call<StallFavoriteInfo> favoriteCall = AppUtils.service.setStallFavorite(id, favoriteCheckbox.isChecked());
favoriteCall.enqueue(new Callback<StallFavoriteInfo>() {
Call<FavoriteInfo> favoriteCall = AppUtils.service.setStallFavorite(id, favoriteCheckbox.isChecked());
favoriteCall.enqueue(new Callback<FavoriteInfo>() {
@Override
public void onResponse(Call<StallFavoriteInfo> call, Response<StallFavoriteInfo> response) {
public void onResponse(Call<FavoriteInfo> call, Response<FavoriteInfo> response) {
favoriteCheckbox.setText("Favorite (" + response.body().getCount() + ")");
}
@Override
public void onFailure(Call<StallFavoriteInfo> call, Throwable t) {
public void onFailure(Call<FavoriteInfo> call, Throwable t) {
}
});
}
});
Call<StallFavoriteInfo> countCall = AppUtils.service.getStallFavorite(id);
countCall.enqueue(new Callback<StallFavoriteInfo>() {
Call<FavoriteInfo> countCall = AppUtils.service.getStallFavorite(id);
countCall.enqueue(new Callback<FavoriteInfo>() {
@Override
public void onResponse(Call<StallFavoriteInfo> call, Response<StallFavoriteInfo> response) {
public void onResponse(Call<FavoriteInfo> call, Response<FavoriteInfo> response) {
favoriteCheckbox.setChecked(response.body().isFavorited());
favoriteCheckbox.setText("Favorite (" + response.body().getCount() + ")");
}
@Override
public void onFailure(Call<StallFavoriteInfo> call, Throwable t) {
public void onFailure(Call<FavoriteInfo> call, Throwable t) {
favoriteCheckbox.setText("Favorite (-1)");
}
});
......
package com.testapp.entities;
public class StallFavoriteInfo {
public class FavoriteInfo {
int count;
boolean favorited;
public StallFavoriteInfo(int count, boolean favorited) {
public FavoriteInfo(int count, boolean favorited) {
this.count = count;
this.favorited = favorited;
}
......
......@@ -72,11 +72,11 @@
android:textSize="18sp"
android:textColor="?android:attr/textColorLink" />
<Button
<CheckBox
android:text="Favorite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dish_favorite"
android:id="@+id/favorite_checkbox"
android:layout_marginBottom="10dp"
android:layout_weight="1" />
......
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