Skip to content

Commit 08b250f

Browse files
committed
TimecodeField.validate(): Refactored to return Optional instead of enum case
1 parent 8aa8a1d commit 08b250f

2 files changed

Lines changed: 21 additions & 24 deletions

File tree

Sources/TimecodeKitUI/SwiftUI/TimecodeField/TimecodeField Paste Policy.swift

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,20 @@ extension TimecodeField {
2525
/// - validationPolicy: Validation policy injected from the SwiftUI environment.
2626
/// - inputStyle: Input style injected from the SwiftUI environment.
2727
///
28-
/// - Returns: Paste result determining whether the input timecode passes all of the validation conditions and the
28+
/// - Returns: New timecode instance if the input timecode passes all of the validation conditions and the
2929
/// receiver should accept the new timecode.
3030
/// Note that the timecode returned in this result may have properties that are different from the input timecode.
31-
/// For this reason, when accepting the pasted timecode after this method returns an
32-
/// ``PasteValidationResult/allowed(_:)`` case, you should accept the timecode contained in this case and not the
33-
/// timecode input into this method.
31+
/// For this reason, you should accept the timecode returned from this method and not the timecode input into this
32+
/// method.
3433
static func validate(
3534
pasteResult: Result<Timecode, any Error>,
3635
localTimecodeProperties: Timecode.Properties,
3736
pastePolicy: PastePolicy,
3837
validationPolicy: ValidationPolicy,
3938
inputStyle: InputStyle
40-
) -> PasteValidationResult {
39+
) -> Timecode? {
4140
guard let pastedTimecode = try? pasteResult.get() else {
42-
return .rejected(.pasteRejected)
41+
return nil
4342
}
4443

4544
return validate(
@@ -71,27 +70,26 @@ extension TimecodeField {
7170
/// - validationPolicy: Validation policy. The default is the safest and most common option.
7271
/// - inputStyle: Input style, if applicable. The default is the safest and most common option.
7372
///
74-
/// - Returns: Paste result determining whether the input timecode passes all of the validation conditions and the
73+
/// - Returns: New timecode instance if the input timecode passes all of the validation conditions and the
7574
/// receiver should accept the new timecode.
7675
/// Note that the timecode returned in this result may have properties that are different from the input timecode.
77-
/// For this reason, when accepting the pasted timecode after this method returns an
78-
/// ``PasteValidationResult/allowed(_:)`` case, you should accept the timecode contained in this case and not the
79-
/// timecode input into this method.
76+
/// For this reason, you should accept the timecode returned from this method and not the timecode input into this
77+
/// method.
8078
public static func validate(
8179
pastedTimecode: Timecode,
8280
localTimecodeProperties: Timecode.Properties,
8381
pastePolicy: PastePolicy = .preserveLocalProperties,
8482
validationPolicy: ValidationPolicy = .enforceValid,
8583
inputStyle: InputStyle = .autoAdvance
86-
) -> PasteValidationResult {
84+
) -> Timecode? {
8785
var pastedTimecode = pastedTimecode
8886

8987
switch pastePolicy {
9088
case .preserveLocalProperties:
9189
// ensure that the newly pasted timecode is compatible with local properties.
9290
guard pastedTimecode.frameRate == localTimecodeProperties.frameRate
9391
else {
94-
return .rejected(.pasteRejected)
92+
return nil
9593
}
9694

9795
switch validationPolicy {
@@ -103,7 +101,7 @@ extension TimecodeField {
103101
// ensure other properties (subframes base, upper limit) are compatible
104102
guard let transplantedTimecode = try? pastedTimecode.setting(.components(pastedTimecode.components))
105103
else {
106-
return .rejected(.pasteRejected)
104+
return nil
107105
}
108106
pastedTimecode = transplantedTimecode
109107
}
@@ -135,14 +133,14 @@ extension TimecodeField {
135133
pastePolicy: PastePolicy,
136134
validationPolicy: ValidationPolicy,
137135
inputStyle: InputStyle
138-
) -> PasteValidationResult {
136+
) -> Timecode? {
139137
// validate against validation policy
140138
switch validationPolicy {
141139
case .allowInvalid:
142140
break
143141
case .enforceValid:
144142
guard pastedTimecode.isValid else {
145-
return .rejected(.pasteRejected)
143+
return nil
146144
}
147145
}
148146

@@ -165,13 +163,13 @@ extension TimecodeField {
165163
base: timecodeProperties.subFramesBase
166164
)
167165
else {
168-
return .rejected(.pasteRejected)
166+
return nil
169167
}
170168
case .unbounded:
171169
break
172170
}
173171

174-
return .allowed(pastedTimecode)
172+
return pastedTimecode
175173
}
176174

177175
public enum PasteValidationResult: Equatable, Hashable, Sendable {

Sources/TimecodeKitUI/SwiftUI/TimecodeField/TimecodeField.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,20 +376,19 @@ public struct TimecodeField: View, RejectedInputFeedbackable {
376376
}
377377

378378
private func handle(pasteResult: Result<Timecode, any Error>) {
379-
let result = TimecodeField.validate(
379+
guard let newTimecode = TimecodeField.validate(
380380
pasteResult: pasteResult,
381381
localTimecodeProperties: timecodeProperties,
382382
pastePolicy: timecodeFieldPastePolicy,
383383
validationPolicy: timecodeFieldValidationPolicy,
384384
inputStyle: timecodeFieldInputStyle
385385
)
386-
387-
switch result {
388-
case .allowed(let newTimecode):
389-
timecode = newTimecode
390-
case .rejected(let action):
391-
inputRejectionFeedback(action)
386+
else {
387+
inputRejectionFeedback(.pasteRejected)
388+
return
392389
}
390+
391+
timecode = newTimecode
393392
}
394393

395394
// MARK: - Sync Bindings

0 commit comments

Comments
 (0)