Skip to content

Commit

Permalink
fix when slider button is enabled on init value
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 committed Dec 10, 2024
1 parent 2f8d4b8 commit b9729c9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 35 deletions.
68 changes: 44 additions & 24 deletions Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct Slider: View {
.foregroundColor(.accentColor)
.fontWeight(.semibold)
.padding(.bottom, 10)
.disabled(!viewModel.isActive)
.disabled(viewModel.isButtonDisabled)

Check warning on line 88 in Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift

View check run for this annotation

Codecov / codecov/patch

Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift#L88

Added line #L88 was not covered by tests

HStack(spacing: 0) {
minimumImage?
Expand Down Expand Up @@ -148,7 +148,7 @@ struct Slider: View {
})
)
addTicks(sliderWidth: sliderWidth)
.if(!viewModel.isActive) {
.if(viewModel.isButtonDisabled) {

Check warning on line 151 in Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift

View check run for this annotation

Codecov / codecov/patch

Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift#L151

Added line #L151 was not covered by tests
$0.accentColor(Color.gray)
}
}
Expand All @@ -161,7 +161,7 @@ struct Slider: View {
if gradientColors != nil {
Rectangle()
.foregroundColor(.clear)
.background(viewModel.isActive ?
.background(!viewModel.isButtonDisabled ?

Check warning on line 164 in Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift

View check run for this annotation

Codecov / codecov/patch

Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift#L164

Added line #L164 was not covered by tests
LinearGradient(gradient: Gradient(colors: gradientColors ?? []),
startPoint: .leading,
endPoint: .trailing) :
Expand All @@ -173,7 +173,7 @@ struct Slider: View {

SwiftUI.Slider(value: $viewModel.valueAsDouble, in: range.0...range.1, step: viewModel.step)
.if(gradientColors == nil) {
$0.accentColor(viewModel.isActive ? .accentColor : Color.gray)
$0.accentColor(!viewModel.isButtonDisabled ? .accentColor : Color.gray)

Check warning on line 176 in Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift

View check run for this annotation

Codecov / codecov/patch

Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift#L176

Added line #L176 was not covered by tests
}
.if(gradientColors != nil) { $0.accentColor(.clear) }
}
Expand All @@ -185,35 +185,56 @@ struct Slider: View {
let barRightSize = CGSize(width: CGFloat(offsetX), height: height)
let barLeft = Rectangle()
.if(gradientColors == nil) {
$0.foregroundColor(viewModel.isActive ? .accentColor : Color.gray)
$0.foregroundColor(
!viewModel.isButtonDisabled ? .accentColor : Color.gray
)

Check warning on line 190 in Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift

View check run for this annotation

Codecov / codecov/patch

Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift#L188-L190

Added lines #L188 - L190 were not covered by tests
}
.if(gradientColors != nil) {
$0
.foregroundColor(.clear)
.background(viewModel.isActive ?
LinearGradient(gradient: Gradient(colors: gradientColors ?? []),
startPoint: .leading,
endPoint: .trailing) :
LinearGradient(gradient: Gradient(colors: [Color.gray]),
startPoint: .leading,
endPoint: .trailing))
.background(
!viewModel.isButtonDisabled ?
LinearGradient(
gradient: Gradient(
colors: gradientColors ?? []
),
startPoint: .leading,
endPoint: .trailing
) :
LinearGradient(
gradient: Gradient(
colors: [Color.gray]
),
startPoint: .leading,
endPoint: .trailing
)
)

Check warning on line 211 in Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift

View check run for this annotation

Codecov / codecov/patch

Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift#L195-L211

Added lines #L195 - L211 were not covered by tests
}
let barRight = Color.white
return
ZStack {
barLeft
.modifier(SliderModifier(sliderWidth: width,
size: barLeftSize,
radius: cornerRadius!))
barLeft.modifier(
SliderModifier(
sliderWidth: width,
size: barLeftSize,
radius: cornerRadius!
)
)

Check warning on line 222 in Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift

View check run for this annotation

Codecov / codecov/patch

Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift#L216-L222

Added lines #L216 - L222 were not covered by tests
barRight
.modifier(SliderModifier(sliderWidth: width,
size: barRightSize,
radius: cornerRadius!))
RoundedRectangle(cornerRadius: cornerRadius!)
.stroke(
Color.gray,
lineWidth: borderWidth
.modifier(
SliderModifier(
sliderWidth: width,
size: barRightSize,
radius: cornerRadius!
)

Check warning on line 229 in Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift

View check run for this annotation

Codecov / codecov/patch

Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift#L224-L229

Added lines #L224 - L229 were not covered by tests
)
RoundedRectangle(
cornerRadius: cornerRadius!
)
.stroke(
Color.gray,
lineWidth: borderWidth
)

Check warning on line 237 in Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift

View check run for this annotation

Codecov / codecov/patch

Sources/CareKitEssentials/Cards/iOS/SliderLog/Slider.swift#L231-L237

Added lines #L231 - L237 were not covered by tests
}
}

Expand Down Expand Up @@ -246,7 +267,6 @@ struct Slider: View {
dragValue = dragValue.convert(fromRange: (xrange.min, xrange.max), toRange: (range.0, range.1))
dragValue = round(dragValue / viewModel.step) * viewModel.step
self.viewModel.valueAsDouble = dragValue
self.viewModel.isActive = true
}

private func getOffsetX(sliderWidth: CGFloat) -> CGFloat {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ struct SliderLogButton: CareKitEssentialView {
.font(Font.subheadline.weight(.medium))
.foregroundColor(.accentColor)
}
.disabled(!viewModel.isActive)
.foregroundColor(viewModel.isActive ? .accentColor : Color.gray)
.disabled(viewModel.isButtonDisabled)
.foregroundColor(!viewModel.isButtonDisabled ? .accentColor : Color.gray)

Check warning on line 61 in Sources/CareKitEssentials/Cards/iOS/SliderLog/SliderLogButton.swift

View check run for this annotation

Codecov / codecov/patch

Sources/CareKitEssentials/Cards/iOS/SliderLog/SliderLogButton.swift#L60-L61

Added lines #L60 - L61 were not covered by tests
.padding(.bottom)

Button(action: {}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ open class SliderLogTaskViewModel: CardViewModel {
/// The binded array of previous outcome values.
@Published public var previousValues = [Double]()

/// Specifies if the slider is allowed to be changed.
@Published open var isActive = true
/// Determines when the slider is disabled.
open var isButtonDisabled: Bool {
let currentDouble = valueAsDouble
guard let originalDouble = previousValues.first else {
return false
}
return Int(currentDouble) == Int(originalDouble)
}

Check warning on line 28 in Sources/CareKitEssentials/Cards/iOS/SliderLog/SliderLogTaskViewModel.swift

View check run for this annotation

Codecov / codecov/patch

Sources/CareKitEssentials/Cards/iOS/SliderLog/SliderLogTaskViewModel.swift#L22-L28

Added lines #L22 - L28 were not covered by tests

/// The range that includes all possible values.
public private(set) var range: ClosedRange<Double>
Expand Down Expand Up @@ -63,12 +69,8 @@ open class SliderLogTaskViewModel: CardViewModel {
currentInitialDoubleValue = initialValue
}
var currentInitialOutcome = OCKOutcomeValue(currentInitialDoubleValue)
if let latestOutcomeValue = outcomeValues?.first,
let initialOutcomeDouble = latestOutcomeValue.doubleValue {
if initialOutcomeDouble != currentInitialDoubleValue {
isActive = false
currentInitialOutcome = latestOutcomeValue
}
if let latestOutcomeValue = outcomeValues?.first {
currentInitialOutcome = latestOutcomeValue

Check warning on line 73 in Sources/CareKitEssentials/Cards/iOS/SliderLog/SliderLogTaskViewModel.swift

View check run for this annotation

Codecov / codecov/patch

Sources/CareKitEssentials/Cards/iOS/SliderLog/SliderLogTaskViewModel.swift#L72-L73

Added lines #L72 - L73 were not covered by tests
}
super.init(
event: event,
Expand All @@ -85,6 +87,5 @@ open class SliderLogTaskViewModel: CardViewModel {
if let previousValues = Self.filterAndSortValuesByLatest(outcome.values, by: kind) {

Check warning on line 87 in Sources/CareKitEssentials/Cards/iOS/SliderLog/SliderLogTaskViewModel.swift

View check run for this annotation

Codecov / codecov/patch

Sources/CareKitEssentials/Cards/iOS/SliderLog/SliderLogTaskViewModel.swift#L87

Added line #L87 was not covered by tests
self.previousValues = previousValues.compactMap(\.doubleValue)
}
self.isActive = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ open class DigitalCrownViewModel: CardViewModel {
return Color(red: red, green: green, blue: blue)
}

/// Determines when the slider is disabled.
open var isButtonDisabled: Bool {
guard let currentDouble = value.doubleValue,
let originalDouble = event.outcomeValueDouble else {
Expand Down

0 comments on commit b9729c9

Please sign in to comment.