205 lines
8.1 KiB
Swift
205 lines
8.1 KiB
Swift
//
|
|
// UserProfileDonationAllViewModel.swift
|
|
// SodaLive
|
|
//
|
|
// Created by klaus on 2023/08/29.
|
|
//
|
|
|
|
import Foundation
|
|
import Combine
|
|
|
|
final class UserProfileDonationAllViewModel: ObservableObject {
|
|
private var repository = ExplorerRepository()
|
|
private var memberRepository = UserRepository()
|
|
private var subscription = Set<AnyCancellable>()
|
|
|
|
@Published var errorMessage = ""
|
|
@Published var isShowPopup = false
|
|
@Published var isLoading = false
|
|
|
|
@Published var totalCount = 0
|
|
@Published var accumulatedCansToday = 0
|
|
@Published var accumulatedCansLastWeek = 0
|
|
@Published var accumulatedCansThisMonth = 0
|
|
@Published var isVisibleDonationRank = false
|
|
@Published var userDonationRanking: [UserDonationRankingResponse] = []
|
|
@Published var donationRankingPeriod: DonationRankingPeriod = .weekly
|
|
@Published var selectedRankingPeriod: DonationRankingPeriod? = nil
|
|
|
|
var userId = 0
|
|
var page = 1
|
|
var isLast = false
|
|
private let pageSize = 10
|
|
private var hasLoadedDonationRankingPeriod = false
|
|
|
|
func getCreatorProfileDonationRanking() {
|
|
if (!isLast && !isLoading) {
|
|
isLoading = true
|
|
|
|
let isCreator = userId == UserDefaults.int(forKey: .userId)
|
|
let period: DonationRankingPeriod? = isCreator ? selectedRankingPeriod : nil
|
|
|
|
repository.getCreatorProfileDonationRanking(
|
|
userId: userId,
|
|
page: page,
|
|
size: pageSize,
|
|
period: period
|
|
)
|
|
.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(ApiResponse<GetDonationAllResponse>.self, from: responseData)
|
|
|
|
if let data = decoded.data, decoded.success {
|
|
self.accumulatedCansToday = data.accumulatedCansToday
|
|
self.accumulatedCansLastWeek = data.accumulatedCansLastWeek
|
|
self.accumulatedCansThisMonth = data.accumulatedCansThisMonth
|
|
self.isVisibleDonationRank = data.isVisibleDonationRank
|
|
|
|
if let period = data.donationRankingPeriod {
|
|
self.donationRankingPeriod = period
|
|
self.hasLoadedDonationRankingPeriod = true
|
|
|
|
if self.selectedRankingPeriod == nil {
|
|
self.selectedRankingPeriod = period
|
|
}
|
|
}
|
|
|
|
if !data.userDonationRanking.isEmpty {
|
|
page += 1
|
|
self.totalCount = data.totalCount
|
|
self.userDonationRanking.append(contentsOf: data.userDonationRanking)
|
|
} else {
|
|
isLast = true
|
|
}
|
|
} 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 selectDonationRankingPeriod(_ period: DonationRankingPeriod) {
|
|
guard donationRankingPeriod != period else { return }
|
|
donationRankingPeriod = period
|
|
hasLoadedDonationRankingPeriod = true
|
|
updateDonationRankingPeriod(period)
|
|
}
|
|
|
|
func selectViewRankingPeriod(_ period: DonationRankingPeriod) {
|
|
guard selectedRankingPeriod != period else { return }
|
|
selectedRankingPeriod = period
|
|
resetDonationRanking()
|
|
getCreatorProfileDonationRanking()
|
|
}
|
|
|
|
func toggleVisibleDonationRank() {
|
|
isLoading = true
|
|
|
|
memberRepository.profileUpdate(
|
|
request: ProfileUpdateRequest(
|
|
email: UserDefaults.string(forKey: .email),
|
|
isVisibleDonationRank: !isVisibleDonationRank
|
|
)
|
|
)
|
|
.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(ApiResponse<GetProfileResponse>.self, from: responseData)
|
|
|
|
if decoded.success {
|
|
isVisibleDonationRank.toggle()
|
|
} 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 resetDonationRanking() {
|
|
totalCount = 0
|
|
userDonationRanking = []
|
|
page = 1
|
|
isLast = false
|
|
}
|
|
|
|
private func updateDonationRankingPeriod(_ period: DonationRankingPeriod) {
|
|
memberRepository.profileUpdate(
|
|
request: ProfileUpdateRequest(
|
|
email: UserDefaults.string(forKey: .email),
|
|
donationRankingPeriod: period
|
|
)
|
|
)
|
|
.sink { result in
|
|
switch result {
|
|
case .finished:
|
|
DEBUG_LOG("finish")
|
|
case .failure(let error):
|
|
ERROR_LOG(error.localizedDescription)
|
|
}
|
|
} receiveValue: { [unowned self] response in
|
|
let responseData = response.data
|
|
|
|
do {
|
|
let jsonDecoder = JSONDecoder()
|
|
let decoded = try jsonDecoder.decode(ApiResponse<GetProfileResponse>.self, from: responseData)
|
|
|
|
if !decoded.success {
|
|
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)
|
|
}
|
|
}
|