Search in sources :

Example 1 with RecipeInformation

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

the class RecipeDetailActivity method onStart.

/*
    private void textViewClicked() {
        ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.my_switcher);
        View currentView = switcher.getCurrentView();
        //switcher.showNext(); //or switcher.showPrevious();
        TextView viewText = (TextView) switcher.findViewById(R.id.directionView);
        EditText editText = (EditText) findViewById(R.id.hidden_edit_view);

        if (currentView instanceof EditText) {
            //Save steps and prepare next view (Text view)
            directionsForCooking = editText.getText().toString().split("\n");
            StringBuilder sb = new StringBuilder();
            int count = 1;
            for (String s : directionsForCooking) {
                sb.append(count);
                sb.append(":  ");
                sb.append(s);
                sb.append("\n");

                directionsForCooking[count - 1] = s;
                count++;
            }
            steps = sb.toString();
            viewText.setText(steps);
        }
        else if (currentView instanceof TextView) {
            // Prepare next view (Edit view)
            String tmpSteps = implode("\n", directionsForCooking);
            editText.setText(tmpSteps);
            editText.setFocusableInTouchMode(true);
            editText.requestFocus();
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        }
        switcher.showNext();

    }

    private static String implode(String separator, String... data) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < data.length - 1; i++) {
            //data.length - 1 => to not add separator at the end
            if (!data[i].matches(" *")) {//empty string are ""; " "; "  "; and so on
                sb.append(data[i]);
                sb.append(separator);
            }
        }
        sb.append(data[data.length - 1].trim());
        return sb.toString();
    }
*/
@Override
protected void onStart() {
    super.onStart();
    Intent intent = getIntent();
    final String recipeID = intent.getStringExtra("recipe");
    RecipeInformation result = intent.getParcelableExtra("recipeDetail");
    if (result != null) {
        recipeDetail = result;
        setRecipeInfo();
    } else if (// returns true if internet available
    NetworkHelper.hasNetworkAccess(RecipeDetailActivity.this)) {
        // register to listen the data
        RequestMethod requestPackage = new RequestMethod();
        requestPackage.setEndPoint(urlString + recipeID);
        // or requestPackage.setMethod("POST");
        requestPackage.setMethod("GET");
        new AsyncTask<RequestMethod, Integer, Long>() {

            String resp = "";

            @Override
            protected Long doInBackground(RequestMethod... params) {
                for (RequestMethod r : params) {
                    try {
                        HttpConnection http = new HttpConnection();
                        resp = http.downloadFromFeed(r);
                        GsonBuilder builder = new GsonBuilder();
                        Gson gson = builder.create();
                        Type type;
                        type = new TypeToken<RecipeInformation>() {
                        }.getType();
                        recipeDetail = gson.fromJson(resp, type);
                        Log.d(TAG, recipeDetail.toString());
                    } catch (Exception e) {
                        // e.printStackTrace();
                        return -1L;
                    }
                    Log.d(TAG, resp);
                }
                return 1L;
            }

            @Override
            protected void onPostExecute(Long aLong) {
                super.onPostExecute(aLong);
                setRecipeInfo();
            }
        }.execute(requestPackage);
    } else {
        // Toast.makeText(RecipeListActivity.this,"No Internet Connection",Toast.LENGTH_LONG).show();
        Log.d(TAG, "No Internet Connection");
    }
}
Also used : Type(java.lang.reflect.Type) HttpConnection(com.se491.chef_ly.http.HttpConnection) GsonBuilder(com.google.gson.GsonBuilder) RequestMethod(com.se491.chef_ly.http.RequestMethod) TypeToken(com.google.gson.reflect.TypeToken) AsyncTask(android.os.AsyncTask) Gson(com.google.gson.Gson) Intent(android.content.Intent) RecipeInformation(com.se491.chef_ly.model.RecipeInformation) IOException(java.io.IOException)

Example 2 with RecipeInformation

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

the class RecipeListActivity method onLoadFinished.

// LoaderManager callback method
@Override
public void onLoadFinished(Loader<RecipeList> loader, RecipeList data) {
    int id = loader.getId();
    if (id == FAVORTIESID) {
        favoriteRecipes = data;
        for (RecipeInformation r : favoriteRecipes) {
            // favorites.add(r.getId());
            r.setFavorite(true);
        }
        favs.updateListAdapter(favoriteRecipes);
    } else if (id == queryString.hashCode()) {
        spinner.setVisibility(View.GONE);
        Log.d(TAG, " Recipe Search -> " + data.size());
        if (data.size() > 0) {
            // Collect all recipes so list can be redisplayed when search is closed
            server.setList(data, true);
        } else {
            Log.d(TAG, "Error - No recipes loaded from server");
            Toast.makeText(this, "Sorry we couldn't find any recipes", Toast.LENGTH_SHORT).show();
        // TODO handle case where no recipes are retrieved from server
        }
    }
    Log.d(TAG, "OnLoadFinished " + loader.getId());
}
Also used : Paint(android.graphics.Paint) RecipeInformation(com.se491.chef_ly.model.RecipeInformation)

Example 3 with RecipeInformation

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

the class ListViewFragment method setList.

public void setList(RecipeList newList, final boolean goToTop) {
    list.clear();
    for (RecipeInformation r : newList) {
        list.add(r);
    }
    // If the list view is null just wait 100 ms to give it time to inflate
    if (listView == null) {
        Handler h = new Handler();
        h.postDelayed(new Runnable() {

            @Override
            public void run() {
                ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
                if (goToTop) {
                    listView.setSelectionAfterHeaderView();
                }
            }
        }, 100);
    } else {
        ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
        if (goToTop) {
            listView.setSelectionAfterHeaderView();
        }
    }
}
Also used : Handler(android.os.Handler) BaseAdapter(android.widget.BaseAdapter) RecipeInformation(com.se491.chef_ly.model.RecipeInformation)

Example 4 with RecipeInformation

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

the class ListViewFragment method updateListAdapter.

public void updateListAdapter(RecipeList newList) {
    if (list == null) {
        list = new RecipeList();
    }
    for (RecipeInformation r : newList) {
        list.add(r);
    }
    if (listView != null) {
        ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
        Log.d(TAG + "/" + title, "ListView updated " + title + " " + listView.getAdapter().getCount() + " list " + list.size());
    // listView.setVisibility(View.VISIBLE);
    }
}
Also used : RecipeList(com.se491.chef_ly.model.RecipeList) BaseAdapter(android.widget.BaseAdapter) RecipeInformation(com.se491.chef_ly.model.RecipeInformation)

Example 5 with RecipeInformation

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

the class ListViewFragment method removeFavorite.

public void removeFavorite(RecipeInformation r) {
    int index = list.indexOf(r);
    if (index > 0) {
        list.get(index).setFavorite(false);
    } else {
        Log.d(TAG + "/" + title, "Remove Favorite index -> " + index);
        for (RecipeInformation rec : list) {
            Log.d(TAG + "/" + title, "ID -> " + rec.getId());
        }
    }
    ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
}
Also used : BaseAdapter(android.widget.BaseAdapter) RecipeInformation(com.se491.chef_ly.model.RecipeInformation)

Aggregations

RecipeInformation (com.se491.chef_ly.model.RecipeInformation)9 BaseAdapter (android.widget.BaseAdapter)4 Intent (android.content.Intent)2 Handler (android.os.Handler)2 Gson (com.google.gson.Gson)2 RequestMethod (com.se491.chef_ly.http.RequestMethod)2 Cursor (android.database.Cursor)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 Paint (android.graphics.Paint)1 AsyncTask (android.os.AsyncTask)1 Bundle (android.os.Bundle)1 LoaderManager (android.support.v4.app.LoaderManager)1 View (android.view.View)1 AbsListView (android.widget.AbsListView)1 AdapterView (android.widget.AdapterView)1 ImageView (android.widget.ImageView)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 GsonBuilder (com.google.gson.GsonBuilder)1 TypeToken (com.google.gson.reflect.TypeToken)1