콘텐츠 메인 - 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,53 @@
//
// ContentMainCurationItemView.swift
// SodaLive
//
// Created by klaus on 2023/08/11.
//
import SwiftUI
struct ContentMainCurationItemView: View {
let item: GetAudioContentCurationResponse
var body: some View {
VStack(alignment: .leading, spacing: 0) {
HStack(spacing: 0) {
Text(item.title)
.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: .curationAll(
title: item.title,
curationId: item.curationId
)
)
}
}
Text(item.description)
.font(.custom(Font.medium.rawValue, size: 13))
.foregroundColor(Color(hex: "777777"))
.padding(.top, 4)
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(alignment: .top, spacing: 13.3) {
ForEach(0..<item.contents.count, id: \.self) {
let audioContent = item.contents[$0]
ContentMainItemView(item: audioContent)
}
}
}
.padding(.top, 13.3)
}
}
}

View File

@@ -0,0 +1,42 @@
//
// ContentMainCurationView.swift
// SodaLive
//
// Created by klaus on 2023/08/11.
//
import SwiftUI
struct ContentMainCurationView: View {
@StateObject private var viewModel = ContentMainCurationViewModel()
var body: some View {
VStack {
if !viewModel.curationList.isEmpty {
LazyVStack(spacing: 40) {
ForEach(0..<viewModel.curationList.count, id: \.self) { index in
ContentMainCurationItemView(item: viewModel.curationList[index])
.padding(.horizontal, 13.3)
.onAppear {
if index == viewModel.curationList.count - 1 {
viewModel.getCurationList()
}
}
}
}
}
if viewModel.isLoading {
ActivityIndicatorView()
.frame(width: 100, height: 100)
}
}
.frame(maxWidth: .infinity)
.onAppear {
viewModel.page = 1
viewModel.isLast = false
viewModel.getCurationList()
}
}
}

View File

@@ -0,0 +1,76 @@
//
// ContentMainCurationViewModel.swift
// SodaLive
//
// Created by klaus on 2023/12/11.
//
import Foundation
import Combine
final class ContentMainCurationViewModel: ObservableObject {
private let repository = ContentRepository()
private var subscription = Set<AnyCancellable>()
@Published var errorMessage = ""
@Published var isShowPopup = false
@Published var isLoading = false
@Published var curationList = [GetAudioContentCurationResponse]()
var isLast = false
var page = 1
private let size = 10
func getCurationList() {
if !isLoading && !isLast {
isLoading = true
repository.getCurationList(page: page, size: size)
.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<[GetAudioContentCurationResponse]>.self, from: responseData)
self.isLoading = false
if let data = decoded.data, decoded.success {
if page == 1 {
self.curationList.removeAll()
}
if !data.isEmpty {
page += 1
self.curationList.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)
}
}
}