Search in sources :

Example 1 with ShoppingListItem

use of com.se491.chef_ly.model.ShoppingListItem 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 ShoppingListItem

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

the class RecipeDetailActivity method onDestroy.

@Override
protected void onDestroy() {
    super.onDestroy();
    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    // Remove an item from shopping list if removed by clicking on ingredient
    ArrayList<ShoppingListItem> existing = db.getShoppingList();
    ArrayList<ShoppingListItem> toDelete = new ArrayList<>();
    for (ShoppingListItem item : existing) {
        if (!shoppingList.contains(item)) {
            toDelete.add(item);
        }
    }
    db.deleteItemFromShoppingList(toDelete);
    // Add new items to shopping list
    for (ShoppingListItem item : shoppingList) {
        if (item.getId() == -9999) {
            db.addItemToShoppingList(item.getName(), item.isPurchased());
        }
    }
    if (BuildConfig.DEBUG) {
        RefWatcher refWatcher = CheflyApplication.getRefWatcher(this);
        refWatcher.watch(this);
    }
}
Also used : DatabaseHandler(com.se491.chef_ly.Databases.DatabaseHandler) ArrayList(java.util.ArrayList) RefWatcher(com.squareup.leakcanary.RefWatcher) ShoppingListItem(com.se491.chef_ly.model.ShoppingListItem)

Example 3 with ShoppingListItem

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

the class ShoppingListActivity method onListItemClick.

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    TextView name = (TextView) v.findViewById(R.id.name);
    ShoppingListItem item = shoppingList.get(position);
    if (item.isPurchased()) {
        item.setPurchased(false);
        name.setPaintFlags(name.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
    } else {
        item.setPurchased(true);
        name.setPaintFlags(name.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    }
    ((BaseAdapter) getListAdapter()).notifyDataSetChanged();
}
Also used : TextView(android.widget.TextView) ShoppingListItem(com.se491.chef_ly.model.ShoppingListItem) BaseAdapter(android.widget.BaseAdapter)

Example 4 with ShoppingListItem

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

the class ShoppingListActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shopping_list);
    setListAdapter(new myAdapter(this.getApplicationContext()));
    final DatabaseHandler handler = new DatabaseHandler(this.getApplicationContext());
    Button deletePurchasedBtn;
    deletePurchasedBtn = (Button) findViewById(R.id.clearPurchasedBtn);
    deletePurchasedBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ArrayList<ShoppingListItem> toDelete = new ArrayList<>();
            for (ShoppingListItem item : shoppingList) {
                if (item.isPurchased()) {
                    toDelete.add(item);
                }
            }
            if (toDelete.size() > 0) {
                for (ShoppingListItem item : toDelete) {
                    shoppingList.remove(item);
                }
                DatabaseHandler h = new DatabaseHandler(getApplicationContext());
                h.deleteItemFromShoppingList(toDelete);
                ((BaseAdapter) getListAdapter()).notifyDataSetChanged();
            }
        }
    });
    final Button add = (Button) findViewById(R.id.addBtn);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final EditText newItem = new EditText(getApplicationContext());
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 150);
            params.addRule(RelativeLayout.BELOW, add.getId());
            params.setMargins(20, 2, 20, 2);
            newItem.setLayoutParams(params);
            newItem.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
            newItem.setHint("new grocery list item");
            newItem.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.color.white));
            newItem.setInputType(InputType.TYPE_CLASS_TEXT);
            newItem.setImeOptions(EditorInfo.IME_ACTION_DONE);
            newItem.setMaxLines(1);
            RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_shopping_list);
            layout.addView(newItem);
            newItem.setOnFocusChangeListener(new View.OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    String text = newItem.getText().toString();
                    if (!hasFocus && text.length() > 0) {
                        addText(text);
                        newItem.setText("");
                        newItem.setVisibility(View.GONE);
                    }
                }
            });
            newItem.setOnEditorActionListener(new TextView.OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                        String text = newItem.getText().toString();
                        if (!text.isEmpty()) {
                            addText(text);
                            newItem.setText("");
                        }
                        newItem.setVisibility(View.GONE);
                        return true;
                    }
                    return false;
                }
            });
        }
    });
    shoppingList = handler.getShoppingList();
}
Also used : EditText(android.widget.EditText) ArrayList(java.util.ArrayList) InputMethodManager(android.view.inputmethod.InputMethodManager) ShoppingListItem(com.se491.chef_ly.model.ShoppingListItem) TextView(android.widget.TextView) View(android.view.View) ListView(android.widget.ListView) KeyEvent(android.view.KeyEvent) Button(android.widget.Button) DatabaseHandler(com.se491.chef_ly.Databases.DatabaseHandler) RelativeLayout(android.widget.RelativeLayout) TextView(android.widget.TextView)

Example 5 with ShoppingListItem

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

the class DatabaseHandler method deleteItemFromShoppingList.

public void deleteItemFromShoppingList(ArrayList<ShoppingListItem> items) {
    SQLiteDatabase db = null;
    try {
        db = this.getWritableDatabase();
        for (ShoppingListItem item : items) {
            long result = db.delete(ShoppingList.TABLE_LIST_ITEMS, ShoppingList.COLUMN_LIST_ID + " = " + item.getId(), null);
            Log.d(TAG, "id -> " + item.getId() + " result ->" + result);
        }
    } finally {
        if (db != null)
            db.close();
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ShoppingListItem(com.se491.chef_ly.model.ShoppingListItem)

Aggregations

ShoppingListItem (com.se491.chef_ly.model.ShoppingListItem)6 TextView (android.widget.TextView)3 ArrayList (java.util.ArrayList)3 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 View (android.view.View)2 DatabaseHandler (com.se491.chef_ly.Databases.DatabaseHandler)2 Context (android.content.Context)1 Cursor (android.database.Cursor)1 Bitmap (android.graphics.Bitmap)1 KeyEvent (android.view.KeyEvent)1 RotateAnimation (android.view.animation.RotateAnimation)1 InputMethodManager (android.view.inputmethod.InputMethodManager)1 BaseAdapter (android.widget.BaseAdapter)1 Button (android.widget.Button)1 EditText (android.widget.EditText)1 ImageButton (android.widget.ImageButton)1 ImageView (android.widget.ImageView)1 ListView (android.widget.ListView)1 RelativeLayout (android.widget.RelativeLayout)1 ScrollView (android.widget.ScrollView)1