Xaniasty

Untitled

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