From 8499a9160fe1b4da98f885e89aad36e534e3f490 Mon Sep 17 00:00:00 2001 From: Sergey Kazakov Date: Thu, 21 Apr 2022 16:30:45 +0100 Subject: [PATCH 1/3] removed odd disclosure view --- Sources/SwiftyUIKit/DisclosureView.swift | 74 ------------------------ 1 file changed, 74 deletions(-) delete mode 100644 Sources/SwiftyUIKit/DisclosureView.swift diff --git a/Sources/SwiftyUIKit/DisclosureView.swift b/Sources/SwiftyUIKit/DisclosureView.swift deleted file mode 100644 index aec93b1..0000000 --- a/Sources/SwiftyUIKit/DisclosureView.swift +++ /dev/null @@ -1,74 +0,0 @@ -// -// File.swift -// -// -// Created by Sergey Kazakov on 30.11.2020. -// - -import UIKit - -public extension DisclosureView { - enum ExpandingAxis { - case horizontal - case vertical - case all - } -} - -public final class DisclosureView: UIView { - private let isExpanded: () -> Bool - private var conditionalContraints: [NSLayoutConstraint] - - public init(_ isExpanded: @escaping () -> Bool, - axis: ExpandingAxis = .all, - content: () -> UIView) { - - self.isExpanded = isExpanded - let contentView = content() - let contraints: [NSLayoutConstraint] - - switch axis { - case .horizontal: - contraints = [ - contentView.widthAnchor.constraint( - lessThanOrEqualToConstant: NSLayoutConstraint.defaultLargeConstant) - ] - case .vertical: - contraints = [ - contentView.heightAnchor.constraint( - lessThanOrEqualToConstant: NSLayoutConstraint.defaultLargeConstant) - ] - case .all: - contraints = [ - contentView.heightAnchor.constraint( - lessThanOrEqualToConstant: NSLayoutConstraint.defaultLargeConstant), - contentView.widthAnchor.constraint( - lessThanOrEqualToConstant: NSLayoutConstraint.defaultLargeConstant) - ] - } - - self.conditionalContraints = contraints - super.init(frame: .zero) - add(body: contentView) - - contraints.forEach { - $0.constant = isExpanded() ? NSLayoutConstraint.defaultLargeConstant : 0 - $0.priority = .defaultHigh - $0.isActive = true - } - } - - public override func updateConstraints() { - super.updateConstraints() - print("updateConstraints") - conditionalContraints.forEach { $0.constant = isExpanded() ? NSLayoutConstraint.defaultLargeConstant : 0 } - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } -} - -fileprivate extension NSLayoutConstraint { - static let defaultLargeConstant: CGFloat = 9999 -} From fbf964c574f64a0d55a05b1789bb2533a445997e Mon Sep 17 00:00:00 2001 From: Sergey Kazakov Date: Thu, 21 Apr 2022 16:32:18 +0100 Subject: [PATCH 2/3] added optional view content builder --- Sources/SwiftyUIKit/HStackView.swift | 9 +++++++-- Sources/SwiftyUIKit/VStackView.swift | 8 ++++++-- Sources/SwiftyUIKit/ViewContentBuilder.swift | 7 +++++++ Sources/SwiftyUIKit/ZStackView.swift | 8 ++++++-- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Sources/SwiftyUIKit/HStackView.swift b/Sources/SwiftyUIKit/HStackView.swift index ecfef0f..6482e5b 100644 --- a/Sources/SwiftyUIKit/HStackView.swift +++ b/Sources/SwiftyUIKit/HStackView.swift @@ -7,10 +7,15 @@ import UIKit -extension HStackView { - public convenience init(spacing: CGFloat = 0, @ViewContentBuilder content: () -> [UIView]) { +public extension HStackView { + convenience init(spacing: CGFloat = 0, @ViewContentBuilder content: () -> [UIView]) { self.init(spacing: spacing, content: content()) } + + convenience init(spacing: CGFloat = 0, @OptionalViewContentBuilder optionalContent: () -> [UIView]) { + self.init(spacing: spacing, content: optionalContent()) + } + } public final class HStackView: UIView { diff --git a/Sources/SwiftyUIKit/VStackView.swift b/Sources/SwiftyUIKit/VStackView.swift index f0d50c1..ebbd1f9 100644 --- a/Sources/SwiftyUIKit/VStackView.swift +++ b/Sources/SwiftyUIKit/VStackView.swift @@ -7,10 +7,14 @@ import UIKit -extension VStackView { - public convenience init(spacing: CGFloat = 0, @ViewContentBuilder content: () -> [UIView]) { +public extension VStackView { + convenience init(spacing: CGFloat = 0, @ViewContentBuilder content: () -> [UIView]) { self.init(spacing: spacing, content: content()) } + + convenience init(spacing: CGFloat = 0, @OptionalViewContentBuilder optionalContent: () -> [UIView]) { + self.init(spacing: spacing, content: optionalContent()) + } } public final class VStackView: UIView { diff --git a/Sources/SwiftyUIKit/ViewContentBuilder.swift b/Sources/SwiftyUIKit/ViewContentBuilder.swift index 0cc0ba0..6a2bba3 100644 --- a/Sources/SwiftyUIKit/ViewContentBuilder.swift +++ b/Sources/SwiftyUIKit/ViewContentBuilder.swift @@ -13,3 +13,10 @@ public struct ViewContentBuilder { subviews } } + +@resultBuilder +public struct OptionalViewContentBuilder { + public static func buildBlock(_ subviews: UIView?...) -> [UIView] { + subviews.compactMap { $0 } + } +} diff --git a/Sources/SwiftyUIKit/ZStackView.swift b/Sources/SwiftyUIKit/ZStackView.swift index 00efca1..766cf1d 100644 --- a/Sources/SwiftyUIKit/ZStackView.swift +++ b/Sources/SwiftyUIKit/ZStackView.swift @@ -7,10 +7,14 @@ import UIKit -extension ZStackView { - public convenience init(alignment: Alignment = .all, @ViewContentBuilder content: () -> [UIView]) { +public extension ZStackView { + convenience init(alignment: Alignment = .all, @ViewContentBuilder content: () -> [UIView]) { self.init(alignment: alignment, content: content()) } + + convenience init(alignment: Alignment = .all, @OptionalViewContentBuilder optionalContent: () -> [UIView]) { + self.init(alignment: alignment, content: optionalContent()) + } } public final class ZStackView: UIView { From e384e26afb7e4065ddf986dda82e63c872ac682e Mon Sep 17 00:00:00 2001 From: Sergey Kazakov Date: Thu, 21 Apr 2022 16:32:32 +0100 Subject: [PATCH 3/3] handy padding function --- Sources/SwiftyUIKit/UIView+Padding.swift | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Sources/SwiftyUIKit/UIView+Padding.swift b/Sources/SwiftyUIKit/UIView+Padding.swift index 8b0c6f4..4dc5f16 100644 --- a/Sources/SwiftyUIKit/UIView+Padding.swift +++ b/Sources/SwiftyUIKit/UIView+Padding.swift @@ -27,20 +27,12 @@ public extension UIView { bottomConstant: bottom) } - func padding(vertical: CGFloat = 0) -> UIView { + func padding(vertical: CGFloat = 0, horizontal: CGFloat = 0) -> UIView { PaddingViewDecorator(decorated: self, topConstant: vertical, - leftConstant: 0, - rightConstant: 0, - bottomConstant: vertical) - } - - func padding(horizontal: CGFloat = 0) -> UIView { - PaddingViewDecorator(decorated: self, - topConstant: 0, leftConstant: horizontal, rightConstant: horizontal, - bottomConstant: 0) + bottomConstant: vertical) } }