feat(home): 인기 커뮤니티 섹션을 연결한다
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import SwiftUI
|
||||
|
||||
struct MainHomePopularCommunitySection: View {
|
||||
let items: [HomePopularCommunityPostItem]
|
||||
let onTapCommunity: (Int) -> Void
|
||||
let onTapLike: (Int) -> Void
|
||||
let onTapPurchase: (HomePopularCommunityPostItem) -> Void
|
||||
|
||||
init(
|
||||
items: [HomePopularCommunityPostItem],
|
||||
onTapCommunity: @escaping (Int) -> Void = { _ in },
|
||||
onTapLike: @escaping (Int) -> Void = { _ in },
|
||||
onTapPurchase: @escaping (HomePopularCommunityPostItem) -> Void = { _ in }
|
||||
) {
|
||||
self.items = items
|
||||
self.onTapCommunity = onTapCommunity
|
||||
self.onTapLike = onTapLike
|
||||
self.onTapPurchase = onTapPurchase
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
if !items.isEmpty {
|
||||
VStack(alignment: .leading, spacing: SodaSpacing.s14) {
|
||||
SectionTitle(title: I18n.HomeRecommendation.popularCommunitySectionTitle)
|
||||
|
||||
VStack(alignment: .leading, spacing: SodaSpacing.s8) {
|
||||
ForEach(items, id: \.postId) { item in
|
||||
CommunityPostCard(
|
||||
postId: item.postId,
|
||||
creatorNickname: item.creatorNickname,
|
||||
creatorProfileImageUrl: item.creatorProfileImage,
|
||||
content: item.content,
|
||||
imageUrl: item.imageUrl,
|
||||
audioUrl: item.audioUrl,
|
||||
price: item.price,
|
||||
existOrdered: item.existOrdered,
|
||||
createdAt: item.relativeCreatedAtText(),
|
||||
isLike: item.isLiked,
|
||||
likeCount: item.likeCount,
|
||||
commentCount: item.commentCount,
|
||||
onTapLike: {
|
||||
onTapLike(item.postId)
|
||||
},
|
||||
onTapComment: {
|
||||
onTapCommunity(item.creatorId)
|
||||
},
|
||||
onTapPurchase: {
|
||||
onTapPurchase(item)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, SodaSpacing.s14)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct MainHomePopularCommunitySection_Previews: PreviewProvider {
|
||||
private static let previewImageUrl = "https://picsum.photos/500/500"
|
||||
|
||||
static var previews: some View {
|
||||
MainHomePopularCommunitySection(
|
||||
items: [
|
||||
HomePopularCommunityPostItem(
|
||||
postId: 1,
|
||||
creatorId: 10,
|
||||
creatorNickname: "크리에이터 이름",
|
||||
creatorProfileImage: previewImageUrl,
|
||||
content: "크리에이터가 커뮤니티에 올린 글이 보이는 부분 크리에이터가 커뮤니티에 올린 글이 보이는 부분",
|
||||
imageUrl: nil,
|
||||
audioUrl: nil,
|
||||
price: 0,
|
||||
existOrdered: false,
|
||||
likeCount: 5,
|
||||
commentCount: 6,
|
||||
createdAt: "2026-06-29T12:00:00Z",
|
||||
isLiked: false
|
||||
),
|
||||
HomePopularCommunityPostItem(
|
||||
postId: 2,
|
||||
creatorId: 11,
|
||||
creatorNickname: "크리에이터 이름",
|
||||
creatorProfileImage: previewImageUrl,
|
||||
content: "이미지가 있는 커뮤니티 게시글입니다.",
|
||||
imageUrl: previewImageUrl,
|
||||
audioUrl: "https://example.com/audio.mp3",
|
||||
price: 30,
|
||||
existOrdered: false,
|
||||
likeCount: 5,
|
||||
commentCount: 6,
|
||||
createdAt: "2026-06-29T12:00:00Z",
|
||||
isLiked: true
|
||||
)
|
||||
]
|
||||
)
|
||||
.padding(.vertical, SodaSpacing.s20)
|
||||
.background(Color.black)
|
||||
.previewLayout(.sizeThatFits)
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ struct MainHomeRecommendationView: View {
|
||||
let onTapFollowAll: (@escaping () -> Void) -> Void
|
||||
|
||||
@StateObject private var viewModel = MainHomeRecommendationViewModel()
|
||||
@StateObject private var communityMediaPlayer = CreatorCommunityMediaPlayerManager.shared
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
@@ -75,6 +76,17 @@ struct MainHomeRecommendationView: View {
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
MainHomePopularCommunitySection(
|
||||
items: viewModel.recommendations?.popularCommunityPosts ?? [],
|
||||
onTapCommunity: onTapCommunity,
|
||||
onTapLike: { postId in
|
||||
viewModel.likeCommunityPost(postId: postId)
|
||||
},
|
||||
onTapPurchase: { item in
|
||||
viewModel.openCommunityPostPurchase(postId: item.postId, price: item.price)
|
||||
}
|
||||
)
|
||||
}
|
||||
.padding(.vertical, SodaSpacing.s20)
|
||||
}
|
||||
@@ -90,6 +102,24 @@ struct MainHomeRecommendationView: View {
|
||||
message: viewModel.errorMessage,
|
||||
autohideIn: 2
|
||||
)
|
||||
.sodaToast(
|
||||
isPresented: $communityMediaPlayer.isShowPopup,
|
||||
message: communityMediaPlayer.errorMessage,
|
||||
autohideIn: 2
|
||||
)
|
||||
.overlay {
|
||||
if viewModel.isShowCommunityPostPurchaseDialog {
|
||||
CommunityPostPurchaseDialog(
|
||||
isShowing: $viewModel.isShowCommunityPostPurchaseDialog,
|
||||
can: viewModel.purchasingCommunityPostPrice,
|
||||
confirmAction: viewModel.purchaseCommunityPost
|
||||
)
|
||||
}
|
||||
|
||||
if communityMediaPlayer.isLoading {
|
||||
LoadingView()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,4 +79,9 @@ struct HomePopularCommunityPostItem: Decodable, Hashable {
|
||||
let likeCount: Int
|
||||
let commentCount: Int
|
||||
let createdAt: String
|
||||
let isLiked: Bool
|
||||
|
||||
func relativeCreatedAtText(now: Date = Date()) -> String {
|
||||
DateParser.relativeTimeText(fromUTC: createdAt, fallback: createdAt, now: now)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user