Skip to content

Instantly share code, notes, and snippets.

@vishalpatelbacancy
Created August 25, 2025 10:43
Show Gist options
  • Select an option

  • Save vishalpatelbacancy/ac1ed0580967cf939675db55c175f939 to your computer and use it in GitHub Desktop.

Select an option

Save vishalpatelbacancy/ac1ed0580967cf939675db55c175f939 to your computer and use it in GitHub Desktop.
package com.mybmr.poynt_crm.dialog;
import android.accounts.Account;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import androidx.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;
import com.mybmr.poynt_crm.API.ApiClient;
import com.mybmr.poynt_crm.R;
import com.mybmr.poynt_crm.responseModel.EditCustomerResponse;
import com.mybmr.poynt_crm.responseModelv5.CheckCustomerEmail;
import com.mybmr.poynt_crm.responseModelv5.GetCustomers;
import com.mybmr.poynt_crm.utils.AlertUtils;
import com.mybmr.poynt_crm.utils.Constant;
import com.mybmr.poynt_crm.utils.Pref;
import com.mybmr.poynt_crm.utils.ProgressUtils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.fragment.app.FragmentActivity;
import co.poynt.api.model.Customer;
import co.poynt.api.model.Email;
import co.poynt.api.model.EmailType;
import co.poynt.api.model.Phone;
import co.poynt.api.model.PhoneType;
import co.poynt.os.model.Intents;
import co.poynt.os.model.PoyntError;
import co.poynt.os.services.v1.IPoyntCustomerReadListener;
import co.poynt.os.services.v1.IPoyntCustomerService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Created by root on 21/12/18.
*/
public class EditCustomerDialog extends Dialog implements View.OnClickListener {
private GetCustomers.DataBean.CustomerBean detail;
private CheckCustomerEmail.DataBean dataBean;
private FragmentActivity context;
private String merchantID;
private String gender;
private Spinner spGender;
private EditText etMaritalStatus;
private EditText etAnniversaryBd;
private EditText etSpouceName;
private EditText etFavColor;
private EditText etFavFood;
private Spinner spYearIncome;
private EditText etFbPage;
private EditText etTwitterPage;
private EditText etHowTheyKnow;
private EditText et_fn, et_ln, et_email, et_phone, et_bd;
private CheckBox checkBoxStatus;
private Button btn_done;
private Pattern pattern;
private Matcher matcher;
private Account account;
private String lastCustStatus = "";
private static final String DATE_PATTERN =
"^(0[1-9]|1[0-2])/(0[1-9]|1\\d|2\\d|3[01])/(19|20)\\d{2}$";
private OnSuccessListner listner;
private ProgressUtils progressUtils;
private String ln;
private String fn;
private EditText edtCountryCode;
private IPoyntCustomerService iPoyntCustomerService;
private static final String TAG = "Create Customer";
private ServiceConnection customerServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "onServiceConnected ");
iPoyntCustomerService = IPoyntCustomerService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "onServiceDisconnected ");
}
};
private IPoyntCustomerReadListener customerReadListener = new IPoyntCustomerReadListener.Stub() {
@Override
public void onResponse(Customer customer, PoyntError poyntError) throws RemoteException {
Log.d(TAG, "orderResponse poyntError: " + poyntError);
Log.d(TAG, "orderResponse order: " + customer.getFirstName());
Log.d(TAG, "orderResponse order: " + customer.getLastName());
}
};
private IPoyntCustomerReadListener createCustomerReadListener = new IPoyntCustomerReadListener.Stub() {
@Override
public void onResponse(final Customer customer, PoyntError poyntError) throws RemoteException {
Log.d(TAG, "orderResponse poyntError: " + poyntError);
// Log.d(TAG, "orderResponse order: " + customer.toString());
if (customer != null) {
context.runOnUiThread(new Runnable() {
@Override
public void run() {
//Toast.makeText(getActivity(), "Customer Created", Toast.LENGTH_SHORT).show();
//id = customer.getId();
progressUtils.dismissProgressDialog();
Log.d("customerId", "::" + customer.getId() + "\n" + customer.getBusinessId());
String email = et_email.getText().toString().trim();
String phone = et_phone.getText().toString().trim();
String bd = et_bd.getText().toString().trim();
if (detail!=null) {
edit_cust(fn, ln, email, phone, bd, gender, detail.getIs_active());
}else{
edit_cust(fn, ln, email, phone, bd, gender, dataBean.getIs_active());
}
}
});
}
}
};
public EditCustomerDialog(@NonNull FragmentActivity context,
GetCustomers.DataBean.CustomerBean detail, CheckCustomerEmail.DataBean dataBean) {
super(context);
this.context = context;
this.detail = detail;
this.dataBean=dataBean;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_edit_customer);
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
setCanceledOnTouchOutside(true);
progressUtils = new ProgressUtils(context);
pattern = Pattern.compile(DATE_PATTERN);
initView();
setData();
context.bindService(Intents.getComponentIntent(Intents.COMPONENT_POYNT_CUSTOMER_SERVICE),
customerServiceConnection, context.BIND_AUTO_CREATE);
}
private void initView() {
merchantID = Pref.getValue(context, "m_id", "");
checkBoxStatus = (CheckBox) findViewById(R.id.checkBoxStatus);
et_fn = (EditText) findViewById(R.id.et_first_name);
et_ln = (EditText) findViewById(R.id.et_last_name);
et_email = (EditText) findViewById(R.id.et_email);
et_phone = (EditText) findViewById(R.id.et_phone);
edtCountryCode = (EditText) findViewById(R.id.edtCountryCode);
et_bd = (EditText) findViewById(R.id.et_bd);
spGender = (Spinner) findViewById(R.id.spGender);
etMaritalStatus = (EditText) findViewById(R.id.et_marital_status);
etAnniversaryBd = (EditText) findViewById(R.id.et_anniversary_bd);
etSpouceName = (EditText) findViewById(R.id.et_spouce_name);
etFavColor = (EditText) findViewById(R.id.et_fav_color);
etFavFood = (EditText) findViewById(R.id.et_fav_food);
spYearIncome = (Spinner) findViewById(R.id.spYearIncome);
etFbPage = (EditText) findViewById(R.id.et_fb_page);
etTwitterPage = (EditText) findViewById(R.id.et_twitter_page);
etHowTheyKnow = (EditText) findViewById(R.id.et_how_they_know);
btn_done = (Button) findViewById(R.id.btn_done);
btn_done.setOnClickListener(this);
et_bd.addTextChangedListener(mDateEntryWatcher);
etAnniversaryBd.addTextChangedListener(mDateEntryWatcherAniversary);
View ivClose = findViewById(R.id.ivClose);
ivClose.setOnClickListener(this);
et_phone.setCompoundDrawablesWithIntrinsicBounds(null, null, AppCompatResources.getDrawable(context,R.drawable.ic_help_icon), null);
et_phone.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length()>0){
et_phone.setCompoundDrawablesWithIntrinsicBounds(null, null, context.getResources().getDrawable(R.drawable.ic_help_icon), null);
}else{
et_phone.setCompoundDrawablesWithIntrinsicBounds(null, null, context.getResources().getDrawable(R.drawable.ic_help_icon), null);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
et_phone.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(et_phone.getCompoundDrawables()[2]!=null){
if(event.getX() >= (et_phone.getRight()- et_phone.getLeft() - et_phone.getCompoundDrawables()[2].getBounds().width())) {
showHelpDialog(context.getResources().getString(R.string.alert_add_cust_unique_phone_info));
}
}
}
return false;
}
});
}
public void showHelpDialog(String infoStr) {
if (context != null) {
AlertUtils.showSimpleAlert(context, context.getString(R.string.app_name),
infoStr, new OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).show();
}
}
private void setData() {
if (detail == null)
{
et_fn.setText(dataBean.getFirst_name());
et_fn.setSelection(et_fn.getText().length());
et_ln.setText(dataBean.getLast_name());
et_email.setText(dataBean.getEmail());
String phone = "";
if (dataBean.getPhone().startsWith("1")) {
phone = dataBean.getPhone().substring(1);
et_phone.setText(!TextUtils.isEmpty(phone) ? phone : "");
} else if (dataBean.getPhone().startsWith("0")) {
phone = dataBean.getPhone().substring(1);
et_phone.setText(!TextUtils.isEmpty(phone) ? phone : "");
}else {
et_phone.setText(!TextUtils.isEmpty(dataBean.getPhone()) ? dataBean.getPhone() : "");
}
edtCountryCode.setText(dataBean.getCountry_code());
if (dataBean.getBirth_date() != null && !dataBean.getBirth_date().equalsIgnoreCase("00/00/0000"))
et_bd.setText(dataBean.getBirth_date());
if (dataBean.getGender().equalsIgnoreCase("Male")) {
spGender.setSelection(0);
} else if (dataBean.getGender().equalsIgnoreCase("Female")) {
spGender.setSelection(1);
} else if (dataBean.getGender().equalsIgnoreCase("Not Applicable")) {
spGender.setSelection(2);
}
etMaritalStatus.setText(dataBean.getMarital_status());
etAnniversaryBd.setText(dataBean.getAnniversary_date());
etSpouceName.setText(dataBean.getSpouse_name());
etFavColor.setText(dataBean.getColor());
etFavFood.setText(dataBean.getFood());
etFbPage.setText(dataBean.getFb());
etTwitterPage.setText(dataBean.getTwitter());
etHowTheyKnow.setText(dataBean.getAbout());
// spYearIncome;
String income = dataBean.getIncome();
int selectionIndex;
if (income == null || income.isEmpty()) {
selectionIndex = 0;
} else if (income.contains("500001")) {
selectionIndex = 5;
} else {
String[] incomeList = context.getResources().getStringArray(R.array.yearly_income);
selectionIndex = Arrays.asList(incomeList).indexOf(income);
}
spYearIncome.setSelection(selectionIndex);
checkBoxStatus.setChecked((!TextUtils.isEmpty(dataBean.getIs_active()) && dataBean.getIs_active().equals("1")) ? true : false);
lastCustStatus = dataBean.getIs_active();
checkBoxStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String checkedStr = checkBoxStatus.isChecked() ? "1" : "0";
dataBean.setIs_active(checkedStr);
}
});
}else {
et_fn.setText(detail.getFirst_name());
et_fn.setSelection(et_fn.getText().length());
et_ln.setText(detail.getLast_name());
et_email.setText(detail.getEmail());
String phone = "";
if (detail.getPhone().startsWith("1")) {
phone = detail.getPhone().substring(1);
et_phone.setText(!TextUtils.isEmpty(phone) ? phone : "");
} else if (detail.getPhone().startsWith("0")) {
phone = detail.getPhone().substring(1);
et_phone.setText(!TextUtils.isEmpty(phone) ? phone : "");
}else {
et_phone.setText(!TextUtils.isEmpty(detail.getPhone()) ? detail.getPhone() : "");
}
edtCountryCode.setText(detail.getCountryCode());
if (detail.getBirth_date() != null && !detail.getBirth_date().equalsIgnoreCase("00/00/0000"))
et_bd.setText(detail.getBirth_date());
if (detail.getGender().equalsIgnoreCase("Male")) {
spGender.setSelection(0);
} else if (detail.getGender().equalsIgnoreCase("Female")) {
spGender.setSelection(1);
} else if (detail.getGender().equalsIgnoreCase("Not Applicable")) {
spGender.setSelection(2);
}
etMaritalStatus.setText(detail.getMarital_status());
etAnniversaryBd.setText(detail.getAnniversary_date());
etSpouceName.setText(detail.getSpouse_name());
etFavColor.setText(detail.getColor());
etFavFood.setText(detail.getFood());
etFbPage.setText(detail.getFb());
etTwitterPage.setText(detail.getTwitter());
etHowTheyKnow.setText(detail.getAbout());
// spYearIncome;
String income = detail.getIncome();
int selectionIndex;
if (income == null || income.isEmpty()) {
selectionIndex = 0;
} else if (income.contains("500001")) {
selectionIndex = 5;
} else {
String[] incomeList = context.getResources().getStringArray(R.array.yearly_income);
selectionIndex = Arrays.asList(incomeList).indexOf(income);
}
spYearIncome.setSelection(selectionIndex);
checkBoxStatus.setChecked((!TextUtils.isEmpty(detail.getIs_active()) && detail.getIs_active().equals("1")) ? true : false);
lastCustStatus = detail.getIs_active();
/*checkBoxStatus.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
String checkedStr = checkBoxStatus.isChecked() ? "1" : "0";
detail.setIs_active(checkedStr);
}
});*/
checkBoxStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String checkedStr = checkBoxStatus.isChecked() ? "1" : "0";
detail.setIs_active(checkedStr);
}
});
}
}
public void setOnSuccessListner(OnSuccessListner listner) {
this.listner = listner;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ivClose:
dismiss();
break;
case R.id.btn_done:
editCustomerTask();
break;
}
}
private void editCustomerTask() {
fn = et_fn.getText().toString().trim();
ln = et_ln.getText().toString().trim();
String email = et_email.getText().toString().trim();
String phone = et_phone.getText().toString().trim();
String bd = et_bd.getText().toString().trim();
String aniDate = etAnniversaryBd.getText().toString().trim();
gender = "" + (spGender.getSelectedItemPosition() + 1);
if (email.length() == 0 && phone.length() == 0) {
AlertUtils.showSimpleAlert(context, context.getString(R.string.app_name), "Please Enter at least phone number or email.").show();
} else if (phone.length() > 0 && (phone.length() < 9 || phone.length() > 10)) {
AlertUtils.showSimpleAlert(context, context.getString(R.string.app_name), context.getString(R.string.ac_error_valid_phone)).show();
} else if (TextUtils.isEmpty(edtCountryCode.getText().toString())) {
edtCountryCode.requestFocus();
edtCountryCode.setError(context.getResources().getString(R.string.valid_coutry_code));
}
// else if (bd.length() > 0 && !validate(bd)) {
// AlertUtils.showSimpleAlert(context, context.getString(R.string.app_name), context.getString(R.string.ac_error_bd)).show();
// } else if (aniDate.length() > 0 && !validate(aniDate)) {
// AlertUtils.showSimpleAlert(context, context.getString(R.string.app_name), context.getString(R.string.ac_error_any_date)).show();
// }
else {
if (email.length() == 0) {
email = phone + "@nonameemail.com";
}
Phone strPhone = new Phone();
strPhone.setLocalPhoneNumber(et_phone.getText().toString().trim());
strPhone.setItuCountryCode("1");
strPhone.setType(PhoneType.MOBILE);
Email strEmail = new Email();
strEmail.setEmailAddress(et_email.getText().toString().trim());
strEmail.setType(EmailType.PERSONAL);
if (detail!=null) {
long customerID= Long.parseLong(detail.getCustomer_id());
try {
iPoyntCustomerService.updateCustomer(customerID,strEmail,strPhone, createCustomerReadListener);
} catch (RemoteException e) {
e.printStackTrace();
}
}else{
try {
long customerID= Long.parseLong(dataBean.getCustomer_id());
iPoyntCustomerService.updateCustomer(customerID,strEmail,strPhone, createCustomerReadListener);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
}
private TextWatcher mDateEntryWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String working = "";
try {
working = s.toString();
} catch (Exception e) {
working = "";
}
if (working.length() == 2 && before == 0) {
working += "/";
et_bd.setText(working);
et_bd.setSelection(working.length());
} else if (working.length() == 5 && before == 0) {
working += "/";
et_bd.setText(working);
et_bd.setSelection(working.length());
} else if (working.length() == 11 && before == 0) {
working += "/";
et_bd.setText(working);
et_bd.setSelection(working.length());
}
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
};
private TextWatcher mDateEntryWatcherAniversary = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String working = s.toString();
if (working.length() == 2 && before == 0) {
working += "/";
etAnniversaryBd.setText(working);
etAnniversaryBd.setSelection(working.length());
} else if (working.length() == 5 && before == 0) {
working += "/";
etAnniversaryBd.setText(working);
etAnniversaryBd.setSelection(working.length());
} else if (working.length() == 11 && before == 0) {
working += "/";
etAnniversaryBd.setText(working);
etAnniversaryBd.setSelection(working.length());
}
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
};
public boolean validate(final String date) {
matcher = pattern.matcher(date);
if (matcher.matches()) {
matcher.reset();
if (matcher.find()) {
String day = matcher.group(2);
String month = matcher.group(1);
int year = Integer.parseInt(matcher.group(3));
Log.d("mohit", "" + month + "/" + day + "/" + year);
if (day.equals("31") &&
(month.equals("4") || month.equals("6") || month.equals("9") ||
month.equals("11") || month.equals("04") || month.equals("06") ||
month.equals("09"))) {
return false; // only 1,3,5,7,8,10,12 has 31 days
} else if (month.equals("2") || month.equals("02")) {
//leap year
if (year % 4 == 0) {
if (day.equals("30") || day.equals("31")) {
return false;
} else {
return true;
}
} else {
if (day.equals("29") || day.equals("30") || day.equals("31")) {
return false;
} else {
return true;
}
}
} else {
return true;
}
} else {
return false;
}
} else {
return false;
}
}
private void edit_cust(final String fName, final String lName, final String email, final String phone, final String bd, final String gender, final String status) {
Call<EditCustomerResponse> call;
HashMap<String, String> data = new HashMap<>();
/*if (is_multi_reward) {
data.put("merchant_id", merchantID);
}*/
data.put("merchant_id", merchantID);
if (detail!=null) {
data.put("customer_id", detail.getCustomer_id());
}else{
data.put("customer_id", dataBean.getCustomer_id());
}
data.put("first_name", fName);
data.put("last_name", lName);
data.put("email", email);
data.put("phone", phone);
data.put("country_code", edtCountryCode.getText().toString().trim());
data.put("birth_date", bd);
data.put("gender", gender);
// data.put("status", status);
data.put("is_active", status);
String income = "";
if (spYearIncome.getSelectedItemPosition() > 0) {
income = spYearIncome.getSelectedItem().toString();
if (spYearIncome.getSelectedItemPosition() == 5)
income = "500001";
}
//other new fields.
data.put("employee_ids", Pref.getValue(context, "e_id", ""));
data.put("marital_status", etMaritalStatus.getText().toString());
data.put("spouse_name", etSpouceName.getText().toString());
data.put("color", etFavColor.getText().toString());
data.put("food", etFavFood.getText().toString());
data.put("income", income); // need tichk
data.put("fb", etFbPage.getText().toString());
data.put("twitter", etTwitterPage.getText().toString());
data.put("about", etHowTheyKnow.getText().toString());
data.put("anniversary_date", etAnniversaryBd.getText().toString());
String appAuthToken=Pref.getValue(context, Constant.APP_AUTH_TOKEN_PREF,"");
progressUtils.showProgressDialog(context.getString(R.string.please_wait));
//if (is_multi_reward) {
call = ApiClient.getClient(context).editCustomerMultiLoc(data,appAuthToken);
//} else {
// call = ApiClient.getClient().editCustomer(data);
//}
call.enqueue(new Callback<EditCustomerResponse>() {
public EditCustomerResponse myresp;
@Override
public void onResponse(Call<EditCustomerResponse> call, Response<EditCustomerResponse> response) {
try {
// Log.d("mk", "==" + response.body().toString());
progressUtils.dismissProgressDialog();
if (response.isSuccessful()) {
myresp = response.body();
String msg = TextUtils.join(",", response.body().getMessage());
if (myresp.isStatus()) {
/*boolean chk=status.equals("1")?true:false;
checkBoxStatus.setChecked(chk);*/
Toast.makeText(context, !TextUtils.isEmpty(msg) ? msg : "Customer updated successfully.", Toast.LENGTH_SHORT).show();
listner.onSuccess();
//
// detail.setFirst_name(fName);
// detail.setLast_name(lName);
// detail.setEmail(email);
// detail.setPhone(phone);
// detail.setBirthdate(bd);
} else {
/* boolean chk=status.equals("1")?true:false;
checkBoxStatus.setChecked(!chk);*/
AlertUtils.showSimpleAlert(context, context.getString(R.string.app_name), msg).show();
}
} else {
/* boolean chk=status.equals("1")?true:false;
checkBoxStatus.setChecked(!chk);*/
AlertUtils.showSimpleAlert(context,
context.getString(R.string.app_name), context.getString(R.string.server_error)).show();
}
} catch (Exception e) {
e.printStackTrace();
progressUtils.dismissProgressDialog();
}
}
@Override
public void onFailure(Call<EditCustomerResponse> call, Throwable t) {
// Log error here since request failed
/*boolean chk=status.equals("1")?true:false;
checkBoxStatus.setChecked(!chk);*/
progressUtils.dismissProgressDialog();
AlertUtils.showSimpleAlert(context,
context.getString(R.string.app_name), context.getString(R.string.internet_error)).show();
}
});
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
context.unbindService(customerServiceConnection);
}
public interface OnSuccessListner {
void onSuccess();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment