Xaniasty

Untitled

Jun 12th, 2024
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct AddRecipeView: View {
  4. @State private var name = ""
  5. @State private var description = ""
  6. @State private var estimatedTime = ""
  7. @State private var imageName = ""
  8. @State private var image: UIImage? = nil
  9. @State private var showingImagePicker = false
  10.  
  11. @State private var showingSaveSuccessAlert = false
  12. @State private var saveSuccessMessage = ""
  13.  
  14. var body: some View {
  15. VStack {
  16. TextField("Nazwa", text: $name)
  17. .textFieldStyle(RoundedBorderTextFieldStyle())
  18. .padding()
  19.  
  20. TextField("Opis", text: $description)
  21. .textFieldStyle(RoundedBorderTextFieldStyle())
  22. .padding()
  23.  
  24. TextField("Czas przygotowania", text: $estimatedTime)
  25. .textFieldStyle(RoundedBorderTextFieldStyle())
  26. .padding()
  27.  
  28. Button(action: {
  29. showingImagePicker = true
  30. }) {
  31. Text("Dodaj zdjęcie")
  32. .font(.headline)
  33. .padding()
  34. .background(Color.blue)
  35. .foregroundColor(.white)
  36. .cornerRadius(10)
  37. }
  38. .padding()
  39. .sheet(isPresented: $showingImagePicker) {
  40. ImagePicker(image: $image)
  41. }
  42.  
  43. Button(action: {
  44. saveRecipe()
  45. }) {
  46. Text("Zapisz przepis")
  47. .font(.headline)
  48. .padding()
  49. .background(Color.green)
  50. .foregroundColor(.white)
  51. .cornerRadius(10)
  52. }
  53. .padding()
  54.  
  55. Spacer()
  56. }
  57. .padding()
  58. .navigationTitle("Dodaj przepis")
  59. .alert(isPresented: $showingSaveSuccessAlert) {
  60. Alert(title: Text("Informacja"), message: Text(saveSuccessMessage), dismissButton: .default(Text("OK")))
  61. }
  62. }
  63.  
  64. func saveRecipe() {
  65. if name.isEmpty || description.isEmpty || estimatedTime.isEmpty || imageName.isEmpty {
  66. saveSuccessMessage = "Wszystkie pola są wymagane, w tym zdjęcie"
  67. showingSaveSuccessAlert = true
  68. return
  69. }
  70.  
  71. guard let image = image else {
  72. saveSuccessMessage = "Zdjęcie jest wymagane"
  73. showingSaveSuccessAlert = true
  74. return
  75. }
  76.  
  77. if let data = image.jpegData(compressionQuality: 0.8) {
  78. if let photosURL = Bundle.main.url(forResource: "photos", withExtension: nil) {
  79. let filename = UUID().uuidString + ".jpg"
  80. let photoURL = photosURL.appendingPathComponent(filename)
  81.  
  82. do {
  83. try data.write(to: photoURL)
  84. imageName = filename // Update the imageName with the new filename
  85. } catch {
  86. print("Błąd zapisu zdjęcia: \(error)")
  87. saveSuccessMessage = "Błąd zapisu zdjęcia"
  88. showingSaveSuccessAlert = true
  89. return
  90. }
  91.  
  92. let newRecipe = Recipe(filename: filename, name: name, description: description, estimatedTime: estimatedTime)
  93. saveToJSON(recipe: newRecipe)
  94. } else {
  95. print("Folder photos nie istnieje")
  96. saveSuccessMessage = "Folder photos nie istnieje"
  97. showingSaveSuccessAlert = true
  98. }
  99. }
  100. }
  101.  
  102. func saveToJSON(recipe: Recipe) {
  103. guard let url = Bundle.main.url(forResource: "recipes", withExtension: "json") else {
  104. saveSuccessMessage = "Nie znaleziono pliku recipes.json"
  105. showingSaveSuccessAlert = true
  106. return
  107. }
  108.  
  109. do {
  110. let data = try Data(contentsOf: url)
  111. var recipes = try JSONDecoder().decode([Recipe].self, from: data)
  112. recipes.append(recipe)
  113. let newData = try JSONEncoder().encode(recipes)
  114. try newData.write(to: url)
  115. saveSuccessMessage = "Przepis zapisany pomyślnie"
  116. showingSaveSuccessAlert = true
  117. } catch {
  118. saveSuccessMessage = "Błąd zapisu do JSON: \(error)"
  119. showingSaveSuccessAlert = true
  120. }
  121. }
  122. }
  123.  
  124. struct AddRecipeView_Previews: PreviewProvider {
  125. static var previews: some View {
  126. AddRecipeView()
  127. }
  128. }
  129.  
Add Comment
Please, Sign In to add comment