Skip to content

Commit

Permalink
Added view for single dish and displaying the image, added stripping …
Browse files Browse the repository at this point in the history
…not needed quotes from json
  • Loading branch information
maciejgaciarz committed Jul 16, 2018
1 parent 4cf3d94 commit 092061d
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 39 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
9 changes: 4 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<activity android:name=".ResultActivity"></activity>


<activity android:name=".ResultActivity" />
<activity
android:name=".SingleDishActivity"
android:label="@string/title_activity_single_dish"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

public class JSONObjectWrapper {

private String stripQuotes(String toStrip){
return toStrip.replaceAll("^\"|\"$", "");
}

public List<Recipe> getRecipes(String JSON){

ArrayList<Recipe> recipes = new ArrayList<>();
Expand All @@ -33,9 +37,11 @@ public List<Recipe> getRecipes(String JSON){

JsonObject currentRecipe = (JsonObject) currentRecipeObject.get("recipe");

String label = currentRecipe.get("label").toString();
String recipeUri = currentRecipe.get("uri").toString();
String imageUri = currentRecipe.get("image").toString();
//stripping additional pair of quotes from around downloaded data
String label = stripQuotes(currentRecipe.get("label").toString());
String recipeUri = stripQuotes(currentRecipe.get("uri").toString());
String imageUri = stripQuotes(currentRecipe.get("image").toString());


Recipe recipe = new Recipe(label, imageUri, recipeUri, i);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ListAdapter extends BaseAdapter {

private Activity activity;
private static LayoutInflater inflater=null;
List<Recipe> recipes;
private List<Recipe> recipes;

@Override
public int getCount() {
Expand Down Expand Up @@ -59,14 +59,11 @@ public View getView(int position, View convertView, ViewGroup parent){
Recipe recipe = recipes.get(position);


//load
dishName.setText(recipe.getLabel());
dishDesc.setText(recipe.getInstructionURL());

//strip URL from one pair of additional quites ("")
String strippedURL = recipe.getImageURL().replaceAll("^\"|\"$", "");

Picasso.get().load(strippedURL).into(dishImage);
//download image directly into ImageView
Picasso.get().load(recipe.getImageURL()).into(dishImage);


return vi;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class MainActivity extends AppCompatActivity {

Button searchButton;
EditText foodText;
TextView DebugText;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -24,7 +23,6 @@ protected void onCreate(Bundle savedInstanceState) {

searchButton = findViewById(R.id.SearchButton);
foodText = findViewById(R.id.FoodText);
DebugText = findViewById(R.id.DebugText);


foodText.setFilters(new InputFilter[] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.mgaciarz.myrecipes;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
Expand Down Expand Up @@ -30,6 +32,7 @@
public class ResultActivity extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -40,11 +43,11 @@ protected void onCreate(Bundle savedInstanceState) {
if (extras != null) {
String json = extras.getString("searchValues");

List<Recipe> recipes = new JSONObjectWrapper().getRecipes(json);
final List<Recipe> recipes = new JSONObjectWrapper().getRecipes(json);

ListView list;

list = (ListView) findViewById(R.id.list);
list = findViewById(R.id.list);

ListAdapter adapter = new ListAdapter(this, recipes);

Expand All @@ -60,9 +63,18 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
// Toast.makeText(getApplicationContext(),
// "Click ListItem Number " + position, Toast.LENGTH_SHORT)
// .show();


//start new activity for seeing one dish and pass recipe to it
Intent i = new Intent(getApplicationContext(),SingleDishActivity.class);

i.putExtra("recipe", recipes.get(position));
i.putExtra("position", position);

getApplicationContext().startActivity(i);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.mgaciarz.myrecipes;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import models.Recipe;

public class SingleDishActivity extends AppCompatActivity {

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

ImageView dishImage = findViewById(R.id.singleDishImage);
TextView dishDesc = findViewById(R.id.singleDishDescription);


Bundle extras = getIntent().getExtras();
if (extras != null) {

Recipe recipe = extras.getParcelable("recipe");
int position = extras.getInt("position");

setTitle(recipe.getLabel());

Picasso.get().load(recipe.getImageURL()).into(dishImage);
dishDesc.setText(recipe.getInstructionURL());

}


}



}
46 changes: 42 additions & 4 deletions app/src/main/java/models/Recipe.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package models;


import android.os.Parcel;
import android.os.Parcelable;

import java.util.HashMap;
import java.util.List;

/**
* Created by mgaciarz on 2018-06-07.
*/

public class Recipe {
public class Recipe implements Parcelable {

private String label;

private String imageURL;

private String instructionURL;

private long id;

public long getId() {
Expand All @@ -31,8 +37,6 @@ public Recipe(String label, String imageURL, String instructionURL, long id) {

}

private String instructionURL;

public String getLabel() {
return label;
}
Expand All @@ -56,4 +60,38 @@ public String getInstructionURL() {
public void setInstructionURL(String instructionURL) {
this.instructionURL = instructionURL;
}
}


protected Recipe(Parcel in) {
label = in.readString();
imageURL = in.readString();
instructionURL = in.readString();
id = in.readLong();
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(label);
dest.writeString(imageURL);
dest.writeString(instructionURL);
dest.writeLong(id);
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<Recipe> CREATOR = new Parcelable.Creator<Recipe>() {
@Override
public Recipe createFromParcel(Parcel in) {
return new Recipe(in);
}

@Override
public Recipe[] newArray(int size) {
return new Recipe[size];
}
};
}
15 changes: 1 addition & 14 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,11 @@
android:layout_height="match_parent"
tools:context="com.example.mgaciarz.myrecipes.MainActivity">

<TextView
android:id="@+id/DebugText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="debug json"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.682"
tools:layout_constraintRight_creator="1"
tools:layout_constraintLeft_creator="1" />

<TextView
android:id="@+id/MainText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What would you like to prepare"
android:text="What would you like to prepare?"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/res/layout/activity_single_dish.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
>
<ImageView
android:id="@+id/singleDishImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
/>

</RelativeLayout>
<TextView
android:id="@+id/singleDishDescription"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:autoLink="web"
android:gravity="center"
/>

</LinearLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="title_activity_single_dish">SingleDishActivity</string>
</resources>

0 comments on commit 092061d

Please sign in to comment.