Skip to content

Commit

Permalink
✨ Feat: #17 - 이메일 회원가입 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
usa4060 committed Nov 15, 2023
1 parent 4e261dc commit eb75d1f
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 26 deletions.
8 changes: 2 additions & 6 deletions CMC/Sources/Data/DTO/Auth/EmailDupDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ import Foundation
struct EmailDupDTO: Codable {
let isSuccess: Bool
let code, message: String
let result: EmailDupResponse

struct EmailDupResponse: Codable {
let message: String
}
let result: String

func toDomain() -> EmailDupModel {
return EmailDupModel(
message: result.message
message: result
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class FakeAuthRepository: AuthRepository {
isSuccess: true,
code: "200",
message: "성공",
result: .init(message: "이메일 인증에 성공했어요~!")
result: "사용 가능한 이메일입니다."
)
return Single.just(fakeEmailDupDTO)
}
Expand Down
1 change: 1 addition & 0 deletions CMC/Sources/Domain/Usecases/Auth/DefaultAuthUsecase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ final class DefaultAuthUsecase: AuthUsecase {
}

func emailDup(query: EmailDupQuery) -> Single<EmailDupModel> {

return authRepository.emailDup(query: query)
.map { dto in
return dto.toDomain()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ final class MainSignUpView: BaseView {

override func bind() {

emailTextField.rxType
.withUnretained(self)
.subscribe(onNext: { owner, type in
if type == .focus {
owner.emailErrorCell.reset()
}
})
.disposed(by: disposeBag)

passwordTextField.accessoryState
.observe(on: MainScheduler.instance)
.withUnretained(self)
Expand All @@ -215,7 +224,7 @@ final class MainSignUpView: BaseView {
.observe(on: MainScheduler.instance)
.withUnretained(self)
.subscribe(onNext: { owner, state in
owner.passwordTextField.isSecureTextEntry = !state
owner.confirmPasswordTextField.isSecureTextEntry = !state
})
.disposed(by: disposeBag)

Expand Down Expand Up @@ -244,15 +253,24 @@ final class MainSignUpView: BaseView {

let output = viewModel.transform(input: input)

output.emailValidation
.withUnretained(self)
.subscribe(onNext: { owner, active in
let buttonType: CMCButton.CMCButtonType = active ? .login(.inactive) : .login(.disabled)
owner.emailTextField.accessoryCMCButton.rxType.accept(buttonType)
})
.disposed(by: disposeBag)

output.emailDuplicate
.withUnretained(self)
.subscribe(onNext: { owner, result in
let (notDuplicate, message) = result
let type: CMCErrorMessage.CMCErrorMessageType = notDuplicate ? .success : .disabled
let type: CMCErrorMessage.CMCErrorMessageType = notDuplicate ? .none : .error
let emailType: CMCTextField.TextFieldType = notDuplicate ? .disabled : .error
let errorMessage: String = notDuplicate ? "" : message
owner.emailErrorCell.rxType.accept(type)
owner.emailErrorCell.setErrorMessage(message: message)
owner.emailTextField.rxType.accept(emailType)
owner.emailErrorCell.setErrorMessage(message: errorMessage)
})
.disposed(by: disposeBag)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final class MainSignUpViewModel: ViewModelType {
}

struct Output {
let emailValidation: Observable<Bool>
let emailDuplicate: Observable<(Bool, String)>
let passwordValidations: [Observable<Bool>]
let passwordConfirmRegex: Observable<Bool>
Expand All @@ -44,21 +45,7 @@ final class MainSignUpViewModel: ViewModelType {

func transform(input: Input) -> Output {

input.emailDupTapped
.withLatestFrom(input.email)
.withUnretained(self)
.flatMap { owner, email -> Observable<(Bool, String)> in
let query = EmailDupQuery(email: email)
return owner.authUsecase.emailDup(query: query)
.asObservable()
.map { (true, $0.message) }
.catch { error -> Observable<(Bool, String)> in
guard let customError = (error as? NetworkError)?.errorDescription else { return .empty() }
return .just((false, customError))
}
}
.bind(to: emailDupResult)
.disposed(by: disposeBag)
let emailValidation: Observable<Bool> = Utility.checkEmailValidation(email: input.email, validate: .emailRegex)

let passwordValidations: [Observable<Bool>] = [
.englishRegex,
Expand All @@ -85,7 +72,25 @@ final class MainSignUpViewModel: ViewModelType {
)
.map { $0.0 && $1 && $2 && $3 }

input.emailDupTapped
.withLatestFrom(input.email)
.withUnretained(self)
.flatMap { owner, email -> Observable<(Bool, String)> in
let query = EmailDupQuery(email: email)
return owner.authUsecase.emailDup(query: query)
.asObservable()
.map { (true, $0.message) }
.catch { error -> Observable<(Bool, String)> in
let customError = (error as! NetworkError).errorDescription
return .just((false, customError))
}
}
.bind(to: emailDupResult)
.disposed(by: disposeBag)


return Output(
emailValidation: emailValidation,
emailDuplicate: emailDupResult.asObservable(),
passwordValidations: passwordValidations,
passwordConfirmRegex: passwordConfirmRegex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SignUpViewController: BaseViewController {
let view = MainSignUpView(
viewModel: MainSignUpViewModel(
authUsecase: DefaultAuthUsecase(
authRepository: FakeAuthRepository()
authRepository: DefaultAuthRepository()
)
),
parentViewModel: viewModel
Expand Down
7 changes: 7 additions & 0 deletions CMC/Sources/Utils/Commons/Unitility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class Utility {
}
}

static func checkEmailValidation(email: Observable<String>, validate: EmailValidate) -> Observable<Bool> {
let emailTest = NSPredicate(format: "SELF MATCHES %@", validate.validate)
return email.map { email in
return emailTest.evaluate(with: email)
}
}

static func checkPasswordValidation(password: Observable<String>, validate: PasswordValidate) -> Observable<Bool> {
let passwordTest = NSPredicate(format: "SELF MATCHES %@", validate.validate)
return password.map { password in
Expand Down
12 changes: 12 additions & 0 deletions CMC/Sources/Utils/Commons/ValidationType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@

import Foundation

enum EmailValidate {

case emailRegex

var validate: String {
switch self {
case .emailRegex:
return "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
}
}
}

enum PasswordValidate {

case englishRegex
Expand Down
1 change: 1 addition & 0 deletions DesignSystem/Sources/CMCButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public final class CMCButton: UIView{

rxType
.withUnretained(self)
.observe(on: MainScheduler.instance)
.subscribe(onNext: { owner, type in
owner.configureUISet(type: type)
})
Expand Down

0 comments on commit eb75d1f

Please sign in to comment.