Skip to content

Commit

Permalink
update organization of code in files
Browse files Browse the repository at this point in the history
  • Loading branch information
Adobels committed Nov 21, 2023
1 parent 57de8aa commit 6041c66
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 147 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ disabled_rules:
- type_name
- line_length
- identifier_name
- trailing_comma
opt_in_rules:

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// UIViewDSLEngineConstraintsProtocol.swift
// UIViewDSL+EngineConstraintsProtocol.swift
// UIViewKit
//
// Created by Blazej SLEBODA on 29/09/2023.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// InferredAttributesOwnerStrategy.swift
// UIViewDSL+InferredAttributesOwnerStrategy.swift
// UIViewKit
//
// Created by Blazej SLEBODA on 29/09/2023.
Expand Down
78 changes: 78 additions & 0 deletions Sources/UIViewKit/UIViewDSL/UIViewDSL+Engine.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
73 changes: 2 additions & 71 deletions Sources/UIViewKit/UIViewDSL/UIViewDSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }

0 comments on commit 6041c66

Please sign in to comment.