132 lines
4.2 KiB
Swift
132 lines
4.2 KiB
Swift
import Foundation
|
|
import Combine
|
|
|
|
final class ChannelDonationViewModel: ObservableObject {
|
|
private var repository = ExplorerRepository()
|
|
private var subscription = Set<AnyCancellable>()
|
|
|
|
@Published var errorMessage = ""
|
|
@Published var isShowPopup = false
|
|
@Published var isLoading = false
|
|
|
|
@Published var totalCount = 0
|
|
@Published var donationItems: [GetChannelDonationListItem] = []
|
|
|
|
private var creatorId = 0
|
|
|
|
func setCreatorId(_ creatorId: Int, shouldFetch: Bool = true) {
|
|
guard creatorId > 0 else { return }
|
|
|
|
if self.creatorId != creatorId {
|
|
self.creatorId = creatorId
|
|
}
|
|
|
|
if shouldFetch {
|
|
getChannelDonationList()
|
|
}
|
|
}
|
|
|
|
func getChannelDonationList() {
|
|
guard creatorId > 0, !isLoading else { return }
|
|
|
|
isLoading = true
|
|
|
|
repository.getChannelDonationList(creatorId: creatorId)
|
|
.sink { [weak self] result in
|
|
guard let self = self else { return }
|
|
|
|
switch result {
|
|
case .finished:
|
|
DEBUG_LOG("finish")
|
|
|
|
case .failure(let error):
|
|
ERROR_LOG(error.localizedDescription)
|
|
self.isLoading = false
|
|
self.errorMessage = I18n.Common.commonError
|
|
self.isShowPopup = true
|
|
}
|
|
} receiveValue: { [weak self] response in
|
|
guard let self = self else { return }
|
|
|
|
self.isLoading = false
|
|
|
|
do {
|
|
let jsonDecoder = JSONDecoder()
|
|
let decoded = try jsonDecoder.decode(ApiResponse<GetChannelDonationListResponse>.self, from: response.data)
|
|
|
|
if let data = decoded.data, decoded.success {
|
|
self.totalCount = data.totalCount
|
|
self.donationItems = data.items
|
|
} else {
|
|
self.errorMessage = decoded.message ?? I18n.Common.commonError
|
|
self.isShowPopup = true
|
|
}
|
|
} catch {
|
|
self.errorMessage = I18n.Common.commonError
|
|
self.isShowPopup = true
|
|
}
|
|
}
|
|
.store(in: &subscription)
|
|
}
|
|
|
|
func postChannelDonation(
|
|
can: Int,
|
|
message: String,
|
|
isSecret: Bool,
|
|
reloadAfterSuccess: Bool = true,
|
|
onSuccess: (() -> Void)? = nil
|
|
) {
|
|
guard creatorId > 0, !isLoading else { return }
|
|
|
|
isLoading = true
|
|
|
|
let request = PostChannelDonationRequest(
|
|
creatorId: creatorId,
|
|
can: can,
|
|
isSecret: isSecret,
|
|
message: message
|
|
)
|
|
|
|
repository.postChannelDonation(request: request)
|
|
.sink { [weak self] result in
|
|
guard let self = self else { return }
|
|
|
|
switch result {
|
|
case .finished:
|
|
DEBUG_LOG("finish")
|
|
|
|
case .failure(let error):
|
|
ERROR_LOG(error.localizedDescription)
|
|
self.isLoading = false
|
|
self.errorMessage = I18n.Common.commonError
|
|
self.isShowPopup = true
|
|
}
|
|
} receiveValue: { [weak self] response in
|
|
guard let self = self else { return }
|
|
|
|
do {
|
|
let jsonDecoder = JSONDecoder()
|
|
let decoded = try jsonDecoder.decode(ApiResponseWithoutData.self, from: response.data)
|
|
|
|
self.isLoading = false
|
|
|
|
if decoded.success {
|
|
if reloadAfterSuccess {
|
|
self.getChannelDonationList()
|
|
} else {
|
|
onSuccess?()
|
|
}
|
|
} else {
|
|
self.errorMessage = decoded.message ?? I18n.Common.commonError
|
|
self.isShowPopup = true
|
|
}
|
|
} catch {
|
|
self.isLoading = false
|
|
self.errorMessage = I18n.Common.commonError
|
|
self.isShowPopup = true
|
|
}
|
|
}
|
|
.store(in: &subscription)
|
|
}
|
|
}
|