From d6db993927bcb0626fa186f860457845ef247b68 Mon Sep 17 00:00:00 2001 From: Nikita Vasilev Date: Sat, 15 Feb 2025 21:37:15 +0400 Subject: [PATCH] Implement the extension for the UIControl instance (#6) * Implement the extension for the UIControl instance * Update `CHANGELOG.md` --- CHANGELOG.md | 10 +++- .../Classes/Extensions/FlexUI+UIButton.swift | 47 ---------------- .../Classes/Extensions/FlexUI+UIControl.swift | 56 +++++++++++++++++++ .../Extensions/FlexUI+UITextField.swift | 51 ----------------- 4 files changed, 65 insertions(+), 99 deletions(-) create mode 100644 Sources/FlexUI/Classes/Extensions/FlexUI+UIControl.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index e350ec9..5471181 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,18 @@ All notable changes to this project will be documented in this file. #### 1.x Releases +- `1.2.x` Releases - [1.2.0](#120) - `1.1.x` Releases - [1.1.0](#110) - `1.0.x` Releases - [1.0.0](#100) -## [1.1.0](https://github.com/space-code/flare/releases/tag/1.1.0) +## [1.2.0](https://github.com/space-code/flex-ui/releases/tag/1.2.0) +Released on 2025-02-15. + +#### Added +- Implement the extension for the `UIControl` instance. + - Added in Pull Request [#6](https://github.com/space-code/flex-ui/pull/6). + +## [1.1.0](https://github.com/space-code/flex-ui/releases/tag/1.1.0) Released on 2025-01-27. #### Added diff --git a/Sources/FlexUI/Classes/Extensions/FlexUI+UIButton.swift b/Sources/FlexUI/Classes/Extensions/FlexUI+UIButton.swift index a2013cf..103443b 100644 --- a/Sources/FlexUI/Classes/Extensions/FlexUI+UIButton.swift +++ b/Sources/FlexUI/Classes/Extensions/FlexUI+UIButton.swift @@ -5,8 +5,6 @@ import UIKit -@MainActor private let kMapTable = NSMapTable.weakToStrongObjects() - /// An extension to `FlexUI` that adds helper methods for configuring `UIButton` properties. /// These methods allow for fluent configuration of button properties such as title, image, alignment, and actions. public extension FlexUI where Component: UIButton { @@ -207,49 +205,4 @@ public extension FlexUI where Component: UIButton { component.isEnabled = isEnable return self } - - /// Adds a custom command block to be executed when the button is tapped. - /// - /// - Parameters: - /// - command: The closure to be executed. - /// - event: The event to associate the command with (default is `.touchUpInside`). - /// - Returns: The current instance of `FlexUI` for further configuration. - @discardableResult - @MainActor - func add(command: (() -> Void)?, event: UIControl.Event = .touchUpInside) -> Self { - guard let command = command else { - return self - } - - let buttonCommand = Command(block: command) - component.removeTarget(nil, action: nil, for: event) - component.addTarget(buttonCommand, action: #selector(buttonCommand.action), for: event) - kMapTable.setObject(buttonCommand, forKey: component) - return self - } - - /// Adds a custom command block to be executed when the button is tapped, with access to the button itself. - /// - /// - Parameters: - /// - command: The closure to be executed with the button as the parameter. - /// - event: The event to associate the command with. - /// - Returns: The current instance of `FlexUI` for further configuration. - @discardableResult - @MainActor - func add(command: ((UIButton) -> Void)?, event: UIControl.Event) -> Self { - guard let command = command else { - return self - } - - let buttonCommand = Command { [weak component] in - if let component = component { - command(component) - } - } - - component.removeTarget(nil, action: nil, for: event) - component.addTarget(buttonCommand, action: #selector(buttonCommand.action), for: event) - kMapTable.setObject(buttonCommand, forKey: component) - return self - } } diff --git a/Sources/FlexUI/Classes/Extensions/FlexUI+UIControl.swift b/Sources/FlexUI/Classes/Extensions/FlexUI+UIControl.swift new file mode 100644 index 0000000..4d4ddea --- /dev/null +++ b/Sources/FlexUI/Classes/Extensions/FlexUI+UIControl.swift @@ -0,0 +1,56 @@ +// +// flex-ui +// Copyright © 2025 Space Code. All rights reserved. +// + +import UIKit + +@MainActor private let kMapTable = NSMapTable.weakToStrongObjects() + +/// An extension to `FlexUI` that adds helper methods for configuring `UIControl` properties. +public extension FlexUI where Component: UIControl { + /// Adds a custom command block to be executed when the control is tapped. + /// + /// - Parameters: + /// - command: The closure to be executed. + /// - event: The event to associate the command with (default is `.touchUpInside`). + /// - Returns: The current instance of `FlexUI` for further configuration. + @discardableResult + @MainActor + func add(command: (() -> Void)?, event: UIControl.Event = .touchUpInside) -> Self { + guard let command = command else { + return self + } + + let componentCommand = Command(block: command) + component.removeTarget(nil, action: nil, for: event) + component.addTarget(componentCommand, action: #selector(componentCommand.action), for: event) + kMapTable.setObject(componentCommand, forKey: component) + return self + } + + /// Adds a custom command block to be executed when the control is tapped, with access to the control itself. + /// + /// - Parameters: + /// - command: The closure to be executed with the component as the parameter. + /// - event: The event to associate the command with. + /// - Returns: The current instance of `FlexUI` for further configuration. + @discardableResult + @MainActor + func add(command: ((Component) -> Void)?, event: UIControl.Event) -> Self { + guard let command = command else { + return self + } + + let componentCommand = Command { [weak component] in + if let component = component { + command(component) + } + } + + component.removeTarget(nil, action: nil, for: event) + component.addTarget(componentCommand, action: #selector(componentCommand.action), for: event) + kMapTable.setObject(componentCommand, forKey: component) + return self + } +} diff --git a/Sources/FlexUI/Classes/Extensions/FlexUI+UITextField.swift b/Sources/FlexUI/Classes/Extensions/FlexUI+UITextField.swift index bdb612d..8e31f27 100644 --- a/Sources/FlexUI/Classes/Extensions/FlexUI+UITextField.swift +++ b/Sources/FlexUI/Classes/Extensions/FlexUI+UITextField.swift @@ -5,8 +5,6 @@ import UIKit -@MainActor private let kMapTable = NSMapTable.weakToStrongObjects() - public extension FlexUI where Component: UITextField { /// Sets the font of the text field. /// @@ -142,53 +140,4 @@ public extension FlexUI where Component: UITextField { component.adjustsFontSizeToFitWidth = true return self } - - /// Adds a command to be executed when a specified event occurs on the UIControl component. - /// The method is annotated with `@discardableResult` to allow the return value to be ignored, - /// and `@MainActor` to ensure it runs on the main thread. - /// - /// - Parameters: - /// - command: A closure to be executed when the event occurs. Can be `nil`, in which case no action is added. - /// - event: The `UIControl.Event` that triggers the command. - /// - Returns: The instance of `Self` to allow method chaining. - @discardableResult - @MainActor - func add(command: (() -> Void)?, event: UIControl.Event) -> Self { - guard let command = command else { - return self - } - - let buttonCommand = Command(block: command) - component.removeTarget(nil, action: nil, for: event) - component.addTarget(buttonCommand, action: #selector(buttonCommand.action), for: event) - kMapTable.setObject(buttonCommand, forKey: component) - return self - } - - /// Adds a command to be executed when a specified event occurs on the `UITextField` component. - /// The method is annotated with `@discardableResult` to allow the return value to be ignored, - /// and `@MainActor` to ensure it runs on the main thread. - /// - /// - Parameters: - /// - command: A closure that receives the `UITextField` as a parameter when the event occurs. Can be `nil`. - /// - event: The `UIControl.Event` that triggers the command. - /// - Returns: The instance of `Self` to allow method chaining. - @discardableResult - @MainActor - func add(command: ((UITextField) -> Void)?, event: UIControl.Event) -> Self { - guard let command = command else { - return self - } - - let buttonCommand = Command { [weak component] in - if let component = component { - command(component) - } - } - - component.removeTarget(nil, action: nil, for: event) - component.addTarget(buttonCommand, action: #selector(buttonCommand.action), for: event) - kMapTable.setObject(buttonCommand, forKey: component) - return self - } }