From 62904d96b1b1fb48d4ede2f596d7dc6ad7989c8d Mon Sep 17 00:00:00 2001 From: Yu Sung Date: Wed, 7 Feb 2024 15:09:48 +0900 Subject: [PATCH] =?UTF-8?q?=EC=BD=98=ED=85=90=EC=B8=A0=20=EC=A0=84?= =?UTF-8?q?=EC=B2=B4=EB=B3=B4=EA=B8=B0=20-=20=EC=B9=B4=ED=85=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Content/Category/CategoryApi.swift | 45 +++++++++++++ .../Category/ContentListCategoryView.swift | 59 +++++++++++++++++ .../Category/GetCategoryListResponse.swift | 13 ++++ SodaLive/Sources/Content/ContentApi.swift | 5 +- .../Sources/Content/ContentListView.swift | 11 ++++ .../Content/ContentListViewModel.swift | 64 +++++++++++++++++-- .../Sources/Content/ContentRepository.swift | 9 ++- 7 files changed, 196 insertions(+), 10 deletions(-) create mode 100644 SodaLive/Sources/Content/Category/CategoryApi.swift create mode 100644 SodaLive/Sources/Content/Category/ContentListCategoryView.swift create mode 100644 SodaLive/Sources/Content/Category/GetCategoryListResponse.swift diff --git a/SodaLive/Sources/Content/Category/CategoryApi.swift b/SodaLive/Sources/Content/Category/CategoryApi.swift new file mode 100644 index 0000000..d6083c7 --- /dev/null +++ b/SodaLive/Sources/Content/Category/CategoryApi.swift @@ -0,0 +1,45 @@ +// +// CategoryApi.swift +// SodaLive +// +// Created by klaus on 2/7/24. +// + +import Foundation +import Moya + +enum CategoryApi { + case getCategoryList(creatorId: Int) +} + +extension CategoryApi: TargetType { + var baseURL: URL { + return URL(string: BASE_URL)! + } + + var path: String { + switch self { + case .getCategoryList: + return "/category" + } + } + + var method: Moya.Method { + switch self { + case .getCategoryList: + return .get + } + } + + var task: Moya.Task { + switch self { + case .getCategoryList(let creatorId): + let parameters = ["creatorId": creatorId] as [String : Any] + return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString) + } + } + + var headers: [String : String]? { + return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"] + } +} diff --git a/SodaLive/Sources/Content/Category/ContentListCategoryView.swift b/SodaLive/Sources/Content/Category/ContentListCategoryView.swift new file mode 100644 index 0000000..7b30021 --- /dev/null +++ b/SodaLive/Sources/Content/Category/ContentListCategoryView.swift @@ -0,0 +1,59 @@ +// +// ContentListCategoryView.swift +// SodaLive +// +// Created by klaus on 2/7/24. +// + +import SwiftUI + +struct ContentListCategoryView: View { + let categoryList: [GetCategoryListResponse] + let selectCategory: (Int) -> Void + + @Binding var selectedCategory: String + + var body: some View { + ScrollView(.horizontal, showsIndicators: false) { + HStack(spacing: 8) { + ForEach(0...self, from: responseData) + + if let data = decoded.data, decoded.success { + categoryList.removeAll() + + if !data.isEmpty { + categoryList.append(GetCategoryListResponse(categoryId: 0, category: "전체")) + categoryList.append(contentsOf: data) + } + } else { + if let message = decoded.message { + self.errorMessage = message + } else { + self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다." + } + + self.isShowPopup = true + } + } catch { + print(error) + self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다." + self.isShowPopup = true + } + } + .store(in: &subscription) + } + + func selectCategory(categoryId: Int) { + self.categoryId = categoryId + + page = 1 + isLast = false + getAudioContentList() + } } diff --git a/SodaLive/Sources/Content/ContentRepository.swift b/SodaLive/Sources/Content/ContentRepository.swift index 55c1557..adf4e09 100644 --- a/SodaLive/Sources/Content/ContentRepository.swift +++ b/SodaLive/Sources/Content/ContentRepository.swift @@ -12,9 +12,10 @@ import Moya final class ContentRepository { private let api = MoyaProvider() + private let categoryApi = MoyaProvider() - func getAudioContentList(userId: Int, page: Int, size: Int, sort: ContentListViewModel.Sort) -> AnyPublisher { - return api.requestPublisher(.getAudioContentList(userId: userId, page: page, size: size, sort: sort)) + func getAudioContentList(userId: Int, categoryId: Int, page: Int, size: Int, sort: ContentListViewModel.Sort) -> AnyPublisher { + return api.requestPublisher(.getAudioContentList(userId: userId, categoryId: categoryId, page: page, size: size, sort: sort)) } func getAudioContentDetail(audioContentId: Int) -> AnyPublisher { @@ -120,4 +121,8 @@ final class ContentRepository { func unpinContent(contentId: Int) -> AnyPublisher { return api.requestPublisher(.unpinContent(contentId: contentId)) } + + func getCategoryList(creatorId: Int) -> AnyPublisher { + return categoryApi.requestPublisher(.getCategoryList(creatorId: creatorId)) + } }