Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- compatible swift 4.0 / search enhancements #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions Source/MICountryPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit

class MICountry: NSObject {
let name: String
@objc let name: String
let code: String
var section: Int?
let dialCode: String!
Expand Down Expand Up @@ -54,7 +54,7 @@ open class MICountryPicker: UITableViewController {
let displayName = (locale as NSLocale).displayName(forKey: NSLocale.Key.countryCode, value: countryCode)
let countryData = CallingCodes.filter { $0["code"] == countryCode }
let country: MICountry

if countryData.count > 0, let dialCode = countryData[0]["dial_code"] {
country = MICountry(name: displayName!, code: countryCode, dialCode: dialCode)
} else {
Expand Down Expand Up @@ -106,7 +106,7 @@ open class MICountryPicker: UITableViewController {
open var didSelectCountryClosure: ((String, String) -> ())?
open var didSelectCountryWithCallingCodeClosure: ((String, String, String) -> ())?
open var showCallingCodes = false

convenience public init(completionHandler: @escaping ((String, String) -> ())) {
self.init()
self.didSelectCountryClosure = completionHandler
Expand Down Expand Up @@ -138,12 +138,30 @@ open class MICountryPicker: UITableViewController {

sections.forEach { (section) -> () in
section.countries.forEach({ (country) -> () in
if country.name.characters.count >= searchText.characters.count {
let result = country.name.compare(searchText, options: [.caseInsensitive, .diacriticInsensitive], range: searchText.characters.startIndex ..< searchText.characters.endIndex)
if country.name.count >= searchText.count {
let result = country.name.compare(searchText, options: [.caseInsensitive, .diacriticInsensitive], range: searchText.startIndex ..< searchText.endIndex)
if result == .orderedSame {
filteredList.append(country)
}
}
if country.code.count >= searchText.count {
let result = country.code.compare(searchText, options: [.caseInsensitive, .diacriticInsensitive], range: searchText.startIndex ..< searchText.endIndex)
if result == .orderedSame {
filteredList.append(country)
}
}
if country.dialCode.count >= searchText.count {
if Int(searchText) != nil {

let dialCodeString = "+"+searchText
if dialCodeString.count <= country.dialCode.count {
let result = country.dialCode.compare(dialCodeString, options: [.caseInsensitive, .diacriticInsensitive], range: dialCodeString.startIndex ..< dialCodeString.endIndex)
if result == .orderedSame {
filteredList.append(country)
}
}
}
}
})
}

Expand All @@ -156,14 +174,14 @@ open class MICountryPicker: UITableViewController {
extension MICountryPicker {

override open func numberOfSections(in tableView: UITableView) -> Int {
if searchController.searchBar.text!.characters.count > 0 {
if searchController.searchBar.text!.count > 0 {
return 1
}
return sections.count
}

override open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.searchBar.text!.characters.count > 0 {
if searchController.searchBar.text!.count > 0 {
return filteredList.count
}
return sections[section].countries.count
Expand All @@ -180,19 +198,19 @@ extension MICountryPicker {
let cell: UITableViewCell! = tempCell

let country: MICountry!
if searchController.searchBar.text!.characters.count > 0 {
if searchController.searchBar.text!.count > 0 {
country = filteredList[(indexPath as NSIndexPath).row]
} else {
country = sections[(indexPath as NSIndexPath).section].countries[(indexPath as NSIndexPath).row]

}

if showCallingCodes {
cell.textLabel?.text = country.name + " (" + country.dialCode! + ")"
} else {
cell.textLabel?.text = country.name
}

let bundle = "assets.bundle/"
cell.imageView!.image = UIImage(named: bundle + country.code.lowercased() + ".png", in: Bundle(for: MICountryPicker.self), compatibleWith: nil)
return cell
Expand All @@ -210,8 +228,8 @@ extension MICountryPicker {
}

override open func tableView(_ tableView: UITableView,
sectionForSectionIndexTitle title: String,
at index: Int)
sectionForSectionIndexTitle title: String,
at index: Int)
-> Int {
return collation.section(forSectionIndexTitle: index)
}
Expand All @@ -224,7 +242,7 @@ extension MICountryPicker {
override open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let country: MICountry!
if searchController.searchBar.text!.characters.count > 0 {
if searchController.searchBar.text!.count > 0 {
country = filteredList[(indexPath as NSIndexPath).row]
} else {
country = sections[(indexPath as NSIndexPath).section].countries[(indexPath as NSIndexPath).row]
Expand Down