Files
sodalive-ios/SodaLive/Sources/Chat/Talk/Room/Settings/ChatBgSelectionView.swift
2025-10-23 14:42:50 +09:00

127 lines
4.2 KiB
Swift

//
// ChatBgSelectionView.swift
// SodaLive
//
// Created by klaus on 9/3/25.
//
import SwiftUI
import Kingfisher
struct ChatBgSelectionView: View {
@StateObject var viewModel = ChatBgSelectionViewModel()
private let columns = Array(
repeating: GridItem(.flexible(), spacing: 0),
count: 3
)
let characterId: Int64
let selectedBgImageId: Int
let onTapBgImage: (CharacterImageListItemResponse) -> Void
@Binding var isShowing: Bool
var body: some View {
BaseView(isLoading: $viewModel.isLoading) {
VStack(spacing: 0) {
DetailNavigationBar(title: "배경 이미지 선택") {
isShowing = false
}
//
ScrollView {
LazyVGrid(columns: columns, spacing: 0) {
ForEach(viewModel.galleryItems, id: \.self) { item in
galleryImageView(item: item)
.onAppear {
viewModel.loadMoreIfNeeded(currentItem: item)
}
}
}
.frame(maxWidth: screenSize().width)
}
Spacer()
}
.popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
GeometryReader { geo in
HStack {
Spacer()
Text(viewModel.errorMessage)
.padding(.vertical, 13.3)
.frame(alignment: .center)
.frame(maxWidth: .infinity)
.padding(.horizontal, 33.3)
.font(.custom(Font.medium.rawValue, size: 12))
.background(Color.button)
.foregroundColor(Color.white)
.multilineTextAlignment(.center)
.cornerRadius(20)
.padding(.top, 66.7)
Spacer()
}
}
}
}
.onAppear {
viewModel.characterId = characterId
viewModel.loadInitialData()
}
}
@ViewBuilder
private func galleryImageView(item: CharacterImageListItemResponse) -> some View {
GeometryReader { geo in
let width = geo.size.width
let height = width * 3 / 2
ZStack(alignment: .bottomTrailing) {
//
KFImage(URL(string: item.imageUrl))
.placeholder {
Rectangle()
.fill(Color.gray.opacity(0.3))
}
.cancelOnDisappear(true)
.downsampling(size: CGSize(width: width, height: height))
.resizable()
.scaledToFill()
.frame(width: width, height: height)
.clipped()
.overlay {
Rectangle()
.stroke(lineWidth: 5)
.foregroundColor(.button)
.opacity(selectedBgImageId == item.id ? 1 : 0)
}
if selectedBgImageId == item.id {
Text("현재 배경")
.font(.custom(Font.preRegular.rawValue, size: 12))
.foregroundColor(.white)
.padding(.horizontal, 6)
.padding(.vertical, 2)
.background(Color.button)
.cornerRadius(6.7)
.padding(6)
}
}
.frame(width: width, height: height)
.clipped()
.contentShape(Rectangle())
.onTapGesture { onTapBgImage(item) }
}
.aspectRatio(2/3, contentMode: .fit)
}
}
#Preview {
ChatBgSelectionView(
characterId: 0,
selectedBgImageId: 1,
onTapBgImage: { _ in },
isShowing: .constant(true)
)
}