콘텐츠 메인 - Api 분리
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// ContentMainRankingSortView.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/11/03.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ContentMainRankingSortView: View {
|
||||
let sorts: [String]
|
||||
let selectSort: (String) -> Void
|
||||
|
||||
@Binding var selectedSort: String
|
||||
|
||||
var body: some View {
|
||||
ScrollView(.horizontal, showsIndicators: false) {
|
||||
HStack(alignment: .top, spacing: 8) {
|
||||
ForEach(0..<sorts.count, id: \.self) { index in
|
||||
let sort = sorts[index]
|
||||
Text(sort)
|
||||
.font(.custom(Font.medium.rawValue, size: 14.7))
|
||||
.foregroundColor(Color(hex: selectedSort == sort ? "9970ff" : "777777"))
|
||||
.padding(.horizontal, 13.3)
|
||||
.padding(.vertical, 9.3)
|
||||
.border(
|
||||
Color(hex: selectedSort == sort ? "9970ff" : "eeeeee"),
|
||||
width: 0.5
|
||||
)
|
||||
.cornerRadius(16.7)
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: CGFloat(16.7))
|
||||
.stroke(lineWidth: 0.5)
|
||||
.foregroundColor(Color(hex: selectedSort == sort ? "9970ff" : "eeeeee"))
|
||||
)
|
||||
.onTapGesture {
|
||||
if selectedSort != sort {
|
||||
selectSort(sort)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ContentMainRankingSortView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
ContentMainRankingSortView(
|
||||
sorts: ["전체", "테마1", "테마2"],
|
||||
selectSort: { _ in },
|
||||
selectedSort: .constant("전체")
|
||||
)
|
||||
}
|
||||
}
|
@@ -0,0 +1,107 @@
|
||||
//
|
||||
// ContentMainRankingView.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/10/15.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import Kingfisher
|
||||
|
||||
struct ContentMainRankingView: View {
|
||||
|
||||
@StateObject private var viewModel = ContentMainRankingViewModel()
|
||||
|
||||
let rows = [
|
||||
GridItem(.fixed(60), alignment: .leading),
|
||||
GridItem(.fixed(60), alignment: .leading),
|
||||
GridItem(.fixed(60), alignment: .leading)
|
||||
]
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 16.7) {
|
||||
HStack(spacing: 0) {
|
||||
Text("인기 콘텐츠")
|
||||
.font(.custom(Font.bold.rawValue, size: 18.3))
|
||||
.foregroundColor(Color(hex: "eeeeee"))
|
||||
|
||||
Spacer()
|
||||
|
||||
Image("ic_forward")
|
||||
.onTapGesture {
|
||||
AppState.shared.setAppStep(step: .contentRankingAll)
|
||||
}
|
||||
}
|
||||
|
||||
VStack(spacing: 8) {
|
||||
Text("\(viewModel.dateString)")
|
||||
.font(.custom(Font.bold.rawValue, size: 14.7))
|
||||
.foregroundColor(Color(hex: "eeeeee"))
|
||||
|
||||
Text("※ 인기 콘텐츠의 순위는 매주 업데이트됩니다.")
|
||||
.font(.custom(Font.light.rawValue, size: 13.3))
|
||||
.foregroundColor(Color(hex: "bbbbbb"))
|
||||
}
|
||||
.padding(.vertical, 8)
|
||||
.frame(width: screenSize().width - 26.7)
|
||||
.background(Color(hex: "222222"))
|
||||
|
||||
if !viewModel.contentRankingSortList.isEmpty {
|
||||
ContentMainRankingSortView(
|
||||
sorts: viewModel.contentRankingSortList,
|
||||
selectSort: { viewModel.selectedContentRankingSort = $0 },
|
||||
selectedSort: $viewModel.selectedContentRankingSort
|
||||
)
|
||||
}
|
||||
|
||||
ScrollView(.horizontal, showsIndicators: false) {
|
||||
LazyHGrid(rows: rows, spacing: 13.3) {
|
||||
ForEach(0..<viewModel.contentRankingItemList.count, id: \.self) { index in
|
||||
let content = viewModel.contentRankingItemList[index]
|
||||
HStack(spacing: 0) {
|
||||
KFImage(URL(string: content.coverImageUrl))
|
||||
.resizable()
|
||||
.frame(width: 60, height: 60)
|
||||
.cornerRadius(2.7)
|
||||
|
||||
Text("\(index + 1)")
|
||||
.font(.custom(Font.bold.rawValue, size: 16.7))
|
||||
.foregroundColor(Color(hex: "3bb9f1"))
|
||||
.padding(.horizontal, 12)
|
||||
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
Text(content.title)
|
||||
.lineLimit(2)
|
||||
.font(.custom(Font.medium.rawValue, size: 13.3))
|
||||
.foregroundColor(Color(hex: "d2d2d2"))
|
||||
|
||||
Text(content.creatorNickname)
|
||||
.font(.custom(Font.medium.rawValue, size: 11))
|
||||
.foregroundColor(Color(hex: "777777"))
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: screenSize().width * 0.66, alignment: .leading)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture {
|
||||
AppState
|
||||
.shared
|
||||
.setAppStep(step: .contentDetail(contentId: content.contentId))
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(height: 207)
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.onAppear {
|
||||
viewModel.getContentRankingSortType()
|
||||
viewModel.getContentRanking()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ContentMainRankingView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
ContentMainRankingView()
|
||||
}
|
||||
}
|
@@ -0,0 +1,104 @@
|
||||
//
|
||||
// ContentMainRankingViewModel.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/12/11.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
final class ContentMainRankingViewModel: ObservableObject {
|
||||
|
||||
private let repository = ContentRepository()
|
||||
private var subscription = Set<AnyCancellable>()
|
||||
|
||||
@Published var errorMessage = ""
|
||||
@Published var isShowPopup = false
|
||||
@Published var isLoading = false
|
||||
|
||||
@Published var dateString = ""
|
||||
@Published var contentRankingSortList = [String]()
|
||||
@Published var contentRankingItemList = [GetAudioContentRankingItem]()
|
||||
|
||||
@Published var selectedContentRankingSort = "매출" {
|
||||
didSet {
|
||||
getContentRanking()
|
||||
}
|
||||
}
|
||||
|
||||
func getContentRankingSortType() {
|
||||
repository.getContentRankingSortType()
|
||||
.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<[String]>.self, from: responseData)
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
self.contentRankingSortList.removeAll()
|
||||
self.contentRankingSortList.append(contentsOf: data)
|
||||
} else {
|
||||
if let message = decoded.message {
|
||||
self.errorMessage = message
|
||||
} else {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
}
|
||||
|
||||
self.isShowPopup = true
|
||||
}
|
||||
} catch {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
self.isShowPopup = true
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
}
|
||||
|
||||
func getContentRanking() {
|
||||
isLoading = true
|
||||
|
||||
repository.getContentRanking(page: 1, size: 12, sortType: selectedContentRankingSort)
|
||||
.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<GetAudioContentRanking>.self, from: responseData)
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
dateString = "\(data.startDate)~\(data.endDate)"
|
||||
self.contentRankingItemList.append(contentsOf: data.items)
|
||||
} else {
|
||||
if let message = decoded.message {
|
||||
self.errorMessage = message
|
||||
} else {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
}
|
||||
|
||||
self.isShowPopup = true
|
||||
}
|
||||
} catch {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
self.isShowPopup = true
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user