Files
sodalive-ios/SodaLive/Sources/Chat/Character/CharacterView.swift

147 lines
5.8 KiB
Swift

//
// CharacterView.swift
// SodaLive
//
// Created by klaus on 8/29/25.
//
import SwiftUI
struct CharacterView: View {
@StateObject var viewModel = CharacterViewModel()
private let horizontalPadding: CGFloat = 16
let onSelectCharacter: (Int) -> Void
let onSelectNewCharacterAll: () -> Void
var body: some View {
BaseView(isLoading: $viewModel.isLoading) {
ScrollView(.vertical, showsIndicators: false) {
VStack(alignment: .leading, spacing: 48) {
//
if !viewModel.banners.isEmpty {
AutoSlideCharacterBannerView(items: viewModel.banners) { banner in
onSelectCharacter(banner.characterId)
}
}
//
if !viewModel.recentCharacters.isEmpty {
RecentCharacterSectionView(
titleCount: viewModel.recentCharacters.count,
items: viewModel.recentCharacters
) { ch in
onSelectCharacter(ch.characterId)
}
}
//
if !viewModel.popularCharacters.isEmpty {
CharacterSectionView(
title: "인기 캐릭터",
items: viewModel.popularCharacters,
isShowRank: true,
onTap: { ch in
onSelectCharacter(ch.characterId)
}
)
}
//
if !viewModel.newCharacters.isEmpty {
CharacterSectionView(
title: "신규 캐릭터",
items: viewModel.newCharacters,
isShowRank: false,
trailingTitle: "전체보기",
onTapTrailing: {
onSelectNewCharacterAll()
},
onTap: { ch in
onSelectCharacter(ch.characterId)
}
)
}
if !viewModel.recommendCharacters.isEmpty {
VStack(alignment: .leading, spacing: 16) {
HStack {
Text("추천 캐릭터")
.font(.custom(Font.preBold.rawValue, size: 24))
.foregroundColor(.white)
Spacer()
Image("ic_refresh")
.onTapGesture {
viewModel.refreshRecommendCharacters()
}
}
.padding(.horizontal, 24)
let horizontalPadding: CGFloat = 24
let gridSpacing: CGFloat = 16
let width = (screenSize().width - (horizontalPadding * 2) - gridSpacing) / 2
LazyVGrid(
columns: Array(
repeating: GridItem(
.flexible(),
spacing: gridSpacing,
alignment: .topLeading
),
count: 2
),
alignment: .leading,
spacing: gridSpacing
) {
ForEach(viewModel.recommendCharacters.indices, id: \.self) { idx in
CharacterItemView(
character: viewModel.recommendCharacters[idx],
size: width,
rank: idx + 1,
isShowRank: false
)
}
}
.padding(.horizontal, horizontalPadding)
}
}
}
.padding(.bottom, 24)
}
.background(Color.black.ignoresSafeArea())
.onAppear {
if !viewModel.isLoading {
viewModel.getCharacterMain()
}
}
}
.popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
GeometryReader { geo in
HStack {
Spacer()
Text(viewModel.errorMessage)
.padding(.vertical, 13.3)
.frame(width: geo.size.width - 66.7, alignment: .center)
.font(.custom(Font.medium.rawValue, size: 12))
.background(Color.button)
.foregroundColor(Color.white)
.multilineTextAlignment(.center)
.cornerRadius(20)
.padding(.top, 66.7)
Spacer()
}
}
}
}
}
#Preview {
CharacterView(
onSelectCharacter: { _ in },
onSelectNewCharacterAll: {}
)
}