Skip to content

Commit

Permalink
Minor improvements in EdgeAligningView
Browse files Browse the repository at this point in the history
  • Loading branch information
ekazaev committed Feb 23, 2023
1 parent 66a7b8e commit e5a72f9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 58 deletions.
2 changes: 1 addition & 1 deletion ChatLayout.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'ChatLayout'
s.version = '1.2.15'
s.version = '1.2.16'
s.summary = 'Chat UI Library. It uses custom UICollectionViewLayout to provide you full control over the presentation.'
s.swift_version = '5.7'

Expand Down
114 changes: 63 additions & 51 deletions ChatLayout/Classes/Extras/EdgeAligningView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ public final class EdgeAligningView<CustomView: UIView>: UIView {
/// Bottom edge
case bottom

var otherEdges: [Edge] {
Edge.allCases.filter { $0 != self }
}

}

/// Set of edge constraints to be set as loose.
Expand All @@ -44,23 +40,32 @@ public final class EdgeAligningView<CustomView: UIView>: UIView {
guard flexibleEdges != oldValue else {
return
}
setupContainer()
lastConstraintsUpdateEdges = nil
setNeedsUpdateConstraints()
}
}

/// Contained view.
public var customView: CustomView {
didSet {
guard customView != oldValue else {
guard customView !== oldValue else {
return
}
oldValue.removeFromSuperview()
setupContainer()
}
}

private var rigidConstraints: [Edge: NSLayoutConstraint] = [:]

private var flexibleConstraints: [Edge: NSLayoutConstraint] = [:]

private var centerConstraints: (centerX: NSLayoutConstraint, centerY: NSLayoutConstraint)?

private var addedConstraints: [NSLayoutConstraint] = []

private var lastConstraintsUpdateEdges: Set<Edge>?

/// Initializes and returns a newly allocated `EdgeAligningView`
/// - Parameters:
/// - customView: An instance of `CustomView`
Expand All @@ -87,6 +92,28 @@ public final class EdgeAligningView<CustomView: UIView>: UIView {
fatalError("Use init(with:flexibleEdges:) instead.")
}

public override func updateConstraints() {
guard lastConstraintsUpdateEdges != flexibleEdges else {
super.updateConstraints()
return
}

flexibleEdges.forEach { edge in
rigidConstraints[edge]?.isActive = false
flexibleConstraints[edge]?.isActive = true
}
Set(Edge.allCases).subtracting(flexibleEdges).forEach { edge in
flexibleConstraints[edge]?.isActive = false
rigidConstraints[edge]?.isActive = true
}
centerConstraints?.centerX.isActive = flexibleEdges.contains(.leading) && flexibleEdges.contains(.trailing)
centerConstraints?.centerY.isActive = flexibleEdges.contains(.top) && flexibleEdges.contains(.bottom)

lastConstraintsUpdateEdges = flexibleEdges

super.updateConstraints()
}

private func setupSubviews() {
translatesAutoresizingMaskIntoConstraints = false
insetsLayoutMarginsFromSafeArea = false
Expand All @@ -104,55 +131,40 @@ public final class EdgeAligningView<CustomView: UIView>: UIView {
removeConstraints(addedConstraints)
addedConstraints.removeAll()
}
Set(Edge.allCases).subtracting(flexibleEdges).forEach { setConstraint(for: $0, on: customView, flexible: false) }
flexibleEdges.forEach { setConstraint(for: $0, on: customView, flexible: true) }
setDistributionConstraint(on: customView)
setNeedsLayout()

lastConstraintsUpdateEdges = nil

let rigidConstraints = buildRigidConstraints(customView)
let flexibleConstraints = buildFlexibleConstraints(customView)
let centerConstraints = buildCenterConstraints(customView)

addedConstraints.append(contentsOf: rigidConstraints.values)
addedConstraints.append(contentsOf: flexibleConstraints.values)
addedConstraints.append(centerConstraints.centerX)
addedConstraints.append(centerConstraints.centerY)

self.rigidConstraints = rigidConstraints
self.flexibleConstraints = flexibleConstraints
self.centerConstraints = centerConstraints
setNeedsUpdateConstraints()
}

private func setConstraint(for edge: Edge, on view: UIView, flexible: Bool = false) {
var addedConstraints: [NSLayoutConstraint] = []
switch edge {
case .top:
if flexible {
addedConstraints.append(view.topAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.topAnchor))
} else {
addedConstraints.append(view.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor))
}
case .leading:
if flexible {
addedConstraints.append(view.leadingAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.leadingAnchor))
} else {
addedConstraints.append(view.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor))
}
case .trailing:
if flexible {
addedConstraints.append(view.trailingAnchor.constraint(lessThanOrEqualTo: layoutMarginsGuide.trailingAnchor))
} else {
addedConstraints.append(view.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor))
}
case .bottom:
if flexible {
addedConstraints.append(view.bottomAnchor.constraint(lessThanOrEqualTo: layoutMarginsGuide.bottomAnchor))
} else {
addedConstraints.append(view.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor))
}
}
NSLayoutConstraint.activate(addedConstraints)
self.addedConstraints.append(contentsOf: addedConstraints)
private func buildCenterConstraints(_ view: UIView) -> (centerX: NSLayoutConstraint, centerY: NSLayoutConstraint) {
(centerX: view.centerXAnchor.constraint(equalTo: layoutMarginsGuide.centerXAnchor), centerY: view.centerYAnchor.constraint(equalTo: layoutMarginsGuide.centerYAnchor))
}

private func setDistributionConstraint(on view: UIView) {
if flexibleEdges.contains(.leading), flexibleEdges.contains(.trailing) {
let layoutConstraint = view.centerXAnchor.constraint(equalTo: layoutMarginsGuide.centerXAnchor)
addedConstraints.append(layoutConstraint)
layoutConstraint.isActive = true
}
if flexibleEdges.contains(.top), flexibleEdges.contains(.bottom) {
let layoutConstraint = view.centerYAnchor.constraint(equalTo: layoutMarginsGuide.centerYAnchor)
addedConstraints.append(layoutConstraint)
layoutConstraint.isActive = true
}
private func buildRigidConstraints(_ view: UIView) -> [Edge: NSLayoutConstraint] {
[.top: view.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
.bottom: view.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
.leading: view.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
.trailing: view.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor)]
}

private func buildFlexibleConstraints(_ view: UIView) -> [Edge: NSLayoutConstraint] {
[.top: view.topAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.topAnchor),
.bottom: view.bottomAnchor.constraint(lessThanOrEqualTo: layoutMarginsGuide.bottomAnchor),
.leading: view.leadingAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.leadingAnchor),
.trailing: view.trailingAnchor.constraint(lessThanOrEqualTo: layoutMarginsGuide.trailingAnchor)]
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
ReferencedContainer = "container:ChatLayout.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<LocationScenarioReference
identifier = "com.apple.dt.IDEFoundation.CurrentLocationScenarioIdentifier"
referenceType = "1">
</LocationScenarioReference>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
12 changes: 6 additions & 6 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
PODS:
- ChatLayout (1.2.15):
- ChatLayout/Ultimate (= 1.2.15)
- ChatLayout/Core (1.2.15)
- ChatLayout/Extras (1.2.15):
- ChatLayout (1.2.16):
- ChatLayout/Ultimate (= 1.2.16)
- ChatLayout/Core (1.2.16)
- ChatLayout/Extras (1.2.16):
- ChatLayout/Core
- ChatLayout/Ultimate (1.2.15):
- ChatLayout/Ultimate (1.2.16):
- ChatLayout/Core
- ChatLayout/Extras
- DifferenceKit (1.3.0):
Expand Down Expand Up @@ -35,7 +35,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
ChatLayout: f7938b2e1c4c46dc0bfbc5a617ff7b7b95fc8d86
ChatLayout: 86ed40d6753afb40843cf2069338e50672aa5a33
DifferenceKit: ab185c4d7f9cef8af3fcf593e5b387fb81e999ca
FPSCounter: 884afec377de66637808c4f52ecc3b85a404732b
InputBarAccessoryView: 1d7b0a672b36e370f01f264b3907ef39d03328e3
Expand Down

0 comments on commit e5a72f9

Please sign in to comment.