Improved date handling (DurationComponents)
This commit is contained in:
@@ -91,6 +91,16 @@ struct RecipeDetail: Codable {
|
||||
recipeInstructions = []
|
||||
nutrition = [:]
|
||||
}
|
||||
|
||||
func getKeywordsArray() -> [String] {
|
||||
return keywords.components(separatedBy: ",")
|
||||
}
|
||||
|
||||
mutating func setKeywordsFromArray(_ keywordsArray: [String]) {
|
||||
if !self.keywords.isEmpty {
|
||||
self.keywords = keywordsArray.joined(separator: ",")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension RecipeDetail {
|
||||
|
||||
99
Nextcloud Cookbook iOS Client/Data/DurationComponents.swift
Normal file
99
Nextcloud Cookbook iOS Client/Data/DurationComponents.swift
Normal file
@@ -0,0 +1,99 @@
|
||||
//
|
||||
// Duration.swift
|
||||
// Nextcloud Cookbook iOS Client
|
||||
//
|
||||
// Created by Vincent Meilinger on 11.11.23.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
|
||||
class DurationComponents: ObservableObject {
|
||||
@Published var minuteComponent: String = "00" {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fromPTString(_ PTRepresentation: String) {
|
||||
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)
|
||||
}
|
||||
if let match = PTRepresentation.firstMatch(of: minuteRegex) {
|
||||
self.minuteComponent = String(match.1)
|
||||
}
|
||||
}
|
||||
|
||||
func toPTString() -> String {
|
||||
return "PT\(hourComponent)H\(minuteComponent)M00S"
|
||||
}
|
||||
|
||||
func toText() -> LocalizedStringKey {
|
||||
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"
|
||||
} else {
|
||||
return "-"
|
||||
}
|
||||
}
|
||||
|
||||
static func ptToText(_ ptString: String) -> String {
|
||||
let hourRegex = /([0-9]{1,2})H/
|
||||
let minuteRegex = /([0-9]{1,2})M/
|
||||
|
||||
var intHour = 0
|
||||
var intMinute = 0
|
||||
if let match = ptString.firstMatch(of: hourRegex) {
|
||||
let hourComponent = String(match.1)
|
||||
intHour = Int(hourComponent) ?? 0
|
||||
}
|
||||
if let match = ptString.firstMatch(of: minuteRegex) {
|
||||
let minuteComponent = String(match.1)
|
||||
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"
|
||||
} else {
|
||||
return "-"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
//
|
||||
// DateFormatterExtension.swift
|
||||
// Nextcloud Cookbook iOS Client
|
||||
//
|
||||
// Created by Vincent Meilinger on 14.09.23.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension Formatter {
|
||||
static let positional: DateComponentsFormatter = {
|
||||
let formatter = DateComponentsFormatter()
|
||||
formatter.unitsStyle = .positional
|
||||
return formatter
|
||||
}()
|
||||
|
||||
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 "--"
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,9 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"-" : {
|
||||
|
||||
},
|
||||
":" : {
|
||||
"localizations" : {
|
||||
@@ -88,6 +91,22 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"%lld h" : {
|
||||
|
||||
},
|
||||
"%lld h, %lld min" : {
|
||||
"localizations" : {
|
||||
"en" : {
|
||||
"stringUnit" : {
|
||||
"state" : "new",
|
||||
"value" : "%1$lld h, %2$lld min"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"%lld min" : {
|
||||
|
||||
},
|
||||
"%lld." : {
|
||||
"localizations" : {
|
||||
|
||||
@@ -119,7 +119,7 @@ fileprivate struct RecipeDurationSection: View {
|
||||
if let prepTime = recipeDetail.prepTime {
|
||||
VStack(alignment: .leading) {
|
||||
SecondaryLabel(text: LocalizedStringKey("Preparation"))
|
||||
Text(DateFormatter.formatDate(duration: prepTime))
|
||||
Text(DurationComponents.ptToText(prepTime))
|
||||
.lineLimit(1)
|
||||
}.padding()
|
||||
}
|
||||
@@ -127,7 +127,7 @@ fileprivate struct RecipeDurationSection: View {
|
||||
if let cookTime = recipeDetail.cookTime {
|
||||
VStack(alignment: .leading) {
|
||||
SecondaryLabel(text: LocalizedStringKey("Cooking"))
|
||||
Text(DateFormatter.formatDate(duration: cookTime))
|
||||
Text(DurationComponents.ptToText(cookTime))
|
||||
.lineLimit(1)
|
||||
}.padding()
|
||||
}
|
||||
@@ -135,7 +135,7 @@ fileprivate struct RecipeDurationSection: View {
|
||||
if let totalTime = recipeDetail.totalTime {
|
||||
VStack(alignment: .leading) {
|
||||
SecondaryLabel(text: LocalizedStringKey("Total time"))
|
||||
Text(DateFormatter.formatDate(duration: totalTime))
|
||||
Text(DurationComponents.ptToText(totalTime))
|
||||
.lineLimit(1)
|
||||
}.padding()
|
||||
}
|
||||
|
||||
@@ -13,11 +13,7 @@ import PhotosUI
|
||||
|
||||
struct RecipeEditView: View {
|
||||
@ObservedObject var viewModel: MainViewModel
|
||||
@State var recipe: RecipeDetail = RecipeDetail() {
|
||||
didSet {
|
||||
prepareView()
|
||||
}
|
||||
}
|
||||
@State var recipe: RecipeDetail = RecipeDetail()
|
||||
@Binding var isPresented: Bool
|
||||
@State var uploadNew: Bool = true
|
||||
|
||||
@@ -25,9 +21,9 @@ struct RecipeEditView: View {
|
||||
@State private var alertType: UserAlert = RecipeCreationError.GENERIC
|
||||
@State private var alertAction: () -> () = {}
|
||||
|
||||
@StateObject private var prepDuration: Duration = Duration()
|
||||
@StateObject private var cookDuration: Duration = Duration()
|
||||
@StateObject private var totalDuration: Duration = Duration()
|
||||
@StateObject private var prepDuration: DurationComponents = DurationComponents()
|
||||
@StateObject private var cookDuration: DurationComponents = DurationComponents()
|
||||
@StateObject private var totalDuration: DurationComponents = DurationComponents()
|
||||
@State private var searchText: String = ""
|
||||
@State private var keywords: [String] = []
|
||||
@State private var keywordSuggestions: [String] = []
|
||||
@@ -94,6 +90,7 @@ struct RecipeEditView: View {
|
||||
let (scrapedRecipe, error) = try await RecipeScraper().scrape(url: importURL)
|
||||
if let scrapedRecipe = scrapedRecipe {
|
||||
self.recipe = scrapedRecipe
|
||||
prepareView()
|
||||
}
|
||||
if let error = error {
|
||||
self.alertType = error
|
||||
@@ -200,13 +197,10 @@ struct RecipeEditView: View {
|
||||
}
|
||||
|
||||
func createRecipe() {
|
||||
self.recipe.prepTime = prepDuration.format()
|
||||
self.recipe.cookTime = cookDuration.format()
|
||||
self.recipe.totalTime = totalDuration.format()
|
||||
|
||||
if !self.keywords.isEmpty {
|
||||
self.recipe.keywords = self.keywords.joined(separator: ",")
|
||||
}
|
||||
self.recipe.prepTime = prepDuration.toPTString()
|
||||
self.recipe.cookTime = cookDuration.toPTString()
|
||||
self.recipe.totalTime = totalDuration.toPTString()
|
||||
self.recipe.setKeywordsFromArray(keywords)
|
||||
}
|
||||
|
||||
func recipeValid() -> Bool {
|
||||
@@ -316,19 +310,16 @@ struct RecipeEditView: View {
|
||||
}
|
||||
|
||||
func prepareView() {
|
||||
if uploadNew { return }
|
||||
if let prepTime = recipe.prepTime {
|
||||
prepDuration.initFromPT(prepTime)
|
||||
prepDuration.fromPTString(prepTime)
|
||||
}
|
||||
if let cookTime = recipe.cookTime {
|
||||
cookDuration.initFromPT(cookTime)
|
||||
cookDuration.fromPTString(cookTime)
|
||||
}
|
||||
if let totalTime = recipe.totalTime {
|
||||
totalDuration.initFromPT(totalTime)
|
||||
}
|
||||
for keyword in self.recipe.keywords.components(separatedBy: ",") {
|
||||
keywords.append(keyword)
|
||||
totalDuration.fromPTString(totalTime)
|
||||
}
|
||||
self.keywords = recipe.getKeywordsArray()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -381,7 +372,7 @@ fileprivate struct EditableListSection: View {
|
||||
|
||||
fileprivate struct DurationPicker: View {
|
||||
@State var title: LocalizedStringKey
|
||||
@ObservedObject var duration: Duration
|
||||
@ObservedObject var duration: DurationComponents
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
@@ -405,54 +396,7 @@ fileprivate struct DurationPicker: View {
|
||||
|
||||
|
||||
|
||||
fileprivate class Duration: ObservableObject {
|
||||
@Published var minuteComponent: String = "00" {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func initFromPT(_ PTRepresentation: String) {
|
||||
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)
|
||||
}
|
||||
if let match = PTRepresentation.firstMatch(of: minuteRegex) {
|
||||
self.minuteComponent = String(match.1)
|
||||
}
|
||||
}
|
||||
|
||||
func format() -> String {
|
||||
return "PT\(hourComponent)H\(minuteComponent)M00S"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user