Skip to content

Commit

Permalink
Imported code from separate test project to implement retrofit and re…
Browse files Browse the repository at this point in the history
…cyclerview with intents.
  • Loading branch information
nathanmeade committed Jun 20, 2019
1 parent f8ecdb7 commit 5fdb4bd
Show file tree
Hide file tree
Showing 13 changed files with 484 additions and 14 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:recyclerview-v7:28.0.0'
}
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.bakingapp">

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".RecipeStepsActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/example/android/bakingapp/Ingredient.java
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;
}
}
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 app/src/main/java/com/example/android/bakingapp/MainActivity.java
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 app/src/main/java/com/example/android/bakingapp/Recipe.java
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 {
}*/
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;
}

}

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);
}
}
Loading

0 comments on commit 5fdb4bd

Please sign in to comment.