-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the extension for the UIControl instance (#6)
* Implement the extension for the UIControl instance * Update `CHANGELOG.md`
- Loading branch information
1 parent
565ecd8
commit d6db993
Showing
4 changed files
with
65 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// flex-ui | ||
// Copyright © 2025 Space Code. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
@MainActor private let kMapTable = NSMapTable<AnyObject, Command>.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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters