Creating and updating recipes works in the new edit view
This commit is contained in:
@@ -10,64 +10,42 @@ import SwiftUI
|
||||
|
||||
|
||||
class DurationComponents: ObservableObject {
|
||||
@Published var secondComponent: String = "00" {
|
||||
@Published var secondComponent: Int = 0 {
|
||||
didSet {
|
||||
if secondComponent.count > 2 {
|
||||
secondComponent = oldValue
|
||||
} else if secondComponent.count == 1 {
|
||||
secondComponent = "0\(secondComponent)"
|
||||
} else if secondComponent.count == 0 {
|
||||
secondComponent = "00"
|
||||
if secondComponent > 59 {
|
||||
secondComponent = 59
|
||||
} else if secondComponent < 0 {
|
||||
secondComponent = 0
|
||||
}
|
||||
let filtered = secondComponent.filter { $0.isNumber }
|
||||
if secondComponent != filtered {
|
||||
secondComponent = filtered
|
||||
}
|
||||
}
|
||||
@Published var minuteComponent: Int = 0 {
|
||||
didSet {
|
||||
if minuteComponent > 59 {
|
||||
minuteComponent = 59
|
||||
} else if minuteComponent < 0 {
|
||||
minuteComponent = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Published var minuteComponent: String = "00" {
|
||||
@Published var hourComponent: Int = 0 {
|
||||
didSet {
|
||||
if minuteComponent.count > 2 {
|
||||
minuteComponent = oldValue
|
||||
} else if minuteComponent.count == 1 {
|
||||
minuteComponent = "0\(minuteComponent)"
|
||||
} else if minuteComponent.count == 0 {
|
||||
minuteComponent = "00"
|
||||
}
|
||||
let filtered = minuteComponent.filter { $0.isNumber }
|
||||
if minuteComponent != filtered {
|
||||
minuteComponent = filtered
|
||||
if hourComponent < 0 {
|
||||
hourComponent = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Published var hourComponent: String = "00" {
|
||||
didSet {
|
||||
if hourComponent.count > 2 {
|
||||
hourComponent = oldValue
|
||||
} else if hourComponent.count == 1 {
|
||||
hourComponent = "0\(hourComponent)"
|
||||
} else if hourComponent.count == 0 {
|
||||
hourComponent = "00"
|
||||
}
|
||||
let filtered = hourComponent.filter { $0.isNumber }
|
||||
if hourComponent != filtered {
|
||||
hourComponent = filtered
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var displayString: String {
|
||||
let intHour = Int(hourComponent) ?? 0
|
||||
let intMinute = Int(minuteComponent) ?? 0
|
||||
|
||||
if intHour != 0 && intMinute != 0 {
|
||||
return "\(intHour) h \(intMinute) min"
|
||||
} else if intHour == 0 && intMinute != 0 {
|
||||
return "\(intMinute) min"
|
||||
} else if intHour != 0 && intMinute == 0 {
|
||||
return "\(intHour) h"
|
||||
if hourComponent != 0 && minuteComponent != 0 {
|
||||
return "\(hourComponent) h \(minuteComponent) min"
|
||||
} else if hourComponent == 0 && minuteComponent != 0 {
|
||||
return "\(minuteComponent) min"
|
||||
} else if hourComponent != 0 && minuteComponent == 0 {
|
||||
return "\(hourComponent) h"
|
||||
} else {
|
||||
return "-"
|
||||
}
|
||||
@@ -78,10 +56,10 @@ class DurationComponents: ObservableObject {
|
||||
let hourRegex = /([0-9]{1,2})H/
|
||||
let minuteRegex = /([0-9]{1,2})M/
|
||||
if let match = PTRepresentation.firstMatch(of: hourRegex) {
|
||||
duration.hourComponent = String(match.1)
|
||||
duration.hourComponent = Int(match.1) ?? 0
|
||||
}
|
||||
if let match = PTRepresentation.firstMatch(of: minuteRegex) {
|
||||
duration.minuteComponent = String(match.1)
|
||||
duration.minuteComponent = Int(match.1) ?? 0
|
||||
}
|
||||
return duration
|
||||
}
|
||||
@@ -90,41 +68,47 @@ class DurationComponents: ObservableObject {
|
||||
let hourRegex = /([0-9]{1,2})H/
|
||||
let minuteRegex = /([0-9]{1,2})M/
|
||||
if let match = PTRepresentation.firstMatch(of: hourRegex) {
|
||||
self.hourComponent = String(match.1)
|
||||
self.hourComponent = Int(match.1) ?? 0
|
||||
}
|
||||
if let match = PTRepresentation.firstMatch(of: minuteRegex) {
|
||||
self.minuteComponent = String(match.1)
|
||||
self.minuteComponent = Int(match.1) ?? 0
|
||||
}
|
||||
}
|
||||
|
||||
private func stringFormatComponents() -> (String, String, String) {
|
||||
let sec = secondComponent < 10 ? "0\(secondComponent)" : "\(secondComponent)"
|
||||
let min = minuteComponent < 10 ? "0\(minuteComponent)" : "\(minuteComponent)"
|
||||
let hr = hourComponent < 10 ? "0\(hourComponent)" : "\(hourComponent)"
|
||||
return (hr, min, sec)
|
||||
}
|
||||
|
||||
func toPTString() -> String {
|
||||
return "PT\(hourComponent)H\(minuteComponent)M00S"
|
||||
let (hr, min, sec) = stringFormatComponents()
|
||||
return "PT\(hr)H\(min)M\(sec)S"
|
||||
}
|
||||
|
||||
func toTimerText() -> String {
|
||||
var timeString = ""
|
||||
if hourComponent != "00" {
|
||||
timeString.append("\(hourComponent):")
|
||||
let (hr, min, sec) = stringFormatComponents()
|
||||
if hourComponent != 0 {
|
||||
timeString.append("\(hr):")
|
||||
}
|
||||
timeString.append("\(minuteComponent):")
|
||||
timeString.append("\(secondComponent)")
|
||||
timeString.append("\(min):")
|
||||
timeString.append(sec)
|
||||
return timeString
|
||||
}
|
||||
|
||||
func toSeconds() -> Double {
|
||||
guard let hours = Double(hourComponent) else { return 0 }
|
||||
guard let minutes = Double(minuteComponent) else { return 0 }
|
||||
guard let seconds = Double(secondComponent) else { return 0 }
|
||||
return hours * 3600 + minutes * 60 + seconds
|
||||
return Double(hourComponent) * 3600 + Double(minuteComponent) * 60 + Double(secondComponent)
|
||||
}
|
||||
|
||||
func fromSeconds(_ totalSeconds: Int) {
|
||||
let hours = totalSeconds / 3600
|
||||
let minutes = (totalSeconds % 3600) / 60
|
||||
let seconds = totalSeconds % 60
|
||||
self.hourComponent = String(hours)
|
||||
self.minuteComponent = String(minutes)
|
||||
self.secondComponent = String(seconds)
|
||||
self.hourComponent = Int(hours)
|
||||
self.minuteComponent = Int(minutes)
|
||||
self.secondComponent = Int(seconds)
|
||||
}
|
||||
|
||||
static func ptToText(_ ptString: String) -> String? {
|
||||
|
||||
Reference in New Issue
Block a user