Files
sodalive-ios/SodaLive/Sources/Chat/Character/Banner/AutoSlideCharacterBannerView.swift
Yu Sung 62012bd722 feat(image): DownsampledKFImage 추가 및 캐릭터/배너에 공통 적용
- KFImage 공통 옵션(다운샘플링, scaleFactor, backgroundDecode,
  cancelOnDisappear, retry) 캡슐화한 DownsampledKFImage 추가
- 채팅-캐릭터 탭 Character/Recent/배너 뷰에서 인라인 KFImage 제거 → 공통 뷰 적용
- 수평 리스트 HStack → LazyHStack으로 교체해 프리로딩/메모리 개선

Why: 대형 원본 디코딩으로 인한 메모리 스파이크 완화 및 일관된
이미지 로딩 정책 적용. 유지보수성 및 성능 향상.
2025-10-23 17:10:04 +09:00

110 lines
3.2 KiB
Swift

//
// AutoSlideCharacterBannerView.swift
// SodaLive
//
// Created by klaus on 8/29/25.
//
import SwiftUI
import Kingfisher
struct AutoSlideCharacterBannerView: View {
var items: [CharacterBannerResponse] = []
var onTap: (CharacterBannerResponse) -> Void = { _ in }
@State private var currentIndex: Int = 0
@State private var height: CGFloat = 0
private let timer = Timer.publish(every: 4, on: .main, in: .common).autoconnect()
var body: some View {
VStack(spacing: 8) {
TabView(selection: $currentIndex) {
ForEach(0..<items.count, id: \.self) { index in
let item = items[index]
AutoSlideCharacterBannerPage(
item: item,
width: screenSize().width,
height: height,
onTap: onTap
)
.tag(index)
}
}
.tabViewStyle(.page(indexDisplayMode: .never))
.frame(maxWidth: .infinity)
.frame(height: height)
.onAppear {
self.height = screenSize().width * 0.53
}
.onDisappear {
timer.upstream.connect().cancel()
}
.onReceive(timer) { _ in
guard !items.isEmpty else { return }
withAnimation { currentIndex = (currentIndex + 1) % items.count }
}
HStack(spacing: 4) {
ForEach(0..<items.count, id: \.self) { index in
Capsule()
.foregroundColor(
index == currentIndex
? .button
: .gray90
)
.frame(
width: index == currentIndex ? 18 : 6,
height: 6
)
.tag(index)
}
}
}
}
}
#Preview {
AutoSlideCharacterBannerView(
items: [
CharacterBannerResponse(characterId: 1, imageUrl: "https://picsum.photos/1000/300")
]
)
.padding()
.background(Color.black)
}
private struct AutoSlideCharacterBannerPage: View {
let item: CharacterBannerResponse
let width: CGFloat
let height: CGFloat
let onTap: (CharacterBannerResponse) -> Void
@State private var boundURL: URL?
var body: some View {
Group {
if let boundURL {
DownsampledKFImage(
url: boundURL,
size: CGSize(width: width, height: height)
).cornerRadius(12)
} else {
Color.clear
.frame(width: width, height: height)
.cornerRadius(12)
}
}
.contentShape(Rectangle())
.onTapGesture { onTap(item) }
.onAppear {
let encoded = item.imageUrl.addingPercentEncoding(
withAllowedCharacters: .urlQueryAllowed
) ?? item.imageUrl
boundURL = URL(string: encoded)
}
.onDisappear {
boundURL = nil
}
}
}