Search in sources :

Example 1 with AllRidesFragmentPagerAdapter

use of br.ufrj.caronae.adapters.AllRidesFragmentPagerAdapter in project caronae-android by caronae.

the class AllRidesFrag method listAllRides.

private void listAllRides(final int pageNum) {
    final Snackbar snackbar = makeNoConexionSnack();
    progressBar2.setVisibility(View.VISIBLE);
    if (isAdded() && (getActivity() != null)) {
        String going = null;
        String neighborhoods = null;
        String zone = null;
        String hub = null;
        String campus = null;
        String filtersJsonString = SharedPref.getFiltersPref();
        if (!filtersJsonString.equals(SharedPref.MISSING_PREF)) {
            RideFiltersForJson rideFilters = new Gson().fromJson(filtersJsonString, RideFiltersForJson.class);
            neighborhoods = rideFilters.getLocation();
            if (!rideFilters.getCampus().equals("Todos os Campi")) {
                hub = rideFilters.getCenter();
                campus = rideFilters.getCampus();
            }
            zone = rideFilters.getZone();
        }
        CaronaeAPI.service(getContext()).listAllRides(pageNum + "", going, neighborhoods, zone, hub, "", campus).enqueue(new Callback<RideForJsonDeserializer>() {

            @Override
            public void onResponse(Call<RideForJsonDeserializer> call, Response<RideForJsonDeserializer> response) {
                if (response.isSuccessful()) {
                    progressBar2.setVisibility(View.GONE);
                    RideForJsonDeserializer data = response.body();
                    List<RideForJson> rideOffers = data.getData();
                    if (rideOffers != null && !rideOffers.isEmpty()) {
                        Collections.sort(rideOffers, new RideOfferComparatorByDateAndTime());
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
                        Date todayDate = new Date();
                        String todayString = simpleDateFormat.format(todayDate);
                        simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.US);
                        String time = simpleDateFormat.format(todayDate);
                        Iterator<RideForJson> it = rideOffers.iterator();
                        while (it.hasNext()) {
                            RideForJson rideOffer = it.next();
                            if (Util.formatBadDateWithYear(rideOffer.getDate()).equals(todayString) && Util.formatTime(rideOffer.getTime()).compareTo(time) < 0)
                                it.remove();
                            else {
                                rideOffer.setDbId(rideOffer.getId().intValue());
                                if (rideOffer.isGoing())
                                    if (!checkIfRideIsInList(goingRides, rideOffer))
                                        goingRides.add(rideOffer);
                                    else if (!checkIfRideIsInList(notGoingRides, rideOffer))
                                        notGoingRides.add(rideOffer);
                            }
                        }
                    }
                    if (isAdded()) {
                        viewPager.setAdapter(new AllRidesFragmentPagerAdapter(getChildFragmentManager(), goingRides, notGoingRides, App.getInst().getResources().getStringArray(R.array.tab_tags)));
                        tabLayout.setupWithViewPager(viewPager);
                        if (PAGE_WAS_GOING)
                            viewPager.setCurrentItem(0);
                        else
                            viewPager.setCurrentItem(1);
                        tabLayout.setBackground(ContextCompat.getDrawable(App.getInst(), R.drawable.transparency_gradient_top_botton));
                        configureTabIndicators();
                    }
                } else {
                    Util.treatResponseFromServer(response);
                    progressBar2.setVisibility(View.GONE);
                    Log.e("listAllRides", response.message());
                }
            }

            @Override
            public void onFailure(Call<RideForJsonDeserializer> call, Throwable t) {
                progressBar2.setVisibility(View.GONE);
                Log.e("listAllRides", t.getMessage());
                snackbar.setAction("CONECTAR", new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        listAllRides(pageNum);
                    }
                });
            }
        });
    } else {
        Util.toast("Activity not atached");
        return;
    }
}
Also used : RideForJson(br.ufrj.caronae.models.modelsforjson.RideForJson) RideForJsonDeserializer(br.ufrj.caronae.models.modelsforjson.RideForJsonDeserializer) RideFiltersForJson(br.ufrj.caronae.models.modelsforjson.RideFiltersForJson) Gson(com.google.gson.Gson) BindView(butterknife.BindView) View(android.view.View) Date(java.util.Date) AllRidesFragmentPagerAdapter(br.ufrj.caronae.adapters.AllRidesFragmentPagerAdapter) RideOfferComparatorByDateAndTime(br.ufrj.caronae.comparators.RideOfferComparatorByDateAndTime) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat) Snackbar(android.support.design.widget.Snackbar)

Aggregations

Snackbar (android.support.design.widget.Snackbar)1 View (android.view.View)1 AllRidesFragmentPagerAdapter (br.ufrj.caronae.adapters.AllRidesFragmentPagerAdapter)1 RideOfferComparatorByDateAndTime (br.ufrj.caronae.comparators.RideOfferComparatorByDateAndTime)1 RideFiltersForJson (br.ufrj.caronae.models.modelsforjson.RideFiltersForJson)1 RideForJson (br.ufrj.caronae.models.modelsforjson.RideForJson)1 RideForJsonDeserializer (br.ufrj.caronae.models.modelsforjson.RideForJsonDeserializer)1 BindView (butterknife.BindView)1 Gson (com.google.gson.Gson)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 List (java.util.List)1