Skip to content

Commit

Permalink
Add localizedName property to Country model.
Browse files Browse the repository at this point in the history
  • Loading branch information
kizitonwose committed Feb 23, 2019
1 parent 5d994ec commit 74e10a2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
17 changes: 7 additions & 10 deletions CountryPickerView/CountryPickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,16 @@ public enum SearchBarPosition {
}

public struct Country {
public var name: String
public var code: String
public var phoneCode: String
public var flag: UIImage {
public var name: String
public var code: String
public var phoneCode: String
public var localizedName: String? {
return Locale.current.localizedString(forRegionCode: code)
}
public var flag: UIImage {
return UIImage(named: "CountryPickerView.bundle/Images/\(code.uppercased())",
in: Bundle(for: CountryPickerView.self), compatibleWith: nil)!
}

internal init(name: String, code: String, phoneCode: String) {
self.name = name
self.code = code
self.phoneCode = phoneCode
}
}

public func ==(lhs: Country, rhs: Country) -> Bool {
Expand Down
8 changes: 4 additions & 4 deletions CountryPickerView/CountryPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ extension CountryPickerViewController {

var header = Set<String>()
countriesArray.forEach{
let name = Locale.current.localizedString(forRegionCode: $0.code) ?? $0.name
let name = $0.localizedName ?? $0.name
header.insert(String(name[name.startIndex]))
}

var data = [String: [Country]]()

countriesArray.forEach({
let name = Locale.current.localizedString(forRegionCode: $0.code) ?? $0.name
let name = $0.localizedName ?? $0.name
let index = String(name[name.startIndex])
var dictValue = data[index] ?? [Country]()
dictValue.append($0)
Expand Down Expand Up @@ -145,7 +145,7 @@ extension CountryPickerViewController {
let country = isSearchMode ? searchResults[indexPath.row]
: countries[sectionsTitles[indexPath.section]]![indexPath.row]

let countryName = Locale.current.localizedString(forRegionCode: country.code) ?? country.name
let countryName = country.localizedName ?? country.name
let name = dataSource.showPhoneCodeInList ? "\(countryName) (\(country.phoneCode))" : countryName
cell.imageView?.image = country.flag

Expand Down Expand Up @@ -234,7 +234,7 @@ extension CountryPickerViewController: UISearchResultsUpdating {
}

searchResults.append(contentsOf: indexArray.filter({
let countryName = Locale.current.localizedString(forRegionCode: $0.code) ?? $0.name
let countryName = $0.localizedName ?? $0.name
return countryName.lowercased().hasPrefix(text.lowercased())
}))
}
Expand Down

0 comments on commit 74e10a2

Please sign in to comment.