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/CountryPickerView.swift b/CountryPickerView/CountryPickerView.swift index f811e6d..5a6801b 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?.excludedCountries(in: self) ?? [] + return countries.filter { return !excluded.contains($0) } + } } //MARK: Helper methods diff --git a/CountryPickerView/CountryPickerViewController.swift b/CountryPickerView/CountryPickerViewController.swift index e2b7166..10389d2 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?.excludedCountries(in: view) ?? excludedCountries(in: view) + } } diff --git a/CountryPickerView/Delegate+DataSource.swift b/CountryPickerView/Delegate+DataSource.swift index 3ce33fd..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. @@ -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 excludedCountries(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 excludedCountries(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 [] }