Skip to content

Commit

Permalink
Merge pull request #15 from graycampbell/refactoring
Browse files Browse the repository at this point in the history
Update country calling code to include '+'
  • Loading branch information
graycampbell committed Jan 6, 2018
2 parents 232fd18 + d96e8b2 commit d5e303b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 136 deletions.
4 changes: 0 additions & 4 deletions GCCountryPicker.xcodeproj/project.pbxproj
Expand Up @@ -17,7 +17,6 @@
E2135FFF1FAD6A19005B0ACF /* GCSearchResultsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2135FF31FAD6A19005B0ACF /* GCSearchResultsController.swift */; };
E21360001FAD6A19005B0ACF /* GCSearchResultsDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2135FF41FAD6A19005B0ACF /* GCSearchResultsDelegate.swift */; };
E21360011FAD6A19005B0ACF /* GCSearchResultsDisplayMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2135FF51FAD6A19005B0ACF /* GCSearchResultsDisplayMode.swift */; };
E21360021FAD6A19005B0ACF /* GCTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2135FF61FAD6A19005B0ACF /* GCTableViewCell.swift */; };
E21360401FAD6FD7005B0ACF /* GCCountryPicker.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E2644B3B1F7D92DD0034EA06 /* GCCountryPicker.framework */; };
E213604C1FAD7123005B0ACF /* GCCountryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E213604B1FAD7123005B0ACF /* GCCountryTests.swift */; };
/* End PBXBuildFile section */
Expand All @@ -43,7 +42,6 @@
E2135FF31FAD6A19005B0ACF /* GCSearchResultsController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GCSearchResultsController.swift; sourceTree = "<group>"; };
E2135FF41FAD6A19005B0ACF /* GCSearchResultsDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GCSearchResultsDelegate.swift; sourceTree = "<group>"; };
E2135FF51FAD6A19005B0ACF /* GCSearchResultsDisplayMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GCSearchResultsDisplayMode.swift; sourceTree = "<group>"; };
E2135FF61FAD6A19005B0ACF /* GCTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GCTableViewCell.swift; sourceTree = "<group>"; };
E2135FF71FAD6A19005B0ACF /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
E213603B1FAD6FD7005B0ACF /* GCCountryPickerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GCCountryPickerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
E21360481FAD6FEA005B0ACF /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -83,7 +81,6 @@
E2135FF31FAD6A19005B0ACF /* GCSearchResultsController.swift */,
E2135FF41FAD6A19005B0ACF /* GCSearchResultsDelegate.swift */,
E2135FF51FAD6A19005B0ACF /* GCSearchResultsDisplayMode.swift */,
E2135FF61FAD6A19005B0ACF /* GCTableViewCell.swift */,
E2135FF71FAD6A19005B0ACF /* Info.plist */,
);
name = GCCountryPicker;
Expand Down Expand Up @@ -244,7 +241,6 @@
E2135FFB1FAD6A19005B0ACF /* GCCountryPickerDelegate.swift in Sources */,
E2135FF81FAD6A19005B0ACF /* GCCountry.swift in Sources */,
E21360001FAD6A19005B0ACF /* GCSearchResultsDelegate.swift in Sources */,
E21360021FAD6A19005B0ACF /* GCTableViewCell.swift in Sources */,
E2135FFE1FAD6A19005B0ACF /* GCSearchResult.swift in Sources */,
E2135FFA1FAD6A19005B0ACF /* GCCountryPickerDataSource.swift in Sources */,
);
Expand Down
4 changes: 3 additions & 1 deletion Sources/GCCountryPicker/GCCountry.swift
Expand Up @@ -54,6 +54,8 @@ public final class GCCountry: NSObject {

let callingCodes: [String: String] = ["AD": "376", "AE": "971", "AF": "93", "AG": "1", "AI": "1", "AL": "355", "AM": "374", "AO": "244", "AQ": "672", "AR": "54", "AS": "1", "AT": "43", "AU": "61", "AW": "297", "AX": "358", "AZ": "994", "BA": "387", "BB": "1", "BD": "880", "BE": "32", "BF": "226", "BG": "359", "BH": "973", "BI": "257", "BJ": "229", "BL": "590", "BM": "1", "BN": "673", "BO": "591", "BQ": "599", "BR": "55", "BS": "1", "BT": "975", "BV": "47", "BW": "267", "BY": "375", "BZ": "501", "CA": "1", "CC": "61", "CD": "243", "CF": "236", "CG": "242", "CH": "41", "CI": "225", "CK": "682", "CL": "56", "CM": "237", "CN": "86", "CO": "57", "CR": "506", "CU": "53", "CV": "238", "CW": "599", "CX": "61", "CY": "357", "CZ": "420", "DE": "49", "DJ": "253", "DK": "45", "DM": "1", "DO": "1", "DZ": "213", "EC": "593", "EE": "372", "EG": "20", "EH": "212", "ER": "291", "ES": "34", "ET": "251", "FI": "358", "FJ": "679", "FK": "500", "FM": "691", "FO": "298", "FR": "33", "GA": "241", "GB": "44", "GD": "1", "GE": "995", "GF": "594", "GG": "44", "GH": "233", "GI": "350", "GL": "299", "GM": "220", "GN": "224", "GP": "590", "GQ": "240", "GR": "30", "GS": "500", "GT": "502", "GU": "1", "GW": "245", "GY": "592", "HK": "852", "HN": "504", "HR": "385", "HT": "509", "HU": "36", "ID": "62", "IE": "353", "IL": "972", "IM": "44", "IN": "91", "IO": "246", "IQ": "964", "IR": "98", "IS": "354", "IT": "39", "JE": "44", "JM": "1", "JO": "962", "JP": "81", "KE": "254", "KG": "996", "KH": "855", "KI": "686", "KM": "269", "KN": "1", "KP": "850", "KR": "82", "KW": "965", "KY": "1", "KZ": "7", "LA": "856", "LB": "961", "LC": "1", "LI": "423", "LK": "94", "LR": "231", "LS": "266", "LT": "370", "LU": "352", "LV": "371", "LY": "218", "MA": "212", "MC": "377", "MD": "373", "ME": "382", "MF": "590", "MG": "261", "MH": "692", "MK": "389", "ML": "223", "MM": "95", "MN": "976", "MO": "853", "MP": "1", "MQ": "596", "MR": "222", "MS": "1", "MT": "356", "MU": "230", "MV": "960", "MW": "265", "MX": "52", "MY": "60", "MZ": "258", "NA": "264", "NC": "687", "NE": "227", "NF": "672", "NG": "234", "NI": "505", "NL": "31", "NO": "47", "NP": "977", "NR": "674", "NU": "683", "NZ": "64", "OM": "968", "PA": "507", "PE": "51", "PF": "689", "PG": "675", "PH": "63", "PK": "92", "PL": "48", "PM": "508", "PN": "64", "PR": "1", "PS": "970", "PT": "351", "PW": "680", "PY": "595", "QA": "974", "RE": "262", "RO": "40", "RS": "381", "RU": "7", "RW": "250", "SA": "966", "SB": "677", "SC": "248", "SD": "249", "SE": "46", "SG": "65", "SH": "290", "SI": "386", "SJ": "47", "SK": "421", "SL": "232", "SM": "378", "SN": "221", "SO": "252", "SR": "597", "SS": "211", "ST": "239", "SV": "503", "SX": "1", "SY": "963", "SZ": "268", "TC": "1", "TD": "235", "TF": "262", "TG": "228", "TH": "66", "TJ": "992", "TK": "690", "TL": "670", "TM": "993", "TN": "216", "TO": "676", "TR": "90", "TT": "1", "TV": "688", "TW": "886", "TZ": "255", "UA": "380", "UG": "256", "UM": "1", "US": "1", "UY": "598", "UZ": "998", "VA": "39", "VC": "1", "VE": "58", "VG": "1", "VI": "1", "VN": "84", "VU": "678", "WF": "681", "WS": "685", "YE": "967", "YT": "262", "ZA": "27", "ZM": "260", "ZW": "263"]

return callingCodes[countryCode]
guard let callingCode = callingCodes[countryCode] else { return nil }

return "+" + callingCode
}
}
40 changes: 10 additions & 30 deletions Sources/GCCountryPicker/GCCountryPickerViewController.swift
Expand Up @@ -180,23 +180,14 @@ extension GCCountryPickerViewController: UISearchResultsUpdating {

countryMatchesSearchText = true
}
else if self.displayMode == .withCallingCodes, let _ = country.callingCode?.range(of: "^(\(searchText))", options: .regularExpression, range: nil, locale: nil) {
else if self.displayMode == .withCallingCodes, let _ = country.callingCode?.range(of: "^.{1}(\(searchText))", options: .regularExpression, range: nil, locale: nil) {

countryMatchesSearchText = true
}

if countryMatchesSearchText {

let searchResult: GCSearchResult

if let callingCode = country.callingCode {

searchResult = GCSearchResult(object: country, displayTitle: country.localizedDisplayName, accessoryTitle: "+" + callingCode)
}
else {

searchResult = GCSearchResult(object: country, displayTitle: country.localizedDisplayName, accessoryTitle: nil)
}
let searchResult = GCSearchResult(object: country, displayTitle: country.localizedDisplayName, accessoryTitle: country.callingCode)

searchResults.append(searchResult)
}
Expand Down Expand Up @@ -279,32 +270,21 @@ extension GCCountryPickerViewController {

public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .value1, reuseIdentifier: "Cell")

let country = self.countries[indexPath.section][indexPath.row]

cell.textLabel?.text = country.localizedDisplayName

switch self.displayMode {

case .withCallingCodes:
let cell = tableView.dequeueReusableCell(withIdentifier: GCTableViewCell.identifier) as? GCTableViewCell ?? GCTableViewCell(style: .default)

cell.titleLabel.text = country.localizedDisplayName

if let callingCode = country.callingCode {

cell.accessoryLabel.text = "+" + callingCode
}
else {

cell.accessoryLabel.text = nil
}

return cell
cell.detailTextLabel?.text = country.callingCode

case .withoutCallingCodes:
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") ?? UITableViewCell(style: .default, reuseIdentifier: "TableViewCell")

cell.textLabel?.text = country.localizedDisplayName

return cell
break
}

return cell
}
}
19 changes: 8 additions & 11 deletions Sources/GCCountryPicker/GCSearchResultsController.swift
Expand Up @@ -108,24 +108,21 @@ extension GCSearchResultsController {

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .value1, reuseIdentifier: "Cell")

let searchResult = self.searchResults[indexPath.row]

cell.textLabel?.text = searchResult.displayTitle

switch self.displayMode {

case .withAccessoryTitles:
let cell = tableView.dequeueReusableCell(withIdentifier: GCTableViewCell.identifier) as? GCTableViewCell ?? GCTableViewCell(style: .default)

cell.titleLabel.text = searchResult.displayTitle
cell.accessoryLabel.text = searchResult.accessoryTitle

return cell
cell.detailTextLabel?.text = searchResult.accessoryTitle

case .withoutAccessoryTitles:
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") ?? UITableViewCell(style: .default, reuseIdentifier: "TableViewCell")

cell.textLabel?.text = searchResult.displayTitle

return cell
break
}

return cell
}
}
90 changes: 0 additions & 90 deletions Sources/GCCountryPicker/GCTableViewCell.swift

This file was deleted.

0 comments on commit d5e303b

Please sign in to comment.