Skip to content

Commit d6db993

Browse files
authored
Implement the extension for the UIControl instance (#6)
* Implement the extension for the UIControl instance * Update `CHANGELOG.md`
1 parent 565ecd8 commit d6db993

4 files changed

Lines changed: 65 additions & 99 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22
All notable changes to this project will be documented in this file.
33

44
#### 1.x Releases
5+
- `1.2.x` Releases - [1.2.0](#120)
56
- `1.1.x` Releases - [1.1.0](#110)
67
- `1.0.x` Releases - [1.0.0](#100)
78

8-
## [1.1.0](https://github.com/space-code/flare/releases/tag/1.1.0)
9+
## [1.2.0](https://github.com/space-code/flex-ui/releases/tag/1.2.0)
10+
Released on 2025-02-15.
11+
12+
#### Added
13+
- Implement the extension for the `UIControl` instance.
14+
- Added in Pull Request [#6](https://github.com/space-code/flex-ui/pull/6).
15+
16+
## [1.1.0](https://github.com/space-code/flex-ui/releases/tag/1.1.0)
917
Released on 2025-01-27.
1018

1119
#### Added

Sources/FlexUI/Classes/Extensions/FlexUI+UIButton.swift

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import UIKit
77

8-
@MainActor private let kMapTable = NSMapTable<AnyObject, Command>.weakToStrongObjects()
9-
108
/// An extension to `FlexUI` that adds helper methods for configuring `UIButton` properties.
119
/// These methods allow for fluent configuration of button properties such as title, image, alignment, and actions.
1210
public extension FlexUI where Component: UIButton {
@@ -207,49 +205,4 @@ public extension FlexUI where Component: UIButton {
207205
component.isEnabled = isEnable
208206
return self
209207
}
210-
211-
/// Adds a custom command block to be executed when the button is tapped.
212-
///
213-
/// - Parameters:
214-
/// - command: The closure to be executed.
215-
/// - event: The event to associate the command with (default is `.touchUpInside`).
216-
/// - Returns: The current instance of `FlexUI` for further configuration.
217-
@discardableResult
218-
@MainActor
219-
func add(command: (() -> Void)?, event: UIControl.Event = .touchUpInside) -> Self {
220-
guard let command = command else {
221-
return self
222-
}
223-
224-
let buttonCommand = Command(block: command)
225-
component.removeTarget(nil, action: nil, for: event)
226-
component.addTarget(buttonCommand, action: #selector(buttonCommand.action), for: event)
227-
kMapTable.setObject(buttonCommand, forKey: component)
228-
return self
229-
}
230-
231-
/// Adds a custom command block to be executed when the button is tapped, with access to the button itself.
232-
///
233-
/// - Parameters:
234-
/// - command: The closure to be executed with the button as the parameter.
235-
/// - event: The event to associate the command with.
236-
/// - Returns: The current instance of `FlexUI` for further configuration.
237-
@discardableResult
238-
@MainActor
239-
func add(command: ((UIButton) -> Void)?, event: UIControl.Event) -> Self {
240-
guard let command = command else {
241-
return self
242-
}
243-
244-
let buttonCommand = Command { [weak component] in
245-
if let component = component {
246-
command(component)
247-
}
248-
}
249-
250-
component.removeTarget(nil, action: nil, for: event)
251-
component.addTarget(buttonCommand, action: #selector(buttonCommand.action), for: event)
252-
kMapTable.setObject(buttonCommand, forKey: component)
253-
return self
254-
}
255208
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// flex-ui
3+
// Copyright © 2025 Space Code. All rights reserved.
4+
//
5+
6+
import UIKit
7+
8+
@MainActor private let kMapTable = NSMapTable<AnyObject, Command>.weakToStrongObjects()
9+
10+
/// An extension to `FlexUI` that adds helper methods for configuring `UIControl` properties.
11+
public extension FlexUI where Component: UIControl {
12+
/// Adds a custom command block to be executed when the control is tapped.
13+
///
14+
/// - Parameters:
15+
/// - command: The closure to be executed.
16+
/// - event: The event to associate the command with (default is `.touchUpInside`).
17+
/// - Returns: The current instance of `FlexUI` for further configuration.
18+
@discardableResult
19+
@MainActor
20+
func add(command: (() -> Void)?, event: UIControl.Event = .touchUpInside) -> Self {
21+
guard let command = command else {
22+
return self
23+
}
24+
25+
let componentCommand = Command(block: command)
26+
component.removeTarget(nil, action: nil, for: event)
27+
component.addTarget(componentCommand, action: #selector(componentCommand.action), for: event)
28+
kMapTable.setObject(componentCommand, forKey: component)
29+
return self
30+
}
31+
32+
/// Adds a custom command block to be executed when the control is tapped, with access to the control itself.
33+
///
34+
/// - Parameters:
35+
/// - command: The closure to be executed with the component as the parameter.
36+
/// - event: The event to associate the command with.
37+
/// - Returns: The current instance of `FlexUI` for further configuration.
38+
@discardableResult
39+
@MainActor
40+
func add(command: ((Component) -> Void)?, event: UIControl.Event) -> Self {
41+
guard let command = command else {
42+
return self
43+
}
44+
45+
let componentCommand = Command { [weak component] in
46+
if let component = component {
47+
command(component)
48+
}
49+
}
50+
51+
component.removeTarget(nil, action: nil, for: event)
52+
component.addTarget(componentCommand, action: #selector(componentCommand.action), for: event)
53+
kMapTable.setObject(componentCommand, forKey: component)
54+
return self
55+
}
56+
}

Sources/FlexUI/Classes/Extensions/FlexUI+UITextField.swift

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import UIKit
77

8-
@MainActor private let kMapTable = NSMapTable<AnyObject, Command>.weakToStrongObjects()
9-
108
public extension FlexUI where Component: UITextField {
119
/// Sets the font of the text field.
1210
///
@@ -142,53 +140,4 @@ public extension FlexUI where Component: UITextField {
142140
component.adjustsFontSizeToFitWidth = true
143141
return self
144142
}
145-
146-
/// Adds a command to be executed when a specified event occurs on the UIControl component.
147-
/// The method is annotated with `@discardableResult` to allow the return value to be ignored,
148-
/// and `@MainActor` to ensure it runs on the main thread.
149-
///
150-
/// - Parameters:
151-
/// - command: A closure to be executed when the event occurs. Can be `nil`, in which case no action is added.
152-
/// - event: The `UIControl.Event` that triggers the command.
153-
/// - Returns: The instance of `Self` to allow method chaining.
154-
@discardableResult
155-
@MainActor
156-
func add(command: (() -> Void)?, event: UIControl.Event) -> Self {
157-
guard let command = command else {
158-
return self
159-
}
160-
161-
let buttonCommand = Command(block: command)
162-
component.removeTarget(nil, action: nil, for: event)
163-
component.addTarget(buttonCommand, action: #selector(buttonCommand.action), for: event)
164-
kMapTable.setObject(buttonCommand, forKey: component)
165-
return self
166-
}
167-
168-
/// Adds a command to be executed when a specified event occurs on the `UITextField` component.
169-
/// The method is annotated with `@discardableResult` to allow the return value to be ignored,
170-
/// and `@MainActor` to ensure it runs on the main thread.
171-
///
172-
/// - Parameters:
173-
/// - command: A closure that receives the `UITextField` as a parameter when the event occurs. Can be `nil`.
174-
/// - event: The `UIControl.Event` that triggers the command.
175-
/// - Returns: The instance of `Self` to allow method chaining.
176-
@discardableResult
177-
@MainActor
178-
func add(command: ((UITextField) -> Void)?, event: UIControl.Event) -> Self {
179-
guard let command = command else {
180-
return self
181-
}
182-
183-
let buttonCommand = Command { [weak component] in
184-
if let component = component {
185-
command(component)
186-
}
187-
}
188-
189-
component.removeTarget(nil, action: nil, for: event)
190-
component.addTarget(buttonCommand, action: #selector(buttonCommand.action), for: event)
191-
kMapTable.setObject(buttonCommand, forKey: component)
192-
return self
193-
}
194143
}

0 commit comments

Comments
 (0)