콘텐츠 메인

- 단편 탭 UI 페이지 생성
This commit is contained in:
Yu Sung
2025-02-22 01:41:03 +09:00
parent 6bd27c5301
commit e9e7403579
18 changed files with 1018 additions and 95 deletions

View File

@@ -0,0 +1,42 @@
//
// ContentMainTabContentRepository.swift
// SodaLive
//
// Created by klaus on 2/21/25.
//
import Foundation
import CombineMoya
import Combine
import Moya
final class ContentMainTabContentRepository {
private let api = MoyaProvider<ContentApi>()
func getContentMainContent() -> AnyPublisher<Response, MoyaError> {
return api.requestPublisher(.getContentMainContent)
}
func getNewContentOfTheme(theme: String) -> AnyPublisher<Response, MoyaError> {
return api.requestPublisher(
.getContentMainNewContentOfTheme(
theme: theme,
isAdultContentVisible: UserDefaults.isAdultContentVisible(),
contentType: ContentType(rawValue: UserDefaults.string(forKey: .contentPreference)) ?? ContentType.ALL
)
)
}
func getContentRanking(sortType: String) -> AnyPublisher<Response, MoyaError> {
return api.requestPublisher(.getDailyContentRanking(sortType: sortType))
}
func getRecommendContentByTag(tag: String) -> AnyPublisher<Response, MoyaError> {
return api.requestPublisher(.getRecommendContentByTag(tag: tag))
}
func getPopularContentByCreator(creatorId: Int) -> AnyPublisher<Response, MoyaError> {
return api.requestPublisher(.getPopularContentByCreator(creatorId: creatorId))
}
}

View File

@@ -0,0 +1,104 @@
//
// ContentMainTabContentView.swift
// SodaLive
//
// Created by klaus on 2/21/25.
//
import SwiftUI
struct ContentMainTabContentView: View {
@StateObject var viewModel = ContentMainTabContentViewModel()
var body: some View {
BaseView(isLoading: $viewModel.isLoading) {
ScrollView(.vertical, showsIndicators: false) {
VStack(spacing: 0) {
if !viewModel.bannerList.isEmpty {
ContentMainBannerViewV2(bannerList: viewModel.bannerList)
.padding(.horizontal, 13.3)
}
if !viewModel.contentThemeList.isEmpty {
ContentMainNewContentViewV2(
title: "새로운 단편",
onClickMore: {},
themeList: viewModel.contentThemeList,
contentList: viewModel.newContentList
) {
viewModel.getNewContentOfTheme(theme: $0)
}
.padding(.top, 30)
}
if !viewModel.rankSortTypeList.isEmpty {
ContentMainTabRankContentView(
title: "일간 랭킹",
isMore: false,
onClickMore: {},
sortList: viewModel.rankSortTypeList,
onClickSort: { viewModel.getContentRanking(sort: $0) },
contentList: viewModel.rankContentList
)
.padding(.top, 30)
}
if !viewModel.contentRankCreatorList.isEmpty {
ContentByChannelView(
title: "채널별 추천 단편",
creatorList: viewModel.contentRankCreatorList,
contentList: viewModel.salesCountRankContentList,
onClickCreator: {
viewModel.getPopularContentByCreator(creatorId: $0)
}
)
.padding(.top, 30)
}
if !viewModel.eventBannerList.isEmpty {
SectionEventBannerView(items: viewModel.eventBannerList)
.padding(.top, 30)
}
if !viewModel.tagList.isEmpty {
ContentMainTagCurationView(
tagList: viewModel.tagList,
contentList: viewModel.tagCurationContentList
) {
viewModel.getRecommendContentByTag(tag: $0)
}
.padding(.top, 30)
}
if !viewModel.curationList.isEmpty {
ContentMainCurationViewV2(curationList: viewModel.curationList)
.padding(.top, 30)
}
}
.onAppear {
viewModel.fetchData()
}
}
}
.popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .bottom, autohideIn: 2) {
HStack {
Spacer()
Text(viewModel.errorMessage)
.padding(.vertical, 13.3)
.frame(width: screenSize().width - 66.7, alignment: .center)
.font(.custom(Font.medium.rawValue, size: 12))
.background(Color.button)
.foregroundColor(Color.white)
.multilineTextAlignment(.leading)
.cornerRadius(20)
.padding(.bottom, 66.7)
Spacer()
}
}
}
}
#Preview {
ContentMainTabContentView()
}

View File

@@ -0,0 +1,231 @@
//
// ContentMainTabContentViewModel.swift
// SodaLive
//
// Created by klaus on 2/21/25.
//
import Foundation
import Combine
final class ContentMainTabContentViewModel: ObservableObject {
private let repository = ContentMainTabContentRepository()
private var subscription = Set<AnyCancellable>()
@Published var errorMessage = ""
@Published var isShowPopup = false
@Published var isLoading = false
@Published var bannerList: [GetAudioContentBannerResponse] = []
@Published var contentThemeList: [String] = []
@Published var newContentList: [GetAudioContentMainItem] = []
@Published var rankSortTypeList: [String] = []
@Published var rankContentList: [GetAudioContentRankingItem] = []
@Published var contentRankCreatorList: [ContentCreatorResponse] = []
@Published var salesCountRankContentList: [GetAudioContentRankingItem] = []
@Published var eventBannerList: [EventItem] = []
@Published var tagList: [String] = []
@Published var tagCurationContentList: [GetAudioContentMainItem] = []
@Published var curationList: [GetContentCurationResponse] = []
func fetchData() {
isLoading = true
repository.getContentMainContent()
.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<GetContentMainTabContentResponse>.self, from: responseData)
if let data = decoded.data, decoded.success {
self.bannerList = data.bannerList
self.contentThemeList = ["전체"] + data.contentThemeList
self.newContentList = data.newContentList
self.rankSortTypeList = data.rankSortTypeList
self.rankContentList = data.rankContentList
self.contentRankCreatorList = data.contentRankCreatorList
self.salesCountRankContentList = data.salesCountRankContentList
self.eventBannerList = data.eventBannerList.eventList
self.tagList = data.tagList
self.tagCurationContentList = data.tagCurationContentList
self.curationList = data.curationList
} 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)
}
func getNewContentOfTheme(theme: String) {
isLoading = true
repository.getNewContentOfTheme(theme: theme == "전체" ? "" : theme)
.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<[GetAudioContentMainItem]>.self, from: responseData)
if let data = decoded.data, decoded.success {
self.newContentList = 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)
}
func getContentRanking(sort: String = "매출") {
isLoading = true
repository.getContentRanking(sortType: 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<[GetAudioContentRankingItem]>.self, from: responseData)
if let data = decoded.data, decoded.success {
self.rankContentList = 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)
}
func getRecommendContentByTag(tag: String) {
isLoading = true
repository.getRecommendContentByTag(tag: tag)
.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<[GetAudioContentMainItem]>.self, from: responseData)
if let data = decoded.data, decoded.success {
self.tagCurationContentList = 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)
}
func getPopularContentByCreator(creatorId: Int) {
isLoading = true
repository.getPopularContentByCreator(creatorId: creatorId)
.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<[GetAudioContentRankingItem]>.self, from: responseData)
if let data = decoded.data, decoded.success {
self.salesCountRankContentList = 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)
}
}

View File

@@ -43,6 +43,7 @@ struct ContentMainTabRankContentView: View {
.onTapGesture { onClickMore() }
}
}
.padding(.horizontal, 13.3)
if !sortList.isEmpty {
ContentMainRankingSortView(
@@ -97,6 +98,7 @@ struct ContentMainTabRankContentView: View {
}
}
}
.padding(.horizontal, 13.3)
.frame(height: 207)
}
}

View File

@@ -0,0 +1,231 @@
//
// ContentMainTagCurationView.swift
// SodaLive
//
// Created by klaus on 2/22/25.
//
import SwiftUI
import Kingfisher
struct ContentMainTagCurationView: View {
let tagList: [String]
let contentList: [GetAudioContentMainItem]
let selectTag: (String) -> Void
let tagColumns = [
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible())
]
let contentColumns = [
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible())
]
@State private var selectedTag = ""
var body: some View {
VStack(alignment: .leading, spacing: 13.3) {
Text("태그별 추천 콘텐츠")
.font(.custom(Font.bold.rawValue, size: 18.3))
.foregroundColor(.grayee)
.padding(.horizontal, 13.3)
LazyVGrid(columns: tagColumns, spacing: 6) {
ForEach(0..<tagList.count, id: \.self) { index in
let tag = tagList[index]
Text(tagList[index])
.font(.custom(Font.medium.rawValue, size: 10))
.foregroundColor(
selectedTag == tag ?
.button:
.gray77
)
.padding(.vertical, 10)
.frame(width: (screenSize().width - 18 - 26.7) / 4)
.overlay(
RoundedRectangle(cornerRadius: 2.6)
.strokeBorder(lineWidth: 1)
.foregroundColor(
selectedTag == tag ?
.button:
.gray77
)
)
.onTapGesture {
if selectedTag != tag {
selectedTag = tag
selectTag(tag)
}
}
}
}
.padding(.horizontal, 13.3)
LazyVGrid(columns: contentColumns, spacing: 13.3) {
ForEach(0..<contentList.count, id: \.self) { index in
ContentMainTagCurationContentView(
item: contentList[index],
itemWidth: (screenSize().width - 40) / 3
)
}
}
.padding(.horizontal, 13.3)
}
.onAppear {
selectedTag = tagList[0]
}
}
}
struct ContentMainTagCurationContentView: View {
let item: GetAudioContentMainItem
let itemWidth: CGFloat
var body: some View {
VStack(alignment: .leading, spacing: 8) {
ZStack(alignment: .bottom) {
KFImage(URL(string: item.coverImageUrl))
.cancelOnDisappear(true)
.downsampling(
size: CGSize(
width: itemWidth,
height: itemWidth
)
)
.resizable()
.scaledToFill()
.frame(width: itemWidth, height: itemWidth, alignment: .top)
.cornerRadius(2.7)
VStack(spacing: 0) {
Spacer()
HStack(spacing: 0) {
HStack(spacing: 2) {
if item.price > 0 {
Image("ic_card_can_gray")
Text("\(item.price)")
.font(.custom(Font.medium.rawValue, size: 8.5))
.foregroundColor(Color.white)
} else {
Text("무료")
.font(.custom(Font.medium.rawValue, size: 8.5))
.foregroundColor(Color.white)
}
}
.padding(3)
.background(Color.gray33.opacity(0.7))
.cornerRadius(10)
.padding(.leading, 2.7)
.padding(.bottom, 2.7)
Spacer()
HStack(spacing: 2) {
Text(item.duration)
.font(.custom(Font.medium.rawValue, size: 8.5))
.foregroundColor(Color.white)
}
.padding(3)
.background(Color.gray33.opacity(0.7))
.cornerRadius(10)
.padding(.trailing, 2.7)
.padding(.bottom, 2.7)
}
}
}
.frame(width: itemWidth, height: itemWidth)
Text(item.title)
.font(.custom(Font.medium.rawValue, size: 13.3))
.foregroundColor(Color.grayd2)
.frame(width: itemWidth, alignment: .leading)
.multilineTextAlignment(.leading)
.fixedSize(horizontal: false, vertical: true)
.lineLimit(2)
HStack(spacing: 5.3) {
KFImage(URL(string: item.creatorProfileImageUrl))
.cancelOnDisappear(true)
.downsampling(
size: CGSize(
width: 21.3,
height: 21.3
)
)
.resizable()
.scaledToFill()
.frame(width: 21.3, height: 21.3)
.clipShape(Circle())
.onTapGesture { AppState.shared.setAppStep(step: .creatorDetail(userId: item.creatorId)) }
Text(item.creatorNickname)
.font(.custom(Font.medium.rawValue, size: 12))
.foregroundColor(.gray77)
.lineLimit(1)
}
.padding(.bottom, 10)
}
.onTapGesture {
AppState.shared
.setAppStep(step: .contentDetail(contentId: item.contentId))
}
}
}
#Preview {
ContentMainTagCurationView(
tagList: ["test", "test2", "test3", "test4", "test5", "test6", "test7"],
contentList: [
GetAudioContentMainItem(
contentId: 1,
coverImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
title: "ㅓ처랴햐햫햐햐",
creatorId: 8,
creatorProfileImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
creatorNickname: "유저1",
price: 100,
duration: "00:00:30"
),
GetAudioContentMainItem(
contentId: 2,
coverImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
title: "ㅓ처랴햐햫햐햐",
creatorId: 8,
creatorProfileImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
creatorNickname: "유저2",
price: 0,
duration: "00:00:30"
),
GetAudioContentMainItem(
contentId: 3,
coverImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
title: "ㅓ처랴햐햫햐햐",
creatorId: 8,
creatorProfileImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
creatorNickname: "유저3",
price: 1000,
duration: "00:00:30"
),
GetAudioContentMainItem(
contentId: 4,
coverImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
title: "ㅓ처랴햐햫햐햐",
creatorId: 8,
creatorProfileImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
creatorNickname: "유저3",
price: 50000,
duration: "00:00:30"
)
],
selectTag: { _ in }
)
}

View File

@@ -0,0 +1,20 @@
//
// GetContentMainTabContentResponse.swift
// SodaLive
//
// Created by klaus on 2/21/25.
//
struct GetContentMainTabContentResponse: Decodable {
let bannerList: [GetAudioContentBannerResponse]
let contentThemeList: [String]
let newContentList: [GetAudioContentMainItem]
let rankSortTypeList: [String]
let rankContentList: [GetAudioContentRankingItem]
let contentRankCreatorList: [ContentCreatorResponse]
let salesCountRankContentList: [GetAudioContentRankingItem]
let eventBannerList: GetEventResponse
let tagList: [String]
let tagCurationContentList: [GetAudioContentMainItem]
let curationList: [GetContentCurationResponse]
}