94 lines
3.4 KiB
Swift
94 lines
3.4 KiB
Swift
//
|
|
// SignOutViewModel.swift
|
|
// SodaLive
|
|
//
|
|
// Created by klaus on 2023/08/10.
|
|
//
|
|
|
|
import Foundation
|
|
import Combine
|
|
|
|
final class SignOutViewModel: ObservableObject {
|
|
|
|
private let repository = UserRepository()
|
|
private var subscription = Set<AnyCancellable>()
|
|
|
|
let reasons = [
|
|
"닉네임을 변경하고 싶어서",
|
|
"다른 사용자와의 다툼이 있어서",
|
|
"이용이 불편하고 장애가 많아서",
|
|
"서비스 운영이 마음에 들지 않아서",
|
|
"다른 서비스가 더 좋아서",
|
|
"삭제하고 싶은 내용이 있어서",
|
|
"이용빈도가 낮아서",
|
|
"원하는 콘텐츠나 크리에이터가 없어서",
|
|
"이용요금이 비싸서",
|
|
"기타"
|
|
]
|
|
|
|
@Published var reasonSelectedIndex = -1
|
|
@Published var reason = ""
|
|
@Published var password = ""
|
|
|
|
@Published var errorMessage = ""
|
|
@Published var isShowPopup = false
|
|
@Published var isLoading = false
|
|
|
|
func signOut() {
|
|
if validate() {
|
|
isLoading = true
|
|
let reason = reasonSelectedIndex == reasons.count - 1 ? self.reason : reasons[reasonSelectedIndex]
|
|
repository.signOut(reason: reason, password: password)
|
|
.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 {
|
|
UserDefaults.reset()
|
|
AppState.shared.setAppStep(step: .splash)
|
|
} 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)
|
|
}
|
|
}
|
|
|
|
private func validate() -> Bool {
|
|
if reasonSelectedIndex < 0 || (reasonSelectedIndex == reasons.count - 1 && self.reason.trimmingCharacters(in: .whitespaces).isEmpty) {
|
|
errorMessage = "계정을 삭제하려는 이유를 선택해 주세요."
|
|
isShowPopup = true
|
|
return false
|
|
}
|
|
|
|
if password.isEmpty {
|
|
errorMessage = "비밀번호를 입력해 주세요."
|
|
isShowPopup = true
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
}
|