Browse Source

API connection for diets and ingredients addition

master
Apostolos Fanakis 6 years ago
parent
commit
5a82bbd673
  1. 118
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/diets/DietsActivity.java
  2. 29
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/diets/DietsAdapter.java
  3. 134
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/ingredients/IngredientsActivity.java
  4. 44
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/ingredients/IngredientsAdapter.java
  5. 4
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Diet.java
  6. 5
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/session/SessionManager.java
  7. 7
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/ingredients_row.xml

118
UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/diets/DietsActivity.java

@ -1,12 +1,16 @@
package gr.auth.databases.flavours.activities.diets;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import androidx.appcompat.app.ActionBar;
@ -17,8 +21,16 @@ import gr.auth.databases.flavours.R;
import gr.auth.databases.flavours.activities.AddDietActivity;
import gr.auth.databases.flavours.base.BaseActivity;
import gr.auth.databases.flavours.model.Diet;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import static gr.auth.databases.flavours.session.SessionManager.dietsUserViewUrl;
import static gr.auth.databases.flavours.session.SessionManager.followDietUrl;
public class DietsActivity extends BaseActivity {
public class DietsActivity extends BaseActivity implements DietsAdapter.SubscribeDietsAdapterInteractionListener {
private RecyclerView recyclerView;
private FloatingActionButton FAB;
private ArrayList<Diet> diets = new ArrayList<>();
@ -47,16 +59,13 @@ public class DietsActivity extends BaseActivity {
}
});
RecyclerView recyclerView = findViewById(R.id.diets_list);
recyclerView = findViewById(R.id.diets_list);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
diets.add(new Diet(1, "Μόνο κρέας", "Τρώς πολύ κρέας.", false));
diets.add(new Diet(2, "Σχεδόν μόνο κρέας", "Τρώς αρκετό κρέας.", true));
DietsAdapter dietsAdapter = new DietsAdapter(diets);
recyclerView.setAdapter(dietsAdapter);
DietsTask dietsTask = new DietsTask();
dietsTask.execute();
}
@Override
@ -79,4 +88,99 @@ public class DietsActivity extends BaseActivity {
return super.onOptionsItemSelected(item);
}
@Override
public void onSubscribeDietsAdapterInteraction(Diet diet) {
FollowDietStateTask followDietStateTask = new FollowDietStateTask();
followDietStateTask.execute(diet.getId());
}
private class DietsTask extends AsyncTask<Void, Void, Integer> {
private static final String JSON_TAG_DIETS_LIST = "diets";
private static final String JSON_TAG_DIET_ID = "diet_id";
private static final String JSON_TAG_DIET_NAME = "diet_name";
private static final String JSON_TAG_DIET_DESCRIPTION = "diet_description";
//private static final String JSON_TAG_DIET_IS_APPROVED = "food_calories";
private static final String JSON_TAG_USER_FOLLOWS_DIET_LIST = "followed";
private static final String JSON_TAG_USER_FOLLOWS_DIET_DIET = "diet";
@Override
protected void onPreExecute() {
}
@Override
protected Integer doInBackground(Void... params) {
//Builds the request
Request request = new Request.Builder()
.url(dietsUserViewUrl)
.build();
try {
//Makes request & handles response
Response response = client.newCall(request).execute();
ResponseBody responseBody = response.body();
assert responseBody != null;
String result = responseBody.string();
JSONObject jsonResponse = new JSONObject(result);
ArrayList<Integer> userDiets = new ArrayList<>();
JSONArray jsonUserDiets = jsonResponse.getJSONArray(JSON_TAG_USER_FOLLOWS_DIET_LIST);
for (int dietIndex = 0; dietIndex < jsonUserDiets.length(); ++dietIndex) {
JSONObject diet = jsonUserDiets.getJSONObject(dietIndex);
userDiets.add(diet.getInt(JSON_TAG_USER_FOLLOWS_DIET_DIET));
}
JSONArray jsonDietsList = jsonResponse.getJSONArray(JSON_TAG_DIETS_LIST);
for (int dietIndex = 0; dietIndex < jsonDietsList.length(); ++dietIndex) {
JSONObject diet = jsonDietsList.getJSONObject(dietIndex);
diets.add(new Diet(diet.getInt(JSON_TAG_DIET_ID),
diet.getString(JSON_TAG_DIET_NAME),
diet.getString(JSON_TAG_DIET_DESCRIPTION),
userDiets.contains(diet.getInt(JSON_TAG_DIET_ID))));
}
return 1;
} catch (Exception e) {
e.printStackTrace();
return 2;
}
}
@Override
protected void onPostExecute(Integer result) {
DietsAdapter dietsAdapter = new DietsAdapter(diets, DietsActivity.this);
recyclerView.setAdapter(dietsAdapter);
}
}
private class FollowDietStateTask extends AsyncTask<Integer, Void, Integer> {
@Override
protected void onPreExecute() {
}
@Override
protected Integer doInBackground(Integer... params) {
RequestBody requestBody = RequestBody.create(null, new byte[]{});
//Builds the request
Request request = new Request.Builder()
.patch(requestBody)
.url(followDietUrl + params[0] + "/")
.build();
try {
//Makes request & handles response
client.newCall(request).execute();
return 0;
} catch (Exception e) {
e.printStackTrace();
return 2;
}
}
@Override
protected void onPostExecute(Integer result) {
}
}
}

29
UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/diets/DietsAdapter.java

@ -15,9 +15,11 @@ import gr.auth.databases.flavours.model.Diet;
public class DietsAdapter extends RecyclerView.Adapter<DietsAdapter.DietViewHolder> {
private ArrayList<Diet> diets;
private SubscribeDietsAdapterInteractionListener subscribeDietsAdapterInteractionListener;
DietsAdapter(ArrayList<Diet> diets) {
DietsAdapter(ArrayList<Diet> diets, SubscribeDietsAdapterInteractionListener subscribeDietsAdapterInteractionListener) {
this.diets = diets;
this.subscribeDietsAdapterInteractionListener = subscribeDietsAdapterInteractionListener;
}
@NonNull
@ -29,12 +31,31 @@ public class DietsAdapter extends RecyclerView.Adapter<DietsAdapter.DietViewHold
}
@Override
public void onBindViewHolder(@NonNull DietViewHolder holder, int position) {
public void onBindViewHolder(@NonNull final DietViewHolder holder, final int position) {
holder.name.setText(diets.get(position).getName());
holder.description.setText(diets.get(position).getDescription());
if (diets.get(position).isFollowedByUser()) {
holder.addDietButton.setImageResource(R.drawable.ic_delete_black_18dp);
} else {
holder.addDietButton.setImageResource(R.drawable.ic_add_black_18dp);
}
holder.addDietButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
subscribeDietsAdapterInteractionListener.onSubscribeDietsAdapterInteraction(
diets.get(holder.getAdapterPosition()));
if ((diets.get(position).isFollowedByUser())) {
holder.addDietButton.setImageResource(R.drawable.ic_add_black_18dp);
diets.get(position).setFollowedByUser(!diets.get(position).isFollowedByUser());
} else {
holder.addDietButton.setImageResource(R.drawable.ic_delete_black_18dp);
diets.get(position).setFollowedByUser(!diets.get(position).isFollowedByUser());
}
}
});
}
@Override
@ -53,4 +74,8 @@ public class DietsAdapter extends RecyclerView.Adapter<DietsAdapter.DietViewHold
addDietButton = v.findViewById(R.id.diets_diet_subscribe);
}
}
public interface SubscribeDietsAdapterInteractionListener {
void onSubscribeDietsAdapterInteraction(Diet diet);
}
}

134
UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/ingredients/IngredientsActivity.java

@ -2,6 +2,7 @@ package gr.auth.databases.flavours.activities.ingredients;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
@ -9,6 +10,9 @@ import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import androidx.appcompat.app.ActionBar;
@ -19,13 +23,23 @@ import gr.auth.databases.flavours.R;
import gr.auth.databases.flavours.activities.AddIngredientActivity;
import gr.auth.databases.flavours.base.BaseActivity;
import gr.auth.databases.flavours.model.Ingredient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import static gr.auth.databases.flavours.activities.AddIngredientActivity.INGREDIENT_ADD_RESULT;
import static gr.auth.databases.flavours.session.SessionManager.ingredientsUserViewUrl;
import static gr.auth.databases.flavours.session.SessionManager.prohibitIngredientUrl;
public class IngredientsActivity extends BaseActivity implements IngredientsAdapter.IngredientsAdapterInteractionListener {
public class IngredientsActivity extends BaseActivity
implements IngredientsAdapter.SubscribeIngredientsAdapterInteractionListener,
IngredientsAdapter.IngredientsAdapterInteractionListener {
public static final String INGREDIENT_PICK_RESULT = "INGREDIENT_PICK_RESULT";
private static final int ADD_NEW_INGREDIENT_REQUEST = 2000;
private RecyclerView recyclerView;
private ArrayList<String> userProhibitsIngredients;
private boolean isCalledForResult;
private FloatingActionButton FAB;
private ArrayList<Ingredient> ingredients = new ArrayList<>();
@ -57,21 +71,13 @@ public class IngredientsActivity extends BaseActivity implements IngredientsAdap
}
});
RecyclerView recyclerView = findViewById(R.id.ingredients_list);
recyclerView = findViewById(R.id.ingredients_list);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
ingredients.add(new Ingredient("Μόνο κρέας", false));
ingredients.add(new Ingredient("Σχεδόν μόνο κρέας", true));
if (isCalledForResult) {
ingredientsAdapter = new IngredientsAdapter(this, ingredients, this);
} else {
ingredientsAdapter = new IngredientsAdapter(this, ingredients, null);
}
recyclerView.setAdapter(ingredientsAdapter);
IngredientsTask ingredientsTask = new IngredientsTask();
ingredientsTask.execute();
}
@Override
@ -106,6 +112,12 @@ public class IngredientsActivity extends BaseActivity implements IngredientsAdap
super.onDestroy();
}
@Override
public void onSubscribeIngredientsAdapterInteraction(Ingredient ingredient) {
ProhibitIngredientStateTask prohibitIngredientStateTask = new ProhibitIngredientStateTask();
prohibitIngredientStateTask.execute(ingredient.getName());
}
@Override
public void onIngredientsAdapterInteraction(Ingredient ingredient) {
Intent returnIntent = new Intent();
@ -125,5 +137,101 @@ public class IngredientsActivity extends BaseActivity implements IngredientsAdap
}
}
}
}
private class IngredientsTask extends AsyncTask<Void, Void, Integer> {
private static final String JSON_TAG_INGREDIENTS_LIST = "ingredients";
private static final String JSON_TAG_INGREDIENT_NAME = "ingredient_name";
private static final String JSON_TAG_INGREDIENT_HAS_ALCOHOL = "ingredient_has_alcohol";
private static final String JSON_TAG_USER_PROHIBITS_INGREDIENT_LIST = "prohibits";
private static final String JSON_TAG_USER_PROHIBITS_INGREDIENT = "ingredient_name";
@Override
protected void onPreExecute() {
}
@Override
protected Integer doInBackground(Void... params) {
//Builds the request
Request request = new Request.Builder()
.url(ingredientsUserViewUrl)
.build();
try {
//Makes request & handles response
Response response = client.newCall(request).execute();
ResponseBody responseBody = response.body();
assert responseBody != null;
String result = responseBody.string();
JSONObject jsonResponse = new JSONObject(result);
userProhibitsIngredients = new ArrayList<>();
JSONArray jsonUserProhibitsIngredients = jsonResponse.getJSONArray(JSON_TAG_USER_PROHIBITS_INGREDIENT_LIST);
for (int ingredientIndex = 0; ingredientIndex < jsonUserProhibitsIngredients.length(); ++ingredientIndex) {
JSONObject ingredient = jsonUserProhibitsIngredients.getJSONObject(ingredientIndex);
userProhibitsIngredients.add(ingredient.getString(JSON_TAG_USER_PROHIBITS_INGREDIENT));
}
JSONArray jsonIngredientsList = jsonResponse.getJSONArray(JSON_TAG_INGREDIENTS_LIST);
for (int dietIndex = 0; dietIndex < jsonIngredientsList.length(); ++dietIndex) {
JSONObject ingredient = jsonIngredientsList.getJSONObject(dietIndex);
ingredients.add(new Ingredient(ingredient.getString(JSON_TAG_INGREDIENT_NAME),
ingredient.getBoolean(JSON_TAG_INGREDIENT_HAS_ALCOHOL)));
}
return 1;
} catch (Exception e) {
e.printStackTrace();
return 2;
}
}
@Override
protected void onPostExecute(Integer result) {
if (isCalledForResult) {
ingredientsAdapter = new IngredientsAdapter(IngredientsActivity.this,
ingredients,
userProhibitsIngredients,
null,
IngredientsActivity.this);
} else {
ingredientsAdapter = new IngredientsAdapter(IngredientsActivity.this,
ingredients,
userProhibitsIngredients,
IngredientsActivity.this,
null);
}
recyclerView.setAdapter(ingredientsAdapter);
}
}
private class ProhibitIngredientStateTask extends AsyncTask<String, Void, Integer> {
@Override
protected void onPreExecute() {
}
@Override
protected Integer doInBackground(String... params) {
RequestBody requestBody = RequestBody.create(null, new byte[]{});
//Builds the request
Request request = new Request.Builder()
.patch(requestBody)
.url(prohibitIngredientUrl + params[0] + "/")
.build();
try {
//Makes request & handles response
client.newCall(request).execute();
return 0;
} catch (Exception e) {
e.printStackTrace();
return 2;
}
}
@Override
protected void onPostExecute(Integer result) {
}
}
}

44
UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/ingredients/IngredientsAdapter.java

@ -9,6 +9,7 @@ import android.widget.TextView;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import gr.auth.databases.flavours.R;
@ -17,12 +18,17 @@ import gr.auth.databases.flavours.model.Ingredient;
public class IngredientsAdapter extends RecyclerView.Adapter<IngredientsAdapter.IngredientViewHolder> {
private Context context;
private ArrayList<Ingredient> ingredients;
private ArrayList<String> userProhibitsIngredients;
private SubscribeIngredientsAdapterInteractionListener subscribeIngredientsAdapterInteractionListener;
private IngredientsAdapterInteractionListener ingredientsAdapterInteractionListener;
IngredientsAdapter(Context context, ArrayList<Ingredient> ingredients,
IngredientsAdapter(Context context, ArrayList<Ingredient> ingredients, ArrayList<String> userProhibitsIngredients,
SubscribeIngredientsAdapterInteractionListener subscribeIngredientsAdapterInteractionListener,
IngredientsAdapterInteractionListener ingredientsAdapterInteractionListener) {
this.context = context;
this.ingredients = ingredients;
this.userProhibitsIngredients = userProhibitsIngredients;
this.subscribeIngredientsAdapterInteractionListener = subscribeIngredientsAdapterInteractionListener;
this.ingredientsAdapterInteractionListener = ingredientsAdapterInteractionListener;
}
@ -35,11 +41,39 @@ public class IngredientsAdapter extends RecyclerView.Adapter<IngredientsAdapter.
}
@Override
public void onBindViewHolder(@NonNull final IngredientViewHolder holder, int position) {
public void onBindViewHolder(@NonNull final IngredientViewHolder holder, final int position) {
holder.name.setText(ingredients.get(position).getName());
holder.containsAlcohol.setText(context.getString(R.string.ingredients_contains_alcohol_placeholder,
ingredients.get(position).hasAlcohol() ? "YES" : "NO"));
if (subscribeIngredientsAdapterInteractionListener != null) {
holder.subscribeButton.setVisibility(View.VISIBLE);
if (userProhibitsIngredients.contains(ingredients.get(position).getName())) {
holder.subscribeButton.setImageResource(R.drawable.ic_delete_black_18dp);
} else {
holder.subscribeButton.setImageResource(R.drawable.ic_add_black_18dp);
}
holder.subscribeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
subscribeIngredientsAdapterInteractionListener.onSubscribeIngredientsAdapterInteraction(
ingredients.get(holder.getAdapterPosition()));
if (userProhibitsIngredients.contains(ingredients.get(position).getName())) {
holder.subscribeButton.setImageResource(R.drawable.ic_add_black_18dp);
userProhibitsIngredients.remove(ingredients.get(position).getName());
} else {
holder.subscribeButton.setImageResource(R.drawable.ic_delete_black_18dp);
userProhibitsIngredients.add(ingredients.get(position).getName());
}
}
});
} else {
holder.subscribeButton.setVisibility(View.GONE);
}
if (ingredientsAdapterInteractionListener != null) {
holder.card.setOnClickListener(new View.OnClickListener() {
@Override
@ -59,15 +93,21 @@ public class IngredientsAdapter extends RecyclerView.Adapter<IngredientsAdapter.
static class IngredientViewHolder extends RecyclerView.ViewHolder {
CardView card;
TextView name, containsAlcohol;
AppCompatImageButton subscribeButton;
IngredientViewHolder(View v) {
super(v);
card = v.findViewById(R.id.ingredients_row_card);
name = v.findViewById(R.id.ingredients_ingredient_name);
containsAlcohol = v.findViewById(R.id.ingredients_ingredient_contains_alcohol);
subscribeButton = v.findViewById(R.id.ingredients_ingredient_subscribe);
}
}
public interface SubscribeIngredientsAdapterInteractionListener {
void onSubscribeIngredientsAdapterInteraction(Ingredient ingredient);
}
public interface IngredientsAdapterInteractionListener {
void onIngredientsAdapterInteraction(Ingredient ingredient);
}

4
UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Diet.java

@ -27,4 +27,8 @@ public class Diet {
public boolean isFollowedByUser() {
return isFollowedByUser;
}
public void setFollowedByUser(boolean followedByUser) {
isFollowedByUser = followedByUser;
}
}

5
UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/session/SessionManager.java

@ -62,7 +62,10 @@ public class SessionManager {
public static final String profileUserViewUrl = baseServerUrl + "api/profileUserView/";
public static final String addFoodUrl = baseServerUrl + "api/addFood/";
public static final String addDrinkUrl = baseServerUrl + "api/addDrink/";
public static final String dietsUserViewUrl = baseServerUrl + "api/dietUserView/";
public static final String ingredientsUserViewUrl = baseServerUrl + "api/ingredientUserView/";
public static final String prohibitIngredientUrl = baseServerUrl + "api/prohibitIngredient/";
public static final String followDietUrl = baseServerUrl + "api/followDiet/";
// Client & Cookies
private final OkHttpClient client;

7
UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/ingredients_row.xml

@ -38,5 +38,12 @@
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:text="@string/ingredients_contains_alcohol" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/ingredients_ingredient_subscribe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:src="@drawable/ic_add_black_18dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
Loading…
Cancel
Save