Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- struct AddRecipeView: View {
- @State private var name = ""
- @State private var description = ""
- @State private var estimatedTime = ""
- @State private var imageName = ""
- @State private var image: UIImage? = nil
- @State private var showingImagePicker = false
- @State private var showingSaveSuccessAlert = false
- @State private var saveSuccessMessage = ""
- var body: some View {
- VStack {
- TextField("Nazwa", text: $name)
- .textFieldStyle(RoundedBorderTextFieldStyle())
- .padding()
- TextField("Opis", text: $description)
- .textFieldStyle(RoundedBorderTextFieldStyle())
- .padding()
- TextField("Czas przygotowania", text: $estimatedTime)
- .textFieldStyle(RoundedBorderTextFieldStyle())
- .padding()
- Button(action: {
- showingImagePicker = true
- }) {
- Text("Dodaj zdjęcie")
- .font(.headline)
- .padding()
- .background(Color.blue)
- .foregroundColor(.white)
- .cornerRadius(10)
- }
- .padding()
- .sheet(isPresented: $showingImagePicker) {
- ImagePicker(imageName: $imageName)
- }
- Button(action: {
- saveRecipe()
- }) {
- Text("Zapisz przepis")
- .font(.headline)
- .padding()
- .background(Color.green)
- .foregroundColor(.white)
- .cornerRadius(10)
- }
- .padding()
- Spacer()
- }
- .padding()
- .navigationTitle("Dodaj przepis")
- .alert(isPresented: $showingSaveSuccessAlert) {
- Alert(title: Text("Informacja"), message: Text(saveSuccessMessage), dismissButton: .default(Text("OK")))
- }
- }
- func saveRecipe() {
- if name.isEmpty || description.isEmpty || estimatedTime.isEmpty || imageName.isEmpty {
- saveSuccessMessage = "Wszystkie pola są wymagane, w tym zdjęcie"
- showingSaveSuccessAlert = true
- return
- }
- guard let image = image else {
- saveSuccessMessage = "Zdjęcie jest wymagane"
- showingSaveSuccessAlert = true
- return
- }
- if let data = image.jpegData(compressionQuality: 0.8) {
- if let photosURL = Bundle.main.url(forResource: "photos", withExtension: nil) {
- let filename = UUID().uuidString + ".jpg"
- let photoURL = photosURL.appendingPathComponent(filename)
- do {
- try data.write(to: photoURL)
- imageName = filename // Update the imageName with the new filename
- } catch {
- print("Błąd zapisu zdjęcia: \(error)")
- saveSuccessMessage = "Błąd zapisu zdjęcia"
- showingSaveSuccessAlert = true
- return
- }
- let newRecipe = MyRecipe(filename: filename, name: name, description: description, estimatedTime: estimatedTime)
- saveToJSON(recipe: newRecipe)
- } else {
- print("Folder photos nie istnieje")
- saveSuccessMessage = "Folder photos nie istnieje"
- showingSaveSuccessAlert = true
- }
- }
- }
- func saveToJSON(recipe: MyRecipe) {
- guard let url = Bundle.main.url(forResource: "recipes", withExtension: "json") else {
- saveSuccessMessage = "Nie znaleziono pliku recipes.json"
- showingSaveSuccessAlert = true
- return
- }
- do {
- let data = try Data(contentsOf: url)
- var recipes = try JSONDecoder().decode([MyRecipe].self, from: data)
- recipes.append(recipe)
- let newData = try JSONEncoder().encode(recipes)
- try newData.write(to: url)
- saveSuccessMessage = "Przepis zapisany pomyślnie"
- showingSaveSuccessAlert = true
- } catch {
- saveSuccessMessage = "Błąd zapisu do JSON: \(error)"
- showingSaveSuccessAlert = true
- }
- }
- }
- struct MyRecipe: Identifiable, Codable {
- var id = UUID()
- var filename: String
- var name: String
- var description: String
- var estimatedTime: String
- enum CodingKeys: String, CodingKey {
- case filename
- case name
- case description
- case estimatedTime
- }
- }
Add Comment
Please, Sign In to add comment