From 1d3b936e993f269392a9f7c0469fccdd1e91a0ee Mon Sep 17 00:00:00 2001
From: Vicnet <35202538+VincentMeilinger@users.noreply.github.com>
Date: Thu, 9 Nov 2023 15:44:25 +0100
Subject: [PATCH] Added basic recipe web import functionality to the
RecipeEditView
---
.../xcschemes/xcschememanagement.plist | 2 +-
.../Localizable.xcstrings | 12 ++++++
.../RecipeImport/RecipeScraper.swift | 3 +-
.../Views/RecipeEditView.swift | 41 +++++++++++++++----
4 files changed, 48 insertions(+), 10 deletions(-)
diff --git a/Nextcloud Cookbook iOS Client.xcodeproj/xcuserdata/vincentmeilinger.xcuserdatad/xcschemes/xcschememanagement.plist b/Nextcloud Cookbook iOS Client.xcodeproj/xcuserdata/vincentmeilinger.xcuserdatad/xcschemes/xcschememanagement.plist
index 3091979..16fa277 100644
--- a/Nextcloud Cookbook iOS Client.xcodeproj/xcuserdata/vincentmeilinger.xcuserdatad/xcschemes/xcschememanagement.plist
+++ b/Nextcloud Cookbook iOS Client.xcodeproj/xcuserdata/vincentmeilinger.xcuserdatad/xcschemes/xcschememanagement.plist
@@ -28,7 +28,7 @@
Nextcloud Cookbook iOS Client.xcscheme_^#shared#^_
orderHint
- 1
+ 0
Scraper (Playground) 1.xcscheme
diff --git a/Nextcloud Cookbook iOS Client/Localizable.xcstrings b/Nextcloud Cookbook iOS Client/Localizable.xcstrings
index c17360a..378573c 100644
--- a/Nextcloud Cookbook iOS Client/Localizable.xcstrings
+++ b/Nextcloud Cookbook iOS Client/Localizable.xcstrings
@@ -858,6 +858,12 @@
}
}
}
+ },
+ "Import Recipe" : {
+
+ },
+ "Import recipe from a website" : {
+
},
"Ingredients" : {
"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." : {
"localizations" : {
@@ -1848,6 +1857,9 @@
}
}
}
+ },
+ "URL (e.g. example.com/recipe)" : {
+
},
"Validate" : {
"localizations" : {
diff --git a/Nextcloud Cookbook iOS Client/RecipeImport/RecipeScraper.swift b/Nextcloud Cookbook iOS Client/RecipeImport/RecipeScraper.swift
index f30fcb4..87edaa4 100644
--- a/Nextcloud Cookbook iOS Client/RecipeImport/RecipeScraper.swift
+++ b/Nextcloud Cookbook iOS Client/RecipeImport/RecipeScraper.swift
@@ -20,11 +20,12 @@ class RecipeScraper {
} else {
print("ERROR: Bad url.")
+ return nil
}
guard let html = contents else {
print("ERROR: no contents")
- exit(1)
+ return nil
}
let doc = try SwiftSoup.parse(html)
diff --git a/Nextcloud Cookbook iOS Client/Views/RecipeEditView.swift b/Nextcloud Cookbook iOS Client/Views/RecipeEditView.swift
index db00f71..72f255a 100644
--- a/Nextcloud Cookbook iOS Client/Views/RecipeEditView.swift
+++ b/Nextcloud Cookbook iOS Client/Views/RecipeEditView.swift
@@ -18,7 +18,6 @@ struct RecipeEditView: View {
@Binding var isPresented: Bool
@State var uploadNew: Bool = true
- @State private var image: PhotosPickerItem? = nil
@StateObject private var prepDuration: Duration = Duration()
@StateObject private var cookDuration: Duration = Duration()
@StateObject private var totalDuration: Duration = Duration()
@@ -26,6 +25,8 @@ struct RecipeEditView: View {
@State private var keywords: [String] = []
@State private var keywordSuggestions: [String] = []
+ @State private var importURL: String = ""
+ @State private var showImportSection: Bool = false
@State private var waitingForUpload: Bool = false
var body: some View {
@@ -77,19 +78,43 @@ struct RecipeEditView: View {
Spacer()
}
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)
Section {
TextEditor(text: $recipe.description)
} header: {
Text("Description")
}
- /*
- PhotosPicker(selection: $image, matching: .images, photoLibrary: .shared()) {
- Image(systemName: "photo")
- .symbolRenderingMode(.multicolor)
- }
- .buttonStyle(.borderless)
- */
+
Section() {
NavigationLink(recipe.recipeCategory == "" ? "Category" : "Category: \(recipe.recipeCategory)") {
CategoryPickerView(