Search in sources :

Example 1 with ExtendedIngredient

use of com.se491.chef_ly.model.ExtendedIngredient in project chefly_android by chef-ly.

the class RecipeDetailActivity method setRecipeInfo.

private void setRecipeInfo() {
    final Context c = getApplicationContext();
    TextView author = (TextView) findViewById(R.id.recipeAuthor);
    // TextView description = (TextView) findViewById(R.id.recipeDescription);
    TextView serves = (TextView) findViewById(R.id.recipeServings);
    TextView time = (TextView) findViewById(R.id.recipeTime);
    String recipeName;
    Step[] directions;
    if (recipeDetail == null) {
        recipeTitle.setText(R.string.recipeNotFound);
    } else {
        recipeName = recipeDetail.getTitle();
        recipeTitle.setText(recipeName);
        author.setText(recipeDetail.getCreditText());
        int servings = recipeDetail.getServings();
        Log.d(TAG, "Serves -> " + servings);
        if (servings <= 0) {
            serves.setText(R.string.unknown);
        } else {
            serves.setText(String.valueOf(servings));
        }
        int cookTime = recipeDetail.getReadyInMinutes();
        int hour = 0;
        while (cookTime >= 60) {
            hour++;
            cookTime = cookTime - 60;
        }
        String newTime;
        if (hour < 2) {
            newTime = (hour != 0) ? hour + " hr " : "";
        } else {
            newTime = (hour != 0) ? hour + " hrs " : "";
        }
        newTime += ((cookTime > 0) ? cookTime + " min" : "");
        if (newTime.isEmpty()) {
            time.setText(R.string.unknown);
        } else {
            time.setText(newTime);
        }
        new AsyncTask<RequestMethod, Integer, Long>() {

            Bitmap image = null;

            @Override
            protected Long doInBackground(RequestMethod... params) {
                try {
                    URL url = new URL(recipeDetail.getImage());
                    image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                } catch (IOException e) {
                    Log.d(TAG, "IOException on load image");
                    Log.d(TAG, e.getMessage());
                }
                return 1L;
            }

            @Override
            protected void onPostExecute(Long aLong) {
                if (image != null) {
                    imageView.setImageBitmap(image);
                }
            }
        }.execute();
        ingredients = recipeDetail.getExtendedIngredients();
        if (recipeDetail.getAnalyzedInstructions().length > 0) {
            directions = recipeDetail.getAnalyzedInstructions()[0].getSteps();
        } else {
            directions = new Step[] { new Step(recipeDetail.getInstructions()) };
        }
        //            checkBoxes = new CheckBox[ingredients.length];
        //            int states[][] = {{android.R.attr.state_checked}, {}};
        TableLayout table = (TableLayout) findViewById(R.id.ingredientGroup);
        TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
        int leftMargin = 30;
        int topMargin = 1;
        int rightMargin = 30;
        int bottomMargin = 1;
        tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
        int color1 = getColor(c, R.color.table_color1);
        int color2 = getColor(c, R.color.table_color2);
        int count = 0;
        for (ExtendedIngredient s : ingredients) {
            final TableRow row = new TableRow(c);
            row.setLayoutParams(tableRowParams);
            row.setBackgroundColor(count % 2 == 0 ? color1 : color2);
            row.setPadding(10, 5, 10, 5);
            TextView text = new TextView(c);
            text.setText(s.getOriginalString());
            text.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
            text.setTextSize((getResources().getDimension(R.dimen.text_small) / getResources().getDisplayMetrics().density));
            text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
            text.setPadding(10, 5, 10, 5);
            text.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    ImageView image = (ImageView) row.getChildAt(1);
                    String text = String.valueOf(((TextView) row.getChildAt(0)).getText());
                    if (row.getChildAt(1).getVisibility() == View.GONE) {
                        image.setVisibility(View.VISIBLE);
                        shoppingList.add(new ShoppingListItem(text, false));
                        Toast.makeText(c, text + " added to shopping list", Toast.LENGTH_SHORT).show();
                    } else {
                        image.setVisibility(View.GONE);
                        shoppingList.remove(new ShoppingListItem(text, false));
                        Toast.makeText(c, text + " removed from shopping list", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            row.addView(text);
            ImageView check = new ImageView(c);
            check.setImageResource(R.drawable.shoppinglist);
            check.setLayoutParams(new TableRow.LayoutParams(60, 60, 0.1f));
            if (shoppingList.contains(new ShoppingListItem(s.getOriginalString(), false))) {
                check.setVisibility(View.VISIBLE);
            } else {
                check.setVisibility(View.GONE);
            }
            row.addView(check);
            table.addView(row);
            count++;
        }
        //
        final TableLayout tableDirec = (TableLayout) findViewById(R.id.directionGroup);
        tableDirec.setColumnShrinkable(0, true);
        tableDirec.setVisibility(View.GONE);
        directionsForCooking = new String[directions.length];
        count = 1;
        for (Step s : directions) {
            String step = count + ")  " + s.getStep();
            TableRow row = new TableRow(c);
            row.setLayoutParams(tableRowParams);
            row.setBackgroundColor(count % 2 == 0 ? color1 : color2);
            TextView text = new TextView(c);
            text.setText(step);
            text.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
            text.setTextSize((getResources().getDimension(R.dimen.text_small) / getResources().getDisplayMetrics().density));
            text.setPadding(15, 1, 15, 1);
            text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
            row.addView(text);
            tableDirec.addView(row);
            directionsForCooking[count - 1] = s.getStep();
            count++;
        }
        final ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
        final ImageButton dropdown = (ImageButton) findViewById(R.id.directionsDropdown);
        final RotateAnimation rotatedown = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotatedown.setDuration(250);
        rotatedown.setFillAfter(true);
        final RotateAnimation rotateup = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateup.setDuration(250);
        rotateup.setFillAfter(true);
        dropdown.setOnClickListener(new View.OnClickListener() {

            private boolean isClicked = false;

            @Override
            public void onClick(View v) {
                if (!isClicked) {
                    tableDirec.setVisibility(View.VISIBLE);
                    dropdown.startAnimation(rotatedown);
                    isClicked = true;
                    sv.post(new Runnable() {

                        @Override
                        public void run() {
                            //sv.fullScroll(ScrollView.FOCUS_DOWN);
                            sv.smoothScrollBy(0, 500);
                        }
                    });
                } else {
                    dropdown.startAnimation(rotateup);
                    tableDirec.setVisibility(View.GONE);
                    isClicked = false;
                //sv.fullScroll(ScrollView.FOCUS_DOWN);
                }
            }
        });
    }
}
Also used : RequestMethod(com.se491.chef_ly.http.RequestMethod) Step(com.se491.chef_ly.model.Step) ExtendedIngredient(com.se491.chef_ly.model.ExtendedIngredient) URL(java.net.URL) RotateAnimation(android.view.animation.RotateAnimation) Bitmap(android.graphics.Bitmap) ImageButton(android.widget.ImageButton) TextView(android.widget.TextView) ImageView(android.widget.ImageView) TableLayout(android.widget.TableLayout) Context(android.content.Context) IOException(java.io.IOException) ShoppingListItem(com.se491.chef_ly.model.ShoppingListItem) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) ScrollView(android.widget.ScrollView) TableRow(android.widget.TableRow)

Example 2 with ExtendedIngredient

use of com.se491.chef_ly.model.ExtendedIngredient in project chefly_android by chef-ly.

the class RecipeDetailActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipe_detail);
    Button getCookingBtn;
    //EditText editTextDesciption;
    recipeTitle = (TextView) findViewById(R.id.recipeName);
    imageView = (ImageView) findViewById(R.id.image);
    //editTextDesciption = (EditText) findViewById(R.id.hidden_edit_view);
    getCookingBtn = (Button) findViewById(R.id.getCookingBtn);
    getCookingBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent cookingIntent = new Intent(RecipeDetailActivity.this, GetCookingActivity.class);
            if (directionsForCooking != null) {
                cookingIntent.putExtra("directions", directionsForCooking);
                ArrayList<String> ingredList = new ArrayList<String>();
                for (ExtendedIngredient ingre : ingredients) {
                    ingredList.add(ingre.getOriginalString());
                }
                cookingIntent.putExtra("ingredients", ingredList);
                startActivity(cookingIntent);
                finish();
            } else {
                Toast.makeText(RecipeDetailActivity.this, "Could not find recipe directions", Toast.LENGTH_SHORT).show();
            }
        }
    });
    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    shoppingList = db.getShoppingList();
}
Also used : ImageButton(android.widget.ImageButton) Button(android.widget.Button) DatabaseHandler(com.se491.chef_ly.Databases.DatabaseHandler) ArrayList(java.util.ArrayList) Intent(android.content.Intent) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) ExtendedIngredient(com.se491.chef_ly.model.ExtendedIngredient)

Aggregations

View (android.view.View)2 ImageButton (android.widget.ImageButton)2 ImageView (android.widget.ImageView)2 ScrollView (android.widget.ScrollView)2 TextView (android.widget.TextView)2 ExtendedIngredient (com.se491.chef_ly.model.ExtendedIngredient)2 Context (android.content.Context)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 RotateAnimation (android.view.animation.RotateAnimation)1 Button (android.widget.Button)1 TableLayout (android.widget.TableLayout)1 TableRow (android.widget.TableRow)1 DatabaseHandler (com.se491.chef_ly.Databases.DatabaseHandler)1 RequestMethod (com.se491.chef_ly.http.RequestMethod)1 ShoppingListItem (com.se491.chef_ly.model.ShoppingListItem)1 Step (com.se491.chef_ly.model.Step)1 IOException (java.io.IOException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1