-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancements, Refactoring, and Fixes for UIViewDSL Component and Asso…
…ciated Logic (#2) * Try to address .swiftpm build sssues on SwiftPackageIndex * Fix multiple issues related to `.swiftpm` build and UI components: - Resolve `.swiftpm` build problems on SwiftPackageIndex. - Correct VerticalStack's axis for designated initializer. - Enhance view hierarchy display for all subviews. - Introduce related tests. * Set Swift minimum version to 5.7 for DSL language features * dd Swift and Platform Compatibility Badges to README.md * Update missing informations in files headers * Refactor anchor constraints to assert UIView presence * Add tests * WIP * move more logic to UIViewDSLEngine * organize UIViewDSL logic in separate files, write different strategies for autolayout constraints * Update UIViewDSLEngineConstraintsProtocol to public * set UIViewDSL to public * set public init methods on ConstraintsStrategy * set InferredConstraintsStrategy as experimental + protocolUIViewDSLEngineConstraintsProtocol improvements * remove prints from code
- Loading branch information
Showing
32 changed files
with
937 additions
and
395 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
.swiftpm/xcode/xcshareddata/xcschemes/UIViewKit-Package.xcscheme
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,106 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "1500" | ||
version = "1.7"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "UIViewKit" | ||
BuildableName = "UIViewKit" | ||
BlueprintName = "UIViewKit" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "UIViewKitPreviewsDemo" | ||
BuildableName = "UIViewKitPreviewsDemo" | ||
BlueprintName = "UIViewKitPreviewsDemo" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "NO" | ||
buildForArchiving = "NO" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "UIViewKitTests" | ||
BuildableName = "UIViewKitTests" | ||
BlueprintName = "UIViewKitTests" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
shouldAutocreateTestPlan = "YES"> | ||
<Testables> | ||
<TestableReference | ||
skipped = "NO"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "UIViewKitTests" | ||
BuildableName = "UIViewKitTests" | ||
BlueprintName = "UIViewKitTests" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</TestableReference> | ||
</Testables> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "UIViewKit" | ||
BuildableName = "UIViewKit" | ||
BlueprintName = "UIViewKit" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
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
58 changes: 58 additions & 0 deletions
58
Sources/UIViewKit/AutoresizingStrategy/InferredAttributesOwnerStrategy.swift
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,58 @@ | ||
// | ||
// InferredAttributesOwnerStrategy.swift | ||
// | ||
// | ||
// Created by Blazej SLEBODA on 29/09/2023. | ||
// | ||
|
||
import UIKit | ||
|
||
public class InferredAttributesOwnerStrategy: UIViewDSLEngineConstraintsProtocol { | ||
|
||
// MARK: - Private Properties | ||
|
||
private var constraintsToApply: [(UIView, [NSLayoutConstraint])] = [] | ||
|
||
|
||
// MARK: - UIViewDSLEngineConstraintsProtocol Methods | ||
|
||
public func addConstraints(for owner: UIView, constraints: [NSLayoutConstraint]) { | ||
guard !constraints.isEmpty else { return } | ||
constraints.forEach { | ||
if !UIViewDSLHelper.involvesOwnerView(owner, in: $0) { | ||
fatalError("Added constraints do not involve the specified owner view. Please ensure that constraints are correctly defined for the owner view.") | ||
} | ||
} | ||
constraintsToApply.append((owner, constraints)) | ||
} | ||
|
||
public func ibSubviewsWillExecute(on rootView: UIView) { | ||
if !constraintsToApply.isEmpty { | ||
fatalError("Attempted to begin subviews definition while constraintsToApply is not empty. This indicates that there may have been a previous incomplete or erroneous subviews definition process.") | ||
} | ||
} | ||
|
||
public func ibSubviewsDidExecute(on rootView: UIView) { | ||
activateAutoLayout() | ||
} | ||
|
||
public func ibAttributesDidExecute(on rootView: UIView) { | ||
activateAutoLayout() | ||
} | ||
|
||
// MARK: - Initializer Methods | ||
|
||
public init() { } | ||
|
||
// MARK: - Private Methods | ||
|
||
private func activateAutoLayout() { | ||
var allConstraints: [NSLayoutConstraint] = [] | ||
constraintsToApply.forEach { owner, constraints in | ||
allConstraints.append(contentsOf: constraints) | ||
owner.translatesAutoresizingMaskIntoConstraints = false | ||
} | ||
NSLayoutConstraint.activate(allConstraints) | ||
constraintsToApply.removeAll() | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
Sources/UIViewKit/AutoresizingStrategy/InferredConstraintsStrategy.swift
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,69 @@ | ||
// | ||
// InferredConstraintsStrategy.swift | ||
// | ||
// | ||
// Created by Blazej SLEBODA on 29/09/2023. | ||
// | ||
|
||
import UIKit | ||
|
||
class InferredConstraintsStrategy: UIViewDSLEngineConstraintsProtocol { | ||
|
||
|
||
// MARK: - Private Properties | ||
|
||
private var constraintsToApply: [(UIView, [NSLayoutConstraint])] = [] | ||
|
||
// MARK: - UIViewDSLEngineConstraintsProtocol Methods | ||
|
||
func ibSubviewsWillExecute(on rootView: UIView) { | ||
if !constraintsToApply.isEmpty { | ||
fatalError("Attempted to begin subviews definition while constraintsToApply is not empty. This indicates that there may have been a previous incomplete or erroneous subviews definition process.") | ||
} | ||
} | ||
|
||
func ibSubviewsDidExecute(on rootView: UIView) { | ||
activateAutoLayout() | ||
} | ||
|
||
func addConstraints(for owner: UIView, constraints: [NSLayoutConstraint]) { | ||
guard !constraints.isEmpty else { return } | ||
constraints.forEach { | ||
if !UIViewDSLHelper.involvesOwnerView(owner, in: $0) { | ||
fatalError("Added constraints do not involve the specified owner view. Please ensure that constraints are correctly defined for the owner view.") | ||
} | ||
} | ||
constraintsToApply.append((owner, constraints)) | ||
} | ||
|
||
func ibAttributesDidExecute(on rootView: UIView) { | ||
activateAutoLayout() | ||
} | ||
|
||
// MARK: - Initializer Methods | ||
|
||
init() { } | ||
|
||
// MARK: - Private Methods | ||
|
||
private func activateAutoLayout() { | ||
var allConstraints: [NSLayoutConstraint] = [] | ||
constraintsToApply.forEach { owner, constraints in | ||
allConstraints.append(contentsOf: constraints) | ||
for constraint in constraints { | ||
if let firstView = constraint.firstItem as? UIView { | ||
if firstView.superview != nil { | ||
firstView.translatesAutoresizingMaskIntoConstraints = false | ||
} | ||
} | ||
if let secondView = constraint.secondItem as? UIView { | ||
if secondView.superview != nil { | ||
secondView.translatesAutoresizingMaskIntoConstraints = false | ||
} | ||
} | ||
} | ||
} | ||
NSLayoutConstraint.activate(allConstraints) | ||
constraintsToApply.removeAll() | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Sources/UIViewKit/AutoresizingStrategy/UIViewDSLEngineConstraintsProtocol.swift
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,15 @@ | ||
// | ||
// UIViewDSLEngineConstraintsProtocol.swift | ||
// | ||
// | ||
// Created by Blazej SLEBODA on 29/09/2023. | ||
// | ||
|
||
import UIKit | ||
|
||
public protocol UIViewDSLEngineConstraintsProtocol: AnyObject { | ||
func addConstraints(for owner: UIView, constraints: [NSLayoutConstraint]) | ||
func ibSubviewsWillExecute(on rootView: UIView) | ||
func ibSubviewsDidExecute(on rootView: UIView) | ||
func ibAttributesDidExecute(on rootView: UIView) | ||
} |
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,19 @@ | ||
// | ||
// UIViewDSL+Attributes.swift | ||
// | ||
// | ||
// Created by Blazej SLEBODA on 29/09/2023. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UIViewDSL where Self: UIView { | ||
|
||
@MainActor | ||
@discardableResult | ||
public func ibAttributes(@NSLayoutConstraintBuilder _ block: (Self) -> [NSLayoutConstraint]) -> Self { | ||
let constraintsGenerated = block(self) | ||
UIViewDSLEngine.shared.addConstraints(for: self, constraints: constraintsGenerated) | ||
return self | ||
} | ||
} |
Oops, something went wrong.