Advertisement
Xaniasty

Untitled

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