Commit 4fbdc581 authored by Willard's avatar Willard

Add ability to favorite stalls

parent 4f4cf8d4
...@@ -7,6 +7,7 @@ import com.testapp.entities.DishType; ...@@ -7,6 +7,7 @@ import com.testapp.entities.DishType;
import com.testapp.entities.Location; import com.testapp.entities.Location;
import com.testapp.entities.NewUserResult; import com.testapp.entities.NewUserResult;
import com.testapp.entities.Stall; import com.testapp.entities.Stall;
import com.testapp.entities.StallFavoriteInfo;
import com.testapp.entities.StallReview; import com.testapp.entities.StallReview;
import com.testapp.entities.Token; import com.testapp.entities.Token;
...@@ -51,6 +52,13 @@ public interface CanteeneoApiInterface { ...@@ -51,6 +52,13 @@ public interface CanteeneoApiInterface {
@GET("api/stalls") @GET("api/stalls")
Call<Stall> getStallByName(@Query("name") String name); Call<Stall> getStallByName(@Query("name") String name);
@FormUrlEncoded
@POST("api/stalls/{id}/favorites")
Call<StallFavoriteInfo> setStallFavorite(@Path("id") int id, @Field("favorited") boolean favorited);
@GET("api/stalls/{id}/favorites")
Call<StallFavoriteInfo> getStallFavorite(@Path("id") int id);
@GET("api/stalls/{id}/dishes") @GET("api/stalls/{id}/dishes")
Call<List<Dish>> getDishesByStall(@Path("id") int id); Call<List<Dish>> getDishesByStall(@Path("id") int id);
......
...@@ -8,6 +8,7 @@ import android.util.Log; ...@@ -8,6 +8,7 @@ import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
...@@ -16,6 +17,7 @@ import android.widget.Toast; ...@@ -16,6 +17,7 @@ import android.widget.Toast;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import com.testapp.entities.Dish; import com.testapp.entities.Dish;
import com.testapp.entities.Stall; import com.testapp.entities.Stall;
import com.testapp.entities.StallFavoriteInfo;
import com.testapp.entities.StallReview; import com.testapp.entities.StallReview;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -32,6 +34,7 @@ public class StallViewActivity extends AppCompatActivity { ...@@ -32,6 +34,7 @@ public class StallViewActivity extends AppCompatActivity {
private ArrayList<StallReview> reviews = new ArrayList<>(); private ArrayList<StallReview> reviews = new ArrayList<>();
private StallReviewAdapter srAdapter; private StallReviewAdapter srAdapter;
private DishAdapter dAdapter; private DishAdapter dAdapter;
private CheckBox favoriteCheckbox;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
...@@ -69,11 +72,43 @@ public class StallViewActivity extends AppCompatActivity { ...@@ -69,11 +72,43 @@ public class StallViewActivity extends AppCompatActivity {
Stall s = response.body(); Stall s = response.body();
id = s.getId(); id = s.getId();
TextView location = (TextView) findViewById(R.id.stall_location); TextView location = (TextView) findViewById(R.id.stall_location);
location.setText(s.getLocation() + " " + id); location.setText(s.getLocation());
TextView description = (TextView) findViewById(R.id.stall_description); TextView description = (TextView) findViewById(R.id.stall_description);
description.setText(s.getDescription()); description.setText(s.getDescription());
ImageView image = (ImageView) findViewById(R.id.stall_logo); ImageView image = (ImageView) findViewById(R.id.stall_logo);
Picasso.with(getApplicationContext()).load("http://" + getResources().getString(R.string.server_ip) + ":5000/static/uploads/"+ s.getImagePath()).fit().centerCrop().into(image); Picasso.with(getApplicationContext()).load("http://" + getResources().getString(R.string.server_ip) + ":5000/static/uploads/"+ s.getImagePath()).fit().centerCrop().into(image);
favoriteCheckbox = (CheckBox)findViewById(R.id.favorite_checkbox);
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>() {
@Override
public void onResponse(Call<StallFavoriteInfo> call, Response<StallFavoriteInfo> response) {
favoriteCheckbox.setText("Favorite (" + response.body().getCount() + ")");
}
@Override
public void onFailure(Call<StallFavoriteInfo> call, Throwable t) {
}
});
}
});
Call<StallFavoriteInfo> countCall = AppUtils.service.getStallFavorite(id);
countCall.enqueue(new Callback<StallFavoriteInfo>() {
@Override
public void onResponse(Call<StallFavoriteInfo> call, Response<StallFavoriteInfo> response) {
favoriteCheckbox.setChecked(response.body().isFavorited());
favoriteCheckbox.setText("Favorite (" + response.body().getCount() + ")");
}
@Override
public void onFailure(Call<StallFavoriteInfo> call, Throwable t) {
favoriteCheckbox.setText("Favorite (-1)");
}
});
ListView lvDishes = (ListView) findViewById(R.id.stall_dishes); ListView lvDishes = (ListView) findViewById(R.id.stall_dishes);
dAdapter = new DishAdapter(StallViewActivity.this, dishes); dAdapter = new DishAdapter(StallViewActivity.this, dishes);
...@@ -116,7 +151,6 @@ public class StallViewActivity extends AppCompatActivity { ...@@ -116,7 +151,6 @@ public class StallViewActivity extends AppCompatActivity {
dishes.clear(); dishes.clear();
dishes.addAll(newDishes); dishes.addAll(newDishes);
dAdapter.notifyDataSetChanged(); dAdapter.notifyDataSetChanged();
Toast.makeText(StallViewActivity.this, newDishes.size() + "", Toast.LENGTH_LONG).show();
} }
@Override @Override
......
package com.testapp.entities;
public class StallFavoriteInfo {
int count;
boolean favorited;
public StallFavoriteInfo(int count, boolean favorited) {
this.count = count;
this.favorited = favorited;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public boolean isFavorited() {
return favorited;
}
public void setFavorited(boolean favorited) {
this.favorited = favorited;
}
}
...@@ -36,6 +36,13 @@ ...@@ -36,6 +36,13 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<TextView
android:text="Location: "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
/>
<TextView <TextView
android:text="Location" android:text="Location"
android:layout_width="wrap_content" android:layout_width="wrap_content"
...@@ -43,12 +50,14 @@ ...@@ -43,12 +50,14 @@
android:id="@+id/stall_location" android:id="@+id/stall_location"
android:layout_weight="1" /> android:layout_weight="1" />
<Button <CheckBox
android:text="Favorite" android:text="Favorite"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/stall_favorite" android:layout_gravity="right"
android:layout_weight="1" /> android:id="@+id/favorite_checkbox"
android:layout_weight="1"
android:elevation="0dp"/>
</LinearLayout> </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