Skip to content

Commit

Permalink
TimecodeField: Refactored timecode paste handler to respect `timeco…
Browse files Browse the repository at this point in the history
…deFieldValidationPolicy`
  • Loading branch information
orchetect committed Oct 17, 2024
1 parent bb1daab commit bb60448
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions Sources/TimecodeKitUI/SwiftUI/TimecodeField/TimecodeField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public struct TimecodeField: View {
@Environment(\.timecodeFieldHighlightStyle) private var timecodeHighlightStyle
@Environment(\.timecodeSubFramesStyle) private var timecodeSubFramesStyle
@Environment(\.timecodeValidationStyle) private var timecodeValidationStyle
@Environment(\.timecodeFieldValidationPolicy) private var timecodeFieldValidationPolicy
@Environment(\.timecodeFieldValidationAnimation) private var timecodeFieldValidationAnimation

// MARK: - Internal view modifiers
Expand Down Expand Up @@ -198,11 +199,7 @@ public struct TimecodeField: View {

// handle user-initiated paste event locally or propagated up from a child view
.onPasteCommandOfTimecode(propertiesForString: timecodeProperties) { pasteResult in
do {
timecode = try pasteResult.get()
} catch {
errorFeedback()
}
handle(pasteResult: pasteResult)
}

// update focus if view is disabled
Expand Down Expand Up @@ -325,6 +322,27 @@ public struct TimecodeField: View {
Timecode.Properties(rate: frameRate, base: subFramesBase, limit: upperLimit)
}

private func handle(pasteResult: Result<Timecode, any Error>) {
do {
let pastedTimecode = try pasteResult.get()

// validate if necessary before accepting the pasted timecode
switch timecodeFieldValidationPolicy {
case .allowInvalid:
timecode = pastedTimecode
case .enforceValid:
guard timecode.isValid else {
errorFeedback()
return
}
// TODO: handle additional case when configured to only paste values and not mutate timecode properties
timecode = pastedTimecode
}
} catch {
errorFeedback()
}
}

// MARK: - Sync Bindings

private func timecodeBinding() -> Binding<Timecode> {
Expand Down

0 comments on commit bb60448

Please sign in to comment.