From 02ec7ff3c3d2ccdf5a2a4593fff509554a0f4888 Mon Sep 17 00:00:00 2001 From: Kizito Nwose Date: Sat, 22 Feb 2020 14:29:54 +0100 Subject: [PATCH] Fix logic for country name and code visibility on the view. --- CountryPickerView/CountryPickerView.swift | 29 ++++++++++++++----- .../DemoViewController.swift | 9 ++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/CountryPickerView/CountryPickerView.swift b/CountryPickerView/CountryPickerView.swift index 4a94a12..c22e614 100644 --- a/CountryPickerView/CountryPickerView.swift +++ b/CountryPickerView/CountryPickerView.swift @@ -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. @@ -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 @@ -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 } diff --git a/CountryPickerViewDemo/CountryPickerViewDemo/DemoViewController.swift b/CountryPickerViewDemo/CountryPickerViewDemo/DemoViewController.swift index 2328776..2987e71 100644 --- a/CountryPickerViewDemo/CountryPickerViewDemo/DemoViewController.swift +++ b/CountryPickerViewDemo/CountryPickerViewDemo/DemoViewController.swift @@ -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)