콘텐츠 메인

- 추천 시리즈 UI 추가
This commit is contained in:
Yu Sung
2024-05-07 17:49:36 +09:00
parent 83d51a525b
commit bcba83a8a7
9 changed files with 100 additions and 125 deletions

View File

@@ -0,0 +1,45 @@
//
// ContentMainRecommendSeriesView.swift
// SodaLive
//
// Created by klaus on 5/7/24.
//
import SwiftUI
struct ContentMainRecommendSeriesView: View {
@StateObject private var viewModel = ContentMainRecommendSeriesViewModel()
var body: some View {
ZStack {
if !viewModel.seriesList.isEmpty {
VStack(alignment: .leading, spacing: 13.3) {
Text("추천 시리즈")
.font(.custom(Font.bold.rawValue, size: 18.3))
.foregroundColor(Color.grayee)
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .top, spacing: 13.3) {
ForEach(0..<viewModel.seriesList.count, id: \.self) {
let item = viewModel.seriesList[$0]
SeriesListBigItemView(item: item, isVisibleCreator: true)
}
}
}
}
.padding(.bottom, 26.7)
.padding(.horizontal, 13.3)
} else {
EmptyView()
}
}
.onAppear {
viewModel.getRecommendSeriesList()
}
}
}
#Preview {
ContentMainRecommendSeriesView()
}

View File

@@ -0,0 +1,60 @@
//
// ContentMainRecommendSeriesViewModel.swift
// SodaLive
//
// Created by klaus on 5/7/24.
//
import Foundation
import Combine
final class ContentMainRecommendSeriesViewModel: ObservableObject {
private let repository = SeriesRepository()
private var subscription = Set<AnyCancellable>()
@Published var errorMessage = ""
@Published var isShowPopup = false
@Published var isLoading = false
@Published var seriesList = [SeriesListItem]()
func getRecommendSeriesList() {
repository.getRecommendSeriesList()
.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<[SeriesListItem]>.self, from: responseData)
self.isLoading = false
if let data = decoded.data, decoded.success {
self.seriesList.removeAll()
self.seriesList.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
self.isLoading = false
}
}
.store(in: &subscription)
}
}