Пример #1
0
 @Override
 protected void onAttachedToWindow() {
   mTickerStopped = false;
   super.onAttachedToWindow();
   mHandler = new Handler();
   mTicker =
       new Runnable() {
         public void run() {
           if (mTickerStopped) return;
           updateClock();
           long now = SystemClock.uptimeMillis();
           long next = now + (1000 - now % 1000);
           mHandler.postAtTime(mTicker, next);
         }
       };
   mTicker.run();
 }
  private void placeOrder() {
    EditText[] entries =
        new EditText[] {
          phoneNumberField,
          address1Field,
          address2Field,
          cityField,
          stateField,
          zipCodeField,
          countryField
        };
    for (EditText entry : entries) {
      entry.setEnabled(false);
    }

    user.setFirstName(firstNameField.getText().toString());
    user.setLastName(lastNameField.getText().toString());
    user.setPhone(phoneNumberField.getText().toString());
    user.setAddress1(address1Field.getText().toString());
    user.setAddress2(address2Field.getText().toString());
    user.setCity(cityField.getText().toString());
    user.setState(stateField.getText().toString());
    user.setZipCode(zipCodeField.getText().toString());
    Country selectedCountry = Countries.getCountryForName(countryField.getText().toString());
    if (selectedCountry != null) {
      user.setCountry(selectedCountry.getCode());
    }

    ProgressDialog progressDialog =
        ProgressDialog.show(getActivity(), "Please wait...", "Placing Order", true);

    RoboVMWebService.getInstance()
        .placeOrder(
            user,
            (response) -> {
              progressDialog.hide();
              progressDialog.dismiss();
              for (EditText entry : entries) {
                entry.setEnabled(true);
              }

              if (response.isSuccess()) {
                RoboVMWebService.getInstance().getBasket().clear();

                Toast.makeText(getActivity(), "Your order has been placed!", Toast.LENGTH_LONG)
                    .show();

                if (orderPlacedListener != null) {
                  orderPlacedListener.run();
                }
              } else {
                List<ValidationError> errors = response.getErrors();
                String alertMessage = "An unexpected error occurred! Please try again later!";

                if (errors != null) { // We handle only the first error.
                  ValidationError error = errors.get(0);

                  String message = error.getMessage();
                  String field = error.getField();
                  if (field == null) {
                    alertMessage = message;
                  } else {
                    switch (field) {
                      case "firstName":
                        alertMessage = "First name is required";
                        break;
                      case "lastName":
                        alertMessage = "Last name is required";
                        break;
                      case "address1":
                        alertMessage = "Address is required";
                        break;
                      case "city":
                        alertMessage = "City is required";
                        break;
                      case "zipCode":
                        alertMessage = "ZIP code is required";
                        break;
                      case "phone":
                        alertMessage = "Phone number is required";
                        break;
                      case "country":
                        alertMessage = "Country is required";
                        break;
                      default:
                        alertMessage = message;
                        break;
                    }
                  }
                }
                Toast.makeText(getActivity(), alertMessage, Toast.LENGTH_LONG).show();
              }
            });
  }