Skip to content

Commit

Permalink
Merge pull request #1030 from adevinta/prerelease_0_16_3
Browse files Browse the repository at this point in the history
Prerelease 0 16 3
  • Loading branch information
michael-zimmermann authored Jun 28, 2024
2 parents f94b77d + 140ec59 commit 89efbb1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ public struct TextFieldAddons<LeftView: View, RightView: View, LeftAddon: View,
}
.frame(maxHeight: maxHeight)
.allowsHitTesting(self.viewModel.textFieldViewModel.isUserInteractionEnabled)
.border(width: self.viewModel.borderWidth * self.scaleFactor, radius: self.viewModel.borderRadius, colorToken: self.viewModel.textFieldViewModel.borderColor)
.border(
width: self.viewModel.borderWidth * self.scaleFactor,
radius: self.viewModel.borderRadius * self.scaleFactor,
colorToken: self.viewModel.textFieldViewModel.borderColor
)
.opacity(self.viewModel.dim)
.accessibilityElement(children: .contain)
.accessibilityIdentifier(TextFieldAddonsAccessibilityIdentifier.view)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public final class TextFieldAddonsUIView: UIControl {
public let textField: TextFieldUIView
/// Current leftAddon, set using setLeftAddon(_:, _withPadding:)
private(set) public var leftAddon: UIView?
private var leftAddonCenterXConstraint: NSLayoutConstraint = NSLayoutConstraint()
/// Current rightAddon, set using setRightAddon(_:, _withPadding:)
private(set) public var rightAddon: UIView?
private var rightAddonCenterXConstraint: NSLayoutConstraint = NSLayoutConstraint()

private var leftAddonContainer = UIView()
private var leftSeparatorView = UIView()
Expand Down Expand Up @@ -152,7 +154,7 @@ public final class TextFieldAddonsUIView: UIControl {
self.viewModel.$borderWidth.removeDuplicates().subscribe(in: &self.cancellables) { [weak self] borderWidth in
guard let self else { return }
let width = borderWidth * self.scaleFactor
self.setBorderWidth(width)
self.setBorderWidthAndRefreshAddonsXCenter(width)
self.setLeftSpacing(self.viewModel.leftSpacing, borderWidth: width)
self.setRightSpacing(self.viewModel.rightSpacing, borderWidth: width)
self.leftSeparatorWidthConstraint.constant = width
Expand All @@ -161,7 +163,7 @@ public final class TextFieldAddonsUIView: UIControl {

self.viewModel.$borderRadius.removeDuplicates().subscribe(in: &self.cancellables) { [weak self] borderRadius in
guard let self else { return }
self.setCornerRadius(borderRadius)
self.setCornerRadius(borderRadius * self.scaleFactor)
}

self.viewModel.$leftSpacing.removeDuplicates().subscribe(in: &self.cancellables) { [weak self] leftSpacing in
Expand Down Expand Up @@ -197,6 +199,20 @@ public final class TextFieldAddonsUIView: UIControl {
self.trailingConstraint.constant = (self.rightAddonContainer.isHidden ? -rightSpacing : .zero) - borderWidth
}

private func setBorderWidthAndRefreshAddonsXCenter(_ borderWidth: CGFloat) {
self.setBorderWidth(borderWidth)
self.setLeftAddonCenterXConstant(borderWidth: borderWidth)
self.setRightAddonCenterXConstant(borderWidth: borderWidth)
}

private func setLeftAddonCenterXConstant(borderWidth: CGFloat) {
self.leftAddonCenterXConstraint.constant = -borderWidth / 2.0
}

private func setRightAddonCenterXConstant(borderWidth: CGFloat) {
self.rightAddonCenterXConstraint.constant = borderWidth / 2.0
}

public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)

Expand All @@ -207,7 +223,8 @@ public final class TextFieldAddonsUIView: UIControl {
guard previousTraitCollection?.preferredContentSizeCategory != self.traitCollection.preferredContentSizeCategory else { return }

self._scaleFactor.update(traitCollection: self.traitCollection)
self.setBorderWidth(self.borderWidth)
self.setCornerRadius(self.viewModel.borderRadius * self.scaleFactor)
self.setBorderWidthAndRefreshAddonsXCenter(self.borderWidth)
self.setLeftSpacing(self.viewModel.leftSpacing, borderWidth: self.borderWidth)
self.setRightSpacing(self.viewModel.rightSpacing, borderWidth: self.borderWidth)
self.leftSeparatorWidthConstraint.constant = self.borderWidth
Expand All @@ -221,20 +238,23 @@ public final class TextFieldAddonsUIView: UIControl {
/// - withPadding: adds a padding on the addon if `true`, default is `false`
public func setLeftAddon(_ leftAddon: UIView?, withPadding: Bool = false) {
if let oldValue = self.leftAddon, oldValue.isDescendant(of: self.leftAddonContainer) {
oldValue.removeConstraint(self.leftSeparatorWidthConstraint)
oldValue.removeFromSuperview()
}
if let leftAddon {
self.leftAddonContainer.addSubview(leftAddon)
leftAddon.setContentHuggingPriority(.defaultHigh, for: .horizontal)
leftAddon.translatesAutoresizingMaskIntoConstraints = false
self.leftAddonCenterXConstraint = leftAddon.centerXAnchor.constraint(equalTo: self.leftAddonContainer.centerXAnchor)
NSLayoutConstraint.activate([
leftAddon.trailingAnchor.constraint(equalTo: self.leftSeparatorView.leadingAnchor, constant: withPadding ? -self.viewModel.leftSpacing : 0),
leftAddon.centerXAnchor.constraint(equalTo: self.leftAddonContainer.centerXAnchor, constant: -self.borderWidth / 2.0),
self.leftAddonCenterXConstraint,
leftAddon.centerYAnchor.constraint(equalTo: self.leftAddonContainer.centerYAnchor)
])
}
self.leftAddon = leftAddon
self.leftAddonContainer.isHidden = self.leftAddon == nil
self.setLeftAddonCenterXConstant(borderWidth: self.borderWidth)
self.setLeftSpacing(self.viewModel.leftSpacing, borderWidth: self.borderWidth)
}

Expand All @@ -244,20 +264,23 @@ public final class TextFieldAddonsUIView: UIControl {
/// - withPadding: adds a padding on the addon if `true`, default is `false`
public func setRightAddon(_ rightAddon: UIView? = nil, withPadding: Bool = false) {
if let oldValue = self.rightAddon, oldValue.isDescendant(of: self.rightAddonContainer) {
oldValue.removeConstraint(self.rightAddonCenterXConstraint)
oldValue.removeFromSuperview()
}
if let rightAddon {
self.rightAddonContainer.addSubview(rightAddon)
rightAddon.setContentHuggingPriority(.defaultHigh, for: .horizontal)
rightAddon.translatesAutoresizingMaskIntoConstraints = false
self.rightAddonCenterXConstraint = rightAddon.centerXAnchor.constraint(equalTo: self.rightAddonContainer.centerXAnchor)
NSLayoutConstraint.activate([
rightAddon.leadingAnchor.constraint(equalTo: self.rightSeparatorView.trailingAnchor, constant: withPadding ? self.viewModel.rightSpacing : 0),
rightAddon.centerXAnchor.constraint(equalTo: self.rightAddonContainer.centerXAnchor, constant: self.borderWidth / 2.0),
self.rightAddonCenterXConstraint,
rightAddon.centerYAnchor.constraint(equalTo: self.rightAddonContainer.centerYAnchor)
])
}
self.rightAddon = rightAddon
self.rightAddonContainer.isHidden = self.rightAddon == nil
self.setRightAddonCenterXConstant(borderWidth: self.borderWidth)
self.setRightSpacing(self.viewModel.rightSpacing, borderWidth: self.borderWidth)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ struct TextFieldViewInternal<LeftView: View, RightView: View>: View {
}
.tint(self.viewModel.textColor.color)
.allowsHitTesting(self.viewModel.isUserInteractionEnabled)
.border(width: self.viewModel.borderWidth * self.scaleFactor, radius: self.viewModel.borderRadius, colorToken: self.viewModel.borderColor)
.border(
width: self.viewModel.borderWidth * self.scaleFactor,
radius: self.viewModel.borderRadius * self.scaleFactor,
colorToken: self.viewModel.borderColor
)
.frame(height: self.height)
.opacity(self.viewModel.dim)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public final class TextFieldUIView: UITextField {

self.viewModel.$borderRadius.removeDuplicates().subscribe(in: &self.cancellables) { [weak self] borderRadius in
guard let self else { return }
self.setCornerRadius(borderRadius)
self.setCornerRadius(borderRadius * self.scaleFactor)
}

self.viewModel.$leftSpacing.removeDuplicates().subscribe(in: &self.cancellables) { [weak self] dim in
Expand Down Expand Up @@ -276,6 +276,7 @@ public final class TextFieldUIView: UITextField {

self._height.update(traitCollection: self.traitCollection)
self._scaleFactor.update(traitCollection: self.traitCollection)
self.setCornerRadius(self.viewModel.borderRadius * self.scaleFactor)
self.setBorderWidth(self.viewModel.borderWidth * self.scaleFactor)
self.invalidateIntrinsicContentSize()
}
Expand Down

0 comments on commit 89efbb1

Please sign in to comment.