Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Dec 3, 2024
1 parent 20a2f50 commit 6b0b06a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
30 changes: 15 additions & 15 deletions Splito/UI/Login/EmailLogin/EmailLoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import SwiftUI
import BaseStyle

struct EmailLoginView: View {

@StateObject var viewModel: EmailLoginViewModel

@FocusState private var focusedField: EmailLoginViewModel.EmailLoginField?

var body: some View {
GeometryReader { proxy in
VStack(alignment: .leading, spacing: 0) {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
AppLogoView(geometry: .constant(proxy))

Group {
EmailFieldView(email: $viewModel.email, focusedField: $focusedField)

PasswordFieldView(password: $viewModel.password, focusedField: $focusedField,
isPasswordVisible: viewModel.isPasswordVisible,
handlePasswordEyeTap: viewModel.handlePasswordEyeTap,
onEditingChanged: viewModel.onEditingChanged(abc:))

Spacer()

PrimaryButton(text: "Sign in", isEnabled: !viewModel.email.isEmpty && !viewModel.password.isEmpty,
showLoader: viewModel.showLoader, onClick: viewModel.onEmailLoginClick)

Button("Forgot your password?") {}
.padding()
.underline()
Expand All @@ -58,15 +58,15 @@ struct EmailLoginView: View {
}

private struct EmailFieldView: View {

@Binding var email: String
var focusedField: FocusState<EmailLoginViewModel.EmailLoginField?>.Binding

var body: some View {
Text("Email address")
.font(.subTitle1())
.foregroundStyle(secondaryText)

TextField("Your email address", text: $email)
.autocapitalization(.none)
.keyboardType(.emailAddress)
Expand All @@ -85,19 +85,19 @@ private struct EmailFieldView: View {
}

private struct PasswordFieldView: View {

@Binding var password: String
var focusedField: FocusState<EmailLoginViewModel.EmailLoginField?>.Binding

let isPasswordVisible: Bool
var handlePasswordEyeTap: () -> Void
var onEditingChanged: (Bool) -> Void

var body: some View {
Text("Password")
.font(.subTitle1())
.foregroundStyle(secondaryText)

HStack {
if isPasswordVisible {
TextField("Your password", text: $password, onEditingChanged: onEditingChanged)
Expand Down
22 changes: 11 additions & 11 deletions Splito/UI/Login/EmailLogin/EmailLoginViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public class EmailLoginViewModel: BaseViewModel, ObservableObject {
self.router = router
}

func onEmailSignUp(email: String, password: String) {
func onEmailSignUp() {
FirebaseAuth.Auth.auth().createUser(withEmail: email, password: password) { [weak self] authResult, error in
guard let self = self else { return }
if let error = error {
LogE("Error during sign up: \(error.localizedDescription)")
self.alert = .init(message: "Sign-up failed: \(error.localizedDescription)")
if let error {
LogE("EmailLoginViewModel: Error during sign up: \(error)")
self.alert = .init(message: "Sign-up failed: \(error)")
self.showAlert = true
} else if let authResult = authResult {
} else if let authResult {
let user = AppUser(id: authResult.user.uid, firstName: "", lastName: "",
emailId: email, phoneNumber: nil, loginType: .Email)
Task {
Expand All @@ -45,12 +45,12 @@ public class EmailLoginViewModel: BaseViewModel, ObservableObject {
func onEmailLoginClick() {
FirebaseAuth.Auth.auth().signIn(withEmail: email, password: password) { [weak self] _, error in
guard let self = self else { return }
if let error = error {
LogE("Error during login: \(error.localizedDescription)")
self.alert = .init(message: "Login failed: \(error.localizedDescription)")
if let error {
LogE("EmailLoginViewModel: Error during login: \(error)")
self.alert = .init(message: "Login failed: \(error)")
self.showAlert = true
} else {
LogD("User logged in successfully.")
LogD("EmailLoginViewModel: User logged in successfully.")
self.onLoginSuccess()
}
}
Expand All @@ -62,9 +62,9 @@ public class EmailLoginViewModel: BaseViewModel, ObservableObject {
self.preference.isVerifiedUser = true
self.preference.user = user
self.onLoginSuccess()
LogD("VerifyOtpViewModel: \(#function) User stored successfully.")
LogD("EmailLoginViewModel: \(#function) User stored successfully.")
} catch {
LogE("VerifyOtpViewModel: \(#function) Failed to store user: \(error).")
LogE("EmailLoginViewModel: \(#function) Failed to store user: \(error).")
self.alert = .init(message: error.localizedDescription)
self.showAlert = true
}
Expand Down

0 comments on commit 6b0b06a

Please sign in to comment.