Added basic recipe web import functionality to the RecipeEditView

This commit is contained in:
Vicnet
2023-11-09 15:44:25 +01:00
parent 03f821c1ba
commit 1d3b936e99
4 changed files with 48 additions and 10 deletions

View File

@@ -28,7 +28,7 @@
<key>Nextcloud Cookbook iOS Client.xcscheme_^#shared#^_</key> <key>Nextcloud Cookbook iOS Client.xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>1</integer> <integer>0</integer>
</dict> </dict>
<key>Scraper (Playground) 1.xcscheme</key> <key>Scraper (Playground) 1.xcscheme</key>
<dict> <dict>

View File

@@ -858,6 +858,12 @@
} }
} }
} }
},
"Import Recipe" : {
},
"Import recipe from a website" : {
}, },
"Ingredients" : { "Ingredients" : {
"localizations" : { "localizations" : {
@@ -1320,6 +1326,9 @@
} }
} }
} }
},
"Paste the url of a recipe you would like to import in the above, and we will try to fill in the fields for you. This feature does not work with every website. If your favourite website is not supported, feel free to reach out for help. You can find the contact details in the app settings." : {
}, },
"Please check your credentials and internet connection." : { "Please check your credentials and internet connection." : {
"localizations" : { "localizations" : {
@@ -1848,6 +1857,9 @@
} }
} }
} }
},
"URL (e.g. example.com/recipe)" : {
}, },
"Validate" : { "Validate" : {
"localizations" : { "localizations" : {

View File

@@ -20,11 +20,12 @@ class RecipeScraper {
} else { } else {
print("ERROR: Bad url.") print("ERROR: Bad url.")
return nil
} }
guard let html = contents else { guard let html = contents else {
print("ERROR: no contents") print("ERROR: no contents")
exit(1) return nil
} }
let doc = try SwiftSoup.parse(html) let doc = try SwiftSoup.parse(html)

View File

@@ -18,7 +18,6 @@ struct RecipeEditView: View {
@Binding var isPresented: Bool @Binding var isPresented: Bool
@State var uploadNew: Bool = true @State var uploadNew: Bool = true
@State private var image: PhotosPickerItem? = nil
@StateObject private var prepDuration: Duration = Duration() @StateObject private var prepDuration: Duration = Duration()
@StateObject private var cookDuration: Duration = Duration() @StateObject private var cookDuration: Duration = Duration()
@StateObject private var totalDuration: Duration = Duration() @StateObject private var totalDuration: Duration = Duration()
@@ -26,6 +25,8 @@ struct RecipeEditView: View {
@State private var keywords: [String] = [] @State private var keywords: [String] = []
@State private var keywordSuggestions: [String] = [] @State private var keywordSuggestions: [String] = []
@State private var importURL: String = ""
@State private var showImportSection: Bool = false
@State private var waitingForUpload: Bool = false @State private var waitingForUpload: Bool = false
var body: some View { var body: some View {
@@ -77,19 +78,43 @@ struct RecipeEditView: View {
Spacer() Spacer()
} }
Form { Form {
if showImportSection {
Section {
TextField("URL (e.g. example.com/recipe)", text: $importURL)
.onSubmit {
do {
if let recipe = try RecipeScraper().scrape(url: importURL) {
self.recipe = recipe
}
} catch {
print("Error")
}
}
} header: {
Text("Import Recipe")
} footer: {
Text("Paste the url of a recipe you would like to import in the above, and we will try to fill in the fields for you. This feature does not work with every website. If your favourite website is not supported, feel free to reach out for help. You can find the contact details in the app settings.")
}
} else {
Section {
Button() {
withAnimation{
showImportSection = true
}
} label: {
Text("Import recipe from a website")
}
}
}
TextField("Title", text: $recipe.name) TextField("Title", text: $recipe.name)
Section { Section {
TextEditor(text: $recipe.description) TextEditor(text: $recipe.description)
} header: { } header: {
Text("Description") Text("Description")
} }
/*
PhotosPicker(selection: $image, matching: .images, photoLibrary: .shared()) {
Image(systemName: "photo")
.symbolRenderingMode(.multicolor)
}
.buttonStyle(.borderless)
*/
Section() { Section() {
NavigationLink(recipe.recipeCategory == "" ? "Category" : "Category: \(recipe.recipeCategory)") { NavigationLink(recipe.recipeCategory == "" ? "Category" : "Category: \(recipe.recipeCategory)") {
CategoryPickerView( CategoryPickerView(