Recipe creation, editing and deletion are now supported

This commit is contained in:
Vicnet
2023-10-04 11:23:05 +02:00
parent 77c07bb0b1
commit 85a8e631d0
14 changed files with 453 additions and 146 deletions

View File

@@ -11,11 +11,30 @@ extension Date {
static var zero: Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
if let date = dateFormatter.date(from:"00:00") {
return date
} else {
return Date()
}
}
static func toPTRepresentation(date: Date) -> String? {
// PT0H18M0S
let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: date)
if let hour = dateComponents.hour, let minute = dateComponents.minute {
return "PT\(hour)H\(minute)M0S"
}
return nil
}
static func fromPTRepresentation(_ representation: String) -> Date {
let (hour, minute) = DateFormatter.stringToComponents(duration: representation)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
if let date = dateFormatter.date(from:"\(hour):\(minute)") {
return date
} else {
return Date()
}
}
}

View File

@@ -13,29 +13,45 @@ extension Formatter {
formatter.unitsStyle = .positional
return formatter
}()
}
func formatDate(duration: String) -> String {
var duration = duration
if duration.hasPrefix("PT") { duration.removeFirst(2) }
var hour: Int = 0, minute: Int = 0
if let index = duration.firstIndex(of: "H") {
hour = Int(duration[..<index]) ?? 0
duration.removeSubrange(...index)
}
if let index = duration.firstIndex(of: "M") {
minute = Int(duration[..<index]) ?? 0
duration.removeSubrange(...index)
}
if hour == 0 && minute != 0 {
return "\(minute)min"
static func formatDate(duration: String) -> String {
var duration = duration
if duration.hasPrefix("PT") { duration.removeFirst(2) }
var hour: Int = 0, minute: Int = 0
if let index = duration.firstIndex(of: "H") {
hour = Int(duration[..<index]) ?? 0
duration.removeSubrange(...index)
}
if let index = duration.firstIndex(of: "M") {
minute = Int(duration[..<index]) ?? 0
duration.removeSubrange(...index)
}
if hour == 0 && minute != 0 {
return "\(minute)min"
}
if hour != 0 && minute == 0 {
return "\(hour)h"
}
if hour != 0 && minute != 0 {
return "\(hour)h \(minute)"
}
return "--"
}
if hour != 0 && minute == 0 {
return "\(hour)h"
static func stringToComponents(duration: String) -> (Int, Int) {
var duration = duration
if duration.hasPrefix("PT") { duration.removeFirst(2) }
var hour: Int = 0, minute: Int = 0
if let index = duration.firstIndex(of: "H") {
hour = Int(duration[..<index]) ?? 0
duration.removeSubrange(...index)
}
if let index = duration.firstIndex(of: "M") {
minute = Int(duration[..<index]) ?? 0
duration.removeSubrange(...index)
}
return (hour, minute)
}
if hour != 0 && minute != 0 {
return "\(hour)h \(minute)"
}
return "--"
}