Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- struct Recipe: 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
- }
- }
- 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: PhotoGridView(isUserLoggedIn: $isUserLoggedIn)) {
- 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) {
- LoginView(showingLoginSheet: $showingLoginSheet, login: $login, password: $password, isUserLoggedIn: $isUserLoggedIn, showingLoginFailedAlert: $showingLoginFailedAlert, loginFailedMessage: $loginFailedMessage)
- }
- }
- .padding(.horizontal, 40)
- Spacer()
- }
- .navigationBarTitle("Ekran Logowania", displayMode: .inline)
- }
- }
- }
- }
- 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")
- }
- }
- }
- struct PhotoGridView: 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")
- }
- }
- }
- struct AddRecipeView: View {
- @State private var name = ""
- @State private var description = ""
- @State private var estimatedTime = ""
- @State private var filename = ""
- @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: $filename)
- }
- 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(id: UUID(), filename: filename, name: name, description: description, estimatedTime: estimatedTime)
- var recipes = [Recipe]()
- if let data = UserDefaults.standard.data(forKey: "recipes") {
- if let decoded = try? JSONDecoder().decode([Recipe].self, from: data) {
- recipes = decoded
- }
- }
- recipes.append(newRecipe)
- if let encoded = try? JSONEncoder().encode(recipes) {
- UserDefaults.standard.set(encoded, forKey: "recipes")
- }
- }
- }
- struct LoginView: View {
- @Binding var showingLoginSheet: Bool
- @Binding var login: String
- @Binding var password: String
- @Binding var isUserLoggedIn: Bool
- @Binding var showingLoginFailedAlert: Bool
- @Binding var loginFailedMessage: String
- var body: some View {
- VStack {
- Text("Logowanie")
- .font(.headline)
- .padding()
- TextField("Login", text: $login)
- .autocapitalization(.none)
- .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()
- }
- }
- 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)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement