generated from Xixn2/Tuist_Modular_Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
333 additions
and
503 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
Projects/App/Sources/Feature/UserInfoFeature/Sources/UserInfoViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,59 @@ | ||
import Foundation | ||
import Combine | ||
import Moya | ||
import Domain | ||
|
||
final class UserInfoViewModel: ObservableObject { | ||
// 입력된 사용자 정보 | ||
@Published var name: String = "" | ||
@Published var number: String = "" | ||
|
||
// 네트워크 요청 상태를 나타내는 퍼블리셔 | ||
@Published var isLoading: Bool = false | ||
@Published var errorMessage: String? = nil | ||
|
||
// Moya 프로바이더 초기화 | ||
private let provider = MoyaProvider<UserAPI>() | ||
|
||
private func fetchAccessToken() -> String? { | ||
return KeyChain.shared.read(key: Const.KeyChainKey.accessToken) | ||
} | ||
|
||
// 사용자 정보 제출 함수 | ||
func submitUserInfo() { | ||
// 로딩 상태 시작 | ||
isLoading = true | ||
errorMessage = nil | ||
print("name: \(name), number: \(number)") | ||
|
||
// 인증 토큰 (실제 앱에서는 안전한 저장소에서 가져와야 함) | ||
guard let authorizationToken = fetchAccessToken() else { return } | ||
|
||
// API 호출 | ||
provider.request(.userInfoInput(authorization: authorizationToken, name: name, number: number, file: nil)) { [weak self] result in | ||
// 로딩 상태 종료 | ||
DispatchQueue.main.async { | ||
self?.isLoading = false | ||
} | ||
|
||
switch result { | ||
case .success(let response): | ||
// 서버 응답 처리 | ||
if (200...299).contains(response.statusCode) { | ||
// 성공 처리 | ||
print("사용자 정보가 성공적으로 전송되었습니다.") | ||
} else { | ||
// 서버 오류 메시지 처리 | ||
DispatchQueue.main.async { | ||
self?.errorMessage = "서버 오류: \(response.statusCode)" | ||
} | ||
} | ||
case .failure(let error): | ||
// 네트워크 오류 처리 | ||
DispatchQueue.main.async { | ||
self?.errorMessage = "네트워크 오류: \(error.localizedDescription)" | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.