Search in sources :

Example 11 with RideForJson

use of br.ufrj.caronae.models.modelsforjson.RideForJson in project caronae-android by caronae.

the class MyActiveRidesAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
    final RideForJson rideOffer = ridesList.get(position);
    int color = ContextCompat.getColor(activity, R.color.zone_outros);
    int bgRes = R.drawable.bg_bt_raise_zone_outros;
    if (rideOffer.getZone().equals("Centro")) {
        color = ContextCompat.getColor(activity, R.color.zone_centro);
        bgRes = R.drawable.bg_bt_raise_zone_centro;
    }
    if (rideOffer.getZone().equals("Zona Sul")) {
        color = ContextCompat.getColor(activity, R.color.zone_sul);
        bgRes = R.drawable.bg_bt_raise_zone_sul;
    }
    if (rideOffer.getZone().equals("Zona Oeste")) {
        color = ContextCompat.getColor(activity, R.color.zone_oeste);
        bgRes = R.drawable.bg_bt_raise_zone_oeste;
    }
    if (rideOffer.getZone().equals("Zona Norte")) {
        color = ContextCompat.getColor(activity, R.color.zone_norte);
        bgRes = R.drawable.bg_bt_raise_zone_norte;
    }
    if (rideOffer.getZone().equals("Baixada")) {
        color = ContextCompat.getColor(activity, R.color.zone_baixada);
        bgRes = R.drawable.bg_bt_raise_zone_baixada;
    }
    if (rideOffer.getZone().equals("Grande Niterói")) {
        color = ContextCompat.getColor(activity, R.color.zone_niteroi);
        bgRes = R.drawable.bg_bt_raise_zone_niteroi;
    }
    viewHolder.location_tv.setTextColor(color);
    String profilePicUrl = rideOffer.getDriver().getProfilePicUrl();
    if (profilePicUrl != null && !profilePicUrl.isEmpty()) {
        Picasso.with(activity).load(profilePicUrl).placeholder(R.drawable.user_pic).error(R.drawable.user_pic).transform(new RoundedTransformation()).into(viewHolder.photo_iv);
    }
    if (App.getUser().getDbId() != rideOffer.getDriver().getDbId())
        viewHolder.photo_iv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(activity, ProfileAct.class);
                intent.putExtra("user", new Gson().toJson(rideOffer.getDriver()));
                intent.putExtra("from", "myactiveridesadapter");
                activity.startActivity(intent);
            }
        });
    String timeText;
    if (rideOffer.isGoing())
        timeText = activity.getResources().getString(R.string.arrivingAt, Util.formatTime(rideOffer.getTime()));
    else
        timeText = activity.getResources().getString(R.string.leavingAt, Util.formatTime(rideOffer.getTime()));
    viewHolder.time_tv.setText(timeText);
    viewHolder.date_tv.setText(Util.formatBadDateWithoutYear(rideOffer.getDate()));
    viewHolder.name_tv.setText(rideOffer.getDriver().getName());
    String location;
    if (rideOffer.isGoing())
        location = rideOffer.getNeighborhood() + " ➜ " + rideOffer.getHub();
    else
        location = rideOffer.getHub() + " ➜ " + rideOffer.getNeighborhood();
    viewHolder.location_tv.setText(location);
    viewHolder.cardView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            NewChatMsgIndicator.deleteAll(NewChatMsgIndicator.class, "db_id = ?", rideOffer.getDbId() + "");
            viewHolder.newMsgIndicator_iv.setVisibility(View.INVISIBLE);
            int colorChat = ContextCompat.getColor(activity, R.color.gray);
            viewHolder.newMsgIndicator_iv.setColorFilter(colorChat);
            Intent intent = new Intent(activity, ActiveRideAct.class);
            intent.putExtra("ride", rideOffer);
            activity.startActivity(intent);
        }
    });
    final int finalColor = color;
    final int finalBgRes = bgRes;
    final String finallocation = location;
    viewHolder.newMsgIndicator_iv.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            List<ChatAssets> l = ChatAssets.find(ChatAssets.class, "ride_id = ?", rideOffer.getDbId() + "");
            if (l == null || l.isEmpty())
                new ChatAssets(rideOffer.getDbId() + "", finallocation, finalColor, finalBgRes, Util.formatBadDateWithoutYear(rideOffer.getDate()), Util.formatTime(rideOffer.getTime())).save();
            Intent intent = new Intent(activity, ChatAct.class);
            intent.putExtra("rideId", rideOffer.getDbId() + "");
            activity.startActivity(intent);
        }
    });
    boolean found = false;
    for (NewChatMsgIndicator newChatMsgIndicator : newChatMsgIndicatorList) {
        if (newChatMsgIndicator.getDbId() == rideOffer.getDbId()) {
            viewHolder.newMsgIndicator_iv.setVisibility(View.VISIBLE);
            found = true;
            break;
        }
    }
    if (!found) {
        int colorChat = ContextCompat.getColor(activity, R.color.gray);
        viewHolder.newMsgIndicator_iv.setColorFilter(colorChat);
        viewHolder.newMsgIndicator_iv.setVisibility(View.INVISIBLE);
    }
}
Also used : ActiveRideAct(br.ufrj.caronae.acts.ActiveRideAct) RideForJson(br.ufrj.caronae.models.modelsforjson.RideForJson) ChatAssets(br.ufrj.caronae.models.ChatAssets) NewChatMsgIndicator(br.ufrj.caronae.models.NewChatMsgIndicator) Gson(com.google.gson.Gson) Intent(android.content.Intent) ImageView(android.widget.ImageView) CircleImageView(de.hdodenhof.circleimageview.CircleImageView) View(android.view.View) CardView(android.support.v7.widget.CardView) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) RoundedTransformation(br.ufrj.caronae.RoundedTransformation) List(java.util.List) ChatAct(br.ufrj.caronae.acts.ChatAct)

Example 12 with RideForJson

use of br.ufrj.caronae.models.modelsforjson.RideForJson in project caronae-android by caronae.

the class ChatAct method getRideFromServer.

private Ride getRideFromServer(final Activity activity) {
    final RideForJson[] ride = { null };
    activity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            final ProgressDialog pd = ProgressDialog.show(activity, "", activity.getString(R.string.wait), true, true);
            CaronaeAPI.service(activity).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();
                        Log.e("RIDE", "rides encontradas: " + rideWithUsersList.size());
                        if (rideWithUsersList == null || rideWithUsersList.isEmpty()) {
                            pd.dismiss();
                            return;
                        }
                        for (int rideIndex = 0; rideIndex < rideWithUsersList.size(); rideIndex++) {
                            if ((rideWithUsersList.get(rideIndex).getId() + "").equals(rideId)) {
                                ride[0] = rideWithUsersList.get(rideIndex);
                            }
                        }
                        if (ride[0] != null) {
                            String location;
                            if (ride[0].isGoing())
                                location = ride[0].getNeighborhood() + " -> " + ride[0].getHub();
                            else
                                location = ride[0].getHub() + " -> " + ride[0].getNeighborhood();
                            ChatAssets chatAssets = new ChatAssets(rideId, location, Util.getColorbyZone(ride[0].getZone()), Util.getBgResByZone(ride[0].getZone()), Util.formatBadDateWithoutYear(ride[0].getDate()), Util.formatTime(ride[0].getTime()));
                            chatAssets.save();
                            configureActivityWithChatAssets(chatAssets);
                        } else {
                            Intent intent = new Intent(getApplicationContext(), MainAct.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            finish();
                        }
                        pd.dismiss();
                    } else {
                        Util.treatResponseFromServer(response);
                        pd.dismiss();
                        Log.e("getMyActiveRides", response.message());
                    }
                }

                @Override
                public void onFailure(Call<List<RideForJson>> call, Throwable t) {
                    pd.dismiss();
                    Log.e("getMyActiveRides", t.getMessage());
                }
            });
        }
    });
    return ride[0];
}
Also used : Call(retrofit2.Call) RideForJson(br.ufrj.caronae.models.modelsforjson.RideForJson) ChatAssets(br.ufrj.caronae.models.ChatAssets) Intent(android.content.Intent) ProgressDialog(android.app.ProgressDialog) ChatMessageSendResponse(br.ufrj.caronae.models.ChatMessageSendResponse) Response(retrofit2.Response) Callback(retrofit2.Callback)

Example 13 with RideForJson

use of br.ufrj.caronae.models.modelsforjson.RideForJson in project caronae-android by caronae.

the class MyRidesAdapter method configureMyOfferRide.

private void configureMyOfferRide(int position, final MyRidesAdapter.ViewHolder holder) {
    final Ride ride = (Ride) rides.get(position);
    int color = Util.getColorbyZone(ride.getZone());
    holder.location_tv.setTextColor(color);
    if (ride.isGoing())
        holder.time_tv.setText(activity.getString(R.string.arrivingAt, Util.formatBadHour(ride.getTime())));
    else
        holder.time_tv.setText(activity.getString(R.string.leavingAt, Util.formatBadHour(ride.getTime())));
    if (ride.getDate().contains("-")) {
        ride.setDate(Util.formatBadDateWithYear(ride.getDate()));
    }
    holder.date_tv.setText(Util.formatGoodDateWithoutYear(ride.getDate()));
    holder.slots_tv.setText(activity.getString(R.string.Xslots, ride.getSlots(), (Integer.parseInt(ride.getSlots()) > 1 ? "s" : "")));
    String location;
    if (ride.isGoing())
        location = ride.getNeighborhood() + " ➜ " + ride.getHub();
    else
        location = ride.getHub() + " ➜ " + ride.getNeighborhood();
    holder.location_tv.setText(location);
    String s = activity.getString(R.string.repeats);
    if (ride.isRoutine()) {
        s += ride.getWeekDays().contains("7") ? activity.getString(R.string.sunday) : "";
        s += ride.getWeekDays().contains("1") ? activity.getString(R.string.monday) : "";
        s += ride.getWeekDays().contains("2") ? activity.getString(R.string.tuesday) : "";
        s += ride.getWeekDays().contains("3") ? activity.getString(R.string.wednesday) : "";
        s += ride.getWeekDays().contains("4") ? activity.getString(R.string.thursday) : "";
        s += ride.getWeekDays().contains("5") ? activity.getString(R.string.friday) : "";
        s += ride.getWeekDays().contains("6") ? activity.getString(R.string.saturday) : "";
        s = s.substring(0, s.length() - 1);
    } else {
        s = activity.getString(R.string.notRoutine);
    }
    holder.routine_tv.setText(s);
    holder.delete_bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (ride.isRoutine()) {
                Dialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {

                    @Override
                    protected void onBuildDone(Dialog dialog) {
                        dialog.layoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    }

                    @Override
                    public void onPositiveActionClicked(DialogFragment fragment) {
                        RadioGroup radioGroup = (RadioGroup) fragment.getDialog().findViewById(R.id.radioGroup);
                        int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
                        switch(checkedRadioButton) {
                            case R.id.all_rb:
                                deleteAllRidesFromRoutine();
                                break;
                            case R.id.single_rb:
                                deleteSingleRide();
                                break;
                        }
                        super.onPositiveActionClicked(fragment);
                    }

                    @Override
                    public void onNegativeActionClicked(DialogFragment fragment) {
                        super.onNegativeActionClicked(fragment);
                    }
                };
                builder.title(activity.getString(R.string.attention)).positiveAction(activity.getString(R.string.ok)).negativeAction(activity.getString(R.string.cancel)).contentView(R.layout.delete_routine_dialog);
                DialogFragment fragment = DialogFragment.newInstance(builder);
                fragment.show(activity.getSupportFragmentManager(), null);
            } else {
                deleteSingleRide();
            }
        }

        private void deleteAllRidesFromRoutine() {
            Dialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {

                @Override
                public void onPositiveActionClicked(DialogFragment fragment) {
                    final ProgressDialog pd = ProgressDialog.show(activity, "", activity.getString(R.string.wait), true, true);
                    final String routineId = ride.getRoutineId();
                    CaronaeAPI.service(activity.getApplicationContext()).deleteAllRidesFromRoutine(routineId).enqueue(new Callback<ResponseBody>() {

                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            if (response.isSuccessful()) {
                                pd.dismiss();
                                Util.toast(R.string.ridesDeleted);
                                if (routineId != null) {
                                    Iterator<Object> it = rides.iterator();
                                    while (it.hasNext()) {
                                        Object object = it.next();
                                        if (object.getClass() == Ride.class) {
                                            Ride ride2 = (Ride) object;
                                            if (ride2.getRoutineId().equals(routineId))
                                                it.remove();
                                        }
                                    }
                                }
                                notifyDataSetChanged();
                                Ride.deleteAll(Ride.class, "routine_id = ?", routineId);
                            } else {
                                Util.treatResponseFromServer(response);
                                pd.dismiss();
                                Util.toast(activity.getString(R.string.errorDeleteRide));
                                Log.e("deleteAllFromRoutine", response.message());
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable t) {
                            pd.dismiss();
                            Util.toast(activity.getString(R.string.errorDeleteRide));
                            Log.e("deleteAllFromRoutine", t.getMessage());
                        }
                    });
                    super.onPositiveActionClicked(fragment);
                }

                @Override
                public void onNegativeActionClicked(DialogFragment fragment) {
                    super.onNegativeActionClicked(fragment);
                }
            };
            ((SimpleDialog.Builder) builder).message(activity.getString(R.string.warnDeleteRidesCouldBeActive)).title(activity.getString(R.string.attention)).positiveAction(activity.getString(R.string.ok)).negativeAction(activity.getString(R.string.cancel));
            DialogFragment fragment = DialogFragment.newInstance(builder);
            fragment.show(activity.getSupportFragmentManager(), null);
        }

        private void deleteSingleRide() {
            Dialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {

                @Override
                public void onPositiveActionClicked(DialogFragment fragment) {
                    final ProgressDialog pd = ProgressDialog.show(activity, "", activity.getString(R.string.wait), true, true);
                    CaronaeAPI.service(activity.getApplicationContext()).deleteRide(ride.getDbId() + "").enqueue(new Callback<ResponseBody>() {

                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            if (response.isSuccessful()) {
                                pd.dismiss();
                                Util.toast(R.string.rideDeleted);
                                rides.remove(ride);
                                notifyItemRemoved(holder.getAdapterPosition());
                                for (int rideCounter = 0; rideCounter < rides.size(); rideCounter++) {
                                    if (rides.get(rideCounter).getClass() == RideForJson.class) {
                                        RideForJson rideForJson = (RideForJson) rides.get(rideCounter);
                                        if (rideForJson.getId() == ride.getId()) {
                                            rides.remove(rideForJson);
                                            notifyItemRemoved(rideCounter);
                                        }
                                    }
                                }
                                ride.delete();
                            } else {
                                Util.treatResponseFromServer(response);
                                pd.dismiss();
                                Util.toast(activity.getString(R.string.errorDeleteRide));
                                Log.e("deleteRide", response.message());
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable t) {
                            pd.dismiss();
                            Util.toast(activity.getString(R.string.errorDeleteRide));
                            Log.e("deleteRide", t.getMessage());
                        }
                    });
                    super.onPositiveActionClicked(fragment);
                }

                @Override
                public void onNegativeActionClicked(DialogFragment fragment) {
                    super.onNegativeActionClicked(fragment);
                }
            };
            ((SimpleDialog.Builder) builder).message(activity.getString(R.string.warnDeleteRideCouldBeActive)).title(activity.getString(R.string.attention)).positiveAction(activity.getString(R.string.ok)).negativeAction(activity.getString(R.string.cancel));
            DialogFragment fragment = DialogFragment.newInstance(builder);
            fragment.show(activity.getSupportFragmentManager(), null);
        }
    });
    final int colorToSend = color;
    final boolean[] isOpen = { false, false };
    holder.myRideLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
    holder.delete_bt.setBackgroundColor(color);
    holder.myRideLayout.addSwipeListener(new SwipeLayout.SwipeListener() {

        @Override
        public void onStartOpen(SwipeLayout layout) {
            isOpen[0] = true;
            isOpen[1] = true;
        }

        @Override
        public void onOpen(SwipeLayout layout) {
        }

        @Override
        public void onStartClose(SwipeLayout layout) {
        }

        @Override
        public void onClose(SwipeLayout layout) {
            isOpen[1] = false;
        }

        @Override
        public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
        }

        @Override
        public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
        }
    });
    holder.share_bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, Util.getTextToShareRide(ride));
            intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Check out this site!");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(intent.createChooser(intent, "Compartilhar Carona"));
        }
    });
    holder.myRideLayout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (!isOpen[0]) {
                final ProgressDialog pd = ProgressDialog.show(activity, "", activity.getResources().getString(R.string.wait), true, true);
                RideRequestReceived.deleteAll(RideRequestReceived.class, "db_id = ?", ride.getDbId() + "");
                holder.newRequest_iv.setVisibility(View.INVISIBLE);
                CaronaeAPI.service(activity.getApplicationContext()).getRequesters(ride.getDbId() + "").enqueue(new Callback<List<User>>() {

                    @Override
                    public void onResponse(Call<List<User>> call, Response<List<User>> response) {
                        if (response.isSuccessful()) {
                            pd.dismiss();
                            List<User> users = response.body();
                            if (users.isEmpty()) {
                                Util.toast(R.string.noRequesters);
                            } else {
                                Intent intent = new Intent(activity, RequestersListAct.class);
                                intent.putParcelableArrayListExtra("users", (ArrayList<User>) users);
                                intent.putExtra("rideId", ride.getDbId());
                                intent.putExtra("color", colorToSend);
                                activity.startActivity(intent);
                            }
                        } else {
                            Util.treatResponseFromServer(response);
                            pd.dismiss();
                            Util.toast(R.string.errorGetRequesters);
                            Log.e("getRequesters", response.message());
                        }
                    }

                    @Override
                    public void onFailure(Call<List<User>> call, Throwable t) {
                        pd.dismiss();
                        Util.toast(R.string.errorGetRequesters);
                        Log.e("getRequesters", t.getMessage());
                    }
                });
            } else if (isOpen[0] && !isOpen[1]) {
                isOpen[0] = false;
            }
        }
    });
    boolean found = false;
    for (RideRequestReceived requestReceived : rideRequestReceivedList) {
        if (requestReceived.getDbId() == ride.getDbId()) {
            holder.newRequest_iv.setVisibility(View.VISIBLE);
            found = true;
            break;
        }
    }
    if (!found) {
        CaronaeAPI.service(activity.getApplicationContext()).getRequesters(ride.getDbId() + "").enqueue(new Callback<List<User>>() {

            @Override
            public void onResponse(Call<List<User>> call, Response<List<User>> response) {
                if (response.isSuccessful()) {
                    List<User> requesters = response.body();
                    if (requesters.isEmpty()) {
                        holder.newRequest_iv.setVisibility(View.INVISIBLE);
                    } else {
                        holder.newRequest_iv.setVisibility(View.VISIBLE);
                        new RideRequestReceived(Integer.valueOf(ride.getDbId())).save();
                    }
                } else {
                    Util.toast(R.string.errorGetRequesters);
                    Log.e("getRequesters", response.message());
                }
            }

            @Override
            public void onFailure(Call<List<User>> call, Throwable t) {
                Util.toast(R.string.errorGetRequesters);
                Log.e("getRequesters", t.getMessage());
            }
        });
    }
}
Also used : User(br.ufrj.caronae.models.User) RadioGroup(android.widget.RadioGroup) DialogFragment(com.rey.material.app.DialogFragment) ProgressDialog(android.app.ProgressDialog) SimpleDialog(com.rey.material.app.SimpleDialog) ProgressDialog(android.app.ProgressDialog) SimpleDialog(com.rey.material.app.SimpleDialog) Dialog(com.rey.material.app.Dialog) SwipeLayout(com.daimajia.swipe.SwipeLayout) RideRequestReceived(br.ufrj.caronae.models.RideRequestReceived) ArrayList(java.util.ArrayList) List(java.util.List) Call(retrofit2.Call) RideForJson(br.ufrj.caronae.models.modelsforjson.RideForJson) Intent(android.content.Intent) ImageView(android.widget.ImageView) CircleImageView(de.hdodenhof.circleimageview.CircleImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) Response(retrofit2.Response) Callback(retrofit2.Callback) Ride(br.ufrj.caronae.models.Ride)

Aggregations

RideForJson (br.ufrj.caronae.models.modelsforjson.RideForJson)13 View (android.view.View)7 Intent (android.content.Intent)6 RecyclerView (android.support.v7.widget.RecyclerView)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 TextView (android.widget.TextView)5 ProgressDialog (android.app.ProgressDialog)4 ImageView (android.widget.ImageView)4 RideOfferComparatorByDateAndTime (br.ufrj.caronae.comparators.RideOfferComparatorByDateAndTime)4 Gson (com.google.gson.Gson)4 CircleImageView (de.hdodenhof.circleimageview.CircleImageView)4 Date (java.util.Date)4 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)3 RoundedTransformation (br.ufrj.caronae.RoundedTransformation)3 BindView (butterknife.BindView)3 SimpleDateFormat (java.text.SimpleDateFormat)3 ActiveRideAct (br.ufrj.caronae.acts.ActiveRideAct)2 ChatAssets (br.ufrj.caronae.models.ChatAssets)2 NewChatMsgIndicator (br.ufrj.caronae.models.NewChatMsgIndicator)2