|
@ -20,15 +20,19 @@ import androidx.recyclerview.widget.RecyclerView; |
|
|
import gr.auth.databases.flavours.R; |
|
|
import gr.auth.databases.flavours.R; |
|
|
import gr.auth.databases.flavours.activities.AddDietActivity; |
|
|
import gr.auth.databases.flavours.activities.AddDietActivity; |
|
|
import gr.auth.databases.flavours.base.BaseActivity; |
|
|
import gr.auth.databases.flavours.base.BaseActivity; |
|
|
|
|
|
import gr.auth.databases.flavours.base.BaseApplication; |
|
|
import gr.auth.databases.flavours.model.Diet; |
|
|
import gr.auth.databases.flavours.model.Diet; |
|
|
import gr.auth.databases.flavours.utils.FollowDietStateTask; |
|
|
import gr.auth.databases.flavours.utils.FollowDietStateTask; |
|
|
import okhttp3.Request; |
|
|
import okhttp3.Request; |
|
|
|
|
|
import okhttp3.RequestBody; |
|
|
import okhttp3.Response; |
|
|
import okhttp3.Response; |
|
|
import okhttp3.ResponseBody; |
|
|
import okhttp3.ResponseBody; |
|
|
|
|
|
|
|
|
|
|
|
import static gr.auth.databases.flavours.session.SessionManager.acceptDietUrl; |
|
|
import static gr.auth.databases.flavours.session.SessionManager.dietsUserViewUrl; |
|
|
import static gr.auth.databases.flavours.session.SessionManager.dietsUserViewUrl; |
|
|
|
|
|
|
|
|
public class DietsActivity extends BaseActivity implements DietsAdapter.SubscribeDietsAdapterInteractionListener { |
|
|
public class DietsActivity extends BaseActivity implements |
|
|
|
|
|
DietsAdapter.AcceptDietAdapterInteractionListener, DietsAdapter.SubscribeDietsAdapterInteractionListener { |
|
|
private RecyclerView recyclerView; |
|
|
private RecyclerView recyclerView; |
|
|
private FloatingActionButton FAB; |
|
|
private FloatingActionButton FAB; |
|
|
private ArrayList<Diet> diets = new ArrayList<>(); |
|
|
private ArrayList<Diet> diets = new ArrayList<>(); |
|
@ -88,6 +92,13 @@ public class DietsActivity extends BaseActivity implements DietsAdapter.Subscrib |
|
|
return super.onOptionsItemSelected(item); |
|
|
return super.onOptionsItemSelected(item); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void onAcceptDietAdapterInteraction(Diet diet) { |
|
|
|
|
|
AcceptDietTask acceptDietTask = new AcceptDietTask(); |
|
|
|
|
|
acceptDietTask.execute(diet.getId()); |
|
|
|
|
|
diets.get(diets.indexOf(diet)).setAccepted(true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onSubscribeDietsAdapterInteraction(Diet diet) { |
|
|
public void onSubscribeDietsAdapterInteraction(Diet diet) { |
|
|
FollowDietStateTask followDietStateTask = new FollowDietStateTask(client); |
|
|
FollowDietStateTask followDietStateTask = new FollowDietStateTask(client); |
|
@ -99,7 +110,7 @@ public class DietsActivity extends BaseActivity implements DietsAdapter.Subscrib |
|
|
private static final String JSON_TAG_DIET_ID = "diet_id"; |
|
|
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_NAME = "diet_name"; |
|
|
private static final String JSON_TAG_DIET_DESCRIPTION = "diet_description"; |
|
|
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_DIET_IS_ACCEPTED = "diet_is_approved"; |
|
|
|
|
|
|
|
|
private static final String JSON_TAG_USER_FOLLOWS_DIET_LIST = "followed"; |
|
|
private static final String JSON_TAG_USER_FOLLOWS_DIET_LIST = "followed"; |
|
|
private static final String JSON_TAG_USER_FOLLOWS_DIET_DIET = "diet"; |
|
|
private static final String JSON_TAG_USER_FOLLOWS_DIET_DIET = "diet"; |
|
@ -137,7 +148,8 @@ public class DietsActivity extends BaseActivity implements DietsAdapter.Subscrib |
|
|
diets.add(new Diet(diet.getInt(JSON_TAG_DIET_ID), |
|
|
diets.add(new Diet(diet.getInt(JSON_TAG_DIET_ID), |
|
|
diet.getString(JSON_TAG_DIET_NAME), |
|
|
diet.getString(JSON_TAG_DIET_NAME), |
|
|
diet.getString(JSON_TAG_DIET_DESCRIPTION), |
|
|
diet.getString(JSON_TAG_DIET_DESCRIPTION), |
|
|
userDiets.contains(diet.getInt(JSON_TAG_DIET_ID)))); |
|
|
userDiets.contains(diet.getInt(JSON_TAG_DIET_ID)), |
|
|
|
|
|
diet.getBoolean(JSON_TAG_DIET_IS_ACCEPTED))); |
|
|
} |
|
|
} |
|
|
return 1; |
|
|
return 1; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
@ -148,8 +160,39 @@ public class DietsActivity extends BaseActivity implements DietsAdapter.Subscrib |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
protected void onPostExecute(Integer result) { |
|
|
protected void onPostExecute(Integer result) { |
|
|
DietsAdapter dietsAdapter = new DietsAdapter(diets, DietsActivity.this); |
|
|
DietsAdapter dietsAdapter = new DietsAdapter(diets, DietsActivity.this, |
|
|
|
|
|
DietsActivity.this); |
|
|
recyclerView.setAdapter(dietsAdapter); |
|
|
recyclerView.setAdapter(dietsAdapter); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private class AcceptDietTask 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(acceptDietUrl + params[0] + "/") |
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
//Makes request & handles response
|
|
|
|
|
|
BaseApplication.getInstance().getClient().newCall(request).execute(); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
return 2; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
protected void onPostExecute(Integer result) { |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|