diff --git a/SodaLive/Sources/Content/ContentApi.swift b/SodaLive/Sources/Content/ContentApi.swift index 94f2843..a5ff4af 100644 --- a/SodaLive/Sources/Content/ContentApi.swift +++ b/SodaLive/Sources/Content/ContentApi.swift @@ -29,7 +29,7 @@ enum ContentApi { case getNewContentThemeList case getNewContentAllOfTheme(theme: String, page: Int, size: Int) case getAudioContentListByCurationId(curationId: Int, page: Int, size: Int, sort: ContentCurationViewModel.Sort) - case getContentRanking(page: Int, size: Int) + case getContentRanking(page: Int, size: Int, sortType: String) } extension ContentApi: TargetType { @@ -220,10 +220,11 @@ extension ContentApi: TargetType { return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString) - case .getContentRanking(let page, let size): + case .getContentRanking(let page, let size, let sortType): let parameters = [ "page": page - 1, "size": size, + "sort-type": sortType ] as [String : Any] return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString) diff --git a/SodaLive/Sources/Content/ContentRepository.swift b/SodaLive/Sources/Content/ContentRepository.swift index 74104f9..9cfb605 100644 --- a/SodaLive/Sources/Content/ContentRepository.swift +++ b/SodaLive/Sources/Content/ContentRepository.swift @@ -93,7 +93,7 @@ final class ContentRepository { return api.requestPublisher(.getAudioContentListByCurationId(curationId: curationId, page: page, size: size, sort: sort)) } - func getContentRanking(page: Int, size: Int) -> AnyPublisher { - return api.requestPublisher(.getContentRanking(page: page, size: size)) + func getContentRanking(page: Int, size: Int, sortType: String = "매출") -> AnyPublisher { + return api.requestPublisher(.getContentRanking(page: page, size: size, sortType: sortType)) } } diff --git a/SodaLive/Sources/Content/Main/ContentMainRankingSortView.swift b/SodaLive/Sources/Content/Main/ContentMainRankingSortView.swift new file mode 100644 index 0000000..19a91de --- /dev/null +++ b/SodaLive/Sources/Content/Main/ContentMainRankingSortView.swift @@ -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.. Void + @Binding var selectedSort: String + let rows = [ GridItem(.fixed(60), alignment: .leading), GridItem(.fixed(60), alignment: .leading), @@ -47,6 +51,9 @@ struct ContentMainRankingView: View { .background(Color(hex: "222222")) .padding(.top, 13.3) + ContentMainRankingSortView(sorts: sorts, selectSort: selectSort, selectedSort: $selectedSort) + .padding(.vertical, 16.7) + ScrollView(.horizontal, showsIndicators: false) { LazyHGrid(rows: rows, spacing: 13.3) { ForEach(0.. 0 { diff --git a/SodaLive/Sources/Content/Main/ContentMainViewModel.swift b/SodaLive/Sources/Content/Main/ContentMainViewModel.swift index 98d4d2e..09f0581 100644 --- a/SodaLive/Sources/Content/Main/ContentMainViewModel.swift +++ b/SodaLive/Sources/Content/Main/ContentMainViewModel.swift @@ -24,6 +24,7 @@ final class ContentMainViewModel: ObservableObject { @Published var orderList = [GetAudioContentMainItem]() @Published var themeList = [String]() @Published var curationList = [GetAudioContentCurationResponse]() + @Published var contentRankingSortList = [String]() @Published var contentRanking: GetAudioContentRanking? = nil @Published var selectedTheme = "전체" { @@ -32,6 +33,12 @@ final class ContentMainViewModel: ObservableObject { } } + @Published var selectedContentRankingSort = "매출" { + didSet { + getContentRanking() + } + } + func getMain() { isLoading = true @@ -66,6 +73,7 @@ final class ContentMainViewModel: ObservableObject { self.orderList.append(contentsOf: data.orderList) self.curationList.append(contentsOf: data.curationList) self.contentRanking = data.contentRanking + self.contentRankingSortList.append(contentsOf: data.contentRankingSortTypeList) self.themeList.append("전체") self.themeList.append(contentsOf: data.themeList) @@ -124,4 +132,42 @@ final class ContentMainViewModel: ObservableObject { } .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.self, from: responseData) + + if let data = decoded.data, decoded.success { + self.contentRanking = 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) + } } diff --git a/SodaLive/Sources/Content/Main/GetAudioContentMainResponse.swift b/SodaLive/Sources/Content/Main/GetAudioContentMainResponse.swift index b0cec79..a3eb34a 100644 --- a/SodaLive/Sources/Content/Main/GetAudioContentMainResponse.swift +++ b/SodaLive/Sources/Content/Main/GetAudioContentMainResponse.swift @@ -14,6 +14,7 @@ struct GetAudioContentMainResponse: Decodable { let themeList: [String] let newContentList: [GetAudioContentMainItem] let curationList: [GetAudioContentCurationResponse] + let contentRankingSortTypeList: [String] let contentRanking: GetAudioContentRanking }