Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
JsonUtil set
MainActivity almost complete
  • Loading branch information
vaibhav committed Dec 5, 2017
0 parents commit e214c4b
Show file tree
Hide file tree
Showing 41 changed files with 1,002 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
32 changes: 32 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "vaibhav.mishu.com.bakingapp"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

compile 'com.android.support:cardview-v7:26.1.0'
compile 'com.squareup.okhttp3:okhttp:3.9.1'
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package vaibhav.mishu.com.bakingapp;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("vaibhav.mishu.com.bakingapp", appContext.getPackageName());
}
}
23 changes: 23 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vaibhav.mishu.com.bakingapp">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
81 changes: 81 additions & 0 deletions app/src/main/java/vaibhav/mishu/com/bakingapp/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package vaibhav.mishu.com.bakingapp;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.lang.reflect.Array;
import java.util.ArrayList;

import vaibhav.mishu.com.bakingapp.util.JsonUtil;
import vaibhav.mishu.com.bakingapp.util.NetworkUtil;

public class MainActivity extends AppCompatActivity {

ArrayList<JsonUtil.Recipe> recipes;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

GetRecipesTask task = new GetRecipesTask(this);
task.execute();

ArrayList<String> recipeNames = new ArrayList<>();
recipeNames.add("Nutella Pie");
recipeNames.add("Brownies");
recipeNames.add("Yellow Cake");
recipeNames.add("Cheese Cake");

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.recipe_item, R.id.recipe_name);
adapter.addAll(recipeNames);
ListView listView = findViewById(R.id.recipe_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,"clicked: " + position, Toast.LENGTH_SHORT).show();
}
});
}

static class GetRecipesTask extends AsyncTask<Void,Void,ArrayList<JsonUtil.Recipe>>{

ArrayList<JsonUtil.Recipe> recipes;

private WeakReference<MainActivity> activityReference;

GetRecipesTask(MainActivity context){
activityReference = new WeakReference<>(context);
}

@Override
protected ArrayList<JsonUtil.Recipe> doInBackground(Void... voids) {
try {
recipes = JsonUtil.jsonStingToRecipes(
NetworkUtil.getResponseFromHttpUrl(NetworkUtil.buildURL())
);
return recipes;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(ArrayList<JsonUtil.Recipe> recipes) {
super.onPostExecute(recipes);
activityReference.get().recipes = recipes;
}
}
}
99 changes: 99 additions & 0 deletions app/src/main/java/vaibhav/mishu/com/bakingapp/util/JsonUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package vaibhav.mishu.com.bakingapp.util;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
* Created by Vaibhav on 12/5/2017.
* To parse the json
*/

public class JsonUtil {

private static final String LOG_TAG = JsonUtil.class.getSimpleName();

public static ArrayList<Recipe> jsonStingToRecipes(String jsonResponse) {
JSONArray rootJsonArray, ingredientsArray, stepsArray;
JSONObject recipeJson, ingredientJson, stepJson;
ArrayList<String> ingredientList;
ArrayList<Step> stepList;
String recipeName;
int recipeServings;
ArrayList<Recipe> recipesList = new ArrayList<Recipe>();

try {
rootJsonArray = new JSONArray(jsonResponse);
for(int i=0; i<rootJsonArray.length(); i++){ //loop through recipes list

ingredientList = new ArrayList<String>();
stepList = new ArrayList<Step>();

recipeJson = rootJsonArray.getJSONObject(i);
ingredientsArray = recipeJson.getJSONArray("ingredients");
stepsArray = recipeJson.getJSONArray("steps");

for (int j=0; j<ingredientsArray.length(); j++){ //loop through INGREDIENTS
ingredientJson = ingredientsArray.getJSONObject(j);
ingredientList.add(
ingredientJson.getString("ingredient") + " (" +
ingredientJson.getString("quantity") + " " +
ingredientJson.getString("measure") + ")"
);
}
for (int j=0; j<stepsArray.length(); j++){ //loop through STEPS
stepJson = stepsArray.getJSONObject(j);
stepList.add(new Step(
stepJson.getString("shortDescription"),
stepJson.getString("description"),
stepJson.getString("videoURL"),
stepJson.getString("thumbnailURL")
));
}

recipeName = recipeJson.getString("name");
recipeServings = recipeJson.getInt("servings");

//add to recipes list:
recipesList.add(new Recipe(recipeName, ingredientList, stepList, recipeServings));

}

return recipesList;
} catch (JSONException e) {
Log.e(LOG_TAG, "Problem converting to JSON array: " + e);
e.printStackTrace();
}
return null;
}

public static class Recipe {
String name;
ArrayList<String> ingredients;
ArrayList<Step> steps;
int servings;

Recipe(String recipeName, ArrayList<String> recipeIngedients, ArrayList<Step> recipeSteps, int serves){
name = recipeName;
ingredients = recipeIngedients;
steps = recipeSteps;
servings = serves;
}
}

public static class Step {
String shortDescription, description, videoURL, thumbnailURL;

Step(String shDescr, String descr, String video, String thumb){
shortDescription = shDescr;
description = descr;
videoURL = video;
thumbnailURL = thumb;
}
}

}
Loading

0 comments on commit e214c4b

Please sign in to comment.