콘텐츠 메인 - Api 분리

This commit is contained in:
Yu Sung
2023-12-11 20:01:26 +09:00
parent 77a145d61c
commit c09920942c
25 changed files with 886 additions and 590 deletions

View File

@@ -0,0 +1,55 @@
//
// ContentMainNewContentThemeView.swift
// SodaLive
//
// Created by klaus on 2023/08/11.
//
import SwiftUI
struct ContentMainNewContentThemeView: View {
let themes: [String]
let selectTheme: (String) -> Void
@Binding var selectedTheme: String
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .top, spacing: 8) {
ForEach(0..<themes.count, id: \.self) { index in
let theme = themes[index]
Text(theme)
.font(.custom(Font.medium.rawValue, size: 14.7))
.foregroundColor(Color(hex: selectedTheme == theme ? "9970ff" : "777777"))
.padding(.horizontal, 13.3)
.padding(.vertical, 9.3)
.border(
Color(hex: selectedTheme == theme ? "9970ff" : "eeeeee"),
width: 0.5
)
.cornerRadius(16.7)
.overlay(
RoundedRectangle(cornerRadius: CGFloat(16.7))
.stroke(lineWidth: 0.5)
.foregroundColor(Color(hex: selectedTheme == theme ? "9970ff" : "eeeeee"))
)
.onTapGesture {
if selectedTheme != theme {
selectTheme(theme)
}
}
}
}
}
}
}
struct ContentMainNewContentThemeView_Previews: PreviewProvider {
static var previews: some View {
ContentMainNewContentThemeView(
themes: ["전체", "테마1", "테마2"],
selectTheme: { _ in },
selectedTheme: .constant("전체")
)
}
}

View File

@@ -0,0 +1,68 @@
//
// ContentMainNewContentView.swift
// SodaLive
//
// Created by klaus on 2023/08/11.
//
import SwiftUI
struct ContentMainNewContentView: View {
@StateObject private var viewModel = ContentMainNewContentViewModel()
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")
.resizable()
.frame(width: 20, height: 20)
.onTapGesture {
AppState.shared.setAppStep(step: .newContentAll)
}
}
if !viewModel.themeList.isEmpty {
ContentMainNewContentThemeView(
themes: viewModel.themeList,
selectTheme: { theme in
viewModel.selectedTheme = theme
},
selectedTheme: $viewModel.selectedTheme
)
}
if !viewModel.newContentList.isEmpty {
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .top, spacing: 13.3) {
ForEach(0..<viewModel.newContentList.count, id: \.self) {
ContentMainItemView(item: viewModel.newContentList[$0])
}
}
}
}
if viewModel.isLoading {
ActivityIndicatorView()
.frame(width: 100, height: 100)
}
}
.frame(maxWidth: .infinity)
.onAppear {
viewModel.getThemeList()
viewModel.getNewContentOfTheme()
}
}
}
struct ContentMainNewContentView_Previews: PreviewProvider {
static var previews: some View {
ContentMainNewContentView()
}
}

View File

@@ -0,0 +1,104 @@
//
// ContentMainNewContentViewModel.swift
// SodaLive
//
// Created by klaus on 2023/12/11.
//
import Foundation
import Combine
final class ContentMainNewContentViewModel: ObservableObject {
private let repository = ContentRepository()
private var subscription = Set<AnyCancellable>()
@Published var errorMessage = ""
@Published var isShowPopup = false
@Published var isLoading = false
@Published var themeList = [String]()
@Published var newContentList = [GetAudioContentMainItem]()
@Published var selectedTheme = "전체" {
didSet {
newContentList.removeAll()
getNewContentOfTheme()
}
}
func getThemeList() {
repository.getNewContentThemeList()
.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.themeList.append("전체")
self.themeList.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 getNewContentOfTheme() {
isLoading = true
repository.getNewContentOfTheme(theme: selectedTheme == "전체" ? "" : selectedTheme)
.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<[GetAudioContentMainItem]>.self, from: responseData)
if let data = decoded.data, decoded.success {
self.newContentList.removeAll()
self.newContentList.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)
}
}