Skip to content

Instantly share code, notes, and snippets.

@johnzuh
Created October 22, 2013 12:34
Show Gist options
  • Save johnzuh/7099873 to your computer and use it in GitHub Desktop.
Save johnzuh/7099873 to your computer and use it in GitHub Desktop.
SABC form extension for magento android app
/**
* Magento Mobile
*
* NOTICE OF LICENSE
*
* This source file is subject to the Magento Mobile License
* that is bundled with this package in the file LICENSE_MOBILE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.magentocommerce.com/license/mobile-source-code
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
*
* @copyright Copyright (c) 2010 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license http://www.magentocommerce.com/license/mobile-source-code
*/
package com.magentocommerce.magentomobile;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.ToggleButton;
import com.magentocommerce.magentomobile.ContactUtils.ContactsLoaderInterface;
import com.magentocommerce.magentomobile.Loader.LoaderInterface;
import com.magentocommerce.magentomobile.SelectionAdapter.AdapterInterface;
/**
* AddressBookRecord is abstract class, which extends LinearLayout and implements ViewComponent, NewAddressInterface, ContactsLoaderInterface interfaces. It is aimed for
* creating billing(shipping) address while doing checkout.
*/
public abstract class CheckoutAddressCreationView extends LinearLayout implements ViewComponent, AdapterInterface, ContactsLoaderInterface{
/**
* The XmlNode object with countries data - names, codes.
*/
protected XmlNode countries = null;
protected String vatTypeFieldName = null;
protected String titleFieldName = null;
protected String idTypeFieldName = null;
protected XmlNode vat_types = null;
protected XmlNode titles = null ;
protected XmlNode id_types = null;
protected int selectedVatType = -1;
protected int selectedTitle = -1;
protected int selectedIdType = -1;
/**
* String with url, which used for saving address data.
*/
protected String url_save_address = null;
/**
* String with url, which used for loading address data.
*/
protected String url = null;
protected XmlNode SABCForm = null;
protected String url_sabc = null;
protected String url_sabc_save = null;
public static boolean hasTelevision = false;
public Loader SABCLoader;
/**
* The position of the selected country.
*/
protected int selectedCountry = -1;
/**
* The position of the selected region.
*/
protected int selectedRegion = -1;
/**
* The name of region field with "select" type.
*/
protected String regionSelectFieldName = null;
/**
* The name of region field with "text" type.
*/
protected String regionTextFieldName = null;
/**
* The name of country field with "select" type.
*/
protected String countryFieldName = null;
/**
* The data with information about address book record's fields, which is loaded from the server.
*/
protected XmlNode form = null;
/**
* Array with listview's items.
*/
protected View fields[] = null;
/**
* The title of the ViewComponent.
*/
protected String title = null;
/**
* String array, which contains contact field names
*/
protected String[] strContactTags;
/**
* String array, which contains address field names
*/
protected String[] strAddressTags;
/**
* ArrayList of address parameters, that we send to the server.
*/
protected ArrayList <NameValuePair> nameValuePairs = new ArrayList <NameValuePair>();
/**
* ArrayList of default address parameters.
*/
protected ArrayList <NameValuePair> defaultValuePairs = null;
/**
* The object of the ListView class, which is used for displaying the list of the fields.
*/
protected ListView list;
/**
* A flag that indicates whether the address book is empty or not.
*/
protected boolean isEmptyAddressBook;
/**
* The object, which implements LoaderInterface. Used while sending address fields values to the server.
*/
protected LoaderInterface addressSaveListener;
protected Loader addressSaveLoader;
protected boolean dataReloadRequested = false;
/**
* The object, which implements LoaderInterface. Used while loading address book record fields from the server.
*/
protected LoaderInterface addressLoaderListener = new LoaderInterface() {
@Override
public void preExecute() {
findViewById(R.id.Title).setVisibility(View.GONE);
findViewById(R.id.FieldList).setVisibility(View.GONE);
findViewById(R.id.ProgressContainer).setVisibility(View.VISIBLE);
}
@Override
public void preExecuteInBackground() {
}
@Override
public void postExecuteInBackground(XmlNode data, boolean success) {
}
@Override
public void postExecute(XmlNode data, boolean success) {
findViewById(R.id.ProgressContainer).setVisibility(View.GONE);
if (success) {
findViewById(R.id.Title).setVisibility(View.VISIBLE);
findViewById(R.id.FieldList).setVisibility(View.VISIBLE);
form = data;
list.setAdapter(new AddressCreationAdapter());
if (defaultValuePairs != null) {
initWithDefaultValues(defaultValuePairs);
}
}
}
};
LoaderInterface SABCLoaderListener = new LoaderInterface() {
@Override
public void preExecute() {
}
@Override
public void preExecuteInBackground() {
}
@Override
public void postExecuteInBackground(XmlNode data, boolean success) {
}
@Override
public void postExecute(XmlNode data, boolean success) {
if (success) {
SABCForm = data;
String hastv = SABCForm.getChildWithName("field").getAttributeValue("id");
if (hastv.equalsIgnoreCase("has_tv") ) {
hasTelevision=false;
}else{
hasTelevision=true;
}
}
}
};
/**
* Class constructor.
*/
public CheckoutAddressCreationView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void init() {
MagentoActivity activity = ((MagentoActivity)getContext());
((TitleBar)findViewById(R.id.title_layout)).init(title);
setBackgroundColor(MagentoActivity.Colors.scrollBackgroundColor);
findViewById(R.id.title_home_button).setVisibility(GONE);
findViewById(R.id.title_home_button_sep).setVisibility(GONE);
findViewById(R.id.title_button_1).setVisibility(GONE);
findViewById(R.id.title_button_2).setVisibility(GONE);
findViewById(R.id.title_sep_1).setVisibility(GONE);
findViewById(R.id.title_sep_2).setVisibility(GONE);
url_sabc = activity.getResources().getString(R.string.url_sabc_form);
list = (ListView)findViewById(R.id.FieldList);
list.setBackgroundColor(MagentoActivity.Colors.scrollBackgroundColor);
list.setCacheColorHint(MagentoActivity.Colors.scrollBackgroundColor);
list.setDivider(MagentoActivity.getDivider(MagentoActivity.Colors.tintColor));
list.setDividerHeight(MagentoActivity.dip2px(1));
findViewById(R.id.Title).setBackgroundDrawable(MagentoActivity.getGradientDrawable(MagentoActivity.Colors.primaryColor, 0x000A0A0A));
CustomTextButton newAddressButton = ((CustomTextButton)findViewById(R.id.NewAddressButton));
newAddressButton.setTintColor(MagentoActivity.Colors.secondaryColor);
newAddressButton.setTypeface(activity.fontManager.getFontFace("Title1"));
newAddressButton.setTextSize(activity.fontManager.getFontSize("Title1"));
newAddressButton.setTextColor(activity.fontManager.getFontColor("Title1"));
newAddressButton.setText(activity.locMan.getString("Continue"));
newAddressButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
saveAddressData(addressSaveListener, null);
}
});
}
@Override
public void loadRemoteData(CountDownLatch doneSignal) {
new Loader(url, null, true, addressLoaderListener, false, doneSignal).execute();
new Loader(url_sabc, null, true, SABCLoaderListener, false, doneSignal).execute();
}
/**
* Gets the index of the tagName in strContactTags array
* @param tagName is the name of contact field
* @return index of the tagName in the strContactTags. -1 otherwise
*/
public int getContactIndex(String tagName) {
for (int i = 0; i < strContactTags.length; i++) {
if (strContactTags[i].equalsIgnoreCase(tagName)) {
return i;
}
}
return -1;
}
/**
* Gets the index of the tagName in strAddressTags array
* @param tagName is the name of contact field
* @return index of the tagName in the strAddressTags. -1 otherwise
*/
public int getAddressIndex(String tagName) {
for (int i = 0; i < strAddressTags.length; i++) {
if (strAddressTags[i].equalsIgnoreCase(tagName)) {
return i;
}
}
return -1;
}
/**
* Checks the values, that were filled into the form and sends them to the server
*/
public Loader saveAddressData(LoaderInterface listener, CountDownLatch doneSignal) {
addressSaveLoader = null;
MagentoActivity activity = ((MagentoActivity)getContext());
try {
nameValuePairs.clear();
for (int i = 0; i < form.getChildNodes().size(); i++) {
//create field
XmlNode field = form.getChildNodes().get(i);
String name = field.getAttributeValue("name");
String type = field.getAttributeValue("type");
String label = field.getAttributeValue("label");
Boolean required = false;
if (field.getAttributeValue("required")!=null && field.getAttributeValue("required").equalsIgnoreCase("true")) {
required = true;
}
if (name.equalsIgnoreCase(regionSelectFieldName) && selectedCountry>=0 && countries.getChildNodes().get(selectedCountry).getChildWithName("regions") == null) {
required = false;
}
if (getSelectedVatType()>=0 && getSelectedTitle()>=0 && getSelectedIdType()>=0 ) {
required = false;
}
LinearLayout layout = (LinearLayout)((AddressCreationAdapter)list.getAdapter()).formItems.get(i);
if (type.equalsIgnoreCase("text")) {
TextView textView = (TextView)fields[i+1].findViewById(R.id.EditText);
XmlNode validators = field.getChildWithName("validators");
if (required&&textView.getText().toString().trim().length()==0) {
throw new Exception("Field "+label+" is required!");
} else if (validators!=null) {
for (int k = 0; k < validators.getChildNodes().size(); k++) {
XmlNode validator = validators.getChildNodes().get(k);
String validatorMessage = validator.getAttributeValue("message");
String validatorType = validator.getAttributeValue("type");
String validatorValue = validator.getNodeValue();
if (!FieldValidator.checkTextWithValidator(validatorType, validatorValue, textView.getText().toString(), null, null)) {
throw new Exception(validatorMessage);
}
}
}
nameValuePairs.add(new BasicNameValuePair(name,textView.getText().toString()));
}
else if (type.equalsIgnoreCase("select")) {
if(name.equalsIgnoreCase(CheckoutAddressCreationView.this.regionSelectFieldName)) {
if (selectedCountry>=0 && selectedRegion>=0 && countries.getChildNodes().get(selectedCountry).getChildWithName("regions") != null && countries.getChildNodes().get(selectedCountry).getChildWithName("regions").getChildNodes().size() > selectedRegion){
XmlNode region = countries.getChildNodes().get(selectedCountry).getChildWithName("regions").getChildNodes().get(selectedRegion);
nameValuePairs.add(new BasicNameValuePair(name,region.getChildWithName("value").getNodeValue()));
} else if (required) {
throw new Exception(label + " " + MagentoActivity.getContext().locMan.getString("RequiredFieldMessage"));
}
}
else if (name.equalsIgnoreCase(CheckoutAddressCreationView.this.countryFieldName)) {
try {
Spinner s = (Spinner)layout.findViewWithTag(name);
XmlNode values = field.getChildWithName("values") ;
String value = values.getChildNodes().get( s.getSelectedItemPosition() ).getNodeValue();
nameValuePairs.add( new BasicNameValuePair( name, value ) );
}catch (Exception e) {
//nameValuePairs.clear();
//nameValuePairs = null;
activity.dialog_text = e.getLocalizedMessage();
//activity.showDialogOnUiThread(name + " " + MagentoActivity.getContext().locMan.getString("RequiredFieldMessage"));
throw new Exception(label + " " + MagentoActivity.getContext().locMan.getString("RequiredFieldMessage"));
}
}
else if (name.equalsIgnoreCase(CheckoutAddressCreationView.this.vatTypeFieldName)) {
try {
Spinner s = (Spinner)layout.findViewWithTag(name);
XmlNode values = field.getChildWithName("values") ;
String value = values.getChildNodes().get( s.getSelectedItemPosition() ).getNodeValue();
nameValuePairs.add( new BasicNameValuePair( name, value ) );
}catch (Exception e) {
//nameValuePairs.clear();
//nameValuePairs = null;
activity.dialog_text = e.getLocalizedMessage();
//activity.showDialogOnUiThread(name + " " + MagentoActivity.getContext().locMan.getString("RequiredFieldMessage"));
throw new Exception(label + " " + MagentoActivity.getContext().locMan.getString("RequiredFieldMessage"));
}
}
else if (name.equalsIgnoreCase(CheckoutAddressCreationView.this.titleFieldName)) {
try {
//Spinner s = (Spinner)layout.findViewWithTag(name);
XmlNode values = field.getChildWithName("values") ;
String value = values.getChildNodes().get(selectedTitle).getNodeValue();
nameValuePairs.add( new BasicNameValuePair( name, value ) );
}catch (Exception e) {
//nameValuePairs.clear();
//nameValuePairs = null;
activity.dialog_text = e.getLocalizedMessage();
//activity.showDialogOnUiThread(name + " " + MagentoActivity.getContext().locMan.getString("RequiredFieldMessage"));
throw new Exception(label + " " + MagentoActivity.getContext().locMan.getString("RequiredFieldMessage"));
}
}
else if (name.equalsIgnoreCase(CheckoutAddressCreationView.this.idTypeFieldName)) {
try {
//Spinner s = (Spinner)layout.findViewWithTag(name);
XmlNode values = field.getChildWithName("values") ;
String value = values.getChildNodes().get(selectedIdType).getNodeValue();
nameValuePairs.add( new BasicNameValuePair( name, value ) );
}catch (Exception e) {
//nameValuePairs.clear();
//nameValuePairs = null;
activity.dialog_text = e.getLocalizedMessage();
//activity.showDialogOnUiThread(name + " " + MagentoActivity.getContext().locMan.getString("RequiredFieldMessage"));
throw new Exception(label + " " + MagentoActivity.getContext().locMan.getString("RequiredFieldMessage"));
}
}
} else if(type.equalsIgnoreCase("checkbox")) {
ToggleButton button = (ToggleButton)fields[i+1].findViewById(R.id.ToggleButton);
if (button.isChecked()) {
nameValuePairs.add(new BasicNameValuePair(name,"1"));
} else {
nameValuePairs.add(new BasicNameValuePair(name,"0"));
}
}
}
addressSaveLoader = new Loader(url_save_address, nameValuePairs, true, listener);
addressSaveLoader.execute();
} catch (Exception e) {
nameValuePairs.clear();
activity.dialog_text = e.getLocalizedMessage();// "Field "+name+"is required!";
activity.showDialogOnUiThread(MagentoActivity.DIALOG_ERROR_TEXT);
}
return addressSaveLoader;
}
public void selectCountry(int position) {
Spinner s;
AddressCreationAdapter addressCreationAdapter = (AddressCreationAdapter)list.getAdapter();
for (int i = 0; i < form.getChildNodes().size(); i++) {
XmlNode field = form.getChildNodes().get(i);
String name = field.getAttributeValue("name");
if (name != null) {
if (name.equalsIgnoreCase(countryFieldName)) {
s = (Spinner)((LinearLayout)fields[i+1]).getChildAt(0);
if (position >= 0) {
s.setSelection(position);
} else {
s.setSelection(s.getAdapter().getCount()-1);
}
((SelectionAdapter)s.getAdapter()).notifyDataSetChanged();
} else if(name.equalsIgnoreCase(regionTextFieldName)) {
if (position==-1 || countries.getChildNodes().get(position).getChildWithName("regions") == null) {
fields[i+1].setVisibility(View.VISIBLE);
} else if (countries.getChildNodes().get(position).getChildNodes().size()>0){
fields[i+1].setVisibility(View.GONE);
}
} else if(name.equalsIgnoreCase(regionSelectFieldName)) {
if (position==-1 || countries.getChildNodes().get(position).getChildWithName("regions") == null) {
fields[i+1].setVisibility(View.GONE);
} else {
fields[i+1].setVisibility(View.VISIBLE);
s = (Spinner)((LinearLayout)fields[i+1]).getChildAt(0);
SelectionAdapter selectionAdapter;
if (selectedCountry != position) {
if (s == null) {
s = new LocationSpinner(getContext(), countries.getChildNodes().get(position).getChildWithName("regions"), getResources().getString(R.string.spinner_prompt)+" region");
((LinearLayout)fields[i+1]).addView(s);
((LinearLayout.LayoutParams)s.getLayoutParams()).topMargin = 2;
((LinearLayout.LayoutParams)s.getLayoutParams()).leftMargin = 5;
((LinearLayout.LayoutParams)s.getLayoutParams()).rightMargin = 5;
((LinearLayout.LayoutParams)s.getLayoutParams()).width = LayoutParams.FILL_PARENT;
((LinearLayout.LayoutParams)s.getLayoutParams()).height = LayoutParams.FILL_PARENT;
}
selectionAdapter = new SelectionAdapter(countries.getChildNodes().get(position).getChildWithName("regions"), 1);
//selectionAdapter.delegate = this;
((Spinner)s).setAdapter(selectionAdapter);
selectionAdapter.spinner = (LocationSpinner) s;
s.setAdapter(selectionAdapter);
} else {
selectionAdapter = (SelectionAdapter) s.getAdapter();
}
selectedRegion = -1;
((LocationSpinner)s).updateDataSet(countries.getChildNodes().get(position).getChildWithName("regions"));
s.setSelection(selectionAdapter.getCount()-1);
selectionAdapter.notifyDataSetChanged();
}
}
}
}
selectedCountry = position;
addressCreationAdapter.notifyDataSetChanged();
}
/**
* Used for region selection.
* @param position is the position of the selected region.
*/
public void selectRegion(int position) {
selectedRegion = position;
for (int i = 0; i < form.getChildNodes().size(); i++) {
XmlNode field = form.getChildNodes().get(i);
String name = field.getAttributeValue("name");
if (name != null && name.equalsIgnoreCase(regionSelectFieldName)) {
Spinner s = (Spinner)((LinearLayout)fields[i+1]).getChildAt(0);
if (selectedRegion >= 0) {
s.setSelection(selectedRegion);
} else {
s.setSelection(s.getAdapter().getCount()-1);
}
((SelectionAdapter)s.getAdapter()).notifyDataSetChanged();
}
}
}
/**
* Used for Vat Type Selection.
* @param position is the position of the selected VatType.
*/
public void selectVatType(int position) {
selectedVatType = position;
for (int i = 0; i < form.getChildNodes().size(); i++) {
XmlNode field = form.getChildNodes().get(i);
String name = field.getAttributeValue("name");
if (name != null && name.equalsIgnoreCase(vatTypeFieldName)) {
Spinner s = (Spinner)((LinearLayout)fields[i+1]).getChildAt(0);
if (selectedVatType >= 0) {
s.setSelection(selectedVatType);
} else {
s.setSelection(s.getAdapter().getCount()-1);
}
((SelectionAdapterVTI)s.getAdapter()).notifyDataSetChanged();
}
}
}
public void selectTitle(int position) {
Spinner s;
AddressCreationAdapter addressCreationAdapter = (AddressCreationAdapter)list.getAdapter();
for (int i = 0; i < form.getChildNodes().size(); i++) {
XmlNode field = form.getChildNodes().get(i);
String name = field.getAttributeValue("name");
if (name != null) {
if (name.equalsIgnoreCase(titleFieldName)) {
s = (Spinner)((LinearLayout)fields[i+1]).getChildAt(0);
if (position >= 0) {
s.setSelection(position);
} else {
s.setSelection(s.getAdapter().getCount()-1);
}
((SelectionAdapter)s.getAdapter()).notifyDataSetChanged();
}
}
}
selectedTitle = position;
addressCreationAdapter.notifyDataSetChanged();
}
public void selectIdType(int position) {
Spinner s;
AddressCreationAdapter addressCreationAdapter = (AddressCreationAdapter)list.getAdapter();
for (int i = 0; i < form.getChildNodes().size(); i++) {
XmlNode field = form.getChildNodes().get(i);
String name = field.getAttributeValue("name");
if (name != null) {
if (name.equalsIgnoreCase(idTypeFieldName)) {
s = (Spinner)((LinearLayout)fields[i+1]).getChildAt(0);
if (position >= 0) {
s.setSelection(position);
} else {
s.setSelection(s.getAdapter().getCount()-1);
}
((SelectionAdapter)s.getAdapter()).notifyDataSetChanged();
}
}
}
selectedIdType = position;
addressCreationAdapter.notifyDataSetChanged();
}
/**
* Finds selected country in the data, which is coming from the server.
* @return selected region.
*/
public int getSelectedCountry() {
int position = -1;
int k = countries.getChildNodes().size();
for (int l = 0; l < k && position == -1; l++) {
XmlNode country = countries.getChildNodes().get(l);
String selected = country.getAttributeValue("selected");
if (selected !=null && selected.equalsIgnoreCase("1")) {
position = l;
}
}
return position;
}
/**
* Finds selected region in the data, which is coming from the server.
* @return selected region.
*/
public int getSelectedRegion() {
int position = -1;
if (selectedCountry >= 0) {
try {
int k = countries.getChildNodes().get(selectedCountry).getChildWithName("regions").getChildNodes().size();
for (int l=0; l < k && position == -1; l++) {
XmlNode region = countries.getChildNodes().get(selectedCountry).getChildWithName("regions").getChildNodes().get(l);
String selected = region.getAttributeValue("selected");
if (selected !=null && selected.equalsIgnoreCase("1")) {
position = l;
}
}
} catch (Exception ex) {}
}
return position;
}
public int getSelectedVatType() {
int position = -1;
int k = vat_types.getChildNodes().size();
for (int l = 0; l < k && position == -1; l++) {
XmlNode vat_type = vat_types.getChildNodes().get(l);
String selected = vat_type.getAttributeValue("selected");
if (selected !=null && selected.equalsIgnoreCase("1")) {
position = l;
}
}
return position;
}
public int getSelectedTitle() {
int position = -1;
int k = titles.getChildNodes().size();
for (int l = 0; l < k && position == -1; l++) {
XmlNode title = titles.getChildNodes().get(l);
String selected = title.getAttributeValue("selected");
if (selected !=null && selected.equalsIgnoreCase("1")) {
position = l;
}
}
return position;
}
public int getSelectedIdType() {
int position = -1;
int k = id_types.getChildNodes().size();
for (int l = 0; l < k && position == -1; l++) {
XmlNode id_type = id_types.getChildNodes().get(l);
String selected = id_type.getAttributeValue("selected");
if (selected !=null && selected.equalsIgnoreCase("1")) {
position = l;
}
}
return position;
}
@Override
public String getTitle() {
return title;
}
@Override
public boolean processKeyBack(int action) {
if (!((MagentoActivity)getContext()).viewManager.popView()) {
((MagentoActivity)getContext()).viewManager.clearStack();
((MagentoActivity)getContext()).viewManager.switchToStack("Home");
}
return true;
}
/**
* AddressCreationAdapter is the class, which extends BaseAdapter. A AddressCreationAdapter object acts as a bridge between
* ListView with record's fields and the underlying data for this list.
*/
public class AddressCreationAdapter extends BaseAdapter {
public ArrayList<View> formItems = new ArrayList<View>();
public AddressCreationAdapter() {
super();
MagentoActivity activity = (MagentoActivity)getContext();
for (int i = 0; i < getForm().getChildNodes().size(); i++) {
XmlNode field = getForm().getChildNodes().get(i);
String name = field.getAttributeValue("name");
if (name != null && name.equalsIgnoreCase(countryFieldName)) {
countries = field.getChildWithName("values");
for (int k = 0; k < countries.getChildNodes().size(); k++) {
XmlNode c = countries.getChildNodes().get(k);
XmlNode regions = c.getChildWithName("regions");
}
}
if (name != null && name.equalsIgnoreCase(vatTypeFieldName)) {
vat_types = field.getChildWithName("values");
}
if (name != null && name.equalsIgnoreCase(titleFieldName)) {
titles = field.getChildWithName("values");
}
if (name != null && name.equalsIgnoreCase(idTypeFieldName)) {
id_types = field.getChildWithName("values");
}
}
fields = new View[getForm().getChildNodes().size()+1];
int lastTextViewIndex = getLastTextViewIndex();
LinearLayout layout = new LinearLayout(getContext());
final CustomTextButton contactsButton = new CustomTextButton(getContext());
contactsButton.setTypeface(activity.fontManager.getFontFace("Title2"));
contactsButton.setTextSize(activity.fontManager.getFontSize("Title2"));
contactsButton.setTextColor(activity.fontManager.getFontColor("Title1"));
contactsButton.setText(activity.locMan.getString("AddAddressFromContacts"));
layout.setPadding(MagentoActivity.dip2px(10), MagentoActivity.dip2px(10), MagentoActivity.dip2px(10), MagentoActivity.dip2px(10));
layout.addView(contactsButton);
((LinearLayout.LayoutParams)contactsButton.getLayoutParams()).width = LayoutParams.FILL_PARENT;
fields[0] = layout;
contactsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new ContactUtils().loadContacts(CheckoutAddressCreationView.this);
}
});
for (int i = 0; i < getForm().getChildNodes().size(); i++) {
//create field
LinearLayout fieldLayout = (LinearLayout)LayoutInflater.from(activity).inflate(R.layout.list_view, CheckoutAddressCreationView.this.list, false);
fieldLayout.setTag(Integer.valueOf(formItems.size()) );
formItems.add(fieldLayout);
XmlNode field = getForm().getChildNodes().get(i);
String name = field.getAttributeValue("name");
String type = field.getAttributeValue("type");
String label = field.getAttributeValue("label");
String checked = field.getAttributeValue("checked");
String value = field.getAttributeValue("value");
String id = field.getAttributeValue("id");
if (type.equalsIgnoreCase("text")) {
//LinearLayout fieldLayout = (LinearLayout)LayoutInflater.from(getContext()).inflate(R.layout.list_view, CheckoutAddressCreationView.this.list, false);
//fieldLayout.setTag(i+1);
EditText view = (EditText)fieldLayout.findViewById(R.id.EditText);
view.setHint(label);
view.setTypeface(activity.fontManager.getFontFace("Title3"));
view.setTextSize(activity.fontManager.getFontSize("Title3"));
view.setTextColor(activity.fontManager.getFontColor("Title3"));
// view.setText(value);
fields[i+1] = fieldLayout;
if (name.equalsIgnoreCase(regionTextFieldName) && selectedRegion>=0) {
fieldLayout.setVisibility(View.GONE);
}
if (lastTextViewIndex == i) {
view.setImeOptions(EditorInfo.IME_ACTION_DONE);
}
view.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch (actionId) {
case EditorInfo.IME_ACTION_NEXT:
LinearLayout fieldLayout = (LinearLayout)v.getParent();
int start = ((Integer)fieldLayout.getTag()).intValue();
View view;
int k;
for (k = start+1, view = fields[k]; k < fields.length && !(view instanceof LinearLayout && view.getVisibility() == VISIBLE); k++);
if (k < fields.length) {
LinearLayout layout = (LinearLayout)view;
TextView textView = (TextView)layout.findViewById(R.id.EditText);
if (textView != null && textView.isFocusable() == true) {
list.setSelection(k);
textView.requestFocus();
}
}
break;
default:
break;
}
return false;
}
});
}
else if (type.equalsIgnoreCase("select")) {
final Resources res = activity.getResources();
layout = new LinearLayout(getContext());
LocationSpinner s = null;
SelectionAdapter adapter = null;
VTISpinner v = null;
SelectionAdapterVTI adapterv = null;
if (name.equalsIgnoreCase(regionSelectFieldName)) {
if (selectedCountry>=0) {
s = new LocationSpinner(getContext(), countries.getChildNodes().get(selectedCountry).getChildWithName("regions"), res.getString(R.string.spinner_prompt)+" region");
adapter = new SelectionAdapter(countries.getChildNodes().get(selectedCountry).getChildWithName("regions"), 1);
//adapter.delegate = CheckoutAddressCreationView.this;
adapter.spinner = s;
((Spinner)s).setAdapter(adapter);
s.setSelection(adapter.getCount()-1);
if (selectedRegion<0) {
layout.setVisibility(View.GONE);
} else {
s.setSelection(selectedRegion);
adapter.notifyDataSetChanged();
}
} else {
s = new LocationSpinner(getContext(), null, res.getString(R.string.spinner_prompt)+" region");
layout.setVisibility(View.GONE);
}
} else if (name.equalsIgnoreCase(countryFieldName)) {
s = new LocationSpinner(getContext(), countries, res.getString(R.string.spinner_prompt)+" country");
s.setTag(name);
adapter = new SelectionAdapter(countries, 0);
//adapter.delegate = CheckoutAddressCreationView.this;
adapter.spinner = s;
((Spinner)s).setAdapter(adapter);
if (selectedCountry>=0) {
s.setSelection(selectedCountry);
} else {
s.setSelection(adapter.getCount()-1);
}
adapter.notifyDataSetChanged();
}
else if (name.equalsIgnoreCase(vatTypeFieldName)) {
vat_types = field.getChildWithName("values");
v = new VTISpinner(getContext(), vat_types, res.getString(R.string.spinner_prompt)+" "+label);
v.setTag(name);
adapterv = new SelectionAdapterVTI(vat_types,name,0);
((Spinner)v).setAdapter(adapterv);
adapterv.spinner = v;
v.setSelection(adapterv.getCount()-1);
adapterv.notifyDataSetChanged();
}else if (name.equalsIgnoreCase(titleFieldName)) {
titles = field.getChildWithName("values");
v = new VTISpinner(getContext(), titles, res.getString(R.string.spinner_prompt)+" "+label);
adapterv = new SelectionAdapterVTI(titles,name,1);
v.setTag(name);
((Spinner)v).setAdapter(adapterv);
adapterv.spinner = v;
if (getSelectedTitle()>=0) {
v.setSelection(getSelectedTitle());
} else {
v.setSelection(adapterv.getCount()-1);
}
adapterv.notifyDataSetChanged();
}else if (name.equalsIgnoreCase(idTypeFieldName)) {
id_types = field.getChildWithName("values");
v = new VTISpinner(getContext(), id_types, res.getString(R.string.spinner_prompt)+" "+label);
adapterv = new SelectionAdapterVTI(id_types,name,2);
v.setTag(name);
((Spinner)v).setAdapter(adapterv);
adapterv.spinner = v;
v.setSelection(adapterv.getCount()-1);
adapterv.notifyDataSetChanged();
}
fields[i+1] = fieldLayout;
if (v != null) {
fieldLayout.addView(v);
((LinearLayout.LayoutParams)v.getLayoutParams()).topMargin = 1;
((LinearLayout.LayoutParams)v.getLayoutParams()).leftMargin = 1;
((LinearLayout.LayoutParams)v.getLayoutParams()).rightMargin = 1;
((LinearLayout.LayoutParams)v.getLayoutParams()).width = LayoutParams.FILL_PARENT;
((LinearLayout.LayoutParams)v.getLayoutParams()).height = LayoutParams.FILL_PARENT;
}
//fields[i+1] = layout;
if (s != null) {
fieldLayout.addView(s);
((LinearLayout.LayoutParams)s.getLayoutParams()).topMargin = 2;
((LinearLayout.LayoutParams)s.getLayoutParams()).leftMargin = 5;
((LinearLayout.LayoutParams)s.getLayoutParams()).rightMargin = 5;
((LinearLayout.LayoutParams)s.getLayoutParams()).width = LayoutParams.FILL_PARENT;
((LinearLayout.LayoutParams)s.getLayoutParams()).height = LayoutParams.FILL_PARENT;
}
}
else if (type.equalsIgnoreCase("checkbox")) {
//LinearLayout fieldLayout = (LinearLayout)LayoutInflater.from(getContext()).inflate(R.layout.list_view, CheckoutAddressCreationView.this.list, false);
EditText view = (EditText)fieldLayout.findViewById(R.id.EditText);
view.setFocusable(false);
view.setText(label);
view.setTypeface(activity.fontManager.getFontFace("Title3"));
view.setTextSize(activity.fontManager.getFontSize("Title3"));
view.setTextColor(activity.fontManager.getFontColor("Title3"));
ToggleButton button = (ToggleButton)fieldLayout.findViewById(R.id.ToggleButton);
button.setVisibility(VISIBLE);
button.setText(activity.locMan.getString("Toggle"));
if (checked!=null && checked.equalsIgnoreCase("1")) {
button.setChecked(true);
} else {
button.setChecked(false);
}
if (name.contains("save_in_address_book") && (!activity.checkUserSessionStatus() || isEmptyAddressBook)) {
fieldLayout.setVisibility(GONE);
if (isEmptyAddressBook) {
button.setChecked(true);
}
}
fields[i+1] = fieldLayout;
}
}
activity.removeDialog(MagentoActivity.DIALOG_PROGRESS_INFINITE);
}
/**
* Gets the index of the last text field
* @return index of the last field in the list, which has "text" type
*/
public int getLastTextViewIndex() {
int l = -1;
for (int i = 0; i < getForm().getChildNodes().size(); i++) {
XmlNode field = getForm().getChildNodes().get(i);
String name = field.getAttributeValue("name");
String type = field.getAttributeValue("type");
if (type.equalsIgnoreCase("text") && !(name.equalsIgnoreCase(regionTextFieldName) && selectedRegion>=0)) {
l = i;
}
}
return l;
}
@Override
public int getCount() {
int k = 0;
for (int i = 0; i < fields.length; i++) {
if (fields[i].getVisibility() == VISIBLE) {
k++;
}
}
return k;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public int getItemViewType(int position) {
int i;
int pos;
for (pos = 0, i = 0; i < fields.length && pos <= position; i++) {
if (fields[i].getVisibility() == VISIBLE) {
pos++;
}
}
return i-1;
}
@Override
public int getViewTypeCount() {
return fields.length;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
int i;
int pos;
for (pos = 0, i = 0; i < fields.length && pos <= arg0; i++) {
if (fields[i].getVisibility() == VISIBLE) {
pos++;
}
}
return fields[i-1];
}
}
/**
* Inits form with default values
* @param nameValuePairs is the ArrayList object with default field parameters.
*/
public abstract void initWithDefaultValues(ArrayList<NameValuePair> nameValuePairs);
@Override
public void configurationChanged(int orientation) {
}
@Override
public void reloadRemoteData(boolean immediately) {
if (immediately) {
final MagentoActivity activity = (MagentoActivity)getContext();
if (!(activity).checkUserSessionStatus()) {
activity.viewManager.clearStack();
activity.viewManager.show_view(R.layout.cart_view, R.id.CartView);
} else {
new Loader(url, null, true, addressLoaderListener, false).execute();
}
} else {
dataReloadRequested = true;
}
}
@Override
protected void onAttachedToWindow() {
if (dataReloadRequested) {
dataReloadRequested = false;
reloadRemoteData(true);
}
super.onAttachedToWindow();
}
@Override
public void selectField(int position, int type) {
if (type == 0) {
selectCountry(position);
} else if (type == 1) {
selectRegion(position);
}
}
public void setForm(XmlNode form) {
this.form = form;
}
public XmlNode getForm() {
return form;
}
public void setSABCForm(XmlNode SABCForm) {
this.SABCForm = SABCForm;
}
public XmlNode getSABCForm() {
return SABCForm;
}
@Override
protected void finalize() throws Throwable {
super.finalize();
if (addressSaveLoader != null) {
addressSaveLoader.cancel(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment