diff --git a/FlagPhoneNumber.xcodeproj/project.pbxproj b/FlagPhoneNumber.xcodeproj/project.pbxproj index e6730eed..532fd7a0 100644 --- a/FlagPhoneNumber.xcodeproj/project.pbxproj +++ b/FlagPhoneNumber.xcodeproj/project.pbxproj @@ -33,7 +33,6 @@ 0D19301A2293078C002583B1 /* NBPhoneNumberDesc.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D192FEE2293078B002583B1 /* NBPhoneNumberDesc.m */; }; 0D19301B2293078C002583B1 /* NBNumberFormat.h in Headers */ = {isa = PBXBuildFile; fileRef = 0D192FEF2293078B002583B1 /* NBNumberFormat.h */; settings = {ATTRIBUTES = (Public, ); }; }; 0D19301C2293078C002583B1 /* NBPhoneNumberDefines.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D192FF02293078B002583B1 /* NBPhoneNumberDefines.m */; }; - 0D19301E2293078C002583B1 /* FPNCountryView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0D192FF32293078B002583B1 /* FPNCountryView.xib */; }; 0D19301F2293078C002583B1 /* countryCodes.json in Resources */ = {isa = PBXBuildFile; fileRef = 0D192FF42293078B002583B1 /* countryCodes.json */; }; 0D1930202293078C002583B1 /* FlagKit.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0D192FF52293078B002583B1 /* FlagKit.xcassets */; }; 0D1930222293078C002583B1 /* FPNNibLoadingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D192FF82293078B002583B1 /* FPNNibLoadingView.swift */; }; @@ -51,6 +50,9 @@ 0D79D5E22397CB3600DD3E14 /* FPNOBJCCountryKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D79D5DC2397CB3600DD3E14 /* FPNOBJCCountryKey.swift */; }; 0D79D5E32397CB3600DD3E14 /* FPNUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D79D5DD2397CB3600DD3E14 /* FPNUtils.swift */; }; 0DA9CA0B2293E39500723B96 /* FlagPhoneNumber.h in Headers */ = {isa = PBXBuildFile; fileRef = 0D8C7134229296D40026E0C7 /* FlagPhoneNumber.h */; settings = {ATTRIBUTES = (Public, ); }; }; + F965B3BF24477F8300C873FF /* CountryListTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = F965B3BD24477F8300C873FF /* CountryListTableViewCell.xib */; }; + F965B3C024477F8300C873FF /* CountryListTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = F965B3BE24477F8300C873FF /* CountryListTableViewCell.swift */; }; + F965B3C224477FC200C873FF /* FPNCountryView.xib in Resources */ = {isa = PBXBuildFile; fileRef = F965B3C124477FC100C873FF /* FPNCountryView.xib */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -80,7 +82,6 @@ 0D192FEE2293078B002583B1 /* NBPhoneNumberDesc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NBPhoneNumberDesc.m; sourceTree = ""; }; 0D192FEF2293078B002583B1 /* NBNumberFormat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NBNumberFormat.h; sourceTree = ""; }; 0D192FF02293078B002583B1 /* NBPhoneNumberDefines.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NBPhoneNumberDefines.m; sourceTree = ""; }; - 0D192FF32293078B002583B1 /* FPNCountryView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = FPNCountryView.xib; sourceTree = ""; }; 0D192FF42293078B002583B1 /* countryCodes.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = countryCodes.json; sourceTree = ""; }; 0D192FF52293078B002583B1 /* FlagKit.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = FlagKit.xcassets; sourceTree = ""; }; 0D192FF82293078B002583B1 /* FPNNibLoadingView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FPNNibLoadingView.swift; sourceTree = ""; }; @@ -100,6 +101,9 @@ 0D8C7134229296D40026E0C7 /* FlagPhoneNumber.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FlagPhoneNumber.h; sourceTree = ""; }; 0D8C7135229296D40026E0C7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 0DF48B3922917047006AB39C /* FlagPhoneNumber.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FlagPhoneNumber.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + F965B3BD24477F8300C873FF /* CountryListTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = CountryListTableViewCell.xib; sourceTree = ""; }; + F965B3BE24477F8300C873FF /* CountryListTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CountryListTableViewCell.swift; sourceTree = ""; }; + F965B3C124477FC100C873FF /* FPNCountryView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = FPNCountryView.xib; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -173,7 +177,7 @@ 0D192FF22293078B002583B1 /* Resources */ = { isa = PBXGroup; children = ( - 0D192FF32293078B002583B1 /* FPNCountryView.xib */, + F965B3C124477FC100C873FF /* FPNCountryView.xib */, 0D192FF42293078B002583B1 /* countryCodes.json */, 0D192FF52293078B002583B1 /* FlagKit.xcassets */, ); @@ -206,6 +210,8 @@ 0DF48B1422916F9F006AB39C /* Sources */ = { isa = PBXGroup; children = ( + F965B3BE24477F8300C873FF /* CountryListTableViewCell.swift */, + F965B3BD24477F8300C873FF /* CountryListTableViewCell.xib */, 0D79D5C82397CAAA00DD3E14 /* FlagPhoneNumber-Bridging-Header.h */, 0D8C7134229296D40026E0C7 /* FlagPhoneNumber.h */, 0D8C7135229296D40026E0C7 /* Info.plist */, @@ -305,9 +311,10 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - 0D19301E2293078C002583B1 /* FPNCountryView.xib in Resources */, + F965B3C224477FC200C873FF /* FPNCountryView.xib in Resources */, 0D1930172293078C002583B1 /* NBPhoneNumberMetadata.plist in Resources */, 0D19301F2293078C002583B1 /* countryCodes.json in Resources */, + F965B3BF24477F8300C873FF /* CountryListTableViewCell.xib in Resources */, 0D1930202293078C002583B1 /* FlagKit.xcassets in Resources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -335,6 +342,7 @@ 0D1930282293078C002583B1 /* FPNTextField.swift in Sources */, 0D79D5E02397CB3600DD3E14 /* FPNCountryCode.swift in Sources */, 0D19301A2293078C002583B1 /* NBPhoneNumberDesc.m in Sources */, + F965B3C024477F8300C873FF /* CountryListTableViewCell.swift in Sources */, 0D1930052293078C002583B1 /* NBPhoneNumberUtil.m in Sources */, 0D1930032293078C002583B1 /* NBAsYouTypeFormatter.m in Sources */, 0D79D5E22397CB3600DD3E14 /* FPNOBJCCountryKey.swift in Sources */, diff --git a/Sources/CountryListTableViewCell.swift b/Sources/CountryListTableViewCell.swift new file mode 100644 index 00000000..09c81e9e --- /dev/null +++ b/Sources/CountryListTableViewCell.swift @@ -0,0 +1,36 @@ +// +// CountryListTableViewCell.swift +// FlagPhoneNumber +// +// Created by Ahmed Allam on 4/15/20. +// + +import UIKit + +class CountryListTableViewCell: UITableViewCell { + + @IBOutlet weak var flagLabel: UILabel! + @IBOutlet weak var countryNameLabel: UILabel! + @IBOutlet weak var countryCodeLabel: UILabel! + + override func awakeFromNib() { + super.awakeFromNib() + // Initialization code + } + + override func setSelected(_ selected: Bool, animated: Bool) { + super.setSelected(selected, animated: animated) + + // Configure the view for the selected state + } + + + + func setup(_ country: FPNCountry, showPhoneCode: Bool) { + flagLabel.text = country.flag + countryNameLabel.text = country.name + if showPhoneCode{ + countryCodeLabel.text = country.phoneCode + } + } +} diff --git a/Sources/CountryListTableViewCell.xib b/Sources/CountryListTableViewCell.xib new file mode 100644 index 00000000..f1d338d3 --- /dev/null +++ b/Sources/CountryListTableViewCell.xib @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Sources/FPNCountry.swift b/Sources/FPNCountry.swift index cb33c11f..626e1b8c 100644 --- a/Sources/FPNCountry.swift +++ b/Sources/FPNCountry.swift @@ -1,25 +1,29 @@ import UIKit public struct FPNCountry: Equatable { - - public var code: FPNCountryCode - public var name: String - public var phoneCode: String - var flag: UIImage? - - init(code: String, name: String, phoneCode: String) { - self.name = name - self.phoneCode = phoneCode - self.code = FPNCountryCode(rawValue: code)! - - if let flag = UIImage(named: code, in: Bundle.FlagIcons, compatibleWith: nil) { - self.flag = flag - } else { - self.flag = UIImage(named: "unknown", in: Bundle.FlagIcons, compatibleWith: nil) - } - } - - static public func ==(lhs: FPNCountry, rhs: FPNCountry) -> Bool { - return lhs.code == rhs.code - } + + public var code: FPNCountryCode + public var name: String + public var phoneCode: String + var flag: String = "" + + init(code: String, name: String, phoneCode: String) { + self.name = name + self.phoneCode = phoneCode + self.code = FPNCountryCode(rawValue: code)! + flag = getFlag(country: code) + } + + static public func ==(lhs: FPNCountry, rhs: FPNCountry) -> Bool { + return lhs.code == rhs.code + } + + func getFlag(country:String) -> String { + let base : UInt32 = 127397 + var s = "" + for v in country.unicodeScalars { + s.unicodeScalars.append(UnicodeScalar(base + v.value)!) + } + return String(s) + } } diff --git a/Sources/FPNCountryListViewController.swift b/Sources/FPNCountryListViewController.swift index 8cff2594..d7321db8 100644 --- a/Sources/FPNCountryListViewController.swift +++ b/Sources/FPNCountryListViewController.swift @@ -9,129 +9,129 @@ import UIKit open class FPNCountryListViewController: UITableViewController, UISearchResultsUpdating, UISearchControllerDelegate { - - open var repository: FPNCountryRepository? - open var showCountryPhoneCode: Bool = true - open var searchController: UISearchController = UISearchController(searchResultsController: nil) - open var didSelect: ((FPNCountry) -> Void)? - - var results: [FPNCountry]? - - override open func viewDidLoad() { - super.viewDidLoad() - - tableView.tableFooterView = UIView() - - initSearchBarController() - } - - open func setup(repository: FPNCountryRepository) { - self.repository = repository - } - - private func initSearchBarController() { - searchController.searchResultsUpdater = self - searchController.delegate = self - - if #available(iOS 9.1, *) { - searchController.obscuresBackgroundDuringPresentation = false - } else { - // Fallback on earlier versions - } - - if #available(iOS 11.0, *) { - navigationItem.searchController = searchController - navigationItem.hidesSearchBarWhenScrolling = false - } else { - searchController.dimsBackgroundDuringPresentation = false - searchController.hidesNavigationBarDuringPresentation = true - searchController.definesPresentationContext = true - - // searchController.searchBar.sizeToFit() - tableView.tableHeaderView = searchController.searchBar - } - definesPresentationContext = true - } - - private func getItem(at indexPath: IndexPath) -> FPNCountry { - if searchController.isActive && results != nil && results!.count > 0 { - return results![indexPath.row] - } else { - return repository!.countries[indexPath.row] - } - } - - override open func numberOfSections(in tableView: UITableView) -> Int { - return 1 - } - - override open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - if searchController.isActive { - if let count = searchController.searchBar.text?.count, count > 0 { - return results?.count ?? 0 - } - } - return repository?.countries.count ?? 0 - } - - override open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let cell = UITableViewCell(style: .value1, reuseIdentifier: nil) - let country = getItem(at: indexPath) - - cell.imageView?.image = country.flag - cell.textLabel?.text = country.name - - if showCountryPhoneCode { - cell.detailTextLabel?.text = country.phoneCode - } - - return cell - } - - override open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - let country = getItem(at: indexPath) - - tableView.deselectRow(at: indexPath, animated: true) - - didSelect?(country) - - searchController.isActive = false - searchController.searchBar.resignFirstResponder() - dismiss(animated: true, completion: nil) - } - - // UISearchResultsUpdating - - open func updateSearchResults(for searchController: UISearchController) { - guard let countries = repository?.countries else { return } - - if countries.isEmpty { - results?.removeAll() - return - } else if searchController.searchBar.text == "" { - results?.removeAll() - tableView.reloadData() - return - } - - if let searchText = searchController.searchBar.text, searchText.count > 0 { - results = countries.filter({(item: FPNCountry) -> Bool in - if item.name.lowercased().range(of: searchText.lowercased()) != nil { - return true - } else if item.code.rawValue.lowercased().range(of: searchText.lowercased()) != nil { - return true - } else if item.phoneCode.lowercased().range(of: searchText.lowercased()) != nil { - return true - } - return false - }) - } - tableView.reloadData() - } - - // UISearchControllerDelegate - - open func willDismissSearchController(_ searchController: UISearchController) { - results?.removeAll() - } + + open var repository: FPNCountryRepository? + open var showCountryPhoneCode: Bool = true + open var searchController: UISearchController = UISearchController(searchResultsController: nil) + open var didSelect: ((FPNCountry) -> Void)? + + var results: [FPNCountry]? + + override open func viewDidLoad() { + super.viewDidLoad() + + tableView.tableFooterView = UIView() + registerForListCell() + initSearchBarController() + } + + open func setup(repository: FPNCountryRepository) { + self.repository = repository + } + + func registerForListCell(){ + let nib = UINib(nibName: "CountryListTableViewCell", bundle: Bundle.FlagPhoneNumber()) + tableView.register(nib, forCellReuseIdentifier: "CountryListTableViewCell") + } + + private func initSearchBarController() { + searchController.searchResultsUpdater = self + searchController.delegate = self + + if #available(iOS 9.1, *) { + searchController.obscuresBackgroundDuringPresentation = false + } else { + // Fallback on earlier versions + } + + if #available(iOS 11.0, *) { + navigationItem.searchController = searchController + navigationItem.hidesSearchBarWhenScrolling = false + } else { + searchController.dimsBackgroundDuringPresentation = false + searchController.hidesNavigationBarDuringPresentation = true + searchController.definesPresentationContext = true + + // searchController.searchBar.sizeToFit() + tableView.tableHeaderView = searchController.searchBar + } + definesPresentationContext = true + } + + private func getItem(at indexPath: IndexPath) -> FPNCountry { + if searchController.isActive && results != nil && results!.count > 0 { + return results![indexPath.row] + } else { + return repository!.countries[indexPath.row] + } + } + + override open func numberOfSections(in tableView: UITableView) -> Int { + return 1 + } + + override open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + if searchController.isActive { + if let count = searchController.searchBar.text?.count, count > 0 { + return results?.count ?? 0 + } + } + return repository?.countries.count ?? 0 + } + + override open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "CountryListTableViewCell", for: indexPath) as! CountryListTableViewCell + let country = getItem(at: indexPath) + + cell.setup(country, showPhoneCode: showCountryPhoneCode) + + return cell + } + + override open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + let country = getItem(at: indexPath) + + tableView.deselectRow(at: indexPath, animated: true) + + didSelect?(country) + + searchController.isActive = false + searchController.searchBar.resignFirstResponder() + dismiss(animated: true, completion: nil) + } + + // UISearchResultsUpdating + + open func updateSearchResults(for searchController: UISearchController) { + guard let countries = repository?.countries else { return } + + if countries.isEmpty { + results?.removeAll() + return + } else if searchController.searchBar.text == "" { + results?.removeAll() + tableView.reloadData() + return + } + + if let searchText = searchController.searchBar.text, searchText.count > 0 { + results = countries.filter({(item: FPNCountry) -> Bool in + if item.name.lowercased().range(of: searchText.lowercased()) != nil { + return true + } else if item.code.rawValue.lowercased().range(of: searchText.lowercased()) != nil { + return true + } else if item.phoneCode.lowercased().range(of: searchText.lowercased()) != nil { + return true + } + return false + }) + } + tableView.reloadData() + } + + // UISearchControllerDelegate + + open func willDismissSearchController(_ searchController: UISearchController) { + results?.removeAll() + } } diff --git a/Sources/FPNCountryPicker/FPNCountryView.swift b/Sources/FPNCountryPicker/FPNCountryView.swift index aed0c73d..8cdb1be5 100644 --- a/Sources/FPNCountryPicker/FPNCountryView.swift +++ b/Sources/FPNCountryPicker/FPNCountryView.swift @@ -2,7 +2,7 @@ import UIKit class FPNCountryView: NibLoadingView { - @IBOutlet weak var flagImageView: UIImageView! + @IBOutlet weak var flagLabel: UILabel! @IBOutlet weak var countryNameLabel: UILabel! @IBOutlet weak var countryCodeLabel: UILabel! @@ -15,7 +15,7 @@ class FPNCountryView: NibLoadingView { } func setup(_ country: FPNCountry) { - flagImageView.image = country.flag + flagLabel.text = country.flag countryCodeLabel.text = country.phoneCode countryNameLabel.text = country.name } diff --git a/Sources/FPNTextField.swift b/Sources/FPNTextField.swift index f6d4e414..b966db0e 100644 --- a/Sources/FPNTextField.swift +++ b/Sources/FPNTextField.swift @@ -374,7 +374,7 @@ open class FPNTextField: UITextField { formatter = NBAsYouTypeFormatter(regionCode: countryCode.rawValue) } - flagButton.setImage(selectedCountry?.flag, for: .normal) + flagButton.setTitle(selectedCountry?.flag, for: .normal) if let phoneCode = selectedCountry?.phoneCode { phoneCodeTextField.text = phoneCode diff --git a/Sources/Resources/FPNCountryView.xib b/Sources/Resources/FPNCountryView.xib index 15a1a023..57f30941 100644 --- a/Sources/Resources/FPNCountryView.xib +++ b/Sources/Resources/FPNCountryView.xib @@ -1,9 +1,9 @@ - + - + @@ -11,21 +11,14 @@ - + - + - - - - - - - + + - - - + + + +