feat(audio-content-all): 무료 콘텐츠, 포인트 대여 콘텐츠 전체보기 페이지 UI/API 구현
This commit is contained in:
107
SodaLive/Sources/Content/All/ContentAllView.swift
Normal file
107
SodaLive/Sources/Content/All/ContentAllView.swift
Normal file
@@ -0,0 +1,107 @@
|
||||
//
|
||||
// ContentAllView.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 11/14/25.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ContentAllView: View {
|
||||
|
||||
@StateObject var viewModel = ContentAllViewModel()
|
||||
|
||||
var isFree: Bool = false
|
||||
var isPointAvailableOnly: Bool = false
|
||||
|
||||
var body: some View {
|
||||
NavigationView {
|
||||
BaseView(isLoading: $viewModel.isLoading) {
|
||||
VStack(spacing: 0) {
|
||||
DetailNavigationBar(title: isFree ? "무료 콘텐츠 전체" : isPointAvailableOnly ? "포인트 대여 전체" : "콘텐츠 전체")
|
||||
|
||||
ScrollView(.vertical, showsIndicators: false) {
|
||||
let horizontalPadding: CGFloat = 24
|
||||
let gridSpacing: CGFloat = 16
|
||||
let itemSize = (screenSize().width - (horizontalPadding * 2) - gridSpacing) / 2
|
||||
|
||||
LazyVGrid(
|
||||
columns: Array(
|
||||
repeating: GridItem(
|
||||
.flexible(),
|
||||
spacing: gridSpacing,
|
||||
alignment: .topLeading
|
||||
),
|
||||
count: 2
|
||||
),
|
||||
alignment: .leading,
|
||||
spacing: gridSpacing
|
||||
) {
|
||||
ForEach(viewModel.contentList.indices, id: \.self) { idx in
|
||||
let item = viewModel.contentList[idx]
|
||||
|
||||
NavigationLink {
|
||||
ContentDetailView(contentId: item.contentId)
|
||||
} label: {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
ZStack(alignment: .top) {
|
||||
DownsampledKFImage(
|
||||
url: URL(string: item.coverImageUrl),
|
||||
size: CGSize(width: itemSize, height: itemSize)
|
||||
)
|
||||
.cornerRadius(16)
|
||||
|
||||
HStack(alignment: .top, spacing: 0) {
|
||||
Spacer()
|
||||
|
||||
if item.isPointAvailable {
|
||||
Image("ic_point")
|
||||
.padding(.top, 6)
|
||||
.padding(.trailing, 6)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Text(item.title)
|
||||
.font(.custom(Font.preRegular.rawValue, size: 18))
|
||||
.foregroundColor(.white)
|
||||
.multilineTextAlignment(.leading)
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
.lineLimit(1)
|
||||
.padding(.horizontal, 6)
|
||||
.padding(.top, 8)
|
||||
|
||||
|
||||
Text(item.creatorNickname)
|
||||
.font(.custom(Font.preRegular.rawValue, size: 14))
|
||||
.foregroundColor(Color(hex: "78909C"))
|
||||
.lineLimit(1)
|
||||
.padding(.horizontal, 6)
|
||||
.padding(.top, 4)
|
||||
}
|
||||
.frame(width: itemSize)
|
||||
.contentShape(Rectangle())
|
||||
.onAppear {
|
||||
if idx == viewModel.contentList.count - 1 {
|
||||
viewModel.fetchData()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(horizontalPadding)
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
viewModel.isFree = isFree
|
||||
viewModel.isPointAvailableOnly = isPointAvailableOnly
|
||||
viewModel.fetchData()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ContentAllView()
|
||||
}
|
||||
78
SodaLive/Sources/Content/All/ContentAllViewModel.swift
Normal file
78
SodaLive/Sources/Content/All/ContentAllViewModel.swift
Normal file
@@ -0,0 +1,78 @@
|
||||
//
|
||||
// ContentAllViewModel.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 11/14/25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
final class ContentAllViewModel: ObservableObject {
|
||||
private let repository = ContentRepository()
|
||||
private var subscription = Set<AnyCancellable>()
|
||||
|
||||
@Published var errorMessage = ""
|
||||
@Published var isShowPopup = false
|
||||
@Published var isLoading = false
|
||||
|
||||
@Published var contentList = [AudioContentMainItem]()
|
||||
|
||||
var page = 1
|
||||
var isLast = false
|
||||
private let pageSize = 10
|
||||
|
||||
var isFree: Bool = false
|
||||
var isPointAvailableOnly: Bool = false
|
||||
|
||||
func fetchData() {
|
||||
if !isLast && !isLoading {
|
||||
isLoading = true
|
||||
|
||||
repository.getAllAudioContents(page: page, size: pageSize, isFree: isFree, isPointAvailableOnly: isPointAvailableOnly)
|
||||
.sink { result in
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
}
|
||||
} receiveValue: { [unowned self] response in
|
||||
self.isLoading = false
|
||||
let responseData = response.data
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponse<[AudioContentMainItem]>.self, from: responseData)
|
||||
self.isLoading = false
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
if page == 1 {
|
||||
contentList.removeAll()
|
||||
}
|
||||
|
||||
if !data.isEmpty {
|
||||
page += 1
|
||||
self.contentList.append(contentsOf: data)
|
||||
} else {
|
||||
isLast = true
|
||||
}
|
||||
} else {
|
||||
if let message = decoded.message {
|
||||
self.errorMessage = message
|
||||
} else {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
}
|
||||
|
||||
self.isShowPopup = true
|
||||
}
|
||||
} catch {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
self.isShowPopup = true
|
||||
self.isLoading = false
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user