Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions Talky/View/EditProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
}
}
}
}
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -95,8 +107,7 @@ struct EditProfileView: View {
Spacer()
}
.onAppear{

email = authViewModel.user?.email ?? ""
email = authViewModel.user?.email ?? ""
}
.navigationTitle("Edit Profile")
.navigationBarItems(
Expand All @@ -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
Expand All @@ -125,13 +134,12 @@ struct EditProfileView: View {
ImagePicker(image: $authViewModel.image)
.ignoresSafeArea()
}

}

}

struct EditProfileView_Previews: PreviewProvider {
static var previews: some View {
EditProfileView()
.environmentObject(AuthViewModel())
}
}
71 changes: 31 additions & 40 deletions Talky/View/WelcomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

}
}
}
Expand Down
36 changes: 13 additions & 23 deletions Talky/ViewModel/AuthViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand All @@ -28,7 +27,6 @@ class AuthViewModel: ObservableObject {
getCurrentUser()
}
}


private func getCurrentUser(){
if let savedData = UserDefaults.standard.data(forKey: "xxxx") {
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
}

Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 }
Expand Down