hbb20 / CountryCodePickerProject

Country Code Picker (CCP) is an android library which provides an easy way to search and select country or international phone code. Also supports Android EditText phone mask and international phone validation.
Apache License 2.0
1.52k stars 506 forks source link

set country to null or blank #496

Closed Avramo closed 1 year ago

Avramo commented 3 years ago

I get the User's data from the DB, and sometimes for whatever reason the User has "" (empty string) saved as their country. I use this Picker to edit their country, and if the User does not change his country then I get India "IN" as the selectedCCPCountry since the defaultCCPCountry automatically returns India if no country was set. How can I set the selectedCCPCountry or some other flag to null or "" so that in this case I can know not to change the User's data?

Thanks!

hbb20 commented 3 years ago

It seems like your primary use case is to select country without phone number validation, did I get that right? if so, you should try Android Country Picker (https://github.com/hbb20/AndroidCountryPicker). It has out of box support for non selection and much more. Please let me know if that works for you.

On Mon, Jun 28, 2021 at 07:34 Avramo @.***> wrote:

I get the User's data from the DB, and sometimes for whatever reason the User has "" (empty string) saved as their country. I use this Picker to edit their country, and if the User does not change his country then I get India "IN" as the selectedCCPCountry since the defaultCCPCountry automatically returns India if no country was set. How can I set the selectedCCPCountry or some other flag to null or "" so that in this case I can know not to change the User's data?

Thanks!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/hbb20/CountryCodePickerProject/issues/496, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABFQ32HVJCLTKPM72SRFT3TTVB26FANCNFSM47N7QP5Q .

Avramo commented 3 years ago

Thanks for the quick response!

As I don't want to undo all the code I have already, I made a temporary workaround.
mCountryChangedByUser is false and turns true only when a country is selected inside the CountryCodePicker.

    mCountryCCP.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() {
        @Override
        public void onCountrySelected() {
            mCountryChangedByUser = true;
            mCountryTextTV.setText(mCountryCCP.getSelectedCountryName());
            mUser.setCountry(mCountryCCP.getSelectedCountryNameCode());
        }
    });

If the user did not change his country then just return the original country in my saveUser() function.

if (mCountryChangedByUser) {  

        userUpdate.setCountry(mCountryCCP.getSelectedCountryNameCode() );  

    } else {  
        userUpdate.setCountry(mUser.getCountry());  
    }

Maybe next time I'll use the AndroidCountryPicker.

Thanks!

hbb20 commented 3 years ago

Glad that workaround worked for you. Sorry for the inconvenience.

On Mon, Jun 28, 2021 at 10:02 Avramo @.***> wrote:

Thanks for the quick response!

As I don't want to undo all the code I have already, I made a temporary workaround.

if (mCountryChangedByUser) { userUpdate.setCountry(mCountryCCP.getSelectedCountryNameCode() ); } else { userUpdate.setCountry(mUser.getCountry()); }

Maybe next time I'll use the AndroidCountryPicker.

Thanks!

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/hbb20/CountryCodePickerProject/issues/496#issuecomment-869808145, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABFQ32DCAYT44H7SYQ65BITTVCMHXANCNFSM47N7QP5Q .

Avramo commented 3 years ago

No problem at all! Keep up the great coding!

Thanks!