Skip to content

Commit e5a72f9

Browse files
committed
Minor improvements in EdgeAligningView
1 parent 66a7b8e commit e5a72f9

File tree

4 files changed

+74
-58
lines changed

4 files changed

+74
-58
lines changed

ChatLayout.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'ChatLayout'
3-
s.version = '1.2.15'
3+
s.version = '1.2.16'
44
s.summary = 'Chat UI Library. It uses custom UICollectionViewLayout to provide you full control over the presentation.'
55
s.swift_version = '5.7'
66

ChatLayout/Classes/Extras/EdgeAligningView.swift

+63-51
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ public final class EdgeAligningView<CustomView: UIView>: UIView {
3232
/// Bottom edge
3333
case bottom
3434

35-
var otherEdges: [Edge] {
36-
Edge.allCases.filter { $0 != self }
37-
}
38-
3935
}
4036

4137
/// Set of edge constraints to be set as loose.
@@ -44,23 +40,32 @@ public final class EdgeAligningView<CustomView: UIView>: UIView {
4440
guard flexibleEdges != oldValue else {
4541
return
4642
}
47-
setupContainer()
43+
lastConstraintsUpdateEdges = nil
44+
setNeedsUpdateConstraints()
4845
}
4946
}
5047

5148
/// Contained view.
5249
public var customView: CustomView {
5350
didSet {
54-
guard customView != oldValue else {
51+
guard customView !== oldValue else {
5552
return
5653
}
5754
oldValue.removeFromSuperview()
5855
setupContainer()
5956
}
6057
}
6158

59+
private var rigidConstraints: [Edge: NSLayoutConstraint] = [:]
60+
61+
private var flexibleConstraints: [Edge: NSLayoutConstraint] = [:]
62+
63+
private var centerConstraints: (centerX: NSLayoutConstraint, centerY: NSLayoutConstraint)?
64+
6265
private var addedConstraints: [NSLayoutConstraint] = []
6366

67+
private var lastConstraintsUpdateEdges: Set<Edge>?
68+
6469
/// Initializes and returns a newly allocated `EdgeAligningView`
6570
/// - Parameters:
6671
/// - customView: An instance of `CustomView`
@@ -87,6 +92,28 @@ public final class EdgeAligningView<CustomView: UIView>: UIView {
8792
fatalError("Use init(with:flexibleEdges:) instead.")
8893
}
8994

95+
public override func updateConstraints() {
96+
guard lastConstraintsUpdateEdges != flexibleEdges else {
97+
super.updateConstraints()
98+
return
99+
}
100+
101+
flexibleEdges.forEach { edge in
102+
rigidConstraints[edge]?.isActive = false
103+
flexibleConstraints[edge]?.isActive = true
104+
}
105+
Set(Edge.allCases).subtracting(flexibleEdges).forEach { edge in
106+
flexibleConstraints[edge]?.isActive = false
107+
rigidConstraints[edge]?.isActive = true
108+
}
109+
centerConstraints?.centerX.isActive = flexibleEdges.contains(.leading) && flexibleEdges.contains(.trailing)
110+
centerConstraints?.centerY.isActive = flexibleEdges.contains(.top) && flexibleEdges.contains(.bottom)
111+
112+
lastConstraintsUpdateEdges = flexibleEdges
113+
114+
super.updateConstraints()
115+
}
116+
90117
private func setupSubviews() {
91118
translatesAutoresizingMaskIntoConstraints = false
92119
insetsLayoutMarginsFromSafeArea = false
@@ -104,55 +131,40 @@ public final class EdgeAligningView<CustomView: UIView>: UIView {
104131
removeConstraints(addedConstraints)
105132
addedConstraints.removeAll()
106133
}
107-
Set(Edge.allCases).subtracting(flexibleEdges).forEach { setConstraint(for: $0, on: customView, flexible: false) }
108-
flexibleEdges.forEach { setConstraint(for: $0, on: customView, flexible: true) }
109-
setDistributionConstraint(on: customView)
110-
setNeedsLayout()
134+
135+
lastConstraintsUpdateEdges = nil
136+
137+
let rigidConstraints = buildRigidConstraints(customView)
138+
let flexibleConstraints = buildFlexibleConstraints(customView)
139+
let centerConstraints = buildCenterConstraints(customView)
140+
141+
addedConstraints.append(contentsOf: rigidConstraints.values)
142+
addedConstraints.append(contentsOf: flexibleConstraints.values)
143+
addedConstraints.append(centerConstraints.centerX)
144+
addedConstraints.append(centerConstraints.centerY)
145+
146+
self.rigidConstraints = rigidConstraints
147+
self.flexibleConstraints = flexibleConstraints
148+
self.centerConstraints = centerConstraints
149+
setNeedsUpdateConstraints()
111150
}
112151

113-
private func setConstraint(for edge: Edge, on view: UIView, flexible: Bool = false) {
114-
var addedConstraints: [NSLayoutConstraint] = []
115-
switch edge {
116-
case .top:
117-
if flexible {
118-
addedConstraints.append(view.topAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.topAnchor))
119-
} else {
120-
addedConstraints.append(view.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor))
121-
}
122-
case .leading:
123-
if flexible {
124-
addedConstraints.append(view.leadingAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.leadingAnchor))
125-
} else {
126-
addedConstraints.append(view.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor))
127-
}
128-
case .trailing:
129-
if flexible {
130-
addedConstraints.append(view.trailingAnchor.constraint(lessThanOrEqualTo: layoutMarginsGuide.trailingAnchor))
131-
} else {
132-
addedConstraints.append(view.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor))
133-
}
134-
case .bottom:
135-
if flexible {
136-
addedConstraints.append(view.bottomAnchor.constraint(lessThanOrEqualTo: layoutMarginsGuide.bottomAnchor))
137-
} else {
138-
addedConstraints.append(view.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor))
139-
}
140-
}
141-
NSLayoutConstraint.activate(addedConstraints)
142-
self.addedConstraints.append(contentsOf: addedConstraints)
152+
private func buildCenterConstraints(_ view: UIView) -> (centerX: NSLayoutConstraint, centerY: NSLayoutConstraint) {
153+
(centerX: view.centerXAnchor.constraint(equalTo: layoutMarginsGuide.centerXAnchor), centerY: view.centerYAnchor.constraint(equalTo: layoutMarginsGuide.centerYAnchor))
143154
}
144155

145-
private func setDistributionConstraint(on view: UIView) {
146-
if flexibleEdges.contains(.leading), flexibleEdges.contains(.trailing) {
147-
let layoutConstraint = view.centerXAnchor.constraint(equalTo: layoutMarginsGuide.centerXAnchor)
148-
addedConstraints.append(layoutConstraint)
149-
layoutConstraint.isActive = true
150-
}
151-
if flexibleEdges.contains(.top), flexibleEdges.contains(.bottom) {
152-
let layoutConstraint = view.centerYAnchor.constraint(equalTo: layoutMarginsGuide.centerYAnchor)
153-
addedConstraints.append(layoutConstraint)
154-
layoutConstraint.isActive = true
155-
}
156+
private func buildRigidConstraints(_ view: UIView) -> [Edge: NSLayoutConstraint] {
157+
[.top: view.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
158+
.bottom: view.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
159+
.leading: view.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
160+
.trailing: view.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor)]
161+
}
162+
163+
private func buildFlexibleConstraints(_ view: UIView) -> [Edge: NSLayoutConstraint] {
164+
[.top: view.topAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.topAnchor),
165+
.bottom: view.bottomAnchor.constraint(lessThanOrEqualTo: layoutMarginsGuide.bottomAnchor),
166+
.leading: view.leadingAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.leadingAnchor),
167+
.trailing: view.trailingAnchor.constraint(lessThanOrEqualTo: layoutMarginsGuide.trailingAnchor)]
156168
}
157169

158170
}

Example/ChatLayout.xcodeproj/xcshareddata/xcschemes/ChatLayout-Example.xcscheme

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@
9494
ReferencedContainer = "container:ChatLayout.xcodeproj">
9595
</BuildableReference>
9696
</BuildableProductRunnable>
97+
<LocationScenarioReference
98+
identifier = "com.apple.dt.IDEFoundation.CurrentLocationScenarioIdentifier"
99+
referenceType = "1">
100+
</LocationScenarioReference>
97101
</LaunchAction>
98102
<ProfileAction
99103
buildConfiguration = "Release"

Example/Podfile.lock

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
PODS:
2-
- ChatLayout (1.2.15):
3-
- ChatLayout/Ultimate (= 1.2.15)
4-
- ChatLayout/Core (1.2.15)
5-
- ChatLayout/Extras (1.2.15):
2+
- ChatLayout (1.2.16):
3+
- ChatLayout/Ultimate (= 1.2.16)
4+
- ChatLayout/Core (1.2.16)
5+
- ChatLayout/Extras (1.2.16):
66
- ChatLayout/Core
7-
- ChatLayout/Ultimate (1.2.15):
7+
- ChatLayout/Ultimate (1.2.16):
88
- ChatLayout/Core
99
- ChatLayout/Extras
1010
- DifferenceKit (1.3.0):
@@ -35,7 +35,7 @@ EXTERNAL SOURCES:
3535
:path: "../"
3636

3737
SPEC CHECKSUMS:
38-
ChatLayout: f7938b2e1c4c46dc0bfbc5a617ff7b7b95fc8d86
38+
ChatLayout: 86ed40d6753afb40843cf2069338e50672aa5a33
3939
DifferenceKit: ab185c4d7f9cef8af3fcf593e5b387fb81e999ca
4040
FPSCounter: 884afec377de66637808c4f52ecc3b85a404732b
4141
InputBarAccessoryView: 1d7b0a672b36e370f01f264b3907ef39d03328e3

0 commit comments

Comments
 (0)