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)) + } }