Advertisement
Xaniasty

Untitled

Jun 12th, 2024
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.28 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. import SwiftUI
  101.  
  102. struct UserPhotoGridView: View {
  103. @Binding var isUserLoggedIn: Bool
  104.  
  105. var body: some View {
  106. NavigationView {
  107. VStack {
  108. Text("Photo Grid View for User")
  109.  
  110. NavigationLink(destination: AddRecipeView()) {
  111. Text("Dodaj przepis")
  112. .font(.headline)
  113. .padding()
  114. .background(Color.orange)
  115. .foregroundColor(.white)
  116. .cornerRadius(10)
  117. }
  118. .padding()
  119.  
  120. Button(action: {
  121. isUserLoggedIn = false
  122. }) {
  123. Text("Wyloguj się")
  124. .font(.headline)
  125. .padding()
  126. .background(Color.red)
  127. .foregroundColor(.white)
  128. .cornerRadius(10)
  129. }
  130. .padding()
  131. }
  132. .navigationTitle("Użytkownik")
  133. }
  134. }
  135. }
  136.  
  137.  
  138. ------------------------------------------------------------------------
  139.  
  140. import SwiftUI
  141. import UIKit
  142.  
  143. struct ImagePicker: UIViewControllerRepresentable {
  144. @Binding var imageName: String
  145.  
  146. func makeCoordinator() -> Coordinator {
  147. Coordinator(self)
  148. }
  149.  
  150. func makeUIViewController(context: Context) -> UIImagePickerController {
  151. let picker = UIImagePickerController()
  152. picker.delegate = context.coordinator
  153. picker.sourceType = .photoLibrary
  154. return picker
  155. }
  156.  
  157. func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}
  158.  
  159. class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
  160. var parent: ImagePicker
  161.  
  162. init(_ parent: ImagePicker) {
  163. self.parent = parent
  164. }
  165.  
  166. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  167. if let image = info[.originalImage] as? UIImage {
  168. let fileName = UUID().uuidString + ".jpg"
  169. let imagePath = FileManager.default.temporaryDirectory.appendingPathComponent(fileName)
  170. if let jpegData = image.jpegData(compressionQuality: 0.8) {
  171. try? jpegData.write(to: imagePath)
  172. parent.imageName = fileName
  173. }
  174. }
  175. picker.dismiss(animated: true)
  176. }
  177. }
  178. }
  179.  
  180.  
  181. ---------------------------------
  182.  
  183. import SwiftUI
  184.  
  185. struct ContentView: View {
  186. @State private var showingLoginSheet = false
  187. @State private var login = ""
  188. @State private var password = ""
  189. @State private var showingPassword = false
  190. @State private var showingLoginFailedAlert = false
  191. @State private var loginFailedMessage = ""
  192. @State private var isUserLoggedIn = false
  193.  
  194. var body: some View {
  195. NavigationView {
  196. if isUserLoggedIn {
  197. UserPhotoGridView(isUserLoggedIn: $isUserLoggedIn)
  198. } else {
  199. VStack {
  200. Spacer()
  201.  
  202. Text("Witamy w X")
  203. .font(.largeTitle)
  204. .padding(.bottom, 10)
  205.  
  206. Text("Wybierz sposób logowania")
  207. .font(.subheadline)
  208. .padding(.bottom, 40)
  209.  
  210. VStack(spacing: 20) {
  211. NavigationLink(destination: GuestPhotoGridView()) {
  212. Text("Jako gość")
  213. .font(.headline)
  214. .frame(minWidth: 0, maxWidth: .infinity)
  215. .padding()
  216. .background(Color.blue)
  217. .foregroundColor(.white)
  218. .cornerRadius(10)
  219. }
  220.  
  221. Button(action: {
  222. showingLoginSheet = true
  223. }) {
  224. Text("Zaloguj się")
  225. .font(.headline)
  226. .frame(minWidth: 0, maxWidth: .infinity)
  227. .padding()
  228. .background(Color.green)
  229. .foregroundColor(.white)
  230. .cornerRadius(10)
  231. }
  232. .sheet(isPresented: $showingLoginSheet) {
  233. VStack {
  234. Text("Logowanie")
  235. .font(.headline)
  236. .padding()
  237.  
  238. TextField("Login", text: $login)
  239. .autocapitalization(.none) // Zapobiega automatycznej kapitalizacji
  240. .textFieldStyle(RoundedBorderTextFieldStyle())
  241. .padding()
  242.  
  243. if showingPassword {
  244. TextField("Hasło", text: $password)
  245. .textFieldStyle(RoundedBorderTextFieldStyle())
  246. .padding()
  247. } else {
  248. SecureField("Hasło", text: $password)
  249. .textFieldStyle(RoundedBorderTextFieldStyle())
  250. .padding()
  251. }
  252.  
  253. Button(action: {
  254. showingPassword.toggle()
  255. }) {
  256. HStack {
  257. Image(systemName: showingPassword ? "eye.slash.fill" : "eye.fill")
  258. Text(showingPassword ? "Ukryj hasło" : "Pokaż hasło")
  259. }
  260. .font(.subheadline)
  261. .foregroundColor(.blue)
  262. }
  263. .padding(.bottom, 20)
  264.  
  265. if showingLoginFailedAlert {
  266. Text(loginFailedMessage)
  267. .foregroundColor(.red)
  268. .padding()
  269. }
  270.  
  271. HStack {
  272. Button("Anuluj") {
  273. showingLoginSheet = false
  274. }
  275. .padding()
  276.  
  277. Button("OK") {
  278. if login == "TestUser" && password == "TestPassword" {
  279. showingLoginFailedAlert = false
  280. login = ""
  281. password = ""
  282. showingLoginSheet = false
  283. isUserLoggedIn = true
  284. } else {
  285. loginFailedMessage = "Nieprawidłowy login lub hasło"
  286. showingLoginFailedAlert = true
  287. }
  288. }
  289. .padding()
  290. }
  291. }
  292. .padding()
  293. }
  294. }
  295. .padding(.horizontal, 40)
  296.  
  297. Spacer()
  298. }
  299. .navigationBarTitle("Ekran Logowania", displayMode: .inline)
  300. }
  301. }
  302. }
  303. }
  304.  
  305. struct GuestPhotoGridView: View {
  306. var body: some View {
  307. Text("Photo Grid View for Guest")
  308. }
  309. }
  310.  
  311.  
  312.  
  313.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement