Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pathways.fundilist.fragment;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.support.v4.widget.SwipeRefreshLayout;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.widget.DefaultItemAnimator;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Toast;
- import com.android.volley.Request;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.StringRequest;
- import com.pathways.fundilist.R;
- import com.pathways.fundilist.activity.ProviderLogin;
- import com.pathways.fundilist.adapter.ProviderJobsRecyclerAdapter;
- import com.pathways.fundilist.model.Bid;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import helper.SQLiteHandler;
- import helper.SessionManager;
- import volley.AppController;
- import volley.ConfigUrl;
- /**
- * Created by curv3r on 12/15/16.
- */
- public class Bids extends Fragment {
- private final String LOG_TAG = Bids.class.getSimpleName();
- // Create an {@link ArrayList} that stores Bids
- final ArrayList<Bid> bids = new ArrayList<>();
- // Create {@link SessionManager} instance
- private SessionManager sessionManager;
- private SQLiteHandler db;
- private Bid bid;
- RecyclerView recyclerView;
- SwipeRefreshLayout swipeRefreshLayout;
- ProviderJobsRecyclerAdapter jobsRecyclerAdapter;
- HashMap<String, String> provider;
- String providerId;
- String providerSkill;
- String providerLocation;
- String providerFirstName;
- String providerLastName;
- String providerEmail;
- /**
- * Set a hint to the system about whether this fragment's UI is currently visible
- * to the user. This hint defaults to true and is persistent across fragment instance
- * state save and restore.
- * <p>
- * <p>An app may set this to false to indicate that the fragment's UI is
- * scrolled out of visibility or is otherwise not directly visible to the user.
- * This may be used by the system to prioritize operations such as fragment lifecycle updates
- * or loader ordering behavior.</p>
- * <p>
- * <p><strong>Note:</strong> This method may be called outside of the fragment lifecycle.
- * and thus has no ordering guarantees with regard to fragment lifecycle method calls.</p>
- *
- * @param isVisibleToUser true if this fragment's UI is currently visible to the user (default),
- * false if it is not.
- */
- @Override
- public void setUserVisibleHint(boolean isVisibleToUser) {
- super.setUserVisibleHint(isVisibleToUser);
- if (isVisibleToUser && isResumed()) {
- if (bids.isEmpty()) {
- getBids(providerLocation, providerSkill);
- }
- }
- }
- /**
- * Called to have the fragment instantiate its user interface view.
- * This is optional, and non-graphical fragments can return null (which
- * is the default implementation). This will be called between
- * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}.
- * <p>
- * <p>If you return a View from here, you will later be called in
- * {@link #onDestroyView} when the view is being released.
- *
- * @param inflater The LayoutInflater object that can be used to inflate
- * any views in the fragment,
- * @param container If non-null, this is the parent view that the fragment's
- * UI should be attached to. The fragment should not add the view itself,
- * but this can be used to generate the LayoutParams of the view.
- * @param savedInstanceState If non-null, this fragment is being re-constructed
- * from a previous saved state as given here.
- * @return Return the View for the fragment's UI, or null.
- */
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.frag_bids, container, false);
- swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout_provider_bids);
- recyclerView = (RecyclerView) rootView.findViewById(R.id.provider_bid_recycler_view);
- RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
- recyclerView.setLayoutManager(layoutManager);
- recyclerView.setItemAnimator(new DefaultItemAnimator());
- sessionManager = new SessionManager(getActivity().getApplicationContext());
- db = new SQLiteHandler(getActivity().getApplicationContext());
- if (sessionManager.isLoggedIn()) {
- provider = db.getProvider();
- if (!provider.isEmpty()) {
- providerId = provider.get("service_provider_id");
- providerFirstName = provider.get("provider_first_name");
- providerLastName = provider.get("provider_last_name");
- providerEmail = provider.get("provider_email");
- providerSkill = provider.get("provider_skill");
- providerLocation = provider.get("provider_location");
- Log.d(LOG_TAG, providerId + " " + providerSkill + " " + providerLocation);
- } else {
- startActivity(new Intent(getActivity(), ProviderLogin.class));
- }
- } else {
- Toast.makeText(getActivity(), "You need to login first", Toast.LENGTH_SHORT).show();
- startActivity(new Intent(getActivity(), ProviderLogin.class));
- }
- swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
- @Override
- public void onRefresh() {
- updateJobs();
- }
- });
- //of course bids arraylist is empty when this fragment is created for the first time,
- //so dont bother with bids.isEmpty() check
- getBids(providerLocation, providerSkill);
- // if (bids.isEmpty()) {
- // getBids(providerLocation, providerSkill);
- // }
- return rootView;
- }
- void setUpAdapter(){
- jobsRecyclerAdapter = new ProviderJobsRecyclerAdapter(bids);
- recyclerView.setAdapter(jobsRecyclerAdapter);
- }
- private void updateJobs() {
- getBids(providerLocation, providerSkill);
- jobsRecyclerAdapter.notifyDataSetChanged();
- swipeRefreshLayout.setRefreshing(false);
- }
- /**
- * Returns a formatted date and time string for when the earthquake happened.
- */
- private String getDateString(String jsonString) {
- SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy 'at' HH:mm:ss z");
- return formatter.format(jsonString);
- }
- public void getBids(final String locationID, final String skillID) {
- StringRequest stringRequest = new StringRequest(Request.Method.POST,
- ConfigUrl.PROVIDER_AVAILABLE_JOBS, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- Log.d(LOG_TAG, "Response: " + response);
- try {
- JSONObject jsonObject = new JSONObject(response);
- boolean error = jsonObject.getBoolean("error");
- if (!error) {
- // getting JSONArray node
- JSONArray result = jsonObject.getJSONArray("data");
- Log.d(LOG_TAG, "" + result.length());
- if (result.length() > 0) {
- // loop through the results
- for (int i = 0; i < result.length(); i++) {
- JSONObject obj = result.getJSONObject(i);
- String bidId = obj.getString("job_id");
- String clientID = obj.getString("client_id");
- String clientName = obj.getString("client_name");
- String location = obj.getString("job_location_label");
- String description = obj.getString("job_detail");
- String jobDate = obj.getString("job_date");
- String timeline = obj.getString("job_time");
- String dateCreated = obj.getString("date_created");
- bids.add(new Bid(clientName, description, location, jobDate, bidId, clientID, timeline, dateCreated));
- Log.d(LOG_TAG, bidId + " " + clientID + " Bids size " + bids.size());
- Log.v(LOG_TAG, bids.get(0).getClientName());
- //dont set your adapter within this loop, it's not okay. It means you are resetting the adapter with every iteration
- // jobsRecyclerAdapter = new ProviderJobsRecyclerAdapter(bids);
- // recyclerView.setAdapter(jobsRecyclerAdapter);
- }
- //instead do it outside the loop
- setUpAdapter();
- } else {
- getActivity().runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(getActivity(), "No jobs at this time, please try again later", Toast.LENGTH_LONG).show();
- }
- });
- }
- } else {
- AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
- builder.setTitle("No Jobs");
- builder.setMessage("No jobs available at the moment");
- builder.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- }
- });
- AlertDialog alertDialog = builder.create();
- alertDialog.show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.e(LOG_TAG, "Error: " + error.getMessage());
- Toast.makeText(getActivity(),
- "Problem connecting. Please check your network connection and try again.", Toast.LENGTH_LONG).show();
- }
- }) {
- @Override
- protected Map<String, String> getParams() {
- // Posting parameters to login url
- Map<String, String> params = new HashMap<>();
- params.put("location_id", locationID);
- params.put("skill_id", skillID);
- return params;
- }
- };
- // Adding request to request queue
- AppController.getInstance().addToRequestQueue(stringRequest);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement