Skip to content

Commit

Permalink
Fix logic for country name and code visibility on the view.
Browse files Browse the repository at this point in the history
  • Loading branch information
kizitonwose committed Feb 22, 2020
1 parent c9b436b commit 02ec7ff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
29 changes: 21 additions & 8 deletions CountryPickerView/CountryPickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ public class CountryPickerView: NibView {

/// Show/Hide the country code on the view.
public var showCountryCodeInView = true {
didSet { setup() }
didSet {
if showCountryNameInView && showCountryCodeInView {
showCountryNameInView = false
} else {
setup()
}
}
}

/// Show/Hide the phone code on the view.
Expand All @@ -58,7 +64,13 @@ public class CountryPickerView: NibView {

/// Show/Hide the country name on the view.
public var showCountryNameInView = false {
didSet { setup() }
didSet {
if showCountryCodeInView && showCountryNameInView {
showCountryCodeInView = false
} else {
setup()
}
}
}

/// Change the font of phone code
Expand Down Expand Up @@ -111,13 +123,14 @@ public class CountryPickerView: NibView {
flagImageView.image = selectedCountry.flag
countryDetailsLabel.font = font
countryDetailsLabel.textColor = textColor
if showPhoneCodeInView && showCountryCodeInView {
if showCountryCodeInView && showPhoneCodeInView {
countryDetailsLabel.text = "(\(selectedCountry.code)) \(selectedCountry.phoneCode)"
return
} else if showCountryCodeInView || showPhoneCodeInView {
countryDetailsLabel.text = showCountryCodeInView ? selectedCountry.code : selectedCountry.phoneCode
} else if showCountryNameInView {
countryDetailsLabel.text = selectedCountry.localizedName()
} else if showCountryNameInView && showPhoneCodeInView {
countryDetailsLabel.text = "(\(selectedCountry.localizedName() ?? selectedCountry.name)) \(selectedCountry.phoneCode)"
} else if showCountryCodeInView || showPhoneCodeInView || showCountryNameInView {
countryDetailsLabel.text = showCountryCodeInView ? selectedCountry.code
: showPhoneCodeInView ? selectedCountry.phoneCode
: selectedCountry.localizedName() ?? selectedCountry.name
} else {
countryDetailsLabel.text = nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,19 @@ class DemoViewController: UITableViewController {
switch sender {
case showCountryCodeInView:
cpvMain.showCountryCodeInView = sender.isOn
// We don't want code and name switches to be on at the same time.
// The library does not show both values anyway but we have this so it's
// obvious in the sample. We also do the check when country name switch is toggled.
if sender.isOn && showCountryNameInView.isOn {
showCountryNameInView.setOn(false, animated: true) // Action not called
}
case showPhoneCodeInView:
cpvMain.showPhoneCodeInView = sender.isOn
case showCountryNameInView:
cpvMain.showCountryNameInView = sender.isOn
if sender.isOn && showCountryCodeInView.isOn {
showCountryCodeInView.setOn(false, animated: true)
}
case showPreferredCountries:
if !sender.isOn && showOnlyPreferredCountries.isOn {
showOnlyPreferredCountries.setOn(false, animated: true)
Expand Down

0 comments on commit 02ec7ff

Please sign in to comment.