Skip to content

Commit

Permalink
fix to allow showDifferenceOnEditing to work when title is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
timdubbins committed Jul 25, 2022
1 parent ca7298d commit ed89004
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ var body: some View {
RangeSlider(
lowValue: $lowValue,
highValue: $highValue,
in: 0...10) {
in: 0...10
showDifferenceOnEditing: true) {
onEditing = $0
}
Text(String(onEditing))
Expand Down
41 changes: 27 additions & 14 deletions Sources/RangeSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import SwiftUI
/// RangeSlider(
/// lowValue: $lowValue,
/// highValue: $highValue,
/// in: 0...10) {
/// in: 0...10,
/// showDifferenceOnEditing: true) {
/// onEditing = $0
/// }
/// Text(String(onEditing))
Expand Down Expand Up @@ -98,17 +99,29 @@ public struct RangeSlider: View {
public var body: some View {
VStack {
if title != nil {
Text(displayTitle)
.fontWeight(.semibold)
.font(font)
.foregroundColor(.secondary)
.padding(.bottom, -3)
.transition(.opacity)

// We change the id parameter of the view when
// we change its title. This resets the views
// state, allowing us to animate this change.
.id("RangeSlider" + displayTitle)
ZStack {
// This prevents the displayTitle height from
// changing in the case that title is nil and
// showDifferenceOnEditing is true.
Text("hidden")
.fontWeight(.semibold)
.font(font)
.padding(.bottom, -3)
.hidden()

Text(displayTitle)
.fontWeight(.semibold)
.font(font)
.foregroundColor(.secondary)
.padding(.bottom, -3)
.transition(.opacity)

// We change the id parameter of the view when
// we change its title. This resets the views
// state, allowing us to animate this change.
.id("RangeSlider" + displayTitle)

}
}
GeometryReader { geo in
ZStack {
Expand Down Expand Up @@ -199,7 +212,7 @@ public struct RangeSlider: View {
maximumValue = bounds.upperBound
totalDistance = maximumValue - minimumValue
_displayTitle = State(initialValue: title ?? "")
self.title = title
self.title = showDifferenceOnEditing && title == nil ? "" : title
self.bounds = bounds
self.displayBounds = displayBounds
self.step = step
Expand Down Expand Up @@ -240,7 +253,7 @@ public struct RangeSlider: View {
}
}

if showDifferenceOnEditing && title != nil {
if showDifferenceOnEditing {
displayTitle = String(format: "%0.2f", scale(highValue - lowValue))
}
}
Expand Down

0 comments on commit ed89004

Please sign in to comment.