Advertisement
Xaniasty

Untitled

Jun 12th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.16 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct Recipe: Identifiable, Codable {
  4. var id = UUID()
  5. var filename: String
  6. var name: String
  7. var description: String
  8. var estimatedTime: String
  9.  
  10. enum CodingKeys: String, CodingKey {
  11. case filename
  12. case name
  13. case description
  14. case estimatedTime
  15. }
  16. }
  17.  
  18. struct ContentView: View {
  19. @State private var showingLoginSheet = false
  20. @State private var login = ""
  21. @State private var password = ""
  22. @State private var showingPassword = false
  23. @State private var showingLoginFailedAlert = false
  24. @State private var loginFailedMessage = ""
  25. @State private var isUserLoggedIn = false
  26.  
  27. var body: some View {
  28. NavigationView {
  29. if isUserLoggedIn {
  30. UserPhotoGridView(isUserLoggedIn: $isUserLoggedIn)
  31. } else {
  32. VStack {
  33. Spacer()
  34.  
  35. Text("Witamy w X")
  36. .font(.largeTitle)
  37. .padding(.bottom, 10)
  38.  
  39. Text("Wybierz sposób logowania")
  40. .font(.subheadline)
  41. .padding(.bottom, 40)
  42.  
  43. VStack(spacing: 20) {
  44. NavigationLink(destination: PhotoGridView(isUserLoggedIn: $isUserLoggedIn)) {
  45. Text("Jako gość")
  46. .font(.headline)
  47. .frame(minWidth: 0, maxWidth: .infinity)
  48. .padding()
  49. .background(Color.blue)
  50. .foregroundColor(.white)
  51. .cornerRadius(10)
  52. }
  53.  
  54. Button(action: {
  55. showingLoginSheet = true
  56. }) {
  57. Text("Zaloguj się")
  58. .font(.headline)
  59. .frame(minWidth: 0, maxWidth: .infinity)
  60. .padding()
  61. .background(Color.green)
  62. .foregroundColor(.white)
  63. .cornerRadius(10)
  64. }
  65. .sheet(isPresented: $showingLoginSheet) {
  66. LoginView(showingLoginSheet: $showingLoginSheet, login: $login, password: $password, isUserLoggedIn: $isUserLoggedIn, showingLoginFailedAlert: $showingLoginFailedAlert, loginFailedMessage: $loginFailedMessage)
  67. }
  68. }
  69. .padding(.horizontal, 40)
  70.  
  71. Spacer()
  72. }
  73. .navigationBarTitle("Ekran Logowania", displayMode: .inline)
  74. }
  75. }
  76. }
  77. }
  78.  
  79. struct UserPhotoGridView: View {
  80. @Binding var isUserLoggedIn: Bool
  81.  
  82. var body: some View {
  83. NavigationView {
  84. VStack {
  85. Text("Photo Grid View for User")
  86.  
  87. NavigationLink(destination: AddRecipeView()) {
  88. Text("Dodaj przepis")
  89. .font(.headline)
  90. .padding()
  91. .background(Color.orange)
  92. .foregroundColor(.white)
  93. .cornerRadius(10)
  94. }
  95. .padding()
  96.  
  97. Button(action: {
  98. isUserLoggedIn = false
  99. }) {
  100. Text("Wyloguj się")
  101. .font(.headline)
  102. .padding()
  103. .background(Color.red)
  104. .foregroundColor(.white)
  105. .cornerRadius(10)
  106. }
  107. .padding()
  108. }
  109. .navigationTitle("Użytkownik")
  110. }
  111. }
  112. }
  113.  
  114. struct PhotoGridView: View {
  115. @Binding var isUserLoggedIn: Bool
  116.  
  117. var body: some View {
  118. NavigationView {
  119. VStack {
  120. Text("Photo Grid View for User")
  121.  
  122. NavigationLink(destination: AddRecipeView()) {
  123. Text("Dodaj przepis")
  124. .font(.headline)
  125. .padding()
  126. .background(Color.orange)
  127. .foregroundColor(.white)
  128. .cornerRadius(10)
  129. }
  130. .padding()
  131.  
  132. Button(action: {
  133. isUserLoggedIn = false
  134. }) {
  135. Text("Wyloguj się")
  136. .font(.headline)
  137. .padding()
  138. .background(Color.red)
  139. .foregroundColor(.white)
  140. .cornerRadius(10)
  141. }
  142. .padding()
  143. }
  144. .navigationTitle("Użytkownik")
  145. }
  146. }
  147. }
  148.  
  149. struct AddRecipeView: View {
  150. @State private var name = ""
  151. @State private var description = ""
  152. @State private var estimatedTime = ""
  153. @State private var filename = ""
  154. @State private var showingImagePicker = false
  155.  
  156. var body: some View {
  157. VStack {
  158. TextField("Nazwa", text: $name)
  159. .textFieldStyle(RoundedBorderTextFieldStyle())
  160. .padding()
  161.  
  162. TextField("Opis", text: $description)
  163. .textFieldStyle(RoundedBorderTextFieldStyle())
  164. .padding()
  165.  
  166. TextField("Czas przygotowania", text: $estimatedTime)
  167. .textFieldStyle(RoundedBorderTextFieldStyle())
  168. .padding()
  169.  
  170. Button(action: {
  171. showingImagePicker = true
  172. }) {
  173. Text("Dodaj zdjęcie")
  174. .font(.headline)
  175. .padding()
  176. .background(Color.blue)
  177. .foregroundColor(.white)
  178. .cornerRadius(10)
  179. }
  180. .padding()
  181. .sheet(isPresented: $showingImagePicker) {
  182. ImagePicker(imageName: $filename)
  183. }
  184.  
  185. Button(action: {
  186. saveRecipe()
  187. }) {
  188. Text("Zapisz przepis")
  189. .font(.headline)
  190. .padding()
  191. .background(Color.green)
  192. .foregroundColor(.white)
  193. .cornerRadius(10)
  194. }
  195. .padding()
  196.  
  197. Spacer()
  198. }
  199. .padding()
  200. .navigationTitle("Dodaj przepis")
  201. }
  202.  
  203. func saveRecipe() {
  204. let newRecipe = Recipe(id: UUID(), filename: filename, name: name, description: description, estimatedTime: estimatedTime)
  205. var recipes = [Recipe]()
  206. if let data = UserDefaults.standard.data(forKey: "recipes") {
  207. if let decoded = try? JSONDecoder().decode([Recipe].self, from: data) {
  208. recipes = decoded
  209. }
  210. }
  211. recipes.append(newRecipe)
  212. if let encoded = try? JSONEncoder().encode(recipes) {
  213. UserDefaults.standard.set(encoded, forKey: "recipes")
  214. }
  215. }
  216. }
  217.  
  218. struct LoginView: View {
  219. @Binding var showingLoginSheet: Bool
  220. @Binding var login: String
  221. @Binding var password: String
  222. @Binding var isUserLoggedIn: Bool
  223. @Binding var showingLoginFailedAlert: Bool
  224. @Binding var loginFailedMessage: String
  225.  
  226. var body: some View {
  227. VStack {
  228. Text("Logowanie")
  229. .font(.headline)
  230. .padding()
  231.  
  232. TextField("Login", text: $login)
  233. .autocapitalization(.none)
  234. .textFieldStyle(RoundedBorderTextFieldStyle())
  235. .padding()
  236.  
  237. if showingPassword {
  238. TextField("Hasło", text: $password)
  239. .textFieldStyle(RoundedBorderTextFieldStyle())
  240. .padding()
  241. } else {
  242. SecureField("Hasło", text: $password)
  243. .textFieldStyle(RoundedBorderTextFieldStyle())
  244. .padding()
  245. }
  246.  
  247. Button(action: {
  248. showingPassword.toggle()
  249. }) {
  250. HStack {
  251. Image(systemName: showingPassword ? "eye.slash.fill" : "eye.fill")
  252. Text(showingPassword ? "Ukryj hasło" : "Pokaż hasło")
  253. }
  254. .font(.subheadline)
  255. .foregroundColor(.blue)
  256. }
  257. .padding(.bottom, 20)
  258.  
  259. if showingLoginFailedAlert {
  260. Text(loginFailedMessage)
  261. .foregroundColor(.red)
  262. .padding()
  263. }
  264.  
  265. HStack {
  266. Button("Anuluj") {
  267. showingLoginSheet = false
  268. }
  269. .padding()
  270.  
  271. Button("OK") {
  272. if login == "TestUser" && password == "TestPassword" {
  273. showingLoginFailedAlert = false
  274. login = ""
  275. password = ""
  276. showingLoginSheet = false
  277. isUserLoggedIn = true
  278. } else {
  279. loginFailedMessage = "Nieprawidłowy login lub hasło"
  280. showingLoginFailedAlert = true
  281. }
  282. }
  283. .padding()
  284. }
  285. }
  286. .padding()
  287. }
  288. }
  289.  
  290. import SwiftUI
  291. import UIKit
  292.  
  293. struct ImagePicker: UIViewControllerRepresentable {
  294. @Binding var imageName: String
  295.  
  296. func makeCoordinator() -> Coordinator {
  297. Coordinator(self)
  298. }
  299.  
  300. func makeUIViewController(context: Context) -> UIImagePickerController {
  301. let picker = UIImagePickerController()
  302. picker.delegate = context.coordinator
  303. picker.sourceType = .photoLibrary
  304. return picker
  305. }
  306.  
  307. func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}
  308.  
  309. class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
  310. var parent: ImagePicker
  311.  
  312. init(_ parent: ImagePicker) {
  313. self.parent = parent
  314. }
  315.  
  316. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  317. if let image = info[.originalImage] as? UIImage {
  318. let fileName = UUID().uuidString + ".jpg"
  319. let imagePath = FileManager.default.temporaryDirectory.appendingPathComponent(fileName)
  320. if let jpegData = image.jpegData(compressionQuality: 0.8) {
  321. try? jpegData.write(to: imagePath)
  322. parent.imageName = fileName
  323. }
  324. }
  325. picker.dismiss(animated: true)
  326. }
  327. }
  328. }
  329.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement