@@ -10,42 +10,41 @@ import Foundation
1010import SBPickerSelector
1111import UIKit
1212import UIKitExtensions
13+ import SnapKit
1314
14- protocol DropDownDataSource {
15+ public protocol DropDownDataSource {
1516 func controllerForDropDownDisplaying( _ dropDown: DropDown ) -> UIViewController
16-
1717 func valuesForDropDown( _ dropDown: DropDown ) -> [ String ]
18-
1918 func selectedIndexForDropDown( _ dropDown: DropDown ) -> Int ?
20-
2119 func placeholderKeyForDropDown( _ dropDown: DropDown ) -> String ?
22-
2320 func isRequired( _ dropDown: DropDown ) -> Bool
21+ }
2422
23+ public protocol DropDownUIDataSource {
2524 func getTextColor( ) -> UIColor
26-
25+ func getPlaceholderColor ( ) -> UIColor
2726 func getFontName( ) -> String
28-
2927 func getFontSize( ) -> CGFloat
30-
31- func getRightIcon ( ) -> UIImage
28+ func getRightIcon ( ) -> UIImage ?
29+ func getButtonsColor ( ) -> UIColor
3230}
3331
34- protocol DropDownDelegate {
32+ public protocol DropDownDelegate {
3533 func dropDown( _ dropDown: DropDown ! , selectedValue value: String ! , index: Int )
3634
3735 func dropDownDeselectedValue( _ dropDown: DropDown ! )
3836}
3937
40- public class DropDown : OwnView {
38+ open class DropDown : OwnView {
4139 var dateFormat = " dd/MM/y "
4240
43- var delegate : DropDownDelegate !
44- var dataSource : DropDownDataSource ! {
41+ public var delegate : DropDownDelegate !
42+ public var uiDataSource : DropDownUIDataSource ! {
4543 didSet {
46- updatePlaceholder ( )
44+ updateUI ( )
4745 }
4846 }
47+ public var dataSource : DropDownDataSource !
4948
5049 var picker : SBPickerSelector = SBPickerSelector . picker ( )
5150 var values : [ String ] !
@@ -54,11 +53,38 @@ public class DropDown: OwnView {
5453
5554 var mainColor : UIColor = UIColor . blue
5655
57- @IBOutlet weak var rightIcon : UIImageView !
58- @IBOutlet weak var titleTextField : FormInput !
59-
60- override public func loadView( ) {
61- super. loadView ( )
56+ var rightIcon : UIImageView !
57+ open var titleTextField : FormInput !
58+ var mainButton : UIButton !
59+
60+ override open func loadView( ) {
61+ self . titleTextField = FormInput ( frame: CGRect ( x: 0 , y: 0 , width: 0 , height: 0 ) )
62+ self . titleTextField. borderStyle = . none
63+ self . addSubview ( self . titleTextField)
64+
65+ rightIcon = UIImageView ( frame: CGRect ( ) )
66+ self . addSubview ( rightIcon)
67+
68+ mainButton = UIButton ( type: . custom)
69+ mainButton. addTarget ( self , action: #selector( DropDown . mainButtonTouched ( _: ) ) , for: . touchUpInside)
70+ self . addSubview ( mainButton)
71+
72+ titleTextField. snp. makeConstraints { make in
73+ make. leading. equalTo ( self . snp. leading)
74+ make. height. equalTo ( self )
75+ make. centerY. equalTo ( self )
76+ make. trailing. equalTo ( rightIcon. snp. leading)
77+ }
78+ rightIcon. snp. makeConstraints { make in
79+ make. trailing. equalTo ( self )
80+ make. height. equalTo ( self )
81+ make. width. equalTo ( self . snp. height)
82+ }
83+ mainButton. snp. makeConstraints { maker in
84+ maker. edges. equalTo ( self )
85+ }
86+ self . layoutIfNeeded ( )
87+ self . layoutSubviews ( )
6288
6389 picker. delegate = self
6490 picker. pickerType = SBPickerSelectorType . text
@@ -67,7 +93,7 @@ public class DropDown: OwnView {
6793 picker. cancelButtonTitle = " Annuler "
6894 picker. cancelButton? . tintColor = mainColor
6995
70- updatePlaceholder ( )
96+ updateUI ( )
7197
7298 reloadData ( )
7399
@@ -79,20 +105,27 @@ public class DropDown: OwnView {
79105 )
80106 }
81107
82- func updatePlaceholder( ) {
83- if let _ = dataSource {
108+ func updateUI( ) {
109+ if let _ = dataSource, let _ = uiDataSource {
110+ picker. doneButton? . tintColor = uiDataSource. getButtonsColor ( )
111+ picker. cancelButton? . tintColor = uiDataSource. getButtonsColor ( )
112+
84113 if let existingPlaceholder = dataSource. placeholderKeyForDropDown ( self ) {
85114 updatePlaceholderWithValue ( existingPlaceholder)
86115 }
116+
117+ if let _ = uiDataSource. getRightIcon ( ) {
118+ rightIcon. image = uiDataSource. getRightIcon ( )
119+ }
87120 }
88121 }
89122
90123 func updatePlaceholderWithValue( _ value: String ) {
91124 titleTextField. attributedPlaceholder = NSAttributedString (
92125 string: value,
93126 attributes: [
94- NSForegroundColorAttributeName: self . dataSource . getTextColor ( ) ,
95- NSFontAttributeName: UIFont ( name: self . dataSource . getFontName ( ) , size: self . dataSource . getFontSize ( ) ) !
127+ NSForegroundColorAttributeName: self . uiDataSource . getPlaceholderColor ( ) ,
128+ NSFontAttributeName: UIFont ( name: self . uiDataSource . getFontName ( ) , size: self . uiDataSource . getFontSize ( ) ) !
96129 ]
97130 )
98131 }
@@ -110,8 +143,8 @@ public class DropDown: OwnView {
110143 titleTextField. text! = " "
111144 }
112145
113- func reloadData( ) {
114- updatePlaceholder ( )
146+ public func reloadData( ) {
147+ updateUI ( )
115148 if let itsDataSource = dataSource {
116149 var values : [ String ] = itsDataSource. valuesForDropDown ( self )
117150 var offset = 0
@@ -136,7 +169,7 @@ public class DropDown: OwnView {
136169 }
137170 }
138171
139- @ IBAction func mainButtonTouched( _ sender: AnyObject ) {
172+ func mainButtonTouched( _ sender: AnyObject ) {
140173 presentDropDown ( )
141174 }
142175
@@ -151,20 +184,14 @@ public class DropDown: OwnView {
151184 }
152185}
153186
154- extension DropDown : OwnViewProtocol {
155- public var viewName : String {
156- return " DropDown "
157- }
158- }
159-
160187extension DropDown : SBPickerSelectorDelegate {
161188 public func pickerSelector( _ selector: SBPickerSelector ! , selectedValue value: String ! , index idx: Int ) {
162189 let required = ( nil != dataSource && !dataSource. isRequired ( self ) )
163190 var offset = 0
164191 if ( required && 0 != idx) || !required {
165192 titleTextField. text = value
166- titleTextField. textColor = self . dataSource . getTextColor ( )
167- titleTextField. font = UIFont ( name: self . dataSource . getFontName ( ) , size: self . dataSource . getFontSize ( ) )
193+ titleTextField. textColor = self . uiDataSource . getTextColor ( )
194+ titleTextField. font = UIFont ( name: self . uiDataSource . getFontName ( ) , size: self . uiDataSource . getFontSize ( ) )
168195 if required {
169196 offset = 1
170197 }
@@ -190,8 +217,8 @@ extension DropDown: SBPickerSelectorDelegate {
190217 dateFormatter. dateFormat = self . dateFormat
191218
192219 titleTextField. text = dateFormatter. string ( from: date)
193- titleTextField. textColor = self . dataSource . getTextColor ( )
194- titleTextField. font = UIFont ( name: self . dataSource . getFontName ( ) , size: self . dataSource . getFontSize ( ) )
220+ titleTextField. textColor = self . uiDataSource . getTextColor ( )
221+ titleTextField. font = UIFont ( name: self . uiDataSource . getFontName ( ) , size: self . uiDataSource . getFontSize ( ) )
195222
196223 if let dateSelf: DateDropDown = self as? DateDropDown {
197224 if let dateDropDownDelegate: DateDropDownDelegate = dateSelf. dateDropDownDelegate {
0 commit comments