diff --git a/.swiftlint.yml b/.swiftlint.yml index 70ecdce..457c6c1 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -2,4 +2,5 @@ disabled_rules: - type_name - line_length - identifier_name + - trailing_comma opt_in_rules: diff --git a/Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/InferredConstraintsStrategy.swift b/Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/InferredConstraintsStrategy.swift deleted file mode 100644 index bb49b95..0000000 --- a/Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/InferredConstraintsStrategy.swift +++ /dev/null @@ -1,74 +0,0 @@ -// -// InferredConstraintsStrategy.swift -// UIViewKit -// -// 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() - } - - #if DEBUG - var constraintsToApplyForDebug: [(UIView, [NSLayoutConstraint])] { - constraintsToApply - } - #endif - - // MARK: - Initializer Methods - - init() { } - - // MARK: - Private Methods - - private func activateAutoLayout() { - var allConstraints: [NSLayoutConstraint] = [] - constraintsToApply.forEach { _, 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() - } -} diff --git a/Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/UIViewDSLEngineConstraintsProtocol.swift b/Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/UIViewDSL+EngineConstraintsProtocol.swift similarity index 91% rename from Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/UIViewDSLEngineConstraintsProtocol.swift rename to Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/UIViewDSL+EngineConstraintsProtocol.swift index 93fa7f9..b5a8936 100644 --- a/Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/UIViewDSLEngineConstraintsProtocol.swift +++ b/Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/UIViewDSL+EngineConstraintsProtocol.swift @@ -1,5 +1,5 @@ // -// UIViewDSLEngineConstraintsProtocol.swift +// UIViewDSL+EngineConstraintsProtocol.swift // UIViewKit // // Created by Blazej SLEBODA on 29/09/2023. diff --git a/Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/InferredAttributesOwnerStrategy.swift b/Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/UIViewDSL+InferredAttributesOwnerStrategy.swift similarity index 97% rename from Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/InferredAttributesOwnerStrategy.swift rename to Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/UIViewDSL+InferredAttributesOwnerStrategy.swift index 2c859ff..3701ac2 100644 --- a/Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/InferredAttributesOwnerStrategy.swift +++ b/Sources/UIViewKit/UIViewDSL/AutoresizingStrategy/UIViewDSL+InferredAttributesOwnerStrategy.swift @@ -1,5 +1,5 @@ // -// InferredAttributesOwnerStrategy.swift +// UIViewDSL+InferredAttributesOwnerStrategy.swift // UIViewKit // // Created by Blazej SLEBODA on 29/09/2023. diff --git a/Sources/UIViewKit/UIViewDSL/UIViewDSL+Engine.swift b/Sources/UIViewKit/UIViewDSL/UIViewDSL+Engine.swift new file mode 100644 index 0000000..b6a9a2c --- /dev/null +++ b/Sources/UIViewKit/UIViewDSL/UIViewDSL+Engine.swift @@ -0,0 +1,78 @@ +// +// UIViewDSL+Engine.swift +// +// +// Created by MaxAir on 21/11/2023. +// + +import UIKit + +@MainActor +public class UIViewDSLEngine { + + // MARK: - Public Properties + + public weak var delegate: UIViewDSLEngineConstraintsProtocol? + + // MARK: - Private Properties + + private var ibSubviewsDepthCallCounter: Int = 0 + private var defaultDelegate: UIViewDSLEngineConstraintsProtocol? + + // MARK: - Singleton Instance + + public static let shared: UIViewDSLEngine = { + let instance = UIViewDSLEngine() + instance.setupDefaultDelegate() + return instance + }() + + // MARK: - Initializers + + private init() { } + + // MARK: - Public Methods + + func addSubviews(_ subviews: (UIView) -> [UIView], to owner: UIView) { + beginSubviewsDefinition() + UIViewDSLHelper.addSubviews(subviews(owner), to: owner) + endSubviewsDefinition(on: owner) + } + + func addSubviews(_ subviews: () -> [UIView], to owner: UIView) { + beginSubviewsDefinition() + UIViewDSLHelper.addSubviews(subviews(), to: owner) + endSubviewsDefinition(on: owner) + } + + func addConstraints(for owner: UIView, constraints: [NSLayoutConstraint]) { + delegate?.addConstraints(for: owner, constraints: constraints) + if ibSubviewsDepthCallCounter == 0 { + delegate?.ibAttributesDidExecute(on: owner) + } + } + + #if DEBUG + var constraintsToApplyForDebug: [(UIView, [NSLayoutConstraint])] { + delegate!.constraintsToApplyForDebug + } + #endif + + // MARK: - Private Methods + + private func setupDefaultDelegate() { + defaultDelegate = InferredAttributesOwnerStrategy() + delegate = defaultDelegate + } + + private func beginSubviewsDefinition() { + ibSubviewsDepthCallCounter += 1 + } + + private func endSubviewsDefinition(on owner: UIView) { + ibSubviewsDepthCallCounter -= 1 + if ibSubviewsDepthCallCounter == 0 { + delegate?.ibSubviewsDidExecute(on: owner) + } + } +} diff --git a/Sources/UIViewKit/UIViewDSL/UIViewDSL.swift b/Sources/UIViewKit/UIViewDSL/UIViewDSL.swift index 163e694..4e7621e 100644 --- a/Sources/UIViewKit/UIViewDSL/UIViewDSL.swift +++ b/Sources/UIViewKit/UIViewDSL/UIViewDSL.swift @@ -6,76 +6,7 @@ // import UIKit -import os -public protocol UIViewDSL { }; extension UIView: UIViewDSL { } +public protocol UIViewDSL { } -@MainActor -public class UIViewDSLEngine { - - // MARK: - Public Properties - - public weak var delegate: UIViewDSLEngineConstraintsProtocol? - - // MARK: - Private Properties - - private var ibSubviewsDepthCallCounter: Int = 0 - private var defaultDelegate: UIViewDSLEngineConstraintsProtocol? - - // MARK: - Singleton Instance - - public static let shared: UIViewDSLEngine = { - let instance = UIViewDSLEngine() - instance.setupDefaultDelegate() - return instance - }() - - // MARK: - Initializers - - private init() { } - - // MARK: - Public Methods - - func addSubviews(_ subviews: (UIView) -> [UIView], to owner: UIView) { - beginSubviewsDefinition() - UIViewDSLHelper.addSubviews(subviews(owner), to: owner) - endSubviewsDefinition(on: owner) - } - - func addSubviews(_ subviews: () -> [UIView], to owner: UIView) { - beginSubviewsDefinition() - UIViewDSLHelper.addSubviews(subviews(), to: owner) - endSubviewsDefinition(on: owner) - } - - func addConstraints(for owner: UIView, constraints: [NSLayoutConstraint]) { - delegate?.addConstraints(for: owner, constraints: constraints) - if ibSubviewsDepthCallCounter == 0 { - delegate?.ibAttributesDidExecute(on: owner) - } - } - - #if DEBUG - var constraintsToApplyForDebug: [(UIView, [NSLayoutConstraint])] { - delegate!.constraintsToApplyForDebug - } - #endif - - // MARK: - Private Methods - - private func setupDefaultDelegate() { - defaultDelegate = InferredAttributesOwnerStrategy() - delegate = defaultDelegate - } - - private func beginSubviewsDefinition() { - ibSubviewsDepthCallCounter += 1 - } - - private func endSubviewsDefinition(on owner: UIView) { - ibSubviewsDepthCallCounter -= 1 - if ibSubviewsDepthCallCounter == 0 { - delegate?.ibSubviewsDidExecute(on: owner) - } - } -} +extension UIView: UIViewDSL { }