Advertisement
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 showingImagePicker = false
- 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")
- }
- func saveRecipe() {
- let newRecipe = Recipe(name: name, description: description, estimatedTime: estimatedTime, imageName: imageName)
- saveToJSON(recipe: newRecipe)
- }
- func saveToJSON(recipe: Recipe) {
- guard let url = Bundle.main.url(forResource: "recipes", withExtension: "json") else {
- print("Nie znaleziono pliku recipes.json")
- return
- }
- do {
- let data = try Data(contentsOf: url)
- var recipes = try JSONDecoder().decode([Recipe].self, from: data)
- recipes.append(recipe)
- let newData = try JSONEncoder().encode(recipes)
- try newData.write(to: url)
- } catch {
- print("Błąd zapisu do JSON: \(error)")
- }
- }
- }
- struct Recipe: Codable, Identifiable {
- var id = UUID()
- var name: String
- var description: String
- var estimatedTime: String
- var imageName: String
- }
- struct AddRecipeView_Previews: PreviewProvider {
- static var previews: some View {
- AddRecipeView()
- }
- }
- ------------------------------------------------------------
- import SwiftUI
- struct UserPhotoGridView: View {
- @Binding var isUserLoggedIn: Bool
- var body: some View {
- NavigationView {
- VStack {
- Text("Photo Grid View for User")
- NavigationLink(destination: AddRecipeView()) {
- Text("Dodaj przepis")
- .font(.headline)
- .padding()
- .background(Color.orange)
- .foregroundColor(.white)
- .cornerRadius(10)
- }
- .padding()
- Button(action: {
- isUserLoggedIn = false
- }) {
- Text("Wyloguj się")
- .font(.headline)
- .padding()
- .background(Color.red)
- .foregroundColor(.white)
- .cornerRadius(10)
- }
- .padding()
- }
- .navigationTitle("Użytkownik")
- }
- }
- }
- ------------------------------------------------------------------------
- import SwiftUI
- import UIKit
- struct ImagePicker: UIViewControllerRepresentable {
- @Binding var imageName: String
- func makeCoordinator() -> Coordinator {
- Coordinator(self)
- }
- func makeUIViewController(context: Context) -> UIImagePickerController {
- let picker = UIImagePickerController()
- picker.delegate = context.coordinator
- picker.sourceType = .photoLibrary
- return picker
- }
- func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}
- class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
- var parent: ImagePicker
- init(_ parent: ImagePicker) {
- self.parent = parent
- }
- func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
- if let image = info[.originalImage] as? UIImage {
- let fileName = UUID().uuidString + ".jpg"
- let imagePath = FileManager.default.temporaryDirectory.appendingPathComponent(fileName)
- if let jpegData = image.jpegData(compressionQuality: 0.8) {
- try? jpegData.write(to: imagePath)
- parent.imageName = fileName
- }
- }
- picker.dismiss(animated: true)
- }
- }
- }
- ---------------------------------
- import SwiftUI
- struct ContentView: View {
- @State private var showingLoginSheet = false
- @State private var login = ""
- @State private var password = ""
- @State private var showingPassword = false
- @State private var showingLoginFailedAlert = false
- @State private var loginFailedMessage = ""
- @State private var isUserLoggedIn = false
- var body: some View {
- NavigationView {
- if isUserLoggedIn {
- UserPhotoGridView(isUserLoggedIn: $isUserLoggedIn)
- } else {
- VStack {
- Spacer()
- Text("Witamy w X")
- .font(.largeTitle)
- .padding(.bottom, 10)
- Text("Wybierz sposób logowania")
- .font(.subheadline)
- .padding(.bottom, 40)
- VStack(spacing: 20) {
- NavigationLink(destination: GuestPhotoGridView()) {
- Text("Jako gość")
- .font(.headline)
- .frame(minWidth: 0, maxWidth: .infinity)
- .padding()
- .background(Color.blue)
- .foregroundColor(.white)
- .cornerRadius(10)
- }
- Button(action: {
- showingLoginSheet = true
- }) {
- Text("Zaloguj się")
- .font(.headline)
- .frame(minWidth: 0, maxWidth: .infinity)
- .padding()
- .background(Color.green)
- .foregroundColor(.white)
- .cornerRadius(10)
- }
- .sheet(isPresented: $showingLoginSheet) {
- VStack {
- Text("Logowanie")
- .font(.headline)
- .padding()
- TextField("Login", text: $login)
- .autocapitalization(.none) // Zapobiega automatycznej kapitalizacji
- .textFieldStyle(RoundedBorderTextFieldStyle())
- .padding()
- if showingPassword {
- TextField("Hasło", text: $password)
- .textFieldStyle(RoundedBorderTextFieldStyle())
- .padding()
- } else {
- SecureField("Hasło", text: $password)
- .textFieldStyle(RoundedBorderTextFieldStyle())
- .padding()
- }
- Button(action: {
- showingPassword.toggle()
- }) {
- HStack {
- Image(systemName: showingPassword ? "eye.slash.fill" : "eye.fill")
- Text(showingPassword ? "Ukryj hasło" : "Pokaż hasło")
- }
- .font(.subheadline)
- .foregroundColor(.blue)
- }
- .padding(.bottom, 20)
- if showingLoginFailedAlert {
- Text(loginFailedMessage)
- .foregroundColor(.red)
- .padding()
- }
- HStack {
- Button("Anuluj") {
- showingLoginSheet = false
- }
- .padding()
- Button("OK") {
- if login == "TestUser" && password == "TestPassword" {
- showingLoginFailedAlert = false
- login = ""
- password = ""
- showingLoginSheet = false
- isUserLoggedIn = true
- } else {
- loginFailedMessage = "Nieprawidłowy login lub hasło"
- showingLoginFailedAlert = true
- }
- }
- .padding()
- }
- }
- .padding()
- }
- }
- .padding(.horizontal, 40)
- Spacer()
- }
- .navigationBarTitle("Ekran Logowania", displayMode: .inline)
- }
- }
- }
- }
- struct GuestPhotoGridView: View {
- var body: some View {
- Text("Photo Grid View for Guest")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement