feat(home): 인기 커뮤니티 섹션을 연결한다
This commit is contained in:
@@ -3,14 +3,18 @@ import Combine
|
||||
|
||||
final class MainHomeRecommendationViewModel: ObservableObject {
|
||||
private let repository = MainHomeRecommendationRepository()
|
||||
private let communityRepository = CreatorCommunityRepository()
|
||||
private var subscription = Set<AnyCancellable>()
|
||||
|
||||
@Published var isLoading = false
|
||||
@Published var isShowPopup = false
|
||||
@Published var errorMessage = ""
|
||||
@Published var recommendations: HomeRecommendationResponse?
|
||||
@Published var isShowCommunityPostPurchaseDialog = false
|
||||
@Published var purchasingCommunityPostPrice = 0
|
||||
@Published private(set) var completedFollowKeys = Set<String>()
|
||||
@Published private(set) var followingKeys = Set<String>()
|
||||
private var purchasingCommunityPostId = 0
|
||||
|
||||
func fetchRecommendations() {
|
||||
isLoading = true
|
||||
@@ -104,4 +108,77 @@ final class MainHomeRecommendationViewModel: ObservableObject {
|
||||
func isFollowAllLoading(_ completionKey: String) -> Bool {
|
||||
followingKeys.contains(completionKey)
|
||||
}
|
||||
|
||||
func likeCommunityPost(postId: Int) {
|
||||
guard postId > 0 else { return }
|
||||
|
||||
communityRepository.communityPostLike(postId: postId)
|
||||
.sink { [weak self] result in
|
||||
guard let self else { return }
|
||||
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
self.errorMessage = I18n.Common.commonError
|
||||
self.isShowPopup = true
|
||||
}
|
||||
} receiveValue: { _ in }
|
||||
.store(in: &subscription)
|
||||
}
|
||||
|
||||
func openCommunityPostPurchase(postId: Int, price: Int) {
|
||||
guard postId > 0, price > 0 else { return }
|
||||
|
||||
purchasingCommunityPostId = postId
|
||||
purchasingCommunityPostPrice = price
|
||||
isShowCommunityPostPurchaseDialog = true
|
||||
}
|
||||
|
||||
func purchaseCommunityPost() {
|
||||
let postId = purchasingCommunityPostId
|
||||
guard postId > 0, !isLoading else { return }
|
||||
|
||||
isLoading = true
|
||||
|
||||
communityRepository.purchaseCommunityPost(postId: postId)
|
||||
.sink { [weak self] result in
|
||||
guard let self else { return }
|
||||
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
self.errorMessage = I18n.Common.commonError
|
||||
self.isShowPopup = true
|
||||
self.isLoading = false
|
||||
}
|
||||
} receiveValue: { [weak self] response in
|
||||
guard let self else { return }
|
||||
|
||||
let responseData = response.data
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponse<GetCommunityPostListResponse>.self, from: responseData)
|
||||
|
||||
if decoded.success {
|
||||
self.purchasingCommunityPostId = 0
|
||||
self.purchasingCommunityPostPrice = 0
|
||||
self.fetchRecommendations()
|
||||
} else {
|
||||
self.errorMessage = decoded.message ?? I18n.Common.commonError
|
||||
self.isShowPopup = true
|
||||
self.isLoading = false
|
||||
}
|
||||
} catch {
|
||||
self.errorMessage = I18n.Common.commonError
|
||||
self.isShowPopup = true
|
||||
self.isLoading = false
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user