New app icon and recipe sharing options (pdf and plain text)

This commit is contained in:
VincentMeilinger
2024-01-10 17:52:34 +01:00
parent 3bcf104a5d
commit d180b96ed8
22 changed files with 288 additions and 7 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 126 KiB

After

Width:  |  Height:  |  Size: 140 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.4 KiB

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.6 KiB

After

Width:  |  Height:  |  Size: 9.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.6 KiB

After

Width:  |  Height:  |  Size: 9.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.1 KiB

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.8 KiB

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.9 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View File

@@ -2014,6 +2014,9 @@
}
}
}
},
"PDF Document" : {
},
"Please check the entered URL." : {
"localizations" : {
@@ -2124,6 +2127,9 @@
}
}
}
},
"Recipe" : {
},
"Recipes" : {
"localizations" : {
@@ -2236,6 +2242,7 @@
}
},
"Search recipes" : {
"extractionState" : "stale",
"localizations" : {
"de" : {
"stringUnit" : {
@@ -2366,6 +2373,15 @@
}
}
}
},
"Share as PDF" : {
},
"Share as text" : {
},
"Share recipe" : {
},
"Show help" : {
"localizations" : {

View File

@@ -0,0 +1,156 @@
//
// RecipeToPDF.swift
// Nextcloud Cookbook iOS Client
//
// Created by Vincent Meilinger on 08.01.24.
//
import Foundation
import TPPDF
import SwiftUI
class RecipeExporter {
func createPDF(recipe: RecipeDetail, image: UIImage?) -> URL? {
let document = PDFDocument(format: .a4)
let titleStyle = PDFTextStyle(name: "title", font: UIFont.boldSystemFont(ofSize: 18), color: .black)
let headerStyle = PDFTextStyle(name: "header", font: UIFont.boldSystemFont(ofSize: 16), color: .darkGray)
let textStyle = PDFTextStyle(name: "text", font: UIFont.systemFont(ofSize: 14), color: .black)
let titleSection = PDFSection(columnWidths: [0.5, 0.5])
if let image = image, let resizedImage = cropAndResizeImage(image: image, targetHeight: 150) {
let pdfImg = PDFImage(
image: resizedImage,
size: resizedImage.size,
options: [.rounded],
cornerRadius: 5
)
titleSection.columns[0].add(image: pdfImg)
}
// Title
titleSection.columns[1].add(textObject: PDFSimpleText(text: recipe.name, style: titleStyle))
// Description
if !recipe.description.isEmpty {
titleSection.columns[1].add(space: 10)
titleSection.columns[1].add(textObject: PDFSimpleText(text: recipe.description, style: textStyle))
}
// Time
if let prepTime = recipe.prepTime, let prepTimeString = DurationComponents.ptToText(prepTime) {
let prepString = "Preparation time: \(prepTimeString)"
titleSection.columns[1].add(space: 10)
titleSection.columns[1].add(textObject: PDFSimpleText(text: prepString, style: textStyle))
}
if let cookTime = recipe.cookTime, let cookTimeString = DurationComponents.ptToText(cookTime) {
let cookString = "Cooking time: \(cookTimeString)"
titleSection.columns[1].add(space: 10)
titleSection.columns[1].add(textObject: PDFSimpleText(text: cookString, style: textStyle))
}
document.add(section: titleSection)
// Ingredients
var ingr = ""
for ingredient in recipe.recipeIngredient {
ingr.append("\(ingredient)\n")
}
let section = PDFSection(columnWidths: [0.5, 0.5])
section.columns[0].add(textObject: PDFSimpleText(text: ingr, style: textStyle))
document.add(space: 20)
document.add(section: section)
// Instructions
var instr = ""
for instruction in recipe.recipeInstructions {
instr += instruction + "\n\n"
}
document.add(space: 10)
document.add(textObject: PDFSimpleText(text: instr, style: textStyle))
// Generate PDF
let generator = PDFGenerator(document: document)
do {
return try generator.generateURL(filename: "\(recipe.name).pdf")
} catch {
return nil
}
}
func createText(recipe: RecipeDetail) -> String {
var recipeString = ""
recipeString.append("" + recipe.name + "\n")
recipeString.append(recipe.description + "\n\n")
for ingredient in recipe.recipeIngredient {
recipeString.append("" + ingredient + "\n")
}
recipeString.append("\n")
var counter = 1
for instruction in recipe.recipeInstructions {
recipeString.append("\(counter). " + instruction + "\n")
counter += 1
}
return recipeString
}
func createJson(recipe: RecipeDetail) -> Data? {
return JSONEncoder.safeEncode(recipe)
}
}
private extension RecipeExporter {
func resizeImage(image: UIImage, targetHeight: CGFloat) -> UIImage? {
let size = image.size
let heightRatio = targetHeight / size.height
let newSize = CGSize(width: size.width * heightRatio, height: targetHeight)
let renderer = UIGraphicsImageRenderer(size: newSize)
let resizedImage = renderer.image { (context) in
image.draw(in: CGRect(origin: .zero, size: newSize))
}
return resizedImage
}
func cropAndResizeImage(image: UIImage, targetHeight: CGFloat) -> UIImage? {
let originalSize = image.size
let targetAspectRatio = 4.0 / 3.0
var cropRect: CGRect
// Calculate the rect to crop to 4:3
if originalSize.width / originalSize.height > targetAspectRatio {
// Image is wider than 4:3, crop width
let croppedWidth = originalSize.height * targetAspectRatio
let cropX = (originalSize.width - croppedWidth) / 2.0
cropRect = CGRect(x: cropX, y: 0, width: croppedWidth, height: originalSize.height)
} else {
// Image is narrower than 4:3, crop height
let croppedHeight = originalSize.width / targetAspectRatio
let cropY = (originalSize.height - croppedHeight) / 2.0
cropRect = CGRect(x: 0, y: cropY, width: originalSize.width, height: croppedHeight)
}
// Crop the image
guard let croppedCGImage = image.cgImage?.cropping(to: cropRect) else { return nil }
let croppedImage = UIImage(cgImage: croppedCGImage)
// Resize the cropped image
let resizeRatio = targetHeight / croppedImage.size.height
let resizedSize = CGSize(width: croppedImage.size.width * resizeRatio, height: targetHeight)
let renderer = UIGraphicsImageRenderer(size: resizedSize)
let resizedImage = renderer.image { (context) in
croppedImage.draw(in: CGRect(origin: .zero, size: resizedSize))
}
return resizedImage
}
}

View File

@@ -166,6 +166,7 @@ struct MainView: View {
Text("Refresh all")
Image(systemName: "icloud.and.arrow.down")
}
} label: {
Image(systemName: "ellipsis.circle")
}
@@ -235,7 +236,7 @@ struct RecipeSearchView: View {
.navigationDestination(for: Recipe.self) { recipe in
RecipeDetailView(viewModel: viewModel, recipe: recipe)
}
.searchable(text: $searchText, prompt: "Search recipes")
.searchable(text: $searchText, prompt: "Search recipes/keywords")
}
.navigationTitle("Search recipe")
}
@@ -247,7 +248,9 @@ struct RecipeSearchView: View {
func recipesFiltered() -> [Recipe] {
guard searchText != "" else { return allRecipes }
return allRecipes.filter { recipe in
recipe.name.lowercased().contains(searchText.lowercased())
recipe.name.lowercased().contains(searchText.lowercased()) || // check name for occurence of search term
(recipe.keywords != nil && recipe.keywords!.lowercased().contains(searchText.lowercased())) // check keywords for search term
}
}
}

View File

@@ -19,6 +19,8 @@ struct RecipeDetailView: View {
@State private var presentEditView: Bool = false
@State private var presentNutritionPopover: Bool = false
@State private var presentKeywordPopover: Bool = false
@State private var presentShareSheet: Bool = false
@State private var sharedURL: URL? = nil
var body: some View {
ScrollView(showsIndicators: false) {
@@ -76,6 +78,7 @@ struct RecipeDetailView: View {
RecipeKeywordSection(recipeDetail: recipeDetail)
MoreInformationSection(recipeDetail: recipeDetail)
}
}.padding(.horizontal, 5)
}
@@ -85,12 +88,25 @@ struct RecipeDetailView: View {
.navigationTitle(showTitle ? recipe.name : "")
.toolbar {
if recipeDetail != nil {
Button {
presentEditView = true
} label: {
HStack {
Text("Edit")
Menu {
Button {
presentEditView = true
} label: {
HStack {
Text("Edit")
Image(systemName: "pencil")
}
}
Button {
print("Sharing recipe ...")
self.presentShareSheet = true
} label: {
Text("Share recipe")
Image(systemName: "square.and.arrow.up")
}
} label: {
Image(systemName: "ellipsis.circle")
}
}
}
@@ -107,6 +123,14 @@ struct RecipeDetailView: View {
)
}
}
.sheet(isPresented: $presentShareSheet) {
if let recipeDetail = recipeDetail {
ShareView(recipeDetail: recipeDetail,
recipeImage: recipeImage,
presentShareSheet: $presentShareSheet)
}
}
.task {
recipeDetail = await viewModel.getRecipe(
id: recipe.recipe_id,
@@ -136,6 +160,50 @@ struct RecipeDetailView: View {
}
}
fileprivate struct ShareView: View {
@State var recipeDetail: RecipeDetail
@State var recipeImage: UIImage?
@Binding var presentShareSheet: Bool
@State var exporter = RecipeExporter()
@State var sharedURL: URL? = nil
var body: some View {
VStack(alignment: .leading) {
if let url = sharedURL {
ShareLink(item: url, subject: Text("PDF Document")) {
Image(systemName: "doc")
Text("Share as PDF")
}
.foregroundStyle(.primary)
.bold()
.padding()
}
ShareLink(item: exporter.createText(recipe: recipeDetail), subject: Text("Recipe")) {
Image(systemName: "ellipsis.message")
Text("Share as text")
}
.foregroundStyle(.primary)
.bold()
.padding()
/*ShareLink(item: exporter.createJson(recipe: recipeDetail), subject: Text("Recipe")) {
Image(systemName: "doc.badge.gearshape")
Text("Share as JSON")
}
.foregroundStyle(.primary)
.bold()
.padding()
*/
}
.task {
self.sharedURL = exporter.createPDF(recipe: recipeDetail, image: recipeImage)
}
}
}
fileprivate struct RecipeDurationSection: View {
@State var recipeDetail: RecipeDetail