-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Imported code from separate test project to implement retrofit and re…
…cyclerview with intents.
- Loading branch information
1 parent
f8ecdb7
commit 5fdb4bd
Showing
13 changed files
with
484 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/example/android/bakingapp/Ingredient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.example.android.bakingapp; | ||
|
||
public class Ingredient { | ||
private float quantity; | ||
private String measure; | ||
private String ingredient; | ||
|
||
public float getQuantity() { | ||
return quantity; | ||
} | ||
|
||
public String getMeasure() { | ||
return measure; | ||
} | ||
|
||
public String getIngredient() { | ||
return ingredient; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/example/android/bakingapp/JsonPlaceHolderApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.example.android.bakingapp; | ||
|
||
import java.util.List; | ||
|
||
import retrofit2.Call; | ||
import retrofit2.http.GET; | ||
|
||
public interface JsonPlaceHolderApi { | ||
|
||
@GET("topher/2017/May/59121517_baking/baking.json") | ||
Call<List<Recipe>> getRecipe(); | ||
} |
117 changes: 114 additions & 3 deletions
117
app/src/main/java/com/example/android/bakingapp/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,125 @@ | ||
package com.example.android.bakingapp; | ||
|
||
import android.content.Intent; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.support.v7.widget.GridLayoutManager; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.util.Log; | ||
import android.widget.TextView; | ||
|
||
import com.example.android.bakingapp.Ingredient; | ||
import com.example.android.bakingapp.R; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import retrofit2.Call; | ||
import retrofit2.Callback; | ||
import retrofit2.Response; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.gson.GsonConverterFactory; | ||
|
||
public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter.RecyclerViewAdapterOnClickHandler { | ||
//private TextView textViewResult; | ||
private static final String LOG_TAG = "nathanTest"; | ||
private RecyclerView recyclerView; | ||
private RecyclerViewAdapter recyclerViewAdapter; | ||
private RecyclerViewAdapter.RecyclerViewAdapterOnClickHandler clickHandler; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
//Test comment for second commit and push within Android Studio | ||
clickHandler = this; | ||
recyclerView = findViewById(R.id.recycler_view); | ||
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); | ||
recyclerView.setLayoutManager(linearLayoutManager); | ||
//recyclerView.setHasFixedSize(true); | ||
//textViewResult = findViewById(R.id.text_view_result); | ||
RecipeRepository recipeRepository = new RecipeRepository(); | ||
getJsonParsed(); | ||
} | ||
|
||
public void getJsonParsed(){ | ||
Retrofit retrofit = new Retrofit.Builder() | ||
.baseUrl("https://d17h27t6h515a5.cloudfront.net/") | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build(); | ||
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class); | ||
Call<List<Recipe>> call = jsonPlaceHolderApi.getRecipe(); | ||
call.enqueue(new Callback<List<Recipe>>() { | ||
@Override | ||
public void onResponse(Call<List<Recipe>> call, Response<List<Recipe>> response) { | ||
ArrayList<Integer> mIds = new ArrayList<>(); | ||
ArrayList<String> mTexts = new ArrayList<>(); | ||
ArrayList<Long> mServings = new ArrayList<>(); | ||
ArrayList<String> mImages = new ArrayList<>(); | ||
ArrayList<ArrayList<Ingredient>> mIngredients = new ArrayList<>(); | ||
ArrayList<ArrayList<Step>> mSteps = new ArrayList<>(); | ||
if (!response.isSuccessful()){ | ||
return; | ||
} | ||
List<Recipe> recipes = response.body(); | ||
for (Recipe recipe : recipes) { | ||
String content = ""; | ||
content += "ID: " + recipe.getId() + "\n"; | ||
mIds.add(recipe.getId()); | ||
content += "Text: " + recipe.getText() + "\n"; | ||
mTexts.add(recipe.getText()); | ||
content += "Servings: " + recipe.getServings() + "\n"; | ||
mServings.add(recipe.getServings()); | ||
content += "Image: " + recipe.getImage() + "\n\n"; | ||
mImages.add(recipe.getImage()); | ||
content += "Ingredients:\n"; | ||
ArrayList<Ingredient> ingredients = recipe.getIngredients(); | ||
mIngredients.add(ingredients); | ||
for (Ingredient ingredient : ingredients ){ | ||
content += "Quantity: " + ingredient.getQuantity() + "\n"; | ||
content += "Measure: " + ingredient.getMeasure() + "\n"; | ||
content += "Ingredient: " + ingredient.getIngredient() + "\n\n"; | ||
} | ||
content += "Steps:\n"; | ||
ArrayList<Step> steps = recipe.getSteps(); | ||
mSteps.add(steps); | ||
for (Step step : steps ){ | ||
content += "ID: " + step.getId() + "\n"; | ||
content += "Short Description: " + step.getShortDescription() + "\n"; | ||
content += "Description: " + step.getDescription() + "\n"; | ||
content += "Video URL: " + step.getVideoURL() + "\n"; | ||
content += "Thumbnail: " + step.getThumbnailURL() + "\n\n"; | ||
} | ||
//textViewResult.append(content); | ||
} | ||
recyclerViewAdapter = new RecyclerViewAdapter(mIds, mTexts, mServings, mImages, mIngredients, mSteps, clickHandler); | ||
recyclerView.setAdapter(recyclerViewAdapter); | ||
} | ||
@Override | ||
public void onFailure(Call<List<Recipe>> call, Throwable t) { | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onClick(int id, String text, Long serving, String image, ArrayList<Ingredient> ingredients, ArrayList<Step> steps) { | ||
Intent intent = new Intent(this, RecipeStepsActivity.class); | ||
intent.putExtra("id", id); | ||
intent.putExtra("text", text); | ||
intent.putExtra("serving", serving); | ||
intent.putExtra("image", image); | ||
//how to put extra for arraylists? | ||
/* Bundle ingredientsBundle = new Bundle(); | ||
Bundle stepsBundle = new Bundle(); | ||
ingredientsBundle.putSerializable("ingredients", ingredients); | ||
stepsBundle.putSerializable("steps", steps); | ||
intent.putExtra("ingredients", ingredientsBundle); | ||
intent.putExtra("steps", stepsBundle);*/ | ||
startActivity(intent); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/example/android/bakingapp/Recipe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.example.android.bakingapp; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
import org.json.JSONArray; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class Recipe { | ||
|
||
private int id; | ||
|
||
@SerializedName("name") | ||
private String text; | ||
|
||
private Long servings; | ||
private String image; | ||
private ArrayList<Ingredient> ingredients; | ||
private ArrayList<Step> steps; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public Long getServings() { | ||
return servings; | ||
} | ||
|
||
public String getImage() { | ||
return image; | ||
} | ||
|
||
public ArrayList<Ingredient> getIngredients() { | ||
return ingredients; | ||
} | ||
|
||
public ArrayList<Step> getSteps() { | ||
return steps; | ||
} | ||
} | ||
|
||
/* | ||
public class Ingredients { | ||
}*/ |
96 changes: 96 additions & 0 deletions
96
app/src/main/java/com/example/android/bakingapp/RecipeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.example.android.bakingapp; | ||
|
||
import android.util.Log; | ||
import android.widget.TextView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import retrofit2.Call; | ||
import retrofit2.Callback; | ||
import retrofit2.Response; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.gson.GsonConverterFactory; | ||
|
||
public class RecipeRepository { | ||
|
||
private String content; | ||
private String contentToReturn; | ||
|
||
private static final String LOG_TAG = "nathanTest"; | ||
|
||
public String getJsonParsed(){ | ||
//contentToReturn = ""; | ||
Log.d(LOG_TAG, "start of getJsonParsed"); | ||
Retrofit retrofit = new Retrofit.Builder() | ||
.baseUrl("https://d17h27t6h515a5.cloudfront.net/") | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build(); | ||
|
||
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class); | ||
|
||
Call<List<Recipe>> call = jsonPlaceHolderApi.getRecipe(); | ||
|
||
Log.d(LOG_TAG, "before enqueue"); | ||
call.enqueue(new Callback<List<Recipe>>() { | ||
@Override | ||
public void onResponse(Call<List<Recipe>> call, Response<List<Recipe>> response) { | ||
|
||
if (!response.isSuccessful()){ | ||
//textViewResult.setText("Code: " + response.code()); | ||
return; | ||
} | ||
|
||
List<Recipe> recipes = response.body(); | ||
|
||
for (Recipe recipe : recipes) { | ||
content = ""; | ||
content += "ID: " + recipe.getId() + "\n"; | ||
//Log.d(LOG_TAG, recipe.getText()); | ||
Log.d(LOG_TAG, content); | ||
content += "Text: " + recipe.getText() + "\n"; | ||
content += "Servings: " + recipe.getServings() + "\n"; | ||
content += "Image: " + recipe.getImage() + "\n\n"; | ||
/* JSONArray jsonArray = recipe.getIngredients(); | ||
try { | ||
JSONObject jsonObject = (JSONObject) jsonArray.get(0); | ||
content += jsonObject.get("measure"); | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
}*/ | ||
content += "Ingredients:\n"; | ||
ArrayList<Ingredient> ingredients = recipe.getIngredients(); | ||
for (Ingredient ingredient : ingredients ){ | ||
content += "Quantity: " + ingredient.getQuantity() + "\n"; | ||
content += "Measure: " + ingredient.getMeasure() + "\n"; | ||
content += "Ingredient: " + ingredient.getIngredient() + "\n\n"; | ||
} | ||
|
||
content += "Steps:\n"; | ||
ArrayList<Step> steps = recipe.getSteps(); | ||
for (Step step : steps ){ | ||
content += "ID: " + step.getId() + "\n"; | ||
content += "Short Description: " + step.getShortDescription() + "\n"; | ||
content += "Description: " + step.getDescription() + "\n"; | ||
content += "Video URL: " + step.getVideoURL() + "\n"; | ||
content += "Thumbnail: " + step.getThumbnailURL() + "\n\n"; | ||
} | ||
contentToReturn += content; | ||
//Log.d(LOG_TAG, "contentToReturn: " + contentToReturn); | ||
//textViewResult.append(content); | ||
} | ||
|
||
//Log.d(LOG_TAG, "contentToReturn: " + contentToReturn); | ||
} | ||
|
||
@Override | ||
public void onFailure(Call<List<Recipe>> call, Throwable t) { | ||
//textViewResult.setText(t.getMessage()); | ||
} | ||
}); | ||
Log.d(LOG_TAG, "contentToReturn: " + contentToReturn); | ||
return contentToReturn; | ||
} | ||
|
||
} | ||
|
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/example/android/bakingapp/RecipeStepsActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.example.android.bakingapp; | ||
|
||
import android.content.Intent; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.widget.TextView; | ||
|
||
public class RecipeStepsActivity extends AppCompatActivity { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_recipe_steps); | ||
TextView textView = findViewById(R.id.text_view); | ||
Intent intent = getIntent(); | ||
String id = intent.getStringExtra("text"); | ||
textView.setText(id); | ||
} | ||
} |
Oops, something went wrong.