마이페이지 추가
This commit is contained in:
110
SodaLive/Sources/MyPage/MyPageViewModel.swift
Normal file
110
SodaLive/Sources/MyPage/MyPageViewModel.swift
Normal file
@@ -0,0 +1,110 @@
|
||||
//
|
||||
// MyPageViewModel.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/08/10.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
final class MyPageViewModel: ObservableObject {
|
||||
|
||||
private let repository = UserRepository()
|
||||
private let authRepository = AuthRepository()
|
||||
private var subscription = Set<AnyCancellable>()
|
||||
|
||||
@Published var myPageResponse: MyPageResponse? = nil
|
||||
|
||||
@Published var errorMessage = ""
|
||||
@Published var isShowPopup = false
|
||||
@Published var isLoading = false
|
||||
@Published var isShowAuthView = false
|
||||
|
||||
func getMypage() {
|
||||
isLoading = true
|
||||
|
||||
repository.getMypage()
|
||||
.sink { result in
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
}
|
||||
} receiveValue: { response in
|
||||
self.isLoading = false
|
||||
let responseData = response.data
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponse<MyPageResponse>.self, from: responseData)
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
self.myPageResponse = data
|
||||
} else {
|
||||
if let message = decoded.message {
|
||||
self.errorMessage = message
|
||||
} else {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
}
|
||||
|
||||
self.isShowPopup = true
|
||||
}
|
||||
} catch {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
self.isShowPopup = true
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
}
|
||||
|
||||
func authVerify(_ data: [String: Any]) {
|
||||
isLoading = true
|
||||
|
||||
let _data = data["data"] as? [String: Any]
|
||||
|
||||
if let data = _data {
|
||||
let receiptId = data["receipt_id"] as! String
|
||||
|
||||
authRepository.authCertificate(receiptId: receiptId)
|
||||
.sink { result in
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
}
|
||||
} receiveValue: { [unowned self] response in
|
||||
self.isLoading = false
|
||||
let responseData = response.data
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponseWithoutData.self, from: responseData)
|
||||
|
||||
if decoded.success {
|
||||
self.errorMessage = "본인인증이 완료되었습니다."
|
||||
self.isShowPopup = true
|
||||
self.getMypage()
|
||||
} else {
|
||||
if let message = decoded.message {
|
||||
self.errorMessage = message
|
||||
} else {
|
||||
self.errorMessage = "본인인증 중 오류가 발생했습니다.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
}
|
||||
self.isShowPopup = true
|
||||
}
|
||||
} catch {
|
||||
self.errorMessage = "본인인증 중 오류가 발생했습니다.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
self.isShowPopup = true
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
} else {
|
||||
isLoading = false
|
||||
errorMessage = "본인인증 중 오류가 발생했습니다."
|
||||
isShowPopup = true
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user