feat(chat-room-bg): 배경 이미지 변경 기능 추가

This commit is contained in:
Yu Sung
2025-09-04 16:44:57 +09:00
parent a025e455a0
commit ee67e1eab7
10 changed files with 277 additions and 18 deletions

View File

@@ -8,11 +8,117 @@
import SwiftUI
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 {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
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 * 5 / 4
ZStack(alignment: .bottomTrailing) {
//
AsyncImage(url: URL(string: item.imageUrl)) { image in
image
.resizable()
.scaledToFill()
.frame(width: width, height: height)
.clipped()
} placeholder: {
Rectangle()
.fill(Color.gray.opacity(0.3))
}
.overlay {
Rectangle()
.stroke(lineWidth: 1)
.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(4/5, contentMode: .fit)
}
}
#Preview {
ChatBgSelectionView()
ChatBgSelectionView(
characterId: 0,
selectedBgImageId: 1,
onTapBgImage: { _ in },
isShowing: .constant(true)
)
}

View File

@@ -0,0 +1,96 @@
//
// ChatBgSelectionViewModel.swift
// SodaLive
//
// Created by klaus on 9/4/25.
//
import Foundation
import Combine
import Moya
final class ChatBgSelectionViewModel: ObservableObject {
// MARK: - Published State
@Published var isLoading: Bool = false
@Published var errorMessage: String = ""
@Published var isShowPopup = false
@Published var galleryItems: [CharacterImageListItemResponse] = []
private let repository = CharacterDetailGalleryRepository()
private var subscription = Set<AnyCancellable>()
private var currentPage = 0
private var isLast = false
var characterId: Int64 = 0
// MARK: - Public Methods
func loadInitialData() {
currentPage = 0
isLast = false
galleryItems.removeAll()
loadImageList()
}
func loadMoreIfNeeded(currentItem: CharacterImageListItemResponse) {
guard !isLast,
!isLoading,
let lastItem = galleryItems.last,
lastItem.id == currentItem.id else { return }
loadImageList()
}
// MARK: - Private Methods
private func loadImageList() {
guard !isLoading else { return }
isLoading = true
repository.getMyCharacterImageList(
characterId: characterId,
page: currentPage,
size: 20
)
.receive(on: DispatchQueue.main)
.sink { result in
switch result {
case .finished:
DEBUG_LOG("finish")
case .failure(let error):
ERROR_LOG(error.localizedDescription)
}
} receiveValue: { [weak self] response in
let responseData = response.data
self?.isLoading = false
do {
let jsonDecoder = JSONDecoder()
let decoded = try jsonDecoder.decode(ApiResponse<CharacterImageListResponse>.self, from: responseData)
if let data = decoded.data, decoded.success {
self?.currentPage += 1
self?.handleImageListResponse(data)
} else {
if let message = decoded.message {
self?.errorMessage = message
} else {
self?.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
}
self?.isShowPopup = true
}
} catch {
ERROR_LOG(String(describing: error))
self?.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self?.isShowPopup = true
}
}
.store(in: &subscription)
}
private func handleImageListResponse(_ response: CharacterImageListResponse) {
galleryItems.append(contentsOf: response.items)
isLast = response.items.isEmpty
}
}

View File

@@ -56,6 +56,7 @@ struct ChatSettingsView: View {
.frame(maxWidth: .infinity)
.frame(height: 1)
}
.contentShape(Rectangle())
.onTapGesture { onTapChangeBg() }
HStack(spacing: 0) {
@@ -100,6 +101,7 @@ struct ChatSettingsView: View {
}
.padding(.horizontal, 24)
.padding(.vertical, 12)
.contentShape(Rectangle())
.onTapGesture { onTapResetChatRoom() }
}
}