Files
sodalive-ios/SodaLive/Sources/V2/Component/Banner/BannerCarousel.swift

59 lines
1.7 KiB
Swift

import SwiftUI
struct BannerCarouselItem: Identifiable, Hashable {
let id: String
let imageUrl: String?
init(id: String, imageUrl: String?) {
self.id = id
self.imageUrl = imageUrl
}
}
struct BannerCarousel: View {
let items: [BannerCarouselItem]
let height: CGFloat
let action: (BannerCarouselItem) -> Void
init(
items: [BannerCarouselItem],
height: CGFloat = 120,
action: @escaping (BannerCarouselItem) -> Void = { _ in }
) {
self.items = items
self.height = height
self.action = action
}
var body: some View {
if !items.isEmpty {
TabView {
ForEach(items) { item in
Button {
action(item)
} label: {
DownsampledKFImage(
url: URL(string: item.imageUrl ?? ""),
size: CGSize(width: UIScreen.main.bounds.width - (SodaSpacing.s20 * 2), height: height)
)
.background(Color.gray800)
.clipShape(RoundedRectangle(cornerRadius: SodaSpacing.s14, style: .continuous))
}
.buttonStyle(.plain)
.padding(.horizontal, SodaSpacing.s20)
}
}
.frame(height: height)
.tabViewStyle(.page(indexDisplayMode: items.count > 1 ? .automatic : .never))
}
}
}
struct BannerCarousel_Previews: PreviewProvider {
static var previews: some View {
BannerCarousel(items: [BannerCarouselItem(id: "1", imageUrl: nil)])
.background(Color.black)
.previewLayout(.sizeThatFits)
}
}