From 10ede589c366e07aa0a98913bd67381fc09d4eee Mon Sep 17 00:00:00 2001 From: feni-brian <56452538+feni-brian@users.noreply.github.com> Date: Thu, 1 Feb 2024 22:00:24 +0300 Subject: [PATCH 1/3] Modified Authentication View Modal : - Reformatted the code with reindentation and removal of empty or unnecessary whitespaces; this was for easy reading. - Removed initialisation of immutable value 'metadata' since it wasn't being used and replaced it with an assignment to '_'. Could remove it as well if you wish. --- Talky/ViewModel/AuthViewModel.swift | 36 +++++++++++------------------ 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/Talky/ViewModel/AuthViewModel.swift b/Talky/ViewModel/AuthViewModel.swift index f85d293..0f5f838 100644 --- a/Talky/ViewModel/AuthViewModel.swift +++ b/Talky/ViewModel/AuthViewModel.swift @@ -17,7 +17,6 @@ class AuthViewModel: ObservableObject { // other variables for registering new user @Published var repassword = "" @Published var image: UIImage? - @Published var user: ChatUser? @Published var fcm: String? @@ -28,7 +27,6 @@ class AuthViewModel: ObservableObject { getCurrentUser() } } - private func getCurrentUser(){ if let savedData = UserDefaults.standard.data(forKey: "xxxx") { @@ -57,9 +55,9 @@ class AuthViewModel: ObservableObject { func handleSignOut() async { do{ - try FirebaseManager.shared.auth.signOut() + try FirebaseManager.shared.auth.signOut() loggedOut = true - } catch{ + } catch { print(error) } } @@ -70,15 +68,12 @@ class AuthViewModel: ObservableObject { await persistImageToStorage() await saveUserData() getCurrentUser() - - - } else { viewState = .empty } - if(!currPassword.isEmpty && !newPassword.isEmpty && !newRePassword.isEmpty){ + if (!currPassword.isEmpty && !newPassword.isEmpty && !newRePassword.isEmpty){ await resetPassword(email: email, currPassword: currPassword, newPassword: newPassword, newRePassword: newRePassword) - }else { + } else { viewState = .empty } @@ -89,9 +84,9 @@ class AuthViewModel: ObservableObject { guard let uid = FirebaseManager.shared.auth.currentUser?.uid else { return } let ref = FirebaseManager.shared.storage.reference(withPath: uid) guard let imageData = self.image?.jpegData(compressionQuality: 0.5) else { return } - - - let metadata = ref.putData(imageData, metadata: nil) + + // Initialization of immutable value 'metadata' was never used; considered replacing it with an assignment to '_' or removing it. + _ = ref.putData(imageData, metadata: nil) do { let url = try await ref.downloadURL() @@ -106,12 +101,10 @@ class AuthViewModel: ObservableObject { } } - + func resetPassword(email: String, currPassword: String, newPassword: String, newRePassword: String) async { - guard let user = FirebaseManager.shared.auth.currentUser else { - return - } + guard let user = FirebaseManager.shared.auth.currentUser else { return } if newPassword != newRePassword { viewState = .error @@ -121,17 +114,17 @@ class AuthViewModel: ObservableObject { viewState = .loading let credential = EmailAuthProvider.credential(withEmail: email, password: currPassword) - + do { try await user.reauthenticate(with: credential) try await user.updatePassword(to: newPassword) viewState = .loaded - } catch{ + } catch { print(error) logMessage = "Error : \(error)" viewState = .error } - + } func login() async { @@ -213,12 +206,9 @@ class AuthViewModel: ObservableObject { guard let uid = FirebaseManager.shared.auth.currentUser?.uid else { return } let userData = ["fcm" : userFCM] - await updateUserProfile(uid: uid, updateData: userData) - - } - + private func storeUserInformation() async { guard let uid = FirebaseManager.shared.auth.currentUser?.uid else { return } From 9463bf1f434515e298a9c76e51de585448a9647f Mon Sep 17 00:00:00 2001 From: feni-brian <56452538+feni-brian@users.noreply.github.com> Date: Thu, 1 Feb 2024 22:07:11 +0300 Subject: [PATCH 2/3] Modified the Edit Profile View. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reformatted the code with reindentation and removal of empty or unnecessary whitespaces; this was for easy reading. Safely unwrapped the user's profile image url from the authViewModal because explicitly unwrapping the optional causes previews to crash since there isn't any profile image url present or in app storage. The AsyncImage switch covers known cases, but 'AsyncImagePhase' may have additional unknown values, possibly added in future versions. To handle unknown values, it's best to use the "@unknown default” case. Unfortunately, 'ContentUnavailableView' is only available in iOS 17.0 or newer; however, it serve well here I think. The environment object modifier in the preview provider allows the previews to automatically gain access to that environment object. Without it, it simply crashes. --- Talky/View/EditProfileView.swift | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/Talky/View/EditProfileView.swift b/Talky/View/EditProfileView.swift index 83a50ae..09a447e 100644 --- a/Talky/View/EditProfileView.swift +++ b/Talky/View/EditProfileView.swift @@ -33,19 +33,20 @@ struct EditProfileView: View { .frame(width: 200, height: 200) .cornerRadius(16) } else { - AsyncImage(url: URL(string: authViewModel.user!.profileImageUrl!)) { phase in + // Explicitly unwrapping the optional causes previews to crash since there isn't any profile image url present or in app storage. + AsyncImage(url: URL(string: authViewModel.user?.profileImageUrl ?? "")) { phase in switch phase { case .empty: Rectangle() .frame(width: 200, height: 200) .cornerRadius(16) - .foregroundColor(Color("secondaryDark")) + // `foregroundColor` has been renamed to `foregroundStyle` and will be deprecated in a future version of iOS + .foregroundStyle(Color("secondaryDark")) case .success(let image): // Display the image image .resizable() .aspectRatio(contentMode: .fill) - .frame(width: 200, height: 200) .clipped() .cornerRadius(16) @@ -54,7 +55,16 @@ struct EditProfileView: View { Rectangle() .frame(width: 200, height: 200) .cornerRadius(16) - .foregroundColor(.red) + // `foregroundColor` has been renamed to `foregroundStyle` and will be deprecated in a future version of iOS + .foregroundStyle(Color.red) + // Handle unknown values using "@unknown default” + @unknown default: + if #available(iOS 17.0, *) { + // 'ContentUnavailableView' is only available in iOS 17.0 or newer + ContentUnavailableView("", systemImage: "") + } else { + fatalError() + } } } } @@ -63,7 +73,8 @@ struct EditProfileView: View { shouldShowImagePicker.toggle() } label: { Image(systemName: "camera") - .foregroundColor(.white) + // `foregroundColor` has been renamed to `foregroundStyle` and will be deprecated in a future version of iOS + .foregroundStyle(Color.white) .padding(8) .background(Color("primary")) .cornerRadius(8) @@ -85,7 +96,8 @@ struct EditProfileView: View { .cornerRadius(16) .multilineTextAlignment(.leading) .lineLimit(1) - .foregroundColor(.black) + // `foregroundColor` has been renamed to `foregroundStyle` and will be deprecated in a future version of iOS + .foregroundStyle(Color.black) .opacity(0.5) SecureTextField(placeholder: "Current Password", show: $showPassword, text: $currpassword) SecureTextField(placeholder: "New Password", show: $showPassword, text: $password) @@ -95,8 +107,7 @@ struct EditProfileView: View { Spacer() } .onAppear{ - - email = authViewModel.user?.email ?? "" + email = authViewModel.user?.email ?? "" } .navigationTitle("Edit Profile") .navigationBarItems( @@ -111,11 +122,9 @@ struct EditProfileView: View { Text(authViewModel.viewState == .loading ? "Loading..." : "Save") } ) - .alert(isPresented: $showAlert) { Alert(title: Text(authViewModel.logMessage)) } - .onChange(of: authViewModel.viewState, perform: { newValue in if newValue == .error{ showAlert = true @@ -125,13 +134,12 @@ struct EditProfileView: View { ImagePicker(image: $authViewModel.image) .ignoresSafeArea() } - } - } struct EditProfileView_Previews: PreviewProvider { static var previews: some View { EditProfileView() + .environmentObject(AuthViewModel()) } } From 732952d2377710e2411650f62b25d9c96c5b4679 Mon Sep 17 00:00:00 2001 From: feni-brian <56452538+feni-brian@users.noreply.github.com> Date: Thu, 1 Feb 2024 22:22:31 +0300 Subject: [PATCH 3/3] Modified the Welcome View. - Removed the two declared state variables. They were rendered unnecessary. - Removed the button instances as well. No need for them as well. - Removed the two older versions of the navigation links and replaced them with the latest versions of navigation links. - Reformatted the code with indentation and removal of extra whitespaces and paragraphs. --- Talky/View/WelcomeView.swift | 71 ++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 40 deletions(-) diff --git a/Talky/View/WelcomeView.swift b/Talky/View/WelcomeView.swift index 2227320..cacdba3 100644 --- a/Talky/View/WelcomeView.swift +++ b/Talky/View/WelcomeView.swift @@ -8,63 +8,54 @@ import SwiftUI struct WelcomeView: View { - @State var toRegister = false - @State var toLogin = false + var body: some View { NavigationView{ ZStack{ Color("secondary").ignoresSafeArea() - - NavigationLink(destination: LoginxView(), isActive: $toLogin){ - EmptyView() - } - NavigationLink(destination: RegisterxView(), isActive: $toRegister){ - EmptyView() - } VStack{ - VStack{ - Spacer() - } - .overlay { - ZStack{ - Image("shots") - .padding(.top, 144) - LinearGradient( + VStack{ + Spacer() + } + .overlay { + ZStack{ + Image("shots").padding(.top, 144) + LinearGradient( stops: [ - Gradient.Stop(color: Color(red: 0.93, green: 0.94, blue: 0.97).opacity(0), location: 0.00), - Gradient.Stop(color: Color(red: 0.93, green: 0.94, blue: 0.97), location: 1.00), + Gradient.Stop(color: Color(red: 0.93, green: 0.94, blue: 0.97).opacity(0), location: 0.00), + Gradient.Stop(color: Color(red: 0.93, green: 0.94, blue: 0.97), location: 1.00), ], startPoint: UnitPoint(x: 0.5, y: 0), endPoint: UnitPoint(x: 0.5, y: 1) - ) - } + ) } + } VStack(alignment: .center, spacing: 32){ - Text("Connect with Talky") - .font(.title) - .fontWeight(.bold) - VStack(spacing:16){ - Button{ - toRegister.toggle() + Text("Connect with Talky") + .font(.title) + .fontWeight(.bold) + VStack(spacing:16) { + NavigationLink { + RegisterxView() } label: { - HStack{ + HStack { Text("Start Messaging") - + .padding(.vertical, 20) + .frame(maxWidth: .infinity) + // `foregroundColor` has been renamed to `foregroundStyle` and will be deprecated in a future version of iOS + .foregroundStyle(Color.white) + .background(Color("primary")) + .cornerRadius(16) } - .padding(.vertical, 20) - .frame(maxWidth: .infinity) - .foregroundColor(.white) - .background(Color("primary")) - .cornerRadius(16) } - Button{ - toLogin.toggle() + NavigationLink { + LoginxView() } label: { - HStack{ - Text("Already have account?") - .foregroundColor(Color("secondaryDark")) + HStack { + Text("Already have an account?") + // `foregroundColor` has been renamed to `foregroundStyle` and will be deprecated in a future version of iOS + .foregroundStyle(Color("secondaryDark")) Text("Log in") - } } }