From 5cf0fbd24da6795e6f197def6df4ab3400b848ba Mon Sep 17 00:00:00 2001 From: Kizito Nwose Date: Sun, 2 Aug 2020 17:24:02 +0200 Subject: [PATCH 1/3] Add `excludedCountries` datasource function. --- CountryPickerView/CountryPickerView.swift | 9 +++++++-- CountryPickerView/CountryPickerViewController.swift | 6 +++++- CountryPickerView/Delegate+DataSource.swift | 7 +++++++ .../CountryPickerViewDemo/DemoViewController.swift | 8 +------- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CountryPickerView/CountryPickerView.swift b/CountryPickerView/CountryPickerView.swift index f811e6d..ae38fb0 100644 --- a/CountryPickerView/CountryPickerView.swift +++ b/CountryPickerView/CountryPickerView.swift @@ -100,8 +100,8 @@ public class CountryPickerView: NibView { internal(set) public var selectedCountry: Country { get { return _selectedCountry - ?? countries.first(where: { $0.code == Locale.current.regionCode }) - ?? countries.first! + ?? usableCountries.first(where: { $0.code == Locale.current.regionCode }) + ?? usableCountries.first! } set { _selectedCountry = newValue @@ -198,6 +198,11 @@ public class CountryPickerView: NibView { } return countries }() + + internal var usableCountries: [Country] { + let excluded = dataSource?.excludedCountriesInList(in: self) ?? [] + return countries.filter { return !excluded.contains($0) } + } } //MARK: Helper methods diff --git a/CountryPickerView/CountryPickerViewController.swift b/CountryPickerView/CountryPickerViewController.swift index e2b7166..1d02692 100644 --- a/CountryPickerView/CountryPickerViewController.swift +++ b/CountryPickerView/CountryPickerViewController.swift @@ -45,7 +45,7 @@ extension CountryPickerViewController { func prepareTableItems() { if !showOnlyPreferredSection { - let countriesArray = countryPickerView.countries + let countriesArray = countryPickerView.usableCountries let locale = dataSource.localeForCountryNameInList var groupedData = Dictionary(grouping: countriesArray) { @@ -351,4 +351,8 @@ class CountryPickerViewDataSourceInternal: CountryPickerViewDataSource { var localeForCountryNameInList: Locale { return view.dataSource?.localeForCountryNameInList(in: view) ?? localeForCountryNameInList(in: view) } + + var excludedCountries: [Country] { + return view.dataSource?.excludedCountriesInList(in: view) ?? preferredCountries(in: view) + } } diff --git a/CountryPickerView/Delegate+DataSource.swift b/CountryPickerView/Delegate+DataSource.swift index 3ce33fd..50bf44d 100644 --- a/CountryPickerView/Delegate+DataSource.swift +++ b/CountryPickerView/Delegate+DataSource.swift @@ -79,6 +79,9 @@ public protocol CountryPickerViewDataSource: class { /// The Locale used to generate the name of the cuntries on the list. func localeForCountryNameInList(in countryPickerView: CountryPickerView) -> Locale + + /// An array of countries you wish to exclude from the list of countries. + func excludedCountriesInList(in countryPickerView: CountryPickerView) -> [Country] } // MARK:- CountryPickerViewDataSource default implementations @@ -147,6 +150,10 @@ public extension CountryPickerViewDataSource { func localeForCountryNameInList(in countryPickerView: CountryPickerView) -> Locale { return Locale.current } + + func excludedCountriesInList(in countryPickerView: CountryPickerView) -> [Country] { + return [] + } } diff --git a/CountryPickerViewDemo/CountryPickerViewDemo/DemoViewController.swift b/CountryPickerViewDemo/CountryPickerViewDemo/DemoViewController.swift index 2987e71..0c0119c 100644 --- a/CountryPickerViewDemo/CountryPickerViewDemo/DemoViewController.swift +++ b/CountryPickerViewDemo/CountryPickerViewDemo/DemoViewController.swift @@ -122,13 +122,7 @@ extension DemoViewController: CountryPickerViewDelegate { extension DemoViewController: CountryPickerViewDataSource { func preferredCountries(in countryPickerView: CountryPickerView) -> [Country] { if countryPickerView.tag == cpvMain.tag && showPreferredCountries.isOn { - var countries = [Country]() - ["NG", "US", "GB"].forEach { code in - if let country = countryPickerView.getCountryByCode(code) { - countries.append(country) - } - } - return countries + return ["NG", "US", "GB"].compactMap { countryPickerView.getCountryByCode($0) } } return [] } From d1d06494eaa5496b0f3ce96b524d9043997118e3 Mon Sep 17 00:00:00 2001 From: Kizito Nwose Date: Sun, 2 Aug 2020 17:28:14 +0200 Subject: [PATCH 2/3] Update podspec. --- CountryPickerView.podspec | 2 +- CountryPickerView/Delegate+DataSource.swift | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CountryPickerView.podspec b/CountryPickerView.podspec index 006af6f..1fae69f 100755 --- a/CountryPickerView.podspec +++ b/CountryPickerView.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |spec| spec.name = "CountryPickerView" - spec.version = "3.1.2" + spec.version = "3.1.3" spec.summary = "A simple, customizable view for selecting countries in iOS apps." spec.homepage = "https://github.com/kizitonwose/CountryPickerView" spec.license = "MIT" diff --git a/CountryPickerView/Delegate+DataSource.swift b/CountryPickerView/Delegate+DataSource.swift index 50bf44d..076a9ae 100644 --- a/CountryPickerView/Delegate+DataSource.swift +++ b/CountryPickerView/Delegate+DataSource.swift @@ -6,7 +6,7 @@ // Copyright © 2018 Kizito Nwose. All rights reserved. // -import Foundation +import UIKit public protocol CountryPickerViewDelegate: class { /// Called when the user selects a country from the list. @@ -81,7 +81,7 @@ public protocol CountryPickerViewDataSource: class { func localeForCountryNameInList(in countryPickerView: CountryPickerView) -> Locale /// An array of countries you wish to exclude from the list of countries. - func excludedCountriesInList(in countryPickerView: CountryPickerView) -> [Country] + func excludedCountries(in countryPickerView: CountryPickerView) -> [Country] } // MARK:- CountryPickerViewDataSource default implementations @@ -151,7 +151,7 @@ public extension CountryPickerViewDataSource { return Locale.current } - func excludedCountriesInList(in countryPickerView: CountryPickerView) -> [Country] { + func excludedCountries(in countryPickerView: CountryPickerView) -> [Country] { return [] } } From 69fdc40f6ac1a9ffab1958a1af44d4e9d6fb62a6 Mon Sep 17 00:00:00 2001 From: Kizito Nwose Date: Sun, 2 Aug 2020 17:35:27 +0200 Subject: [PATCH 3/3] Fix logic. --- CountryPickerView/CountryPickerView.swift | 2 +- CountryPickerView/CountryPickerViewController.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CountryPickerView/CountryPickerView.swift b/CountryPickerView/CountryPickerView.swift index ae38fb0..5a6801b 100644 --- a/CountryPickerView/CountryPickerView.swift +++ b/CountryPickerView/CountryPickerView.swift @@ -200,7 +200,7 @@ public class CountryPickerView: NibView { }() internal var usableCountries: [Country] { - let excluded = dataSource?.excludedCountriesInList(in: self) ?? [] + let excluded = dataSource?.excludedCountries(in: self) ?? [] return countries.filter { return !excluded.contains($0) } } } diff --git a/CountryPickerView/CountryPickerViewController.swift b/CountryPickerView/CountryPickerViewController.swift index 1d02692..10389d2 100644 --- a/CountryPickerView/CountryPickerViewController.swift +++ b/CountryPickerView/CountryPickerViewController.swift @@ -353,6 +353,6 @@ class CountryPickerViewDataSourceInternal: CountryPickerViewDataSource { } var excludedCountries: [Country] { - return view.dataSource?.excludedCountriesInList(in: view) ?? preferredCountries(in: view) + return view.dataSource?.excludedCountries(in: view) ?? excludedCountries(in: view) } }