LeadKit/TIUIElements/Sources/Wrappers/Protocols/WrappedViewHolder.swift

184 lines
7.3 KiB
Swift

//
// Copyright (c) 2022 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import UIKit
import TIUIKitCore
import os
public protocol WrappedViewHolder: AnyObject {
associatedtype View: UIView
var wrappedView: View { get }
var contentView: UIView { get }
var wrappedContentInsets: UIEdgeInsets { get set }
var wrappedContentSize: CGSize { get set }
var wrappedContentCenterOffset: UIOffset { get set }
}
public extension WrappedViewHolder {
func wrappedViewEdgeConstraints() -> EdgeConstraints {
EdgeConstraints(leadingConstraint: wrappedView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
trailingConstraint: wrappedView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
topConstraint: wrappedView.topAnchor.constraint(equalTo: contentView.topAnchor),
bottomConstraint: wrappedView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor))
}
func wrappedViewSizeConstraints() -> SizeConstraints {
SizeConstraints(widthConstraint: wrappedView.widthAnchor.constraint(equalToConstant: .zero),
heightConstraint: wrappedView.heightAnchor.constraint(equalToConstant: .zero))
}
func wrappedViewCenterConstraints() -> CenterConstraints {
CenterConstraints(centerXConstraint: wrappedView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
centerYConstraint: wrappedView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor))
}
func createSubviewConstraints() -> SubviewConstraints {
wrappedView.translatesAutoresizingMaskIntoConstraints = false
return SubviewConstraints(edgeConstraints: wrappedViewEdgeConstraints(),
centerConstraints: wrappedViewCenterConstraints(),
sizeConstraints: wrappedViewSizeConstraints())
}
// MARK: - SubviewConstraints shortcut
func update(subviewConstraints: SubviewConstraints) {
subviewConstraints.update(insets: wrappedContentInsets,
size: wrappedContentSize,
centerOffset: wrappedContentCenterOffset)
}
// MARK: - WrappedViewLayout shortcut
func updateContentLayout(from layout: some WrappedViewLayout) {
wrappedContentInsets = adjustInsets(of: layout)
wrappedContentSize = layout.size
wrappedContentCenterOffset = layout.centerOffset
}
private func adjustInsets(of layout: some WrappedViewLayout) -> UIEdgeInsets {
let logger = TIUIElementsLogger(category: TIUIElementsLogger.Category.layout.rawValue)
let adjustedHorizontalInsets = adjustHorizontal(insets: layout.insets,
of: layout,
log: logger.log)
return adjustVertical(insets: adjustedHorizontalInsets,
of: layout,
log: logger.log)
}
private func adjustHorizontal(insets: UIEdgeInsets,
of layout: some WrappedViewLayout,
log: OSLog) -> UIEdgeInsets {
let horizontalInsetsDefined = insets.horizontal().isFinite
let horizontalAlignmentDefined = layout.centerOffset.horizontal.isFinite
switch (horizontalInsetsDefined, horizontalAlignmentDefined) {
case (true, true):
os_log("""
%{public}s:%{public}d:%{public}s: layout defines
- horizontal insets %{public}s
- horizontal center alignment %{public}s.
This can lead to unpredictable placement of the element.
""",
log: log,
type: .info,
#file,
#line,
String(describing: type(of: self)),
"UIEdgeInsets(left: \(insets.left), right: \(insets.right))",
"UIOffset(horizontal: \(layout.centerOffset.horizontal))")
case (true, false), (false, true):
break // everything is ok
case (false, false):
os_log("""
%{public}s:%{public}d:%{public}s: horizontal insets and horizontal center alignment
is not defined. Falling back to zero insets.
""",
log: log,
type: .info,
#file,
#line,
String(describing: type(of: self)))
return insets.horizontal(.zero)
}
return insets
}
private func adjustVertical(insets: UIEdgeInsets,
of layout: some WrappedViewLayout,
log: OSLog) -> UIEdgeInsets {
let verticalInsetsDefined = insets.vertical().isFinite
let verticalAlignmentDefined = layout.centerOffset.vertical.isFinite
switch (verticalInsetsDefined, verticalAlignmentDefined) {
case (true, true):
os_log("""
%{public}s:%{public}d:%{public}s: layout defines
- vertical insets %{public}s
- vertical center alignment %{public}s
This can lead to unpredictable placement of the element.
""",
log: log,
type: .info,
#file,
#line,
String(describing: type(of: self)),
"UIEdgeInsets(top: \(insets.top), bottom: \(insets.bottom))",
"UIOffset(vertical: \(layout.centerOffset.vertical))")
case (true, false), (false, true):
break // everything is ok
case (false, false):
os_log("""
%{public}s:%{public}d:%{public}s: vertical insets and vertical center alignment
is not defined. Falling back to zero insets.
""",
log: log,
type: .info,
#file,
#line,
String(describing: type(of: self)))
return insets.vertical(.zero)
}
return insets
}
}
public extension WrappedViewHolder where Self: UIView {
var contentView: UIView {
self
}
}