From 092061d17aaa03da8886d3e3df4d62352d059176 Mon Sep 17 00:00:00 2001 From: Maciej Gaciarz Date: Mon, 16 Jul 2018 17:23:49 +0200 Subject: [PATCH] Added view for single dish and displaying the image, added stripping not needed quotes from json --- .idea/caches/build_file_checksums.ser | Bin 540 -> 540 bytes app/src/main/AndroidManifest.xml | 9 ++-- .../mgaciarz/myrecipes/JSONObjectWrapper.java | 12 +++-- .../mgaciarz/myrecipes/ListAdapter.java | 9 ++-- .../mgaciarz/myrecipes/MainActivity.java | 2 - .../mgaciarz/myrecipes/ResultActivity.java | 22 +++++++-- .../myrecipes/SingleDishActivity.java | 41 ++++++++++++++++ app/src/main/java/models/Recipe.java | 46 ++++++++++++++++-- app/src/main/res/layout/activity_main.xml | 15 +----- .../main/res/layout/activity_single_dish.xml | 29 +++++++++++ app/src/main/res/values/strings.xml | 3 ++ 11 files changed, 149 insertions(+), 39 deletions(-) create mode 100644 app/src/main/java/com/example/mgaciarz/myrecipes/SingleDishActivity.java create mode 100644 app/src/main/res/layout/activity_single_dish.xml create mode 100644 app/src/main/res/values/strings.xml diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index b3cabb1bbb06640acbba1163c01ceda20b937c8a..ede84a0125dffb32e7749abe7a831d0ea09baff6 100644 GIT binary patch delta 15 XcmbQkGKXct43@>o-vc+!X*gEhG%x}HEqMjY diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ab8a0dc..15c139e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,7 +12,6 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.AppCompat"> - @@ -20,10 +19,10 @@ - - - - + + \ No newline at end of file diff --git a/app/src/main/java/com/example/mgaciarz/myrecipes/JSONObjectWrapper.java b/app/src/main/java/com/example/mgaciarz/myrecipes/JSONObjectWrapper.java index da24afc..69ef329 100644 --- a/app/src/main/java/com/example/mgaciarz/myrecipes/JSONObjectWrapper.java +++ b/app/src/main/java/com/example/mgaciarz/myrecipes/JSONObjectWrapper.java @@ -12,6 +12,10 @@ public class JSONObjectWrapper { + private String stripQuotes(String toStrip){ + return toStrip.replaceAll("^\"|\"$", ""); + } + public List getRecipes(String JSON){ ArrayList recipes = new ArrayList<>(); @@ -33,9 +37,11 @@ public List 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); diff --git a/app/src/main/java/com/example/mgaciarz/myrecipes/ListAdapter.java b/app/src/main/java/com/example/mgaciarz/myrecipes/ListAdapter.java index dbb63c9..ffd3a16 100644 --- a/app/src/main/java/com/example/mgaciarz/myrecipes/ListAdapter.java +++ b/app/src/main/java/com/example/mgaciarz/myrecipes/ListAdapter.java @@ -21,7 +21,7 @@ public class ListAdapter extends BaseAdapter { private Activity activity; private static LayoutInflater inflater=null; - List recipes; + private List recipes; @Override public int getCount() { @@ -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; diff --git a/app/src/main/java/com/example/mgaciarz/myrecipes/MainActivity.java b/app/src/main/java/com/example/mgaciarz/myrecipes/MainActivity.java index 4f3af40..3b20929 100644 --- a/app/src/main/java/com/example/mgaciarz/myrecipes/MainActivity.java +++ b/app/src/main/java/com/example/mgaciarz/myrecipes/MainActivity.java @@ -14,7 +14,6 @@ public class MainActivity extends AppCompatActivity { Button searchButton; EditText foodText; - TextView DebugText; @Override protected void onCreate(Bundle savedInstanceState) { @@ -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[] { diff --git a/app/src/main/java/com/example/mgaciarz/myrecipes/ResultActivity.java b/app/src/main/java/com/example/mgaciarz/myrecipes/ResultActivity.java index ccb7bf4..75515e1 100644 --- a/app/src/main/java/com/example/mgaciarz/myrecipes/ResultActivity.java +++ b/app/src/main/java/com/example/mgaciarz/myrecipes/ResultActivity.java @@ -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; @@ -30,6 +32,7 @@ public class ResultActivity extends AppCompatActivity { + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -40,11 +43,11 @@ protected void onCreate(Bundle savedInstanceState) { if (extras != null) { String json = extras.getString("searchValues"); - List recipes = new JSONObjectWrapper().getRecipes(json); + final List recipes = new JSONObjectWrapper().getRecipes(json); ListView list; - list = (ListView) findViewById(R.id.list); + list = findViewById(R.id.list); ListAdapter adapter = new ListAdapter(this, recipes); @@ -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); } }); } diff --git a/app/src/main/java/com/example/mgaciarz/myrecipes/SingleDishActivity.java b/app/src/main/java/com/example/mgaciarz/myrecipes/SingleDishActivity.java new file mode 100644 index 0000000..e70e824 --- /dev/null +++ b/app/src/main/java/com/example/mgaciarz/myrecipes/SingleDishActivity.java @@ -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()); + + } + + + } + + + +} diff --git a/app/src/main/java/models/Recipe.java b/app/src/main/java/models/Recipe.java index 055c7b2..01e94bc 100644 --- a/app/src/main/java/models/Recipe.java +++ b/app/src/main/java/models/Recipe.java @@ -1,5 +1,9 @@ package models; + +import android.os.Parcel; +import android.os.Parcelable; + import java.util.HashMap; import java.util.List; @@ -7,12 +11,14 @@ * 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() { @@ -31,8 +37,6 @@ public Recipe(String label, String imageURL, String instructionURL, long id) { } - private String instructionURL; - public String getLabel() { return label; } @@ -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 CREATOR = new Parcelable.Creator() { + @Override + public Recipe createFromParcel(Parcel in) { + return new Recipe(in); + } + + @Override + public Recipe[] newArray(int size) { + return new Recipe[size]; + } + }; +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 2aa3e7b..3cd11ba 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,24 +6,11 @@ android:layout_height="match_parent" tools:context="com.example.mgaciarz.myrecipes.MainActivity"> - - + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..85a5b66 --- /dev/null +++ b/app/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + SingleDishActivity +