package ch.pizzapp.android.activity; import android.content.Intent; import android.support.annotation.NonNull; import android.os.Bundle; import android.view.View; import com.afollestad.materialdialogs.DialogAction; import com.afollestad.materialdialogs.MaterialDialog; import java.util.ArrayList; import java.util.HashMap; import java.util.concurrent.locks.ReentrantLock; import butterknife.BindString; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import ch.pizzapp.android.R; import ch.pizzapp.android.api.ApiEndPoints; import ch.pizzapp.android.api.ApiErrorUtils; import ch.pizzapp.android.api.ApiService; import ch.pizzapp.android.api.ResponseArray; import ch.pizzapp.android.api.ResponseObject; import ch.pizzapp.android.helper.DialogHelper; import ch.pizzapp.android.helper.SessionHelper; import ch.pizzapp.android.helper.ViewHelper; import ch.pizzapp.android.model.AddNewAddressResponseModel; import ch.pizzapp.android.model.CityModel; import ch.pizzapp.android.model.CountryModel; import ch.pizzapp.android.model.ZoneModel; import ch.pizzapp.android.view.AppDropdownView; import ch.pizzapp.android.view.AppEditText; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public class AddAddressActivity extends BaseActivity { @BindView(R.id.address1PizzalinkEditText) AppEditText address1AppEditText; @BindView(R.id.cityPizzalinkDropdown) AppDropdownView cityPizzalinkDropdown; @BindView(R.id.postcodePizzalinkEditText) AppEditText postcodeAppEditText; @BindView(R.id.countryPizzalinkDropdown) AppDropdownView countryPizzalinkDropdown; @BindView(R.id.zonePizzalinkDropdown) AppDropdownView zonePizzalinkDropdown; @BindString(R.string.alert_fill_all_fields) String fillAllFieldsText; @BindString(R.string.alert_select_country_first) String selectCountryFirstText; @BindString(R.string.new_address_added) String newAddressAddedText; @BindString(R.string.alert_invalid_post_code) String invalidPostcodeText; private ArrayList cityList = new ArrayList<>(); private ArrayList countryList = new ArrayList<>(); private ArrayList zoneList = new ArrayList<>(); private CityModel selectedCityModel; private CountryModel selectedCountryModel; private ZoneModel selectedZoneModel; private int activeRequestCount = 0; private ReentrantLock lock = new ReentrantLock(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_address); ButterKnife.bind(this); getCityList(); //getCountryList(); } @OnClick({R.id.cityPizzalinkDropdown, R.id.countryPizzalinkDropdown, R.id.zonePizzalinkDropdown, R.id.addNewAddressButton}) protected void onClick(View view){ switch (view.getId()){ case R.id.cityPizzalinkDropdown: showCityDialog(); break; case R.id.countryPizzalinkDropdown: showCountryDialog(); break; case R.id.zonePizzalinkDropdown: if(selectedCountryModel != null){ getZoneList(); } else { DialogHelper.showAlertDialog(BaseActivity.currentActivity, selectCountryFirstText); } break; case R.id.addNewAddressButton: ViewHelper.hideKeyboard(); if(checkFields()) addNewAddress(); break; } } private boolean checkFields(){ /* if(address1PizzalinkEditText.isEmail() || selectedCityModel == null || selectedCountryModel == null || selectedZoneModel == null){ DialogHelper.showAlertDialog(this, fillAllFieldsText); return false; } */ if(address1AppEditText.isEmpty() || selectedCityModel == null || postcodeAppEditText.isEmpty()){ DialogHelper.showAlertDialog(this, fillAllFieldsText); return false; } if(postcodeAppEditText.getText().length() != 4){ DialogHelper.showAlertDialog(this, invalidPostcodeText); return false; } return true; } private void getCityList(){ if(activeRequestCount == 0) DialogHelper.showLoadingDialog(); Call> call = ApiService.apiInterface.getCityList(); call.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { decreaseActiveRequestCount(); if(activeRequestCount == 0) DialogHelper.hideLoadingDialog(); if(response.isSuccessful() && response.body().getData() != null && response.body().isSuccess()){ fillAndNotifyCityList(response.body().getData()); } else if(activeRequestCount == 0){ ApiErrorUtils.parseError(response); } } @Override public void onFailure(Call> call, Throwable t) { decreaseActiveRequestCount(); if(activeRequestCount == 0){ DialogHelper.hideLoadingDialog(); DialogHelper.showFailedDialog(); } } }); increaseActiveRequestCount(); } private void getCountryList(){ if(activeRequestCount == 0) DialogHelper.showLoadingDialog(); increaseActiveRequestCount(); Call> call = ApiService.apiInterface.getCountryList(); call.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { decreaseActiveRequestCount(); if(activeRequestCount == 0) DialogHelper.hideLoadingDialog(); if(response.isSuccessful() && response.body().getData() != null && response.body().isSuccess()){ fillAndNotifyCountryList(response.body().getData()); } else if(activeRequestCount == 0){ ApiErrorUtils.parseError(response); } } @Override public void onFailure(Call> call, Throwable t) { decreaseActiveRequestCount(); if(activeRequestCount == 0){ DialogHelper.hideLoadingDialog(); DialogHelper.showFailedDialog(); } } }); } private void getZoneList(){ DialogHelper.showLoadingDialog(); Call> call = ApiService.apiInterface.getZoneList(selectedCountryModel.getId()); call.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { DialogHelper.hideLoadingDialog(); if(response.isSuccessful() && response.body().getData() != null && response.body().isSuccess()){ fillAndShowZoneList(response.body().getData()); } else { ApiErrorUtils.parseError(response); } } @Override public void onFailure(Call> call, Throwable t) { DialogHelper.hideLoadingDialog(); DialogHelper.showFailedDialog(); } }); } private void fillAndNotifyCityList(ArrayList cities){ CityModel.checkNull(cities); cityList.clear(); cityList.addAll(cities); } private void fillAndNotifyCountryList(ArrayList countries){ CountryModel.checkNull(countries); countryList.clear(); countryList.addAll(countries); } private void fillAndShowZoneList(ArrayList zones){ ZoneModel.checkNull(zones); zoneList.clear(); zoneList.addAll(zones); showZoneDialog(); } private void showCityDialog(){ final ArrayList zoneNameList = new ArrayList<>(); for(CityModel zone : cityList){ zoneNameList.add(zone.getCity()); } DialogHelper.showListDialog(zoneNameList, new MaterialDialog.ListCallback() { @Override public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) { selectedCityModel = cityList.get(position); cityPizzalinkDropdown.setText(selectedCityModel.getCity()); } }); } private void showCountryDialog(){ final ArrayList countryNameList = new ArrayList<>(); for(CountryModel country : countryList){ countryNameList.add(country.getName()); } DialogHelper.showListDialog(countryNameList, new MaterialDialog.ListCallback() { @Override public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) { selectedCountryModel = countryList.get(position); countryPizzalinkDropdown.setText(selectedCountryModel.getName()); } }); } private void showZoneDialog(){ final ArrayList zoneNameList = new ArrayList<>(); for(ZoneModel zone : zoneList){ zoneNameList.add(zone.getName()); } DialogHelper.showListDialog(zoneNameList, new MaterialDialog.ListCallback() { @Override public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) { selectedZoneModel = zoneList.get(position); zonePizzalinkDropdown.setText(selectedZoneModel.getName()); } }); } private void addNewAddress(){ DialogHelper.showLoadingDialog(); Call> call = ApiService.apiInterface.addNewAddress( ApiEndPoints.API_ADD_NEW_ADDRESS + SessionHelper.getCustomerToken().getToken(), getAddNewAddressRequestParams()); call.enqueue(new Callback>() { @Override public void onResponse(Call> call, final Response> response) { DialogHelper.hideLoadingDialog(); if(response.isSuccessful() && response.body().getData() != null && response.body().isSuccess()){ DialogHelper.showOneButtonDialogWithCallback(newAddressAddedText, new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { dialog.dismiss(); Intent newAddressIntent = new Intent(); newAddressIntent.putExtra("newAddressId", response.body().getData().getAddressId()); setResult(RESULT_OK, newAddressIntent); finish(); } }); } else { ApiErrorUtils.parseError(response); } } @Override public void onFailure(Call> call, Throwable t) { DialogHelper.hideLoadingDialog(); DialogHelper.showFailedDialog(); } }); } private HashMap getAddNewAddressRequestParams(){ HashMap params = new HashMap<>(); params.put("address_1", address1AppEditText.getText()); params.put("address_2", ""); params.put("city", selectedCityModel.getCity()); //params.put("postcode", selectedCityModel.getPostcode()); params.put("postcode", postcodeAppEditText.getText()); //params.put("country_id", selectedCountryModel.getId()); //params.put("zone_id", selectedZoneModel.getZoneId()); params.put("country_id", "1"); params.put("zone_id", "1"); return params; } private synchronized void increaseActiveRequestCount(){ lock.lock(); try { activeRequestCount++; }finally { lock.unlock(); } } private synchronized void decreaseActiveRequestCount(){ lock.lock(); try { activeRequestCount--; }finally { lock.unlock(); } } }