@@ -3,16 +3,41 @@ import WordPressShared
33import WordPressUI
44import WordPressKit
55
6+ public struct NUXButtonStyle {
7+ public let normal : ButtonStyle
8+ public let highlighted : ButtonStyle
9+ public let disabled : ButtonStyle
10+
11+ public struct ButtonStyle {
12+ public let backgroundColor : UIColor
13+ public let borderColor : UIColor
14+ public let titleColor : UIColor
15+
16+ public init ( backgroundColor: UIColor , borderColor: UIColor , titleColor: UIColor ) {
17+ self . backgroundColor = backgroundColor
18+ self . borderColor = borderColor
19+ self . titleColor = titleColor
20+ }
21+ }
22+
23+ public init ( normal: ButtonStyle , highlighted: ButtonStyle , disabled: ButtonStyle ) {
24+ self . normal = normal
25+ self . highlighted = highlighted
26+ self . disabled = disabled
27+ }
28+ }
629/// A stylized button used by Login controllers. It also can display a `UIActivityIndicatorView`.
730@objc open class NUXButton : UIButton {
831 @objc var isAnimating : Bool {
932 return activityIndicator. isAnimating
1033 }
1134
35+ var buttonStyle : NUXButtonStyle ?
36+
1237 open override var isEnabled : Bool {
1338 didSet {
1439 if #available( iOS 13 , * ) {
15- activityIndicator. color = isEnabled ? style . primaryTitleColor : style . disabledButtonActivityIndicatorColor
40+ activityIndicator. color = activityIndicatorColor ( isEnabled: isEnabled )
1641 }
1742 }
1843 }
@@ -24,7 +49,7 @@ import WordPressKit
2449 } else {
2550 indicator = UIActivityIndicatorView ( style: . white)
2651 }
27- indicator . color = WordPressAuthenticator . shared . style . primaryTitleColor
52+
2853 indicator. hidesWhenStopped = true
2954 return indicator
3055 } ( )
@@ -106,15 +131,12 @@ import WordPressKit
106131 }
107132 }
108133
109- /// Setup: shorter reference for style
110- ///
111- private let style = WordPressAuthenticator . shared. style
112-
113134 /// Setup: Everything = [Insets, Backgrounds, titleColor(s), titleLabel]
114135 ///
115136 private func configureAppearance( ) {
116137 configureInsets ( )
117138 configureBackgrounds ( )
139+ configureActivityIndicator ( )
118140 configureTitleColors ( )
119141 configureTitleLabel ( )
120142 }
@@ -125,31 +147,79 @@ import WordPressKit
125147 contentEdgeInsets = UIImage . DefaultRenderMetrics. contentInsets
126148 }
127149
150+ /// Setup: ActivityIndicator
151+ ///
152+ private func configureActivityIndicator( ) {
153+ activityIndicator. color = activityIndicatorColor ( )
154+ addSubview ( activityIndicator)
155+ }
156+
128157 /// Setup: BackgroundImage
129158 ///
130159 private func configureBackgrounds( ) {
160+ guard let buttonStyle = buttonStyle else {
161+ legacyConfigureBackgrounds ( )
162+ return
163+ }
164+
165+ let normalImage = UIImage . renderBackgroundImage ( fill: buttonStyle. normal. backgroundColor,
166+ border: buttonStyle. normal. borderColor)
167+
168+ let highlightedImage = UIImage . renderBackgroundImage ( fill: buttonStyle. highlighted. backgroundColor,
169+ border: buttonStyle. highlighted. borderColor)
170+
171+ let disabledImage = UIImage . renderBackgroundImage ( fill: buttonStyle. disabled. backgroundColor,
172+ border: buttonStyle. disabled. borderColor)
173+
174+ setBackgroundImage ( normalImage, for: . normal)
175+ setBackgroundImage ( highlightedImage, for: . highlighted)
176+ setBackgroundImage ( disabledImage, for: . disabled)
177+ }
178+
179+ /// Fallback method to configure the background colors based on the shared `WordPressAuthenticatorStyle`
180+ ///
181+ private func legacyConfigureBackgrounds( ) {
182+ let style = WordPressAuthenticator . shared. style
183+
131184 let normalImage : UIImage
132185 let highlightedImage : UIImage
133- let disabledImage = UIImage . renderBackgroundImage ( fill: style. disabledBackgroundColor, border: style. disabledBorderColor)
186+ let disabledImage = UIImage . renderBackgroundImage ( fill: style. disabledBackgroundColor,
187+ border: style. disabledBorderColor)
134188
135189 if isPrimary {
136- normalImage = UIImage . renderBackgroundImage ( fill: style. primaryNormalBackgroundColor, border: style. primaryNormalBorderColor)
137- highlightedImage = UIImage . renderBackgroundImage ( fill: style. primaryHighlightBackgroundColor, border: style. primaryHighlightBorderColor)
190+ normalImage = UIImage . renderBackgroundImage ( fill: style. primaryNormalBackgroundColor,
191+ border: style. primaryNormalBorderColor)
192+ highlightedImage = UIImage . renderBackgroundImage ( fill: style. primaryHighlightBackgroundColor,
193+ border: style. primaryHighlightBorderColor)
138194 } else {
139- normalImage = UIImage . renderBackgroundImage ( fill: style. secondaryNormalBackgroundColor, border: style. secondaryNormalBorderColor)
140- highlightedImage = UIImage . renderBackgroundImage ( fill: style. secondaryHighlightBackgroundColor, border: style. secondaryHighlightBorderColor)
195+ normalImage = UIImage . renderBackgroundImage ( fill: style. secondaryNormalBackgroundColor,
196+ border: style. secondaryNormalBorderColor)
197+ highlightedImage = UIImage . renderBackgroundImage ( fill: style. secondaryHighlightBackgroundColor,
198+ border: style. secondaryHighlightBorderColor)
141199 }
142200
143201 setBackgroundImage ( normalImage, for: . normal)
144202 setBackgroundImage ( highlightedImage, for: . highlighted)
145203 setBackgroundImage ( disabledImage, for: . disabled)
146-
147- addSubview ( activityIndicator)
148204 }
149205
150206 /// Setup: TitleColor
151207 ///
152208 private func configureTitleColors( ) {
209+ guard let buttonStyle = buttonStyle else {
210+ legacyConfigureTitleColors ( )
211+ return
212+ }
213+
214+ setTitleColor ( buttonStyle. normal. titleColor, for: . normal)
215+ setTitleColor ( buttonStyle. highlighted. titleColor, for: . highlighted)
216+ setTitleColor ( buttonStyle. disabled. titleColor, for: . disabled)
217+ }
218+
219+ /// Fallback method to configure the title colors based on the shared `WordPressAuthenticatorStyle`
220+ ///
221+ private func legacyConfigureTitleColors( ) {
222+ let style = WordPressAuthenticator . shared. style
153223 let titleColorNormal = isPrimary ? style. primaryTitleColor : style. secondaryTitleColor
154224
155225 setTitleColor ( titleColorNormal, for: . normal)
@@ -164,6 +234,18 @@ import WordPressKit
164234 titleLabel? . adjustsFontForContentSizeCategory = true
165235 titleLabel? . textAlignment = . center
166236 }
237+
238+ /// Returns the current color that should be used for the activity indicator
239+ ///
240+ private func activityIndicatorColor( isEnabled: Bool = true ) -> UIColor {
241+ guard let style = buttonStyle else {
242+ let style = WordPressAuthenticator . shared. style
243+
244+ return isEnabled ? style. primaryTitleColor : style. disabledButtonActivityIndicatorColor
245+ }
246+
247+ return isEnabled ? style. normal. titleColor : style. disabled. titleColor
248+ }
167249}
168250
169251// MARK: -
0 commit comments