This commit is contained in:
2023-08-29 13:57:59 +05:30
parent 2a5f54a866
commit 3086f64fe3
6 changed files with 173 additions and 193 deletions

View File

@@ -1,158 +0,0 @@
package com.ssb.simplitend.welcome.welcomepatient.fragments.contacts;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.ssb.simplitend.databinding.CreateContactViewHolderBinding;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class CreateContactAdapter extends ListAdapter<String, CreateContactAdapter.CreateContactViewHolder> {
private static final DiffUtil.ItemCallback<String> DIFF_UTIL = new DiffUtil.ItemCallback<String>() {
@Override
public boolean areItemsTheSame(@NonNull String oldItem, @NonNull String newItem) {
return oldItem.equals(newItem);
}
@Override
public boolean areContentsTheSame(@NonNull String oldItem, @NonNull String newItem) {
return oldItem.equals(newItem);
}
};
private final ArrayList<String> countryCodeList;
@NonNull
private final List<String> changedContactList;
@NonNull
private String default_phone_number;
public CreateContactAdapter(@NonNull ArrayList<String> countryCodeList, @NonNull String default_phone_number){
super(DIFF_UTIL);
this.countryCodeList = countryCodeList;
this.default_phone_number = default_phone_number;
this.changedContactList = new LinkedList<>();
}
public void setDefault_phone_number(@NonNull String default_phone_number) {
this.default_phone_number = default_phone_number;
}
@NonNull
@Override
public CreateContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
CreateContactViewHolderBinding binding = CreateContactViewHolderBinding.inflate(
LayoutInflater.from(parent.getContext()),
parent, false);
binding.countryCodes.setItems(countryCodeList);
binding.countryCodes.setIsFocusable(true);
binding.countryCodes.setDismissWhenNotifiedItemSelected(true);
return new CreateContactViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull CreateContactViewHolder holder, int position) {
holder.binding.phoneNumber.setText("");
if (position < 0 || position >= getItemCount()) return;
// input formatter for phone number
InputFilter phoneFilter = (charSequence, i, i1, spanned, i2, i3) -> {
String some = holder.binding.phoneNumber.getText().toString();
String phone_number_str = charSequence.toString();
String country_code = null;
if (holder.binding.countryCodes.getSelectedIndex() != -1) {
country_code = countryCodeList.get(holder.binding.countryCodes.getSelectedIndex());
}
try {
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(charSequence, "US");
phone_number_str = String.valueOf(phoneNumber.getNationalNumber());
country_code = "+" + phoneNumber.getCountryCode();
} catch (Exception e) {
// nothing
}
if (country_code != null && !countryCodeList.contains(country_code)) {
countryCodeList.add(country_code);
}
if (country_code != null) {
holder.binding.countryCodes.selectItemByIndex(countryCodeList.indexOf(country_code));
}
if (phone_number_str.length() > 10) {
// pasted number length is greater than 10
return phone_number_str.substring(0, 10);
}
String total_phone_number = holder.binding.phoneNumber.getText().toString() + phone_number_str;
if (total_phone_number.length() > 10) {
// max length should be 10
return "";
} else {
return phone_number_str;
}
};
holder.binding.phoneNumber.setFilters(new InputFilter[]{phoneFilter});
// Now setting the phone number and it will be automatically formatted with above filter
holder.binding.phoneNumber.setText(getItem(position));
if (default_phone_number.equals(getItem(position))){
holder.binding.defaultCheck.setSelected(true);
}else{
holder.binding.defaultCheck.setSelected(false);
}
holder.binding.defaultCheck.setOnClickListener(v -> {
if (!default_phone_number.equals(getItem(position))){
default_phone_number = getItem(position);
notifyDataSetChanged();
}
});
}
@Override
public void submitList(@Nullable List<String> list) {
super.submitList(list);
if (list != null){
this.changedContactList.clear();
this.changedContactList.addAll(list);
}
}
public static class CreateContactViewHolder extends RecyclerView.ViewHolder{
public CreateContactViewHolderBinding binding;
public CreateContactViewHolder(@NonNull CreateContactViewHolderBinding binding){
super(binding.getRoot());
this.binding = binding;
}
}
}

View File

@@ -9,10 +9,13 @@ import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
@@ -23,13 +26,15 @@ import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.bumptech.glide.Glide;
import com.github.dhaval2404.imagepicker.ImagePicker;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.ssb.simplitend.R;
import com.ssb.simplitend.apputils.AppUtil;
import com.ssb.simplitend.databinding.CreateContactViewHolderBinding;
import com.ssb.simplitend.databinding.CreateEditContactFragmentBinding;
import com.ssb.simplitend.welcome.welcomepatient.fragments.contacts.mvvm.Contact;
import com.ssb.simplitend.welcome.welcomepatient.fragments.contacts.mvvm.ContactViewModel;
@@ -39,7 +44,6 @@ import com.ssb.simplitend.welcome.welcomepatient.mvvm.WelcomeContracts;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -85,8 +89,6 @@ public class CreateContactFragment extends Fragment implements WelcomeContracts.
// Remote contact list
protected List<ContactListResponse> contactList;
private CreateContactAdapter createContactAdapter;
public CreateContactFragment() {
// required empty const.
}
@@ -113,15 +115,11 @@ public class CreateContactFragment extends Fragment implements WelcomeContracts.
// country code loading
countryCodeList = contactViewModel.loadCountryCodeDropDown(requireContext());
if (contactList == null) contactList = new ArrayList<>();
if (countryCodeList == null) countryCodeList = new ArrayList<>();
if (!countryCodeList.contains("+1")) {
countryCodeList.add("+1");
}
createContactAdapter = new CreateContactAdapter(countryCodeList, "");
binding.contactRv.setLayoutManager(new LinearLayoutManager(requireContext()));
binding.contactRv.setAdapter(createContactAdapter);
progressDialog = new ProgressDialog(requireContext());
Bundle bundle = getArguments();
@@ -185,25 +183,29 @@ public class CreateContactFragment extends Fragment implements WelcomeContracts.
});
binding.nextBtn.setOnClickListener(v -> {
String[] phone_numbers = getAllPhoneNumbers();
if (allOkay()) {
if (to_edit) {
// editing existing contact
Log.d(TAG, "clickEvents: ");
AppUtil.showSOSDecision(requireContext(),
getString(R.string.make_changes),
getString(R.string.yes),
getString(R.string.no),
yes -> {
createEditContact("While we update your contact details.", true, UPDATE_CONTACT + contactData.contact_id);
}, no -> {
});
} else {
createEditContact("While we save the contact details.", false, CREATE_CONTACT);
}
}
// if (allOkay()) {
// if (to_edit) {
// // editing existing contact
//
// AppUtil.showSOSDecision(requireContext(),
// getString(R.string.make_changes),
// getString(R.string.yes),
// getString(R.string.no),
// yes -> {
// createEditContact("While we update your contact details.", true, UPDATE_CONTACT + contactData.contact_id);
// }, no -> {
//
// });
//
// } else {
// createEditContact("While we save the contact details.", false, CREATE_CONTACT);
// }
// }
});
@@ -501,11 +503,134 @@ public class CreateContactFragment extends Fragment implements WelcomeContracts.
// adding contacts to Contact_rv
if (contactData.extra_phone_numbers != null && !contactData.extra_phone_numbers.isEmpty()){
String[] phone_numbers = contactData.extra_phone_numbers.split(",");
createContactAdapter.submitList(Arrays.asList(phone_numbers));
for (String phone_number: phone_numbers){
addContactView(phone_number);
}
}
}
private void addContactView(String contact_number){
CreateContactViewHolderBinding contact_binding = CreateContactViewHolderBinding.inflate(getLayoutInflater());
contact_binding.countryCodes.setItems(countryCodeList);
contact_binding.countryCodes.setIsFocusable(true);
contact_binding.countryCodes.setDismissWhenNotifiedItemSelected(true);
// phone number formatting
// to deal with input pasting in the edit text
InputFilter phoneFilter = (charSequence, i, i1, spanned, i2, i3) -> {
String phone_number_str = charSequence.toString();
String country_code;
if (contact_binding.countryCodes.getSelectedIndex() == -1) {
country_code = "+1";
} else {
country_code = countryCodeList.get(contact_binding.countryCodes.getSelectedIndex());
}
try {
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(charSequence, "US");
phone_number_str = String.valueOf(phoneNumber.getNationalNumber());
country_code = "+" + phoneNumber.getCountryCode();
} catch (Exception e) {
// nothing
}
if (!countryCodeList.contains(country_code)) {
countryCodeList.add(country_code);
}
contact_binding.countryCodes.selectItemByIndex(countryCodeList.indexOf(country_code));
if (phone_number_str.length() > 10) {
// pasted number length is greater than 10
return phone_number_str.substring(0, 10);
}
String total_phone_number = contact_binding.createPhoneNumber.getText().toString() + phone_number_str;
if (total_phone_number.length() > 10) {
// max length should be 10
return "";
} else {
return phone_number_str;
}
};
contact_binding.createPhoneNumber.setFilters(new InputFilter[]{phoneFilter});
contact_binding.createPhoneNumber.setText(contact_number);
contact_binding.defaultCheck.setOnClickListener(v -> {
if (!contact_binding.defaultCheck.isSelected()){
selectDefaultContact(binding.contactTable.indexOfChild(contact_binding.getRoot()));
}
});
binding.contactTable.addView(contact_binding.getRoot());
}
private void selectDefaultContact(int index){
int count = binding.contactTable.getChildCount();
if (index >= 0 && index < count){
// unselecting all other editBoxes
for (int i = 0; i < count; i++) {
View view = binding.contactTable.getChildAt(i);
if (view != null){
ImageButton default_check = view.findViewById(R.id.default_check);
if (default_check != null){
if (i == index){
default_check.setSelected(true);
}else{
default_check.setSelected(false);
}
}
}
}
}
}
/*
returns a string array of size 2
index 0: default phone number
index 1: extra phone number separated by ','
*/
public String[] getAllPhoneNumbers(){
String[] phone_numbers = {"", ""};
int count = binding.contactTable.getChildCount();
for (int i = 0; i < count; i++) {
View view = binding.contactTable.getChildAt(i);
if (view != null){
ImageButton default_check = view.findViewById(R.id.default_check);
EditText phone_input = view.findViewById(R.id.create_phone_number);
if (default_check != null && phone_input != null){
if (default_check.isSelected()){
phone_numbers[0] = phone_input.getText().toString();
}else{
phone_numbers[1] += (phone_input.getText().toString() + ",");
}
}
}
}
// removing last "," added
int last_index = Math.max(phone_numbers[1].length() - 1, 0);
phone_numbers[1] = phone_numbers[1].substring(0, last_index);
return phone_numbers;
}
public void setLayoutDetails(String title, String photo_title, String btn_txt) {
binding.title.setText(title);
binding.photoTv.setText(photo_title);