feat(home): 추천 배너 섹션을 연결한다
This commit is contained in:
@@ -9,6 +9,7 @@ struct MainHomeView: View {
|
||||
let onTapContent: (Int) -> Void
|
||||
let onTapCharacter: (Int) -> Void
|
||||
let onTapCommunity: (Int) -> Void
|
||||
let onTapBanner: (RecommendationBannerResponse) -> Void
|
||||
|
||||
@State private var selectedTab: MainHomeTab = .recommendation
|
||||
|
||||
@@ -48,7 +49,8 @@ struct MainHomeView: View {
|
||||
onTapCreator: onTapCreator,
|
||||
onTapContent: onTapContent,
|
||||
onTapCharacter: onTapCharacter,
|
||||
onTapCommunity: onTapCommunity
|
||||
onTapCommunity: onTapCommunity,
|
||||
onTapBanner: onTapBanner
|
||||
)
|
||||
case .ranking:
|
||||
MainHomeRankingView()
|
||||
@@ -85,7 +87,8 @@ struct MainHomeView_Previews: PreviewProvider {
|
||||
onTapCreator: { _ in },
|
||||
onTapContent: { _ in },
|
||||
onTapCharacter: { _ in },
|
||||
onTapCommunity: { _ in }
|
||||
onTapCommunity: { _ in },
|
||||
onTapBanner: { _ in }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import SwiftUI
|
||||
|
||||
struct MainHomeBannerSection: View {
|
||||
let items: [RecommendationBannerResponse]
|
||||
let onTapBanner: (RecommendationBannerResponse) -> Void
|
||||
|
||||
init(
|
||||
items: [RecommendationBannerResponse],
|
||||
onTapBanner: @escaping (RecommendationBannerResponse) -> Void = { _ in }
|
||||
) {
|
||||
self.items = items
|
||||
self.onTapBanner = onTapBanner
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
if !items.isEmpty {
|
||||
BannerCarousel(items: carouselItems) { item in
|
||||
guard let banner = banner(for: item) else { return }
|
||||
onTapBanner(banner)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var carouselItems: [BannerCarouselItem] {
|
||||
items.enumerated().map { index, item in
|
||||
BannerCarouselItem(
|
||||
id: fallbackId(for: item, index: index),
|
||||
imageUrl: item.imageUrl
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private func banner(for carouselItem: BannerCarouselItem) -> RecommendationBannerResponse? {
|
||||
items.enumerated().first { index, item in
|
||||
fallbackId(for: item, index: index) == carouselItem.id
|
||||
}?.element
|
||||
}
|
||||
|
||||
private func fallbackId(for item: RecommendationBannerResponse, index: Int) -> String {
|
||||
if let eventItem = item.eventItem {
|
||||
return "banner-\(index)-event-\(eventItem.id)"
|
||||
}
|
||||
|
||||
if let creatorId = item.creatorId {
|
||||
return "banner-\(index)-creator-\(creatorId)"
|
||||
}
|
||||
|
||||
if let seriesId = item.seriesId {
|
||||
return "banner-\(index)-series-\(seriesId)"
|
||||
}
|
||||
|
||||
if let link = item.link, !link.isEmpty {
|
||||
return "banner-\(index)-link-\(link)"
|
||||
}
|
||||
|
||||
return "banner-\(index)-image-\(item.imageUrl)"
|
||||
}
|
||||
}
|
||||
|
||||
struct MainHomeBannerSection_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
MainHomeBannerSection(
|
||||
items: [
|
||||
RecommendationBannerResponse(
|
||||
imageUrl: "https://picsum.photos/500/500",
|
||||
eventItem: nil,
|
||||
creatorId: nil,
|
||||
seriesId: nil,
|
||||
link: nil
|
||||
),
|
||||
RecommendationBannerResponse(
|
||||
imageUrl: "https://picsum.photos/500/500",
|
||||
eventItem: nil,
|
||||
creatorId: 1,
|
||||
seriesId: nil,
|
||||
link: nil
|
||||
)
|
||||
]
|
||||
)
|
||||
.background(Color.black)
|
||||
.previewLayout(.sizeThatFits)
|
||||
}
|
||||
}
|
||||
@@ -55,7 +55,7 @@ private struct MainHomeLiveItemView: View {
|
||||
}
|
||||
|
||||
struct MainHomeLiveSection_Previews: PreviewProvider {
|
||||
private static let previewImageUrl = "https://placehold.co/500"
|
||||
private static let previewImageUrl = "https://picsum.photos/500/500"
|
||||
|
||||
static var previews: some View {
|
||||
Group {
|
||||
|
||||
@@ -6,6 +6,7 @@ struct MainHomeRecommendationView: View {
|
||||
let onTapContent: (Int) -> Void
|
||||
let onTapCharacter: (Int) -> Void
|
||||
let onTapCommunity: (Int) -> Void
|
||||
let onTapBanner: (RecommendationBannerResponse) -> Void
|
||||
|
||||
@StateObject private var viewModel = MainHomeRecommendationViewModel()
|
||||
|
||||
@@ -24,6 +25,11 @@ struct MainHomeRecommendationView: View {
|
||||
items: viewModel.recommendations?.lives ?? [],
|
||||
onTapLive: onTapLive
|
||||
)
|
||||
|
||||
MainHomeBannerSection(
|
||||
items: viewModel.recommendations?.banners ?? [],
|
||||
onTapBanner: onTapBanner
|
||||
)
|
||||
}
|
||||
.padding(.vertical, SodaSpacing.s20)
|
||||
}
|
||||
@@ -49,7 +55,8 @@ struct MainHomeRecommendationView_Previews: PreviewProvider {
|
||||
onTapCreator: { _ in },
|
||||
onTapContent: { _ in },
|
||||
onTapCharacter: { _ in },
|
||||
onTapCommunity: { _ in }
|
||||
onTapCommunity: { _ in },
|
||||
onTapBanner: { _ in }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,14 +19,11 @@ struct HomeLiveItem: Decodable, Hashable {
|
||||
}
|
||||
|
||||
struct RecommendationBannerResponse: Decodable, Hashable {
|
||||
let bannerId: Int?
|
||||
let type: String?
|
||||
let title: String?
|
||||
let imageUrl: String?
|
||||
let link: String?
|
||||
let eventId: Int?
|
||||
let imageUrl: String
|
||||
let eventItem: EventItem?
|
||||
let creatorId: Int?
|
||||
let seriesId: Int?
|
||||
let link: String?
|
||||
}
|
||||
|
||||
struct HomeActiveCreatorItem: Decodable, Hashable {
|
||||
|
||||
@@ -161,7 +161,8 @@ struct MainView: View {
|
||||
onTapCreator: handleRecommendationCreatorTap,
|
||||
onTapContent: handleRecommendationContentTap,
|
||||
onTapCharacter: handleRecommendationCharacterTap,
|
||||
onTapCommunity: handleRecommendationCommunityTap
|
||||
onTapCommunity: handleRecommendationCommunityTap,
|
||||
onTapBanner: handleRecommendationBannerTap
|
||||
)
|
||||
case .content:
|
||||
MainPlaceholderTabView(title: MainTab.content.title)
|
||||
@@ -478,6 +479,41 @@ struct MainView: View {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private func handleRecommendationBannerTap(_ item: RecommendationBannerResponse) {
|
||||
if let eventItem = item.eventItem {
|
||||
performRecommendationDetailAction {
|
||||
appState.setAppStep(step: .eventDetail(event: eventItem))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if let creatorId = item.creatorId {
|
||||
handleRecommendationCreatorTap(creatorId: creatorId)
|
||||
return
|
||||
}
|
||||
|
||||
if let seriesId = item.seriesId {
|
||||
performRecommendationDetailAction {
|
||||
appState.setAppStep(step: .seriesDetail(seriesId: seriesId))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
openRecommendationBannerLink(item.link)
|
||||
}
|
||||
|
||||
private func openRecommendationBannerLink(_ link: String?) {
|
||||
guard let link = link?.trimmingCharacters(in: .whitespacesAndNewlines),
|
||||
!link.isEmpty,
|
||||
let url = URL(string: link),
|
||||
UIApplication.shared.canOpenURL(url) else {
|
||||
return
|
||||
}
|
||||
|
||||
UIApplication.shared.open(url)
|
||||
}
|
||||
|
||||
private func performRecommendationDetailAction(
|
||||
requiresAdultContent: Bool = false,
|
||||
action: @escaping () -> Void
|
||||
|
||||
Reference in New Issue
Block a user