Advertisement
Xaniasty

Untitled

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