From 101b04b6a96373a1525bdffc988a55394eb198a0 Mon Sep 17 00:00:00 2001 From: Yu Sung Date: Mon, 29 Apr 2024 19:36:53 +0900 Subject: [PATCH] =?UTF-8?q?=EC=8B=9C=EB=A6=AC=EC=A6=88=20=EC=A0=84?= =?UTF-8?q?=EC=B2=B4=EB=B3=B4=EA=B8=B0=20=ED=8E=98=EC=9D=B4=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/Content/Series/SeriesApi.swift | 51 +++++++++ .../Content/Series/SeriesListAllView.swift | 57 ++++++++++ .../Series/SeriesListAllViewModel.swift | 79 +++++++++++++ .../Content/Series/SeriesRepository.swift | 19 ++++ SodaLive/Sources/ContentView.swift | 3 + .../Series/UserProfileSeriesView.swift | 11 +- .../UI/Component/SeriesListBigItemView.swift | 5 +- .../UI/Component/SeriesListItemView.swift | 104 ++++++++++++++++++ SodaLive/Sources/User/UserApi.swift | 4 +- 9 files changed, 324 insertions(+), 9 deletions(-) create mode 100644 SodaLive/Sources/Content/Series/SeriesApi.swift create mode 100644 SodaLive/Sources/Content/Series/SeriesListAllView.swift create mode 100644 SodaLive/Sources/Content/Series/SeriesListAllViewModel.swift create mode 100644 SodaLive/Sources/Content/Series/SeriesRepository.swift create mode 100644 SodaLive/Sources/UI/Component/SeriesListItemView.swift diff --git a/SodaLive/Sources/Content/Series/SeriesApi.swift b/SodaLive/Sources/Content/Series/SeriesApi.swift new file mode 100644 index 0000000..0979ae2 --- /dev/null +++ b/SodaLive/Sources/Content/Series/SeriesApi.swift @@ -0,0 +1,51 @@ +// +// SeriesApi.swift +// SodaLive +// +// Created by klaus on 4/29/24. +// + +import Foundation +import Moya + +enum SeriesApi { + case getSeriesList(creatorId: Int, sortType: SeriesListAllViewModel.SeriesSortType, page: Int, size: Int) +} + +extension SeriesApi: TargetType { + var baseURL: URL { + return URL(string: BASE_URL)! + } + + var path: String { + switch self { + case .getSeriesList: + return "/audio-content/series" + } + } + + var method: Moya.Method { + switch self { + case .getSeriesList: + return .get + } + } + + var task: Moya.Task { + switch self { + case .getSeriesList(let creatorId, let sortType, let page, let size): + let parameters = [ + "creatorId": creatorId, + "sortType": sortType, + "page": page - 1, + "size": size + ] 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/Series/SeriesListAllView.swift b/SodaLive/Sources/Content/Series/SeriesListAllView.swift new file mode 100644 index 0000000..528dd35 --- /dev/null +++ b/SodaLive/Sources/Content/Series/SeriesListAllView.swift @@ -0,0 +1,57 @@ +// +// SeriesListAllView.swift +// SodaLive +// +// Created by klaus on 4/29/24. +// + +import SwiftUI + +struct SeriesListAllView: View { + + @ObservedObject var viewModel = SeriesListAllViewModel() + + let creatorId: Int + + let columns = [ + GridItem(.flexible()), + GridItem(.flexible()), + GridItem(.flexible()) + ] + + var body: some View { + BaseView(isLoading: $viewModel.isLoading) { + VStack(spacing: 0) { + DetailNavigationBar(title: "시리즈 전체보기") + + ScrollView(.vertical, showsIndicators: false) { + LazyVGrid(columns: columns, spacing: 13.3) { + ForEach(0..() + + enum SeriesSortType: String { + case NEWEST, POPULAR + } + + var creatorId: Int = 0 + + @Published var isLoading = false + @Published var errorMessage = "" + @Published var isShowPopup = false + @Published var seriesList = [SeriesListItem]() + + var page = 1 + var isLast = false + private let pageSize = 10 + + func getSeriesList() { + if !isLoading && !isLast { + repository + .getSeriesList(creatorId: creatorId, sortType: .NEWEST, page: page, size: pageSize) + .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.self, from: responseData) + + if let data = decoded.data, decoded.success { + if page == 1 { + self.seriesList.removeAll() + } + + if !data.items.isEmpty { + page += 1 + self.seriesList.append(contentsOf: data.items) + } else { + isLast = true + } + } 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) + } + } + +} diff --git a/SodaLive/Sources/Content/Series/SeriesRepository.swift b/SodaLive/Sources/Content/Series/SeriesRepository.swift new file mode 100644 index 0000000..591351b --- /dev/null +++ b/SodaLive/Sources/Content/Series/SeriesRepository.swift @@ -0,0 +1,19 @@ +// +// SeriesRepository.swift +// SodaLive +// +// Created by klaus on 4/29/24. +// + +import Foundation +import CombineMoya +import Combine +import Moya + +class SeriesRepository { + private let api = MoyaProvider() + + func getSeriesList(creatorId: Int, sortType: SeriesListAllViewModel.SeriesSortType, page: Int, size: Int) -> AnyPublisher { + return api.requestPublisher(.getSeriesList(creatorId: creatorId, sortType: sortType, page: page, size: size)) + } +} diff --git a/SodaLive/Sources/ContentView.swift b/SodaLive/Sources/ContentView.swift index af23a02..5d95491 100644 --- a/SodaLive/Sources/ContentView.swift +++ b/SodaLive/Sources/ContentView.swift @@ -178,6 +178,9 @@ struct ContentView: View { case .contentAllByTheme(let themeId): ContentAllByThemeView(themeId: themeId) + case .seriesAll(let creatorId): + SeriesListAllView(creatorId: creatorId) + default: EmptyView() .frame(width: 0, height: 0, alignment: .topLeading) diff --git a/SodaLive/Sources/Explorer/Profile/Series/UserProfileSeriesView.swift b/SodaLive/Sources/Explorer/Profile/Series/UserProfileSeriesView.swift index 6ae210f..6676474 100644 --- a/SodaLive/Sources/Explorer/Profile/Series/UserProfileSeriesView.swift +++ b/SodaLive/Sources/Explorer/Profile/Series/UserProfileSeriesView.swift @@ -23,15 +23,20 @@ struct UserProfileSeriesView: View { Text("전체보기") .font(.custom(Font.light.rawValue, size: 11.3)) .foregroundColor(Color.grayee) - .onTapGesture {} + .onTapGesture { + AppState.shared + .setAppStep(step: .seriesAll(creatorId: creatorId)) + } } HStack(spacing: 13.3) { ForEach(0..