Skip to content

Commit

Permalink
feat #164: checkUsername API 동작 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
minsangKang committed Sep 1, 2024
1 parent f050a52 commit 79e8448
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Project_Timer/Data/API/UserAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ enum UserAPI {

extension UserAPI: TargetType {
var baseURL: URL {
return URL(string: NetworkURL.serverURL_V2)!
return URL(string: NetworkURL.serverURL_V2 + "/api/user")!
}

var path: String {
Expand Down
4 changes: 2 additions & 2 deletions Project_Timer/Data/NetworkURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ final class NetworkURL {
static let shared = NetworkURL()
static var serverURL_V2: String {
#if DEBUG || DEVELOP
return "http://ec2-43-200-217-188.ap-northeast-2.compute.amazonaws.com:8080/api/user"
return "http://ec2-43-200-217-188.ap-northeast-2.compute.amazonaws.com:8080"
#else
return "http://ec2-43-200-217-188.ap-northeast-2.compute.amazonaws.com:8080/api/user"
return "http://ec2-43-200-217-188.ap-northeast-2.compute.amazonaws.com:8080"
#endif
}
private(set) var serverURL: String?
Expand Down
4 changes: 2 additions & 2 deletions Project_Timer/Data/Response/CheckUsernameResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import Foundation
struct CheckUsernameResponse: Decodable {
let code: String
let message: String
let isPresent: Bool
let is_present: Bool
}

extension CheckUsernameResponse {
func toDomain() -> CheckUsernameInfo {
return .init(
detailInfo: .init(code: self.code, message: self.message),
isNotExist: !self.isPresent
isNotExist: !self.is_present
)
}
}
4 changes: 2 additions & 2 deletions Project_Timer/Data/TTProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class TTProvider<T: TargetType>: MoyaProvider<T> {
switch result {
case .success(let response):
print("\nTTProvider success", token, "\(token.baseURL)\(token.path)")
// print("-->", String(data: response.data, encoding: .utf8), "\n")
print("-->", String(data: response.data, encoding: .utf8), "\n")
if (200...299).contains(response.statusCode) {
promise(.success(response))
} else {
Expand All @@ -29,7 +29,7 @@ final class TTProvider<T: TargetType>: MoyaProvider<T> {
promise(.success(response))
case .failure(let error):
print("\nTTProvider failure", token, "\(token.baseURL)\(token.path)")
// print("-->", error.localizedDescription, "\n")
print("-->", error.localizedDescription, "\n")
promise(.failure(self.handleError(error)))
}
}
Expand Down
8 changes: 4 additions & 4 deletions Project_Timer/Present/Setting/Main/SettingVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ final class SettingVM {

private func configureSections() {
// MARK: Dev
// if Infos.isDevMode {
if Infos.isDevMode {
self.sections.append(Localized.string(.Settings_Text_ProfileSection))
// }
}

self.sections.append(Localized.string(.Settings_Text_ServiceSection))
self.sections.append(Localized.string(.Settings_Text_SettingSection))
Expand All @@ -43,12 +43,12 @@ final class SettingVM {

var cells: [[SettingCellInfo]] = []
// MARK: Dev
// if Infos.isDevMode {
if Infos.isDevMode {
// Profile
cells.append([
SettingCellInfo(title: Localized.string(.Settings_Button_SingInOption), subTitle: Localized.string(.Settings_Button_SingInOptionDesc), action: .modalFullscreen, destination: .signinSelect)
])
// }
}

// Service
cells.append([
Expand Down
11 changes: 10 additions & 1 deletion Project_Timer/Present/Signin/LoginSelect/SigninSelectView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//

import SwiftUI
import Combine
import Moya

struct SigninSelectView: View {
@EnvironmentObject var environment: SigninSignupEnvironment
Expand Down Expand Up @@ -40,8 +42,15 @@ struct SigninSelectView: View {
switch destination {
case .signupEmail:
let infos = model.signupInfosForEmail
// TODO: DI 수정
let api = TTProvider<UserAPI>(session: Session(interceptor: NetworkInterceptor.shared))
let repository = UserRepository(api: api)
let getUsernameNotExistUseCase = GetUsernameNotExistUseCase(repository: repository)
SignupEmailView(
model: SignupEmailModel(infos: infos)
model: SignupEmailModel(
infos: infos,
getUsernameNotExistUseCase: getUsernameNotExistUseCase
)
)
case .signin:
SigninView()
Expand Down
11 changes: 10 additions & 1 deletion Project_Timer/Present/Signin/LoginView/SigninView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//

import SwiftUI
import Combine
import Moya

struct SigninView: View {
@ObservedObject private var keyboard = KeyboardResponder.shared
Expand Down Expand Up @@ -39,8 +41,15 @@ struct SigninView: View {
Text("findPassword")
case .signup:
let infos = SignupInfosForEmail(type: .normal, venderInfo: nil)
// TODO: DI 수정
let api = TTProvider<UserAPI>(session: Session(interceptor: NetworkInterceptor.shared))
let repository = UserRepository(api: api)
let getUsernameNotExistUseCase = GetUsernameNotExistUseCase(repository: repository)
SignupEmailView(
model: SignupEmailModel(infos: infos)
model: SignupEmailModel(
infos: infos,
getUsernameNotExistUseCase: getUsernameNotExistUseCase
)
)
}
}
Expand Down
53 changes: 45 additions & 8 deletions Project_Timer/Present/Signup/Email/SignupEmailModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,27 @@ class SignupEmailModel: ObservableObject {
case email
case verificationCode
}
enum EmailStatus {
case notValid
case exist
case networkError
case notExist
}

let infos: SignupInfosForEmail
var emailStatus: EmailStatus? {
didSet {
switch self.emailStatus {
case .notValid, .exist, .networkError:
self.isWarningEmail = true
default:
self.isWarningEmail = false
}
}
}
@Published var contentWidth: CGFloat = .zero
@Published var focus: TTSignupTextFieldView.type?
@Published var validEmail: Bool?
@Published var isWarningEmail: Bool = false
@Published var validVerificationCode: Bool?
@Published var getVerificationSuccess: Bool = false
@Published var stage: Stage = .email
Expand All @@ -29,8 +45,12 @@ class SignupEmailModel: ObservableObject {
@Published var verificationCode: String = ""
private var verificationKey = ""

init(infos: SignupInfosForEmail) {
private let getUsernameNotExistUseCase: GetUsernameNotExistUseCase
private var cancellables = Set<AnyCancellable>()

init(infos: SignupInfosForEmail, getUsernameNotExistUseCase: GetUsernameNotExistUseCase) {
self.infos = infos
self.getUsernameNotExistUseCase = getUsernameNotExistUseCase
// vender email 정보를 기본값으로 설정
if let email = infos.venderInfo?.email {
self.email = email
Expand All @@ -39,9 +59,10 @@ class SignupEmailModel: ObservableObject {

// emailTextField underline 컬러
var emailTintColor: Color {
if validEmail == false {
switch self.emailStatus {
case .notValid, .exist, .networkError:
return Colors.wrongTextField.toColor
} else {
default:
return focus == .email ? Color.blue : UIColor.placeholderText.toColor
}
}
Expand Down Expand Up @@ -106,13 +127,29 @@ extension SignupEmailModel {

// email done 액션
func checkEmail() {
validEmail = PredicateChecker.isValidEmail(email)
let validEmail = PredicateChecker.isValidEmail(email)
// stage 변화 -> @FocusState 반영
if validEmail == true {
// API 요청
resetVerificationCode()
self.emailStatus = nil
self.getUsernameNotExistUseCase.execute(username: email)
.sink { [weak self] completion in
if case .failure(let networkError) = completion {
print("ERROR", #function, networkError)
self?.emailStatus = .networkError
}
} receiveValue: { [weak self] checkUsernameInfo in
if checkUsernameInfo.isNotExist {
self?.emailStatus = .notExist
self?.resetVerificationCode()
} else {
print("DetailInfo", #function, checkUsernameInfo.detailInfo)
self?.emailStatus = .exist
}
}
.store(in: &self.cancellables)
} else {
resetEmail()
self.emailStatus = .notValid
self.resetEmail()
}
}

Expand Down
15 changes: 12 additions & 3 deletions Project_Timer/Present/Signup/Email/SignupEmailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//

import SwiftUI
import Combine
import Moya

struct SignupEmailView: View {
@ObservedObject private var keyboard = KeyboardResponder.shared
Expand Down Expand Up @@ -64,10 +66,10 @@ struct SignupEmailView: View {
model.checkEmail()
}
.onChange(of: model.email) { newValue in
model.validEmail = nil
model.emailStatus = nil
}
TTSignupTextFieldUnderlineView(color: model.emailTintColor)
TTSignupTextFieldWarning(warning: Localized.string(.SignUp_Error_WrongEmailFormat), visible: model.validEmail == false)
TTSignupTextFieldWarning(warning: Localized.string(.SignUp_Error_WrongEmailFormat), visible: model.isWarningEmail)
.id(TTSignupTextFieldView.type.email)

if model.stage == .verificationCode {
Expand Down Expand Up @@ -176,8 +178,15 @@ struct SignupEmailView_Previews: PreviewProvider {
static let infos = SignupInfosForEmail(type: .normal, venderInfo: nil)

static var previews: some View {
// TODO: DI 수정
let api = TTProvider<UserAPI>(session: Session(interceptor: NetworkInterceptor.shared))
let repository = UserRepository(api: api)
let getUsernameNotExistUseCase = GetUsernameNotExistUseCase(repository: repository)
SignupEmailView(
model: SignupEmailModel(infos: infos)
model: SignupEmailModel(
infos: infos,
getUsernameNotExistUseCase: getUsernameNotExistUseCase
)
).environmentObject(SigninSignupEnvironment())
}
}

0 comments on commit 79e8448

Please sign in to comment.