Xaniasty

Untitled

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