Skip to content

Commit

Permalink
Refactor code for updating prompt text
Browse files Browse the repository at this point in the history
The original chunk of code for updating the prompt text with example number has been moved from an inline Runnable to a separate private method. This change makes the code cleaner and more maintainable. It also enhances readability as now, instead of an inline block, a method with a self-explanatory name is called, making the code easier to understand. The listeners have been also updated to call the new method.
  • Loading branch information
dlemmermann committed Nov 24, 2023
1 parent 2e412c5 commit 7166dcd
Showing 1 changed file with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,9 @@ protected void layoutChildren(double x, double y, double w, double h) {
resolver = new CountryResolver();
formatter = new PhoneNumberFormatter();

Runnable sampleUpdater = () -> {
if (getSelectedCountry() == null) {
setPromptText(null);
} else {
Phonenumber.PhoneNumber sampleNumber;
if (getExpectedPhoneNumberType() == null) {
sampleNumber = phoneNumberUtil.getExampleNumber(getSelectedCountry().iso2Code());
} else {
sampleNumber = phoneNumberUtil.getExampleNumberForType(getSelectedCountry().iso2Code(), getExpectedPhoneNumberType());
}
setPromptText(formatter.doFormat(phoneNumberUtil.format(sampleNumber, PhoneNumberUtil.PhoneNumberFormat.E164), getSelectedCountry()));
}
};

expectedPhoneNumberTypeProperty().addListener(obs -> sampleUpdater.run());
selectedCountryProperty().addListener(obs -> sampleUpdater.run());
InvalidationListener updateSampleListener = it -> updatePromptTextWithExampleNumber();
expectedPhoneNumberTypeProperty().addListener(updateSampleListener);
selectedCountryProperty().addListener(updateSampleListener);

phoneNumberProperty().addListener((obs, oldV, newV) -> {
if (newV == null) {
Expand Down Expand Up @@ -220,6 +207,20 @@ protected void layoutChildren(double x, double y, double w, double h) {
updateValidPseudoState();
}

private void updatePromptTextWithExampleNumber() {
if (getSelectedCountry() == null) {
setPromptText(null);
} else {
Phonenumber.PhoneNumber sampleNumber;
if (getExpectedPhoneNumberType() == null) {
sampleNumber = phoneNumberUtil.getExampleNumber(getSelectedCountry().iso2Code());
} else {
sampleNumber = phoneNumberUtil.getExampleNumberForType(getSelectedCountry().iso2Code(), getExpectedPhoneNumberType());
}
setPromptText(formatter.doFormat(phoneNumberUtil.format(sampleNumber, PhoneNumberUtil.PhoneNumberFormat.E164), getSelectedCountry()));
}
}

private void updateValidPseudoState() {
pseudoClassStateChanged(INVALID_PSEUDO_CLASS, !isValid());
}
Expand Down

0 comments on commit 7166dcd

Please sign in to comment.