콘텐츠 메인 - 숏플, 라이브 다시 듣기 추가

This commit is contained in:
Yu Sung
2024-02-14 15:49:13 +09:00
parent c787027dcc
commit 7d0c472885
17 changed files with 380 additions and 3 deletions

View File

@@ -0,0 +1,111 @@
//
// ContentAllByThemeView.swift
// SodaLive
//
// Created by klaus on 2/14/24.
//
import SwiftUI
struct ContentAllByThemeView: View {
@StateObject var viewModel = ContentAllByThemeViewModel()
let themeId: Int
let columns = [
GridItem(.flexible(), alignment: .top),
GridItem(.flexible(), alignment: .top),
GridItem(.flexible(), alignment: .top)
]
var body: some View {
NavigationView {
BaseView(isLoading: $viewModel.isLoading) {
VStack(alignment: .leading, spacing: 0) {
DetailNavigationBar(title: viewModel.theme)
HStack(spacing: 13.3) {
Spacer()
Text("최신순")
.font(.custom(Font.medium.rawValue, size: 13.3))
.foregroundColor(
Color(hex: "e2e2e2")
.opacity(viewModel.sort == .NEWEST ? 1 : 0.5)
)
.onTapGesture {
if viewModel.sort != .NEWEST {
viewModel.sort = .NEWEST
}
}
Text("높은 가격순")
.font(.custom(Font.medium.rawValue, size: 13.3))
.foregroundColor(
Color(hex: "e2e2e2")
.opacity(viewModel.sort == .PRICE_HIGH ? 1 : 0.5)
)
.onTapGesture {
if viewModel.sort != .PRICE_HIGH {
viewModel.sort = .PRICE_HIGH
}
}
Text("낮은 가격순")
.font(.custom(Font.medium.rawValue, size: 13.3))
.foregroundColor(
Color(hex: "e2e2e2")
.opacity(viewModel.sort == .PRICE_LOW ? 1 : 0.5)
)
.onTapGesture {
if viewModel.sort != .PRICE_LOW {
viewModel.sort = .PRICE_LOW
}
}
}
.padding(.vertical, 13.3)
.padding(.horizontal, 20)
.background(Color(hex: "161616"))
.padding(.top, 13.3)
HStack(spacing: 0) {
Text("전체")
.font(.custom(Font.medium.rawValue, size: 13.3))
.foregroundColor(Color(hex: "e2e2e2"))
Text("\(viewModel.totalCount)")
.font(.custom(Font.medium.rawValue, size: 13.3))
.foregroundColor(Color(hex: "ff5c49"))
.padding(.leading, 8)
Text("")
.font(.custom(Font.medium.rawValue, size: 13.3))
.foregroundColor(Color(hex: "e2e2e2"))
.padding(.leading, 2)
Spacer()
}
.padding(.vertical, 13.3)
.padding(.horizontal, 20)
ScrollView(.vertical, showsIndicators: false) {
LazyVGrid(columns: columns, spacing: 13.3) {
ForEach(0..<viewModel.contentList.count, id: \.self) { index in
ContentNewAllItemView(item: viewModel.contentList[index])
.onAppear {
if index == viewModel.contentList.count - 1 {
viewModel.getContentList()
}
}
}
}
}
}
.onAppear {
viewModel.themeId = themeId
viewModel.getContentList()
}
}
}
}
}

View File

@@ -0,0 +1,96 @@
//
// ContentAllByThemeViewModel.swift
// SodaLive
//
// Created by klaus on 2/14/24.
//
import Foundation
import Combine
final class ContentAllByThemeViewModel: ObservableObject {
enum Sort: String {
case NEWEST, PRICE_HIGH, PRICE_LOW
}
private let repository = ContentRepository()
private var subscription = Set<AnyCancellable>()
@Published var errorMessage = ""
@Published var isShowPopup = false
@Published var isLoading = false
@Published var theme = ""
@Published var totalCount = 0
@Published var sort = Sort.NEWEST {
didSet {
page = 1
isLast = false
getContentList()
}
}
@Published var contentList: [GetAudioContentMainItem] = []
var themeId = 0
var page = 1
var isLast = false
private let pageSize = 10
func getContentList() {
if (!isLast && !isLoading) {
isLoading = true
repository.getAudioContentByTheme(
themeId: themeId,
page: page,
size: pageSize,
sort: sort
)
.sink { result in
switch result {
case .finished:
DEBUG_LOG("finish")
case .failure(let error):
ERROR_LOG(error.localizedDescription)
}
} receiveValue: { [unowned self] response in
let responseData = response.data
do {
let jsonDecoder = JSONDecoder()
let decoded = try jsonDecoder.decode(ApiResponse<GetContentByThemeResponse>.self, from: responseData)
if let data = decoded.data, decoded.success {
if page == 1 {
self.contentList.removeAll()
}
if !data.items.isEmpty {
page += 1
self.theme = data.theme
self.totalCount = data.totalCount
self.contentList.append(contentsOf: data.items)
} 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)
}
}
}

View File

@@ -0,0 +1,14 @@
//
// GetContentByThemeResponse.swift
// SodaLive
//
// Created by klaus on 2/14/24.
//
import Foundation
struct GetContentByThemeResponse: Decodable {
let theme: String
let totalCount: Int
let items: [GetAudioContentMainItem]
}