@@ -14,6 +14,7 @@ protocol AlertActionSequenceViewDelegate: AnyObject {
1414
1515struct AlertActionSequenceViewModel {
1616 let actions : [ Alert . Action ]
17+ let disabledTintColor : UIColor ?
1718 let separatorColor : UIColor
1819 let separatorWidth : CGFloat
1920}
@@ -34,6 +35,8 @@ final class AlertActionSequenceView: UIControl {
3435 }
3536 }
3637
38+ var isEnabled : Bool = true
39+
3740 private( set) lazy var titleLabel : UILabel = {
3841 let label = UILabel ( )
3942 label. translatesAutoresizingMaskIntoConstraints = false
@@ -104,15 +107,15 @@ final class AlertActionSequenceView: UIControl {
104107 }
105108
106109 if let action = viewModel. actions. first {
107- let actionView = makeActionView ( for: action)
110+ let actionView = makeActionView ( for: action, disabledTintColor : viewModel . disabledTintColor )
108111 stackView. addArrangedSubview ( actionView)
109112 }
110113
111114 for action in viewModel. actions. dropFirst ( ) {
112115 let separator = makeButtonSeparatorView ( viewModel: viewModel)
113116 stackView. addArrangedSubview ( separator)
114117
115- let actionView = makeActionView ( for: action)
118+ let actionView = makeActionView ( for: action, disabledTintColor : viewModel . disabledTintColor )
116119 stackView. addArrangedSubview ( actionView)
117120
118121 if let firstActionView = stackView. arrangedSubviews. first ( where: { $0 !== actionView } ) {
@@ -121,25 +124,37 @@ final class AlertActionSequenceView: UIControl {
121124 }
122125 }
123126
124- private func makeActionView( for action: Alert . Action ) -> ActionView {
127+ private func makeActionView( for action: Alert . Action , disabledTintColor : UIColor ? ) -> ActionView {
125128 let actionView = ActionView ( )
126129 actionView. translatesAutoresizingMaskIntoConstraints = false
127130
131+ updateAppearance ( for: actionView, action: action, disabledTintColor: disabledTintColor)
132+
133+ action. actionStateHandler = { [ weak actionView, weak action, weak self] isEnabled in
134+ guard let actionView = actionView, let action = action else { return }
135+ self ? . updateAppearance ( for: actionView, action: action, disabledTintColor: disabledTintColor)
136+ }
137+
138+ return actionView
139+ }
140+
141+ private func updateAppearance( for actionView: ActionView , action: Alert . Action , disabledTintColor: UIColor ? ) {
142+ actionView. isEnabled = action. isEnabled
128143 actionView. titleLabel. text = action. title
129144
145+ let disabledTintColor = disabledTintColor ?? UIColor ( white: 0.48 , alpha: 0.8 )
146+
130147 switch action. style {
131148 case . default:
132149 actionView. titleLabel. font = UIFont . systemFont ( ofSize: 17 , weight: . regular)
133- actionView. titleLabel. textColor = tintColor
150+ actionView. titleLabel. textColor = action . isEnabled ? tintColor : disabledTintColor
134151 case . primary:
135152 actionView. titleLabel. font = UIFont . systemFont ( ofSize: 17 , weight: . semibold)
136- actionView. titleLabel. textColor = tintColor
153+ actionView. titleLabel. textColor = action . isEnabled ? tintColor : disabledTintColor
137154 case let . custom( font, textColor) :
138155 actionView. titleLabel. font = font
139- actionView. titleLabel. textColor = textColor
156+ actionView. titleLabel. textColor = action . isEnabled ? textColor : disabledTintColor
140157 }
141-
142- return actionView
143158 }
144159
145160 private func makeButtonSeparatorView( viewModel: AlertActionSequenceViewModel ) -> UIView {
@@ -189,7 +204,7 @@ final class AlertActionSequenceView: UIControl {
189204 guard let actionViewFrame = actionView. superview? . convert ( actionView. frame, to: self ) else {
190205 continue
191206 }
192- let isHighlighted = actionViewFrame. contains ( point)
207+ let isHighlighted = actionViewFrame. contains ( point) && actionView . isEnabled
193208 actionView. isHighlighted = isHighlighted
194209
195210 if isHighlighted, highlightedView != actionView {
@@ -226,7 +241,8 @@ final class AlertActionSequenceView: UIControl {
226241
227242 @objc private func handleTap( on actionView: ActionView ) {
228243 let index = stackView. arrangedSubviews
229- . filter { $0 is ActionView }
244+ . compactMap { $0 as? ActionView }
245+ . filter { $0. isEnabled }
230246 . firstIndex ( where: { $0 === actionView } )
231247
232248 if let index = index {
0 commit comments