Skip to content

Commit

Permalink
Remove dependency on SweeterSwift
Browse files Browse the repository at this point in the history
  • Loading branch information
nighthawk committed May 1, 2024
1 parent 9c91d9f commit ca64744
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 28 deletions.
5 changes: 1 addition & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ let package = Package(
products: [
.library(name: "MultiSlider", targets: ["MultiSlider"]),
],
dependencies: [
.package(url: "https://github.com/yonat/SweeterSwift", from: "1.0.4"),
],
targets: [
.target(name: "MultiSlider", dependencies: ["SweeterSwift"], path: "Sources", resources: [.copy("PrivacyInfo.xcprivacy")]),
.target(name: "MultiSlider", path: "Sources", resources: [.copy("PrivacyInfo.xcprivacy")]),
],
swiftLanguageVersions: [.v5]
)
1 change: 0 additions & 1 deletion Sources/MultiSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright © 2016 Yonat Sharon. All rights reserved.
//

import SweeterSwift
import UIKit

@IBDesignable
Expand Down
1 change: 0 additions & 1 deletion Sources/MultiSliderExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by Yonat Sharon on 20.05.2018.
//

import SweeterSwift
import UIKit

extension MultiSlider.Snap {
Expand Down
43 changes: 21 additions & 22 deletions Sources/MultiValueSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#if canImport(SwiftUI)

import SweeterSwift
import SwiftUI

/// Slider clone with multiple thumbs and values, range highlight, optional snap intervals, optional value labels.
Expand Down Expand Up @@ -42,27 +41,27 @@ import SwiftUI
) {
_value = value

uiView.minimumValue =? minimumValue
uiView.maximumValue =? maximumValue
uiView.isContinuous =? isContinuous
uiView.snapStepSize =? snapStepSize
uiView.isHapticSnap =? isHapticSnap
uiView.valueLabelPosition =? valueLabelPosition
uiView.valueLabelAlternatePosition =? valueLabelAlternatePosition
uiView.isValueLabelRelative =? isValueLabelRelative
uiView.orientation =? orientation
uiView.outerTrackColor =? outerTrackColor
uiView.valueLabelColor =? valueLabelColor
uiView.valueLabelFont =? valueLabelFont
uiView.thumbImage =? thumbImage
uiView.showsThumbImageShadow =? showsThumbImageShadow
uiView.minimumImage =? minimumImage
uiView.maximumImage =? maximumImage
uiView.trackWidth =? trackWidth
uiView.hasRoundTrackEnds =? hasRoundTrackEnds
uiView.distanceBetweenThumbs =? distanceBetweenThumbs
uiView.keepsDistanceBetweenThumbs =? keepsDistanceBetweenThumbs
uiView.valueLabelFormatter =? valueLabelFormatter
uiView.minimumValue = minimumValue ?? uiView.minimumValue
uiView.maximumValue = maximumValue ?? uiView.maximumValue
uiView.isContinuous = isContinuous ?? uiView.isContinuous
uiView.snapStepSize = snapStepSize ?? uiView.snapStepSize
uiView.isHapticSnap = isHapticSnap ?? uiView.isHapticSnap
uiView.valueLabelPosition = valueLabelPosition ?? uiView.valueLabelPosition
uiView.valueLabelAlternatePosition = valueLabelAlternatePosition ?? uiView.valueLabelAlternatePosition
uiView.isValueLabelRelative = isValueLabelRelative ?? uiView.isValueLabelRelative
uiView.orientation = orientation ?? uiView.orientation
uiView.outerTrackColor = outerTrackColor ?? uiView.outerTrackColor
uiView.valueLabelColor = valueLabelColor ?? uiView.valueLabelColor
uiView.valueLabelFont = valueLabelFont ?? uiView.valueLabelFont
uiView.thumbImage = thumbImage ?? uiView.thumbImage
uiView.showsThumbImageShadow = showsThumbImageShadow ?? uiView.showsThumbImageShadow
uiView.minimumImage = minimumImage ?? uiView.minimumImage
uiView.maximumImage = maximumImage ?? uiView.maximumImage
uiView.trackWidth = trackWidth ?? uiView.trackWidth
uiView.hasRoundTrackEnds = hasRoundTrackEnds ?? uiView.hasRoundTrackEnds
uiView.distanceBetweenThumbs = distanceBetweenThumbs ?? uiView.distanceBetweenThumbs
uiView.keepsDistanceBetweenThumbs = keepsDistanceBetweenThumbs ?? uiView.keepsDistanceBetweenThumbs
uiView.valueLabelFormatter = valueLabelFormatter ?? uiView.valueLabelFormatter
}

public func makeUIView(context: UIViewRepresentableContext<MultiValueSlider>) -> MultiSlider {
Expand Down
105 changes: 105 additions & 0 deletions Sources/UIView+Sweeter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// UIView+Sweeter.swift
//
// Created by Yonat Sharon on 2019-02-08.
//

import UIKit

extension UIView {
/// Sweeter: Set constant attribute. Example: `constrain(.width, to: 17)`
@discardableResult func constrain(
_ at: NSLayoutConstraint.Attribute,
to: CGFloat = 0,
ratio: CGFloat = 1,
relation: NSLayoutConstraint.Relation = .equal,
priority: UILayoutPriority = .required,
identifier: String? = nil
) -> NSLayoutConstraint {
let constraint = NSLayoutConstraint(
item: self, attribute: at, relatedBy: relation,
toItem: nil, attribute: .notAnAttribute, multiplier: ratio, constant: to
)
constraint.priority = priority
constraint.identifier = identifier
addConstraintWithoutConflict(constraint)
return constraint
}

/// Sweeter: Pin subview at a specific place. Example: `constrain(label, at: .top)`
@discardableResult func constrain(
_ subview: UIView,
at: NSLayoutConstraint.Attribute,
diff: CGFloat = 0,
ratio: CGFloat = 1,
relation: NSLayoutConstraint.Relation = .equal,
priority: UILayoutPriority = .required,
identifier: String? = nil
) -> NSLayoutConstraint {
let constraint = NSLayoutConstraint(
item: subview, attribute: at, relatedBy: relation,
toItem: self, attribute: at, multiplier: ratio, constant: diff
)
constraint.priority = priority
constraint.identifier = identifier
addConstraintWithoutConflict(constraint)
return constraint
}

/// Sweeter: Pin two subviews to each other. Example:
///
/// `constrain(label, at: .leading, to: textField)`
///
/// `constrain(textField, at: .top, to: label, at: .bottom, diff: 8)`
@discardableResult func constrain(
_ subview: UIView,
at: NSLayoutConstraint.Attribute,
to subview2: UIView,
at at2: NSLayoutConstraint.Attribute = .notAnAttribute,
diff: CGFloat = 0,
ratio: CGFloat = 1,
relation: NSLayoutConstraint.Relation = .equal,
priority: UILayoutPriority = .required,
identifier: String? = nil
) -> NSLayoutConstraint {
let at2real = at2 == .notAnAttribute ? at : at2
let constraint = NSLayoutConstraint(
item: subview, attribute: at, relatedBy: relation,
toItem: subview2, attribute: at2real, multiplier: ratio, constant: diff
)
constraint.priority = priority
constraint.identifier = identifier
addConstraintWithoutConflict(constraint)
return constraint
}

/// Sweeter: Add subview pinned to specific places. Example: `addConstrainedSubview(button, constrain: .centerX, .centerY)`
@discardableResult func addConstrainedSubview(_ subview: UIView, constrain: NSLayoutConstraint.Attribute...) -> [NSLayoutConstraint] {
return addConstrainedSubview(subview, constrainedAttributes: constrain)
}

@discardableResult internal func addConstrainedSubview(_ subview: UIView, constrainedAttributes: [NSLayoutConstraint.Attribute]) -> [NSLayoutConstraint] {
subview.translatesAutoresizingMaskIntoConstraints = false
addSubview(subview)
return constrainedAttributes.map { self.constrain(subview, at: $0) }
}

internal func addConstraintWithoutConflict(_ constraint: NSLayoutConstraint) {
removeConstraints(constraints.filter {
constraint.firstItem === $0.firstItem
&& constraint.secondItem === $0.secondItem
&& constraint.firstAttribute == $0.firstAttribute
&& constraint.secondAttribute == $0.secondAttribute
})
addConstraint(constraint)
}

/// Sweeter: The color used to tint the view, as inherited from its superviews.
var actualTintColor: UIColor {
var tintedView: UIView? = self
while let currentView = tintedView, nil == currentView.tintColor {
tintedView = currentView.superview
}
return tintedView?.tintColor ?? UIColor(red: 0, green: 0.5, blue: 1, alpha: 1) // swiftlint:disable:this no_magic_numbers
}
}

0 comments on commit ca64744

Please sign in to comment.