Search in sources :

Example 1 with RideOfferComparatorByDateAndTime

use of br.ufrj.caronae.comparators.RideOfferComparatorByDateAndTime 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)

Example 2 with RideOfferComparatorByDateAndTime

use of br.ufrj.caronae.comparators.RideOfferComparatorByDateAndTime in project caronae-android by caronae.

the class AllRidesListFrag method refreshRideList.

void refreshRideList(final int pageNumber) {
    final long bytesSoFar = TrafficStats.getUidRxBytes(Process.myUid());
    String going = null;
    if (pageIdentifier == AllRidesFragmentPagerAdapter.PAGE_GOING)
        going = "1";
    else
        going = "0";
    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(pageNumber + "", going, neighborhoods, zone, hub, "", campus).enqueue(new retrofit2.Callback<RideForJsonDeserializer>() {

        @Override
        public void onResponse(Call<RideForJsonDeserializer> call, Response<RideForJsonDeserializer> response) {
            totalBytesConsumed = totalBytesConsumed + TrafficStats.getUidRxBytes(Process.myUid()) - bytesSoFar;
            Log.e("CONSUMPTION", "Bytes Consumed: " + totalBytesConsumed);
            switch(pageIdentifier) {
                case 0:
                    Log.e("CONSUMPTION", "Tamanho da lista " + goingRides.size());
                    break;
                case 1:
                    Log.e("CONSUMPTION", "Tamanho da lista " + notGoingRides.size());
                    break;
            }
            if (response.isSuccessful()) {
                if (pageCounter == FIRST_PAGE_TO_LOAD) {
                    goingRides = new ArrayList<>();
                    notGoingRides = new ArrayList<>();
                }
                RideForJsonDeserializer data = response.body();
                List<RideForJson> rideOffers = data.getData();
                if (rideOffers != null && !rideOffers.isEmpty()) {
                    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);
                                }
                            }
                        }
                    }
                    Collections.sort(goingRides, new RideOfferComparatorByDateAndTime());
                    Collections.sort(notGoingRides, new RideOfferComparatorByDateAndTime());
                }
                if (pageIdentifier == AllRidesFragmentPagerAdapter.PAGE_GOING) {
                    if (goingRides == null || goingRides.isEmpty()) {
                    } else {
                        adapter.makeList(goingRides);
                        scrollListener.resetState();
                    }
                } else {
                    if (notGoingRides == null || notGoingRides.isEmpty()) {
                    } else {
                        adapter.makeList(notGoingRides);
                        scrollListener.resetState();
                    }
                }
                refreshLayout.setRefreshing(false);
            } else {
                Util.treatResponseFromServer(response);
                Util.toast(R.string.frag_allrides_errorGetRides);
                refreshLayout.setRefreshing(false);
                Log.e("listAllRides", response.message());
            }
            scrollListener.resetState();
        }

        @Override
        public void onFailure(Call<RideForJsonDeserializer> call, Throwable t) {
            refreshLayout.setRefreshing(false);
            Log.e("listAllRides", t.getMessage());
            scrollListener.resetState();
        }
    });
    if (filter != null) {
        switch(pageNumber) {
            case 0:
                if (filteredGoingList.size() <= 8) {
                    loadOneMorePage();
                }
                if (filteredNotGoingList.size() <= 8) {
                    loadOneMorePage();
                }
                break;
            case 1:
                if (filteredGoingList.size() <= 8) {
                    loadOneMorePage();
                }
                if (filteredNotGoingList.size() <= 8) {
                    loadOneMorePage();
                }
                break;
        }
    }
}
Also used : RideForJson(br.ufrj.caronae.models.modelsforjson.RideForJson) RideForJsonDeserializer(br.ufrj.caronae.models.modelsforjson.RideForJsonDeserializer) RideFiltersForJson(br.ufrj.caronae.models.modelsforjson.RideFiltersForJson) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) Date(java.util.Date) RideOfferComparatorByDateAndTime(br.ufrj.caronae.comparators.RideOfferComparatorByDateAndTime) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with RideOfferComparatorByDateAndTime

use of br.ufrj.caronae.comparators.RideOfferComparatorByDateAndTime in project caronae-android by caronae.

the class MyRidesListFrag method getActiveRides.

private void getActiveRides() {
    getActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            CaronaeAPI.service(getContext()).getMyActiveRides().enqueue(new Callback<List<RideForJson>>() {

                @Override
                public void onResponse(Call<List<RideForJson>> call, Response<List<RideForJson>> response) {
                    if (response.isSuccessful()) {
                        List<RideForJson> rideWithUsersList = response.body();
                        if (rideWithUsersList == null || rideWithUsersList.isEmpty()) {
                            MyRidesFrag.hideProgressBar();
                            myRidesList.setAdapter(new MyActiveRidesAdapter(new ArrayList<RideForJson>(), (MainAct) getActivity()));
                            myRidesList.setHasFixedSize(true);
                            myRidesList.setLayoutManager(new LinearLayoutManager(getActivity()));
                            new LoadRides().execute();
                            return;
                        }
                        ActiveRide.deleteAll(ActiveRide.class);
                        // subscribe to ride id topic
                        for (RideForJson rideWithUsers : rideWithUsersList) {
                            int rideId = rideWithUsers.getId().intValue();
                            rideWithUsers.setDbId(rideId);
                            FirebaseTopicsHandler.subscribeFirebaseTopic(rideId + "");
                            new ActiveRide(rideWithUsers.getDbId(), rideWithUsers.isGoing(), rideWithUsers.getDate()).save();
                        }
                        Collections.sort(rideWithUsersList, new RideOfferComparatorByDateAndTime());
                        addAllActiveRidesToList(rideWithUsersList);
                        MyRidesFrag.hideProgressBar();
                    } else {
                        Util.treatResponseFromServer(response);
                        MyRidesFrag.hideProgressBar();
                        norides_tv.setVisibility(View.VISIBLE);
                        Util.toast(R.string.frag_myactiverides_errorGetActiveRides);
                        Log.e("getMyActiveRides", response.message());
                    }
                    new LoadRides().execute();
                }

                @Override
                public void onFailure(Call<List<RideForJson>> call, Throwable t) {
                    MyRidesFrag.hideProgressBar();
                    norides_tv.setVisibility(View.VISIBLE);
                    Util.toast(R.string.frag_myactiverides_errorGetActiveRides);
                    new LoadRides().execute();
                    Log.e("getMyActiveRides", t.getMessage());
                }
            });
        }
    });
}
Also used : Call(retrofit2.Call) RideForJson(br.ufrj.caronae.models.modelsforjson.RideForJson) MyActiveRidesAdapter(br.ufrj.caronae.adapters.MyActiveRidesAdapter) ArrayList(java.util.ArrayList) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) Response(retrofit2.Response) Callback(retrofit2.Callback) RideOfferComparatorByDateAndTime(br.ufrj.caronae.comparators.RideOfferComparatorByDateAndTime) MainAct(br.ufrj.caronae.acts.MainAct) ActiveRide(br.ufrj.caronae.models.ActiveRide)

Example 4 with RideOfferComparatorByDateAndTime

use of br.ufrj.caronae.comparators.RideOfferComparatorByDateAndTime in project caronae-android by caronae.

the class RideSearchFrag method searchBt.

@OnClick(R.id.search_bt)
public void searchBt() {
    String location = location_et.getText().toString();
    if (location.isEmpty()) {
        Util.toast(getString(R.string.frag_rideSearch_locationEmpty));
        return;
    } else {
        if (location.contains("+")) {
            location = neighborhoods;
        }
    }
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
    String etDateString = date_et.getText().toString();
    Date todayDate = new Date();
    String todayString = simpleDateFormat.format(todayDate);
    try {
        todayDate = simpleDateFormat.parse(todayString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    if (etDateString.isEmpty()) {
        date_et.setText(todayString);
        etDateString = todayString;
    } else {
        try {
            Date etDate = simpleDateFormat.parse(etDateString);
            if (etDate.before(todayDate)) {
                Util.toast(getString(R.string.frag_rideoffersearch_pastdate));
                return;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    String time = time_et.getText().toString();
    if (time.isEmpty()) {
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm", Locale.US);
        String format = simpleDateFormat1.format(new Date());
        time_et.setText(format);
        time = format;
    }
    String center = center_et.getText().toString();
    if (center.equals(Util.getFundaoCenters()[0]) || center.equals("Cidade Universitária"))
        center = "";
    String campus = campi;
    boolean go = radioGroup.getCheckedRadioButtonId() == R.id.go_rb;
    RideSearchFiltersForJson rideSearchFilters = new RideSearchFiltersForJson(location, etDateString, time, center, campus, go, location_et.getText().toString());
    String lastRideSearchFilters = new Gson().toJson(rideSearchFilters);
    SharedPref.saveLastRideSearchFiltersPref(lastRideSearchFilters);
    rideSearchFilters.setDate(Util.formatBadDateWithYear(etDateString));
    Log.e("INPUT", "location: " + location);
    Log.e("INPUT", "data: " + etDateString);
    Log.e("INPUT", "hora: " + time);
    Log.e("INPUT", "center: " + center);
    Log.e("INPUT", "campus: " + campus);
    Log.e("INPUT", "locationResumeField: " + location_et.getText().toString());
    final ProgressDialog pd = ProgressDialog.show(getActivity(), "", getContext().getString(R.string.wait), true, true);
    CaronaeAPI.service(getContext()).listFiltered(rideSearchFilters).enqueue(new Callback<List<RideForJson>>() {

        @Override
        public void onResponse(Call<List<RideForJson>> call, Response<List<RideForJson>> response) {
            if (response.isSuccessful()) {
                List<RideForJson> rideOffers = response.body();
                if (rideOffers != null && !rideOffers.isEmpty()) {
                    Util.expandOrCollapse(lay, false);
                    anotherSearch_bt.setVisibility(View.VISIBLE);
                    Collections.sort(rideOffers, new RideOfferComparatorByDateAndTime());
                    for (RideForJson rideOffer : rideOffers) {
                        rideOffer.setDbId(rideOffer.getId().intValue());
                    }
                    adapter.makeList(rideOffers);
                } else {
                    Util.treatResponseFromServer(response);
                    Util.toast(R.string.frag_rideSearch_noRideFound);
                    adapter.makeList(new ArrayList<RideForJson>());
                }
                pd.dismiss();
            } else {
                pd.dismiss();
                Util.toast(R.string.frag_rideSearch_errorListFiltered);
                Log.e("listFiltered", response.message());
            }
        }

        @Override
        public void onFailure(Call<List<RideForJson>> call, Throwable t) {
            pd.dismiss();
            Util.toast(R.string.frag_rideSearch_errorListFiltered);
            Log.e("listFiltered", t.getMessage());
        }
    });
}
Also used : RideForJson(br.ufrj.caronae.models.modelsforjson.RideForJson) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) ProgressDialog(android.app.ProgressDialog) Date(java.util.Date) RideOfferComparatorByDateAndTime(br.ufrj.caronae.comparators.RideOfferComparatorByDateAndTime) ArrayList(java.util.ArrayList) List(java.util.List) ParseException(java.text.ParseException) RideSearchFiltersForJson(br.ufrj.caronae.models.modelsforjson.RideSearchFiltersForJson) SimpleDateFormat(java.text.SimpleDateFormat) OnClick(butterknife.OnClick)

Aggregations

RideOfferComparatorByDateAndTime (br.ufrj.caronae.comparators.RideOfferComparatorByDateAndTime)4 RideForJson (br.ufrj.caronae.models.modelsforjson.RideForJson)4 ArrayList (java.util.ArrayList)4 Gson (com.google.gson.Gson)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 List (java.util.List)3 RideFiltersForJson (br.ufrj.caronae.models.modelsforjson.RideFiltersForJson)2 RideForJsonDeserializer (br.ufrj.caronae.models.modelsforjson.RideForJsonDeserializer)2 Iterator (java.util.Iterator)2 ProgressDialog (android.app.ProgressDialog)1 Snackbar (android.support.design.widget.Snackbar)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 View (android.view.View)1 MainAct (br.ufrj.caronae.acts.MainAct)1 AllRidesFragmentPagerAdapter (br.ufrj.caronae.adapters.AllRidesFragmentPagerAdapter)1 MyActiveRidesAdapter (br.ufrj.caronae.adapters.MyActiveRidesAdapter)1 ActiveRide (br.ufrj.caronae.models.ActiveRide)1 RideSearchFiltersForJson (br.ufrj.caronae.models.modelsforjson.RideSearchFiltersForJson)1 BindView (butterknife.BindView)1