Xaniasty

Untitled

Jun 12th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 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 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(imageName: $imageName)
  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 || imageName.isEmpty {
  65. saveSuccessMessage = "Wszystkie pola są wymagane, w tym zdjęcie"
  66. showingSaveSuccessAlert = true
  67. return
  68. }
  69.  
  70. let newRecipe = MyRecipe(filename: imageName, name: name, description: description, estimatedTime: estimatedTime)
  71. saveToJSON(recipe: newRecipe)
  72. }
  73.  
  74. func saveToJSON(recipe: MyRecipe) {
  75. guard let url = Bundle.main.url(forResource: "recipes", withExtension: "json") else {
  76. saveSuccessMessage = "Nie znaleziono pliku recipes.json"
  77. showingSaveSuccessAlert = true
  78. return
  79. }
  80.  
  81. do {
  82. let data = try Data(contentsOf: url)
  83. var recipes = try JSONDecoder().decode([MyRecipe].self, from: data)
  84. recipes.append(recipe)
  85. let newData = try JSONEncoder().encode(recipes)
  86. try newData.write(to: url)
  87. saveSuccessMessage = "Przepis zapisany pomyślnie"
  88. showingSaveSuccessAlert = true
  89. } catch {
  90. saveSuccessMessage = "Błąd zapisu do JSON: \(error)"
  91. showingSaveSuccessAlert = true
  92. }
  93. }
  94. }
  95.  
  96. struct MyRecipe: Identifiable, Codable {
  97. var id = UUID()
  98. var filename: String
  99. var name: String
  100. var description: String
  101. var estimatedTime: String
  102.  
  103. enum CodingKeys: String, CodingKey {
  104. case filename
  105. case name
  106. case description
  107. case estimatedTime
  108. }
  109. }
  110.  
Add Comment
Please, Sign In to add comment