@Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

    // Changing the backend url selection (internal, external, etc.)
    if (StringUtils.equals(key, getString(R.string.preference_key_key_base_url))
        || StringUtils.equals(key, getString(R.string.preference_key_value_base_url))) {
      String keyValue = sharedPreferences.getString(key, "");
      Preference keyPreference = findPreference(key);

      String[] keysTab = getResources().getStringArray(R.array.backend_url_keys);
      String[] valuesTab = getResources().getStringArray(R.array.backend_url_values);

      boolean resetBackendEndConnectors = true;

      if (StringUtils.equals(key, getString(R.string.preference_key_key_base_url))) {
        int indexKey = ArrayUtils.indexOf(keysTab, keyValue);

        // Setting the description to the right key
        keyPreference.setSummary(keyValue);

        // For custom key don't empty the field
        if (indexKey < keysTab.length - 1) {

          // If something changed
          if (!StringUtils.equals(
              valuesTab[indexKey],
              mSharedPreferences.getString(
                  getString(R.string.preference_key_value_base_url), ""))) {
            Preference prefValueBackendUrl =
                findPreference(getString(R.string.preference_key_value_base_url));
            prefValueBackendUrl.setSummary(valuesTab[indexKey]);
            ((EditTextPreference) prefValueBackendUrl).setText(valuesTab[indexKey]);

            // Saving the value on the settings
            SharedPreferences.Editor editor = mSharedPreferences.edit();
            editor.putString(
                getString(R.string.preference_key_value_base_url), valuesTab[indexKey]);
            editor.commit();
          } else {
            resetBackendEndConnectors = false;
          }
        }
      }
      // Changing the backend url value
      else {
        // Updating the description (Or custom if no value found)
        Preference prefKeyBackendUrl =
            findPreference(getString(R.string.preference_key_key_base_url));
        prefKeyBackendUrl.setSummary(keysTab[ArrayUtils.indexOf(valuesTab, keyValue)]);
        keyPreference.setSummary(keyValue);
      }

      // Update the url of the backend
      if (resetBackendEndConnectors) {
        B2BApplication.updateUrl(
            mSharedPreferences.getString(getString(R.string.preference_key_value_base_url), ""));
      }
    }
  }
Ejemplo n.º 2
0
  /**
   * Add/Update an item to the cart.
   *
   * @param activity The activity that calls the method
   * @param requestId Identifier for the call
   * @param onAddToCart Listener for success/error after update
   * @param isUpdate Flag for update
   * @param productCode The code of the product to add
   * @param entryNumber The entry number to update on the cart
   * @param quantity The quantity to add
   * @param viewsToDisable Views to disable/enable before/after the request
   * @param onRequestListener Request listener for before/after call actions
   */
  private static void addOrUpdateToCart(
      final Activity activity,
      final String requestId,
      final OnAddToCart onAddToCart,
      final boolean isUpdate,
      final String productCode,
      final int entryNumber,
      final int quantity,
      final List<View> viewsToDisable,
      final OnRequestListener onRequestListener) {

    if (quantity > 0) {
      UIUtils.showLoadingActionBar(activity, true);

      QueryCart queryCart = new QueryCart();
      queryCart.setQuantity(quantity);

      // Update
      if (isUpdate) {
        queryCart.setProduct(entryNumber + "");

        B2BApplication.getContentServiceHelper()
            .updateCartEntry(
                new ResponseReceiver<ProductAdded>() {
                  @Override
                  public void onResponse(Response<ProductAdded> response) {
                    onReceiveAddToCartResponse(activity, requestId, onAddToCart, response);
                  }

                  @Override
                  public void onError(Response<DataError> response) {
                    UIUtils.showLoadingActionBar(activity, false);

                    UIUtils.showError(response, activity);

                    // Update the cart
                    SessionHelper.updateCart(activity, requestId, false);
                  }
                },
                requestId,
                queryCart,
                false,
                viewsToDisable,
                onRequestListener);
      }
      // Add
      else {
        queryCart.setProduct(productCode);

        B2BApplication.getContentServiceHelper()
            .addProductToCart(
                new ResponseReceiver<ProductAdded>() {

                  @Override
                  public void onResponse(Response<ProductAdded> response) {
                    onReceiveAddToCartResponse(activity, requestId, onAddToCart, response);
                  }

                  @Override
                  public void onError(Response<DataError> response) {
                    UIUtils.showLoadingActionBar(activity, false);

                    UIUtils.showError(response, activity);

                    // Update the cart
                    SessionHelper.updateCart(activity, requestId, false);
                  }
                },
                requestId,
                queryCart,
                false,
                viewsToDisable,
                onRequestListener);
      }
    }
  }