70 lines
2.2 KiB
Swift
70 lines
2.2 KiB
Swift
//
|
|
// FindPasswordViewModel.swift
|
|
// SodaLive
|
|
//
|
|
// Created by klaus on 2023/08/09.
|
|
//
|
|
|
|
import Foundation
|
|
import Combine
|
|
|
|
import Moya
|
|
|
|
final class FindPasswordViewModel: ObservableObject {
|
|
private let repository = UserRepository()
|
|
private var subscription = Set<AnyCancellable>()
|
|
|
|
@Published var errorMessage = ""
|
|
@Published var isShowPopup = false
|
|
@Published var isLoading = false
|
|
|
|
@Published var email = ""
|
|
|
|
func findPassword() {
|
|
if email.trimmingCharacters(in: .whitespaces).isEmpty {
|
|
errorMessage = I18n.FindPassword.emailRequired
|
|
isShowPopup = true
|
|
return
|
|
}
|
|
|
|
isLoading = true
|
|
repository.findPassword(email: email)
|
|
.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(ApiResponseWithoutData.self, from: responseData)
|
|
|
|
if decoded.success {
|
|
self.email = ""
|
|
self.errorMessage = I18n.FindPassword.successMessage
|
|
self.isShowPopup = true
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
|
AppState.shared.back()
|
|
}
|
|
} else {
|
|
if let message = decoded.message {
|
|
self.errorMessage = message
|
|
} else {
|
|
self.errorMessage = I18n.Common.commonError
|
|
}
|
|
|
|
self.isShowPopup = true
|
|
}
|
|
} catch {
|
|
self.errorMessage = I18n.Common.commonError
|
|
self.isShowPopup = true
|
|
}
|
|
}
|
|
.store(in: &subscription)
|
|
}
|
|
}
|