Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
13 kB
3
Indexable
Never
  public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        int hourOfDay = 0;
        int minute = 0;
        int hour = Integer.parseInt(mOrder.restaurantCart.getRestaurant().getMaxDeliveryTime()) / 60;
        int min = Integer.parseInt(mOrder.restaurantCart.getRestaurant().getMaxDeliveryTime()) % 60;
        String date = view.getYear() + "-" + (view.getMonth() + 1) + "-" + view.getDayOfMonth();
        final DateTime selectedDate = new DateTime(date);
        final DateTime currentDate = new DateTime();

        nextOpenTimeTemp = UiUtils
                .tryParseDateTime(mOrder.restaurantCart.getRestaurant().getNextOpentime(),
                        "yyyy-MM-dd hh:mm a");

        if (mOrder.deliveryTime == null) {
            if (mOrder.restaurantCart.getRestaurant().getOstatus()) {
                // Restaurant open

                if (UiUtils.compareDates(currentDate, selectedDate) == 0) {
                    // current time + deliveryTime
                    // today's date
                    hourOfDay = currentDate.getHourOfDay() + hour;
                    minute = currentDate.getMinuteOfHour() + min;
                    if (minute >= 60) {
                        hourOfDay = hourOfDay + minute / 60;
                        minute = minute % 60;
                    }
                } else {
                    // rest open time + delivery Time
                    DateTime firstOpenTime = null;

                    // get day name
                    String dayName = DateTimeFormat.forPattern("EEEE").print(selectedDate);
                    for (RestaurantTiming timing : mOrder.restaurantCart.getRestaurant().getRestaurantTimings()) {
                        if (dayName.equalsIgnoreCase(timing.dayName)) {
                            if (timing.openTime != null) {
                                firstOpenTime = DateTimeFormat.forPattern("HH:mm")
                                        .parseDateTime(timing.openTime.get(0));
                            }

                        }
                    }
                    if (firstOpenTime != null) {
                        firstOpenTime = firstOpenTime
                                .withYear(year)
                                .withMonthOfYear(monthOfYear + 1)
                                .withDayOfMonth(dayOfMonth);

                        hourOfDay = firstOpenTime.getHourOfDay() + hour;
                        minute = firstOpenTime.getMinuteOfHour() + min;
                        if (minute >= 60) {
                            hourOfDay = hourOfDay + minute / 60;
                            minute = minute % 60;
                        }
                    }
                }
            } else {
                //Restaurant close
                // rest next open time + delivery Time
                DateTime nextOpenTime = UiUtils
                        .tryParseDateTime(mOrder.restaurantCart.getRestaurant().getNextOpentime(),
                                "yyyy-MM-dd hh:mm a");

                if (nextOpenTime != null) {
                    nextOpenTime = nextOpenTime
                            .withYear(year)
                            .withMonthOfYear(monthOfYear + 1)
                            .withDayOfMonth(dayOfMonth);

                    hourOfDay = nextOpenTime.getHourOfDay() + hour;
                    minute = nextOpenTime.getMinuteOfHour() + min;
                    if (minute >= 60) {
                        hourOfDay = hourOfDay + minute / 60;
                        minute = minute % 60;
                    }
                }
            }
        } else {
            hourOfDay = mOrder.deliveryTime.getHourOfDay();
            minute = mOrder.deliveryTime.getMinuteOfHour();
        }

        // show time picker
        if (mTimePickerDialog == null) {
            mTimePickerDialog = new TimePickerDialog(this, R.style.DatePickerStyle, (view1, hourOfDay1, minute1) -> {

                // set selected time
                DateTime selectedTime = new DateTime()
                        .withHourOfDay(hourOfDay1)
                        .withMinuteOfHour(minute1)
                        .withYear(year)
                        .withMonthOfYear(monthOfYear + 1)
                        .withDayOfMonth(dayOfMonth);
                mInvalid = false;

                if (selectedTime.isAfter(DateTime.now().getMillis())) {
                    // get day name
                    String dayName = DateTimeFormat.forPattern("EEEE").print(selectedTime);

                    // get restaurant timings
                    DateTime firstOpenTime = null;
                    DateTime secondOpenTime = null;
                    DateTime firstCloseTime = null;
                    DateTime secondCloseTime = null;
                    for (RestaurantTiming timing : mOrder.restaurantCart.getRestaurant().getRestaurantTimings()) {
                        if (dayName.equalsIgnoreCase(timing.dayName)) {
                            if (timing.openTime != null && timing.closeTime != null) {
                                if (timing.openTime.size() > 1 && !TextUtils.isEmpty(timing.openTime.get(1))) {
                                    firstOpenTime = DateTimeFormat.forPattern("HH:mm")
                                            .parseDateTime(timing.openTime.get(0));
                                    secondOpenTime = DateTimeFormat.forPattern("HH:mm")
                                            .parseDateTime(timing.openTime.get(1));

                                    firstCloseTime = DateTimeFormat.forPattern("HH:mm")
                                            .parseDateTime(timing.closeTime.get(0));
                                    secondCloseTime = DateTimeFormat.forPattern("HH:mm")
                                            .parseDateTime(timing.closeTime.get(1));

                                } else {
                                    firstOpenTime = DateTimeFormat.forPattern("HH:mm")
                                            .parseDateTime(timing.openTime.get(0));
                                    firstCloseTime = DateTimeFormat.forPattern("HH:mm")
                                            .parseDateTime(timing.closeTime.get(0));
                                }
                            }
                        }
                    }

                    if (firstOpenTime != null) {
                        // set selected date and time
                        mOrder.deliveryTime = selectedTime;

                        firstOpenTime = firstOpenTime
                                .withYear(year)
                                .withMonthOfYear(monthOfYear + 1)
                                .withDayOfMonth(dayOfMonth);

                        firstCloseTime = firstCloseTime
                                .withYear(year)
                                .withMonthOfYear(monthOfYear + 1)
                                .withDayOfMonth(dayOfMonth);

                        firstOpenTime = firstOpenTime.plusHours(hour);
                        firstOpenTime = firstOpenTime.plusMinutes(min);
                        //selected time before open time
                        if (selectedTime.isBefore(firstOpenTime.getMillis())) {
                            firstOpenTime = firstOpenTime.minusDays(1);
                            firstCloseTime = firstCloseTime.minusDays(1);
                        }

                        mTimeValid = UiUtils.checkValidDate(firstOpenTime, firstCloseTime, selectedTime);

                        if (!mTimeValid && secondOpenTime != null) {
                            secondOpenTime = secondOpenTime
                                    .withYear(year)
                                    .withMonthOfYear(monthOfYear + 1)
                                    .withDayOfMonth(dayOfMonth);

                            secondCloseTime = secondCloseTime
                                    .withYear(year)
                                    .withMonthOfYear(monthOfYear + 1)
                                    .withDayOfMonth(dayOfMonth);

                            mTimeValid = UiUtils.checkValidDate(secondOpenTime, secondCloseTime, selectedTime);
                        }

                        if (!mTimeValid) {
                            mInvalid = true;
                            UiUtils.showAlertDialog(this, getString(R.string.restaurant_closed_more_time), null);
//                            ErrorHandler.showErrorMessage(RestaurantCartActivity.this, R.string.restaurant_closed_more_time);
                        }
                    }

//                    mLAyoutDeliveryDate.setVisibility(View.VISIBLE);
//                    mLayoutDeliverNow.setVisibility(View.GONE);
                    String dateToDisplay;
                    String newDateToDisplay;

                    String formattedDate = DateTimeFormat.forPattern("dd MMM yy").print(selectedTime);
                    String formattedTime = DateTimeFormat.forPattern("h:mm a").print(selectedTime)
                            .replace("am", "AM").replace("pm", "PM");

                    if (UiUtils.compareDates(currentDate, selectedTime) == 0) {
                        dateToDisplay = DateTimeFormat.forPattern("h:mm a").print(selectedTime) + getString(R.string.today);
                        newDateToDisplay = dateToDisplay.replace("am", "AM").replace("pm", "PM");
                        binding.layoutOrderDetails.layoutSchedule.txvDetails.setText(getString(R.string.scheduled, newDateToDisplay));
//                        mTxvDeliveryDate.setText(getString(R.string.scheduled, newDateToDisplay));
                    } else if (UiUtils.compareDates(currentDate.plusDays(1), selectedTime) == 0) {
                        dateToDisplay = DateTimeFormat.forPattern("h:mm a").print(selectedTime) + getString(R.string.tomorrow);
                        newDateToDisplay = dateToDisplay.replace("am", "AM").replace("pm", "PM");
                        binding.layoutOrderDetails.layoutSchedule.txvDetails.setText(getString(R.string.scheduled, newDateToDisplay));
                    } else {
                        dateToDisplay = DateTimeFormat.forPattern("h:mm a EEE, dd MMM yyyy").print(selectedTime);
                        newDateToDisplay = dateToDisplay.replace("am", "AM").replace("pm", "PM");
                        binding.layoutOrderDetails.layoutSchedule.txvDetails.setText(getString(R.string.scheduled, newDateToDisplay));
                    }
                    mOrder.deliveryType = DeliveryType.SCHEDULE_DELIVERY;
//                    mRdbDeliverNow.setChecked(false);
//                    mRdbScheduleDelivery.setChecked(true);
//                    mDeleverNow.clearCheck();

                    binding.layoutOrderDetails.layoutSchedule.layoutTimeDetails.setVisibility(View.VISIBLE);
                    binding.layoutOrderDetails.layoutSchedule.edtDate.setText(formattedDate);
                    binding.layoutOrderDetails.layoutSchedule.edtTime.setText(formattedTime);
                    binding.layoutOrderDetails.layoutSchedule.txvSchedule.setText(getString(R.string.remove));

                    if(nextOpenTimeTemp != null){
                        int nexthour = Integer.parseInt(mOrder.restaurantCart.getRestaurant().getMaxDeliveryTime()) / 60;
                        int nextmin = Integer.parseInt(mOrder.restaurantCart.getRestaurant().getMaxDeliveryTime()) % 60;
                        nextOpenTimeTemp = nextOpenTimeTemp.plusHours(nexthour);
                        nextOpenTimeTemp = nextOpenTimeTemp.plusMinutes(nextmin);
                        if (selectedTime.isBefore(nextOpenTimeTemp)){
                            mInvalid = true;
                            UiUtils.showAlertDialog(this, getString(R.string.restaurant_closed_more_time), null);
                            mOrder.deliveryTime = selectedTime;
                        }
                    }

                } else {
                    mInvalid = true;
                    // set selected date and time
                    mOrder.deliveryTime = selectedTime;

                    UiUtils.showAlertDialog(this, getString(R.string.invalid_time), null);
//                    ErrorHandler.showErrorMessage(RestaurantCartActivity.this, R.string.invalid_time);
                }

            }, hourOfDay, minute, false);
            mTimePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), this);
            mTimePickerDialog.show();

            if (mOrder.restaurantCart.getRestaurant().getOstatus()) {
//                mRdbDeliverNow.setChecked(true);
//                mRdbScheduleDelivery.setChecked(false);
//                mScheduleDelvery.clearCheck();
            }
        }
    }
Leave a Comment