@@ -10,45 +10,31 @@ import UIKit
10
10
11
11
open class FPNCountryListViewController : UITableViewController , UISearchResultsUpdating , UISearchControllerDelegate {
12
12
13
- var countries : [ FPNCountry ]
14
- var showCountryPhoneCode : Bool
13
+ open var repository : FPNCountryRepository ?
14
+ open var showCountryPhoneCode : Bool = true
15
+ open var searchController : UISearchController = UISearchController ( searchResultsController: nil )
16
+ open var didSelect : ( ( FPNCountry ) -> Void ) ?
15
17
16
- var searchController : UISearchController ?
17
18
var results : [ FPNCountry ] ?
18
19
19
- public var didSelect : ( ( FPNCountry ) -> Void ) ?
20
-
21
- public init ( countries: [ FPNCountry ] , showCountryPhoneCode: Bool = true ) {
22
- self . countries = countries
23
- self . showCountryPhoneCode = showCountryPhoneCode
24
-
25
- super. init ( nibName: nil , bundle: nil )
26
- }
27
-
28
- required public init ? ( coder aDecoder: NSCoder ) {
29
- fatalError ( " init(coder:) has not been implemented " )
30
- }
31
-
32
20
override open func viewDidLoad( ) {
33
21
super. viewDidLoad ( )
34
22
35
23
tableView. tableFooterView = UIView ( )
36
- navigationItem. leftBarButtonItem = UIBarButtonItem ( barButtonSystemItem: . stop, target: self , action: #selector( dismissController) )
37
24
38
25
initSearchBarController ( )
39
26
}
40
27
41
- @ objc private func dismissController ( ) {
42
- dismiss ( animated : true , completion : nil )
28
+ open func setup ( repository : FPNCountryRepository ) {
29
+ self . repository = repository
43
30
}
44
31
45
32
private func initSearchBarController( ) {
46
- searchController = UISearchController ( searchResultsController: nil )
47
- searchController? . searchResultsUpdater = self
48
- searchController? . delegate = self
33
+ searchController. searchResultsUpdater = self
34
+ searchController. delegate = self
49
35
50
36
if #available( iOS 9 . 1 , * ) {
51
- searchController? . obscuresBackgroundDuringPresentation = false
37
+ searchController. obscuresBackgroundDuringPresentation = false
52
38
} else {
53
39
// Fallback on earlier versions
54
40
}
@@ -57,39 +43,35 @@ open class FPNCountryListViewController: UITableViewController, UISearchResultsU
57
43
navigationItem. searchController = searchController
58
44
navigationItem. hidesSearchBarWhenScrolling = false
59
45
} else {
60
- searchController? . dimsBackgroundDuringPresentation = false
61
- searchController? . hidesNavigationBarDuringPresentation = true
62
- searchController? . definesPresentationContext = true
46
+ searchController. dimsBackgroundDuringPresentation = false
47
+ searchController. hidesNavigationBarDuringPresentation = true
48
+ searchController. definesPresentationContext = true
63
49
64
- // searchController? .searchBar.sizeToFit()
65
- tableView. tableHeaderView = searchController? . searchBar
50
+ // searchController.searchBar.sizeToFit()
51
+ tableView. tableHeaderView = searchController. searchBar
66
52
}
67
53
definesPresentationContext = true
68
54
}
69
55
70
56
private func getItem( at indexPath: IndexPath ) -> FPNCountry {
71
- var array : [ FPNCountry ] !
72
-
73
- if let searchController = searchController, searchController. isActive && results != nil && results!. count > 0 {
74
- array = results
57
+ if searchController. isActive && results != nil && results!. count > 0 {
58
+ return results![ indexPath. row]
75
59
} else {
76
- array = countries
60
+ return repository! . countries [ indexPath . row ]
77
61
}
78
-
79
- return array [ indexPath. row]
80
62
}
81
63
82
64
override open func numberOfSections( in tableView: UITableView ) -> Int {
83
65
return 1
84
66
}
85
67
86
68
override open func tableView( _ tableView: UITableView , numberOfRowsInSection section: Int ) -> Int {
87
- if let searchController = searchController , searchController. isActive {
69
+ if searchController. isActive {
88
70
if let count = searchController. searchBar. text? . count, count > 0 {
89
71
return results? . count ?? 0
90
72
}
91
73
}
92
- return countries. count
74
+ return repository ? . countries. count ?? 0
93
75
}
94
76
95
77
override open func tableView( _ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
@@ -113,14 +95,16 @@ open class FPNCountryListViewController: UITableViewController, UISearchResultsU
113
95
114
96
didSelect ? ( country)
115
97
116
- searchController? . isActive = false
117
- searchController? . searchBar. resignFirstResponder ( )
118
- dismissController ( )
98
+ searchController. isActive = false
99
+ searchController. searchBar. resignFirstResponder ( )
100
+ dismiss ( animated : true , completion : nil )
119
101
}
120
102
121
103
// UISearchResultsUpdating
122
104
123
- public func updateSearchResults( for searchController: UISearchController ) {
105
+ open func updateSearchResults( for searchController: UISearchController ) {
106
+ guard let countries = repository? . countries else { return }
107
+
124
108
if countries. isEmpty {
125
109
results? . removeAll ( )
126
110
return
@@ -147,7 +131,7 @@ open class FPNCountryListViewController: UITableViewController, UISearchResultsU
147
131
148
132
// UISearchControllerDelegate
149
133
150
- public func willDismissSearchController( _ searchController: UISearchController ) {
134
+ open func willDismissSearchController( _ searchController: UISearchController ) {
151
135
results? . removeAll ( )
152
136
}
153
137
}
0 commit comments