Search in sources :

Example 6 with RequestMethod

use of com.se491.chef_ly.http.RequestMethod in project chefly_android by chef-ly.

the class MainActivity method startRecipeLoader.

private void startRecipeLoader() {
    // Get recipes from server
    if (//returns true if internet available
    NetworkHelper.hasNetworkAccess(MainActivity.this)) {
        //Start AsyncTaskLoader to get recipes from server
        RequestMethod requestPackage = new RequestMethod();
        requestPackage.setEndPoint(urlString);
        //  or requestPackage.setMethod("POST");
        requestPackage.setMethod("GET");
        //  serverConnection();
        Bundle bundle = new Bundle();
        bundle.putParcelable("requestPackage", requestPackage);
        getSupportLoaderManager().initLoader(RECIPELISTID, bundle, this).forceLoad();
        splashHandler = new Handler();
        splashHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                //ViewGroup group = (ViewGroup) findViewById(R.id.activity_main);
                setContentView(R.layout.activity_main);
            }
        }, 5000);
    } else {
        //Toast.makeText(RecipeListActivity.this,"No Internet Connection",Toast.LENGTH_LONG).show();
        Log.d(TAG, "No Internet Connection");
    }
}
Also used : RequestMethod(com.se491.chef_ly.http.RequestMethod) Bundle(android.os.Bundle) Handler(android.os.Handler)

Example 7 with RequestMethod

use of com.se491.chef_ly.http.RequestMethod in project chefly_android by chef-ly.

the class MyService method onHandleIntent.

//called automaticly when server starts, receives data as an argument
@Override
protected void onHandleIntent(Intent intent) {
    RequestMethod requestPackage = intent.getParcelableExtra(REQUEST_PACKAGE);
    String sender = intent.getStringExtra("Tag");
    Log.d(TAG, "Sender -> " + sender);
    Log.d(TAG, "Request Package Endpoint-> " + requestPackage.getEndpoint());
    String response;
    try {
        HttpConnection http = new HttpConnection();
        response = http.downloadFromFeed(requestPackage);
    } catch (IOException e) {
        //e.printStackTrace();
        return;
    }
    Log.d(TAG, "Response -> " + response);
    GsonBuilder builder = new GsonBuilder();
    //builder.registerTypeAdapter(Uri.class, new JsonDeserializer<Uri>() {
    //    @Override
    //    public Uri deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    //        return Uri.parse(json.toString());
    //    }
    //});
    Gson gson = builder.create();
    RecipeList dataItems;
    Type type = new TypeToken<RecipeList>() {
    }.getType();
    dataItems = gson.fromJson(response, type);
    Intent messageIntent = new Intent(MY_SERVICE_MESSAGE);
    messageIntent.putExtra(MY_SERVICE_RESPONSE_STRING, response);
    // messageIntent.putExtra(MY_SERVICE_PAYLOAD, "Service all done!"); //pass data back, set key value and message
    if (dataItems != null) {
        //pass back the data
        messageIntent.putExtra(MY_SERVICE_PAYLOAD, dataItems);
    } else {
        // unidentified sender - send empty parcel back
        messageIntent.putExtra(MY_SERVICE_PAYLOAD, new Parcelable() {

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

            @Override
            public void writeToParcel(Parcel dest, int flags) {
            }
        });
    //pass back the data
    }
    LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getApplicationContext());
    manager.sendBroadcast(messageIntent);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Parcel(android.os.Parcel) Gson(com.google.gson.Gson) Intent(android.content.Intent) Parcelable(android.os.Parcelable) IOException(java.io.IOException) LocalBroadcastManager(android.support.v4.content.LocalBroadcastManager) Type(java.lang.reflect.Type) RecipeList(com.se491.chef_ly.model.RecipeList)

Example 8 with RequestMethod

use of com.se491.chef_ly.http.RequestMethod in project chefly_android by chef-ly.

the class RecipeListActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipe_list);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    //Initialize recipe lists
    serverRecipes = new RecipeList();
    favoriteRecipes = new RecipeList();
    //Start AsyncTaskLoader to get FavoriteRecipes
    Credentials cred = CredentialsManager.getCredentials(getApplicationContext());
    String t = cred.getAccessToken();
    Log.d(TAG, "Token -> " + t);
    if (t != null) {
        RequestMethod requestPackageFavs = new RequestMethod();
        requestPackageFavs.setEndPoint(urlFavsString);
        requestPackageFavs.setMethod("GET");
        requestPackageFavs.setHeader("Authorization", "Bearer " + t);
        Bundle bundlefavs = new Bundle();
        bundlefavs.putParcelable("requestPackage", requestPackageFavs);
        getSupportLoaderManager().initLoader(FAVORTIESID, bundlefavs, this).forceLoad();
    } else {
        Toast.makeText(this, "Could not retrieve favorites, token is null", Toast.LENGTH_SHORT).show();
    }
    // PageViewer
    pager = (ViewPager) findViewById(R.id.viewpager);
    Bundle serv = new Bundle();
    serv.putString("title", "Recipes");
    serv.putString("pageNum", "1");
    serv.putString("search", "");
    server = new ListViewFragment();
    server.setArguments(serv);
    Bundle f = new Bundle();
    f.putString("title", "Favorites");
    f.putString("pageNum", "2");
    f.putString("search", "");
    favs = new ListViewFragment();
    favs.setArguments(f);
    ListViewFragment[] frags = { server, favs };
    pager.setAdapter(new RecipeListPagerAdapter(getSupportFragmentManager(), frags));
    pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            //Log.d(TAG, "Position -> " + position);
            if (position == 1) {
                favoritesHeader.setPaintFlags(favoritesHeader.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                recipesHeader.setPaintFlags(0);
            // ingredientsHeader.setPaintFlags(0);
            } else {
                recipesHeader.setPaintFlags(recipesHeader.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                favoritesHeader.setPaintFlags(0);
            // ingredientsHeader.setPaintFlags(0);
            }
        }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
    // Header links
    // ingredientsHeader = (TextView) findViewById(R.id.ingredientsHeader);
    favoritesHeader = (TextView) findViewById(R.id.favortiesHeader);
    recipesHeader = (TextView) findViewById(R.id.recipesHeader);
    View.OnClickListener headerListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (v.getId() == favoritesHeader.getId()) {
                favoritesHeader.setPaintFlags(favoritesHeader.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                recipesHeader.setPaintFlags(0);
                //  ingredientsHeader.setPaintFlags(0);
                pager.setCurrentItem(1);
            } else {
                recipesHeader.setPaintFlags(recipesHeader.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                favoritesHeader.setPaintFlags(0);
                //  ingredientsHeader.setPaintFlags(0);
                pager.setCurrentItem(0);
            }
        }
    };
    // ingredientsHeader.setOnClickListener(headerListener);
    favoritesHeader.setOnClickListener(headerListener);
    recipesHeader.setOnClickListener(headerListener);
    //Tool/Appbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Navigation Drawer
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}
Also used : NavigationView(android.support.design.widget.NavigationView) RequestMethod(com.se491.chef_ly.http.RequestMethod) Bundle(android.os.Bundle) ActionBarDrawerToggle(android.support.v7.app.ActionBarDrawerToggle) ViewPager(android.support.v4.view.ViewPager) NavigationView(android.support.design.widget.NavigationView) SearchView(android.support.v7.widget.SearchView) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) Paint(android.graphics.Paint) RecipeList(com.se491.chef_ly.model.RecipeList) DrawerLayout(android.support.v4.widget.DrawerLayout) Credentials(com.auth0.android.result.Credentials) Toolbar(android.support.v7.widget.Toolbar)

Example 9 with RequestMethod

use of com.se491.chef_ly.http.RequestMethod in project chefly_android by chef-ly.

the class RecipeListActivity method handleIntent.

//search by title
private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        queryString = intent.getStringExtra(SearchManager.QUERY);
        Toast.makeText(this, queryString, Toast.LENGTH_SHORT).show();
        // Put Search Logic Here
        RequestMethod requestPackage = new RequestMethod();
        //find the url
        requestPackage.setEndPoint(urlStringSearch + queryString + urlPageNum + 0);
        //send the post method request
        requestPackage.setMethod("GET");
        Bundle searchRecipes = new Bundle();
        searchRecipes.putParcelable("requestPackage", requestPackage);
        searchRecipes.putString("test", "Handle Intent");
        server.updateSearch(queryString);
        //getSupportLoaderManager().initLoader(SEARCHID, searchRecipes,this).forceLoad();
        getSupportLoaderManager().initLoader(queryString.hashCode(), searchRecipes, this).forceLoad();
    } else {
        Log.d(TAG, "Intent does not equal action search");
    }
}
Also used : RequestMethod(com.se491.chef_ly.http.RequestMethod) Bundle(android.os.Bundle)

Example 10 with RequestMethod

use of com.se491.chef_ly.http.RequestMethod in project chefly_android by chef-ly.

the class RegisterActivity method registerNewUser.

private void registerNewUser() throws UnirestException {
    Log.d(TAG, "Register new user entered");
    RequestMethod requestPackage = new RequestMethod();
    requestPackage.setEndPoint(urlString);
    requestPackage.setParam("client_id", getResources().getText(R.string.auth0_client_id).toString());
    requestPackage.setParam("connection", getResources().getText(R.string.auth0_databaseConnection).toString());
    requestPackage.setParam("email", userEmail);
    requestPackage.setParam("password", password);
    requestPackage.setParam("username", username);
    requestPackage.setMethod("POST");
    Intent intent = new Intent(this, MyService.class);
    intent.putExtra(MyService.REQUEST_PACKAGE, requestPackage);
    startService(intent);
}
Also used : RequestMethod(com.se491.chef_ly.http.RequestMethod) Intent(android.content.Intent)

Aggregations

RequestMethod (com.se491.chef_ly.http.RequestMethod)13 Bundle (android.os.Bundle)6 Intent (android.content.Intent)4 IOException (java.io.IOException)4 View (android.view.View)3 ImageView (android.widget.ImageView)3 TextView (android.widget.TextView)3 Gson (com.google.gson.Gson)3 GsonBuilder (com.google.gson.GsonBuilder)3 Type (java.lang.reflect.Type)3 Handler (android.os.Handler)2 TypeToken (com.google.gson.reflect.TypeToken)2 HttpConnection (com.se491.chef_ly.http.HttpConnection)2 RecipeInformation (com.se491.chef_ly.model.RecipeInformation)2 RecipeList (com.se491.chef_ly.model.RecipeList)2 Context (android.content.Context)1 Bitmap (android.graphics.Bitmap)1 Paint (android.graphics.Paint)1 AsyncTask (android.os.AsyncTask)1 Parcel (android.os.Parcel)1