Improved date handling (DurationComponents)

This commit is contained in:
Vicnet
2023-11-11 11:11:08 +01:00
parent 1598d24b00
commit 13b025771c
7 changed files with 150 additions and 135 deletions

View File

@@ -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()
}

View File

@@ -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"
}
}