add watchOS code from Workout+ to TODO for future reference
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import SwiftUI
|
||||
|
||||
struct ElapsedTimeView: View {
|
||||
var elapsedTime: TimeInterval = 0
|
||||
var showSubseconds: Bool = true
|
||||
@State private var timeFormatter = ElapsedTimeFormatter()
|
||||
|
||||
var body: some View {
|
||||
Text(NSNumber(value: elapsedTime), formatter: timeFormatter)
|
||||
.fontWeight(.semibold)
|
||||
.onChange(of: showSubseconds) { _, newValue in
|
||||
timeFormatter.showSubseconds = newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ElapsedTimeFormatter: Formatter {
|
||||
let componentsFormatter: DateComponentsFormatter = {
|
||||
let formatter = DateComponentsFormatter()
|
||||
formatter.allowedUnits = [.minute, .second]
|
||||
formatter.zeroFormattingBehavior = .pad
|
||||
return formatter
|
||||
}()
|
||||
var showSubseconds = true
|
||||
|
||||
override func string(for value: Any?) -> String? {
|
||||
guard let time = value as? TimeInterval else {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard let formattedString = componentsFormatter.string(from: time) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
if showSubseconds {
|
||||
let hundredths = Int((time.truncatingRemainder(dividingBy: 1)) * 100)
|
||||
let decimalSeparator = Locale.current.decimalSeparator ?? "."
|
||||
return String(format: "%@%@%0.2d", formattedString, decimalSeparator, hundredths)
|
||||
}
|
||||
|
||||
return formattedString
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ElapsedTimeView()
|
||||
}
|
||||
Reference in New Issue
Block a user