From d9a7d6477e98c287cc68910e0a88be065e91c21b Mon Sep 17 00:00:00 2001 From: Roy-wonji Date: Sun, 5 Nov 2023 00:22:04 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=AA=9B[chore]=20:=20=20delet=20=EB=B0=8F?= =?UTF-8?q?=20=20=EC=95=84=ED=82=A4=EC=9D=B4=EB=B8=8C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/Model/Archive/ArchiveModel.swift | 66 +++++++++++++ .../Sources/Model/Archive/DeleteModel.swift | 27 ++++++ .../UI/ViewModel/ArchiveViewViewModel.swift | 94 +++++++++++++++++++ .../Sources/ViewModel/ExploreViewModel.swift | 11 ++- 4 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 PingPong/Projects/Core/Domain/Model/Sources/Model/Archive/ArchiveModel.swift create mode 100644 PingPong/Projects/Core/Domain/Model/Sources/Model/Archive/DeleteModel.swift diff --git a/PingPong/Projects/Core/Domain/Model/Sources/Model/Archive/ArchiveModel.swift b/PingPong/Projects/Core/Domain/Model/Sources/Model/Archive/ArchiveModel.swift new file mode 100644 index 00000000..adf1071d --- /dev/null +++ b/PingPong/Projects/Core/Domain/Model/Sources/Model/Archive/ArchiveModel.swift @@ -0,0 +1,66 @@ +// +// ArchiveModel.swift +// Model +// +// Created by 서원지 on 11/4/23. +// Copyright © 2023 Wonji Suh. All rights reserved. +// + +import Foundation + +// MARK: - Welcome +public struct ArchiveModel: Codable { + public let status: Int? + public let data: [ArchiveResponseModel]? + public let message: String? + + public init(status: Int?, data: [ArchiveResponseModel]?, message: String?) { + self.status = status + self.data = data + self.message = message + } +} + +// MARK: - Datum +public struct ArchiveResponseModel: Codable { + public let regDttm, modDttm: String? + public let regrID: String? + public let regrNm: String? + public let modrID: String? + public let modrNm: String? + public let rmk: String? + public let rowStatus: String? + public let quoteID: Int? + public let content: String? + public let author: String? + public let flavor: String? + public let source: String? + public let mood: String? + + enum CodingKeys: String, CodingKey { + case regDttm, modDttm + case regrID = "regrId" + case regrNm + case modrID = "modrId" + case modrNm, rmk, rowStatus + case quoteID = "quoteId" + case content, author, flavor, source, mood + } + + public init(regDttm: String?, modDttm: String?, regrID: String?, regrNm: String?, modrID: String?, modrNm: String?, rmk: String?, rowStatus: String?, quoteID: Int?, content: String?, author: String?, flavor: String?, source: String?, mood: String?) { + self.regDttm = regDttm + self.modDttm = modDttm + self.regrID = regrID + self.regrNm = regrNm + self.modrID = modrID + self.modrNm = modrNm + self.rmk = rmk + self.rowStatus = rowStatus + self.quoteID = quoteID + self.content = content + self.author = author + self.flavor = flavor + self.source = source + self.mood = mood + } +} diff --git a/PingPong/Projects/Core/Domain/Model/Sources/Model/Archive/DeleteModel.swift b/PingPong/Projects/Core/Domain/Model/Sources/Model/Archive/DeleteModel.swift new file mode 100644 index 00000000..23b8da8c --- /dev/null +++ b/PingPong/Projects/Core/Domain/Model/Sources/Model/Archive/DeleteModel.swift @@ -0,0 +1,27 @@ +// +// DeleteModel.swift +// Model +// +// Created by 서원지 on 11/5/23. +// Copyright © 2023 Wonji Suh. All rights reserved. +// + +import Foundation + +public struct DeleteModel: Codable { + public let status: Int? + public let data: DeleteResponseModel? + public let message: String? + + public init(status: Int?, data: DeleteResponseModel?, message: String?) { + self.status = status + self.data = data + self.message = message + } +} + +// MARK: - DataClass +public struct DeleteResponseModel: Codable { + + public init() {} +} diff --git a/PingPong/Projects/Feature/Archive/Sources/UI/ViewModel/ArchiveViewViewModel.swift b/PingPong/Projects/Feature/Archive/Sources/UI/ViewModel/ArchiveViewViewModel.swift index c1f0435b..5cb69794 100644 --- a/PingPong/Projects/Feature/Archive/Sources/UI/ViewModel/ArchiveViewViewModel.swift +++ b/PingPong/Projects/Feature/Archive/Sources/UI/ViewModel/ArchiveViewViewModel.swift @@ -7,10 +7,104 @@ // import Foundation +import Combine +import Moya +import CombineMoya +import API +import Service +import Model public class ArchiveViewViewModel: ObservableObject { public init() { } @Published public var isAscendingOrder = true + + @Published public var archiveModel: ArchiveModel? + var archiveCancellable: AnyCancellable? + + @Published public var deleteModel: DeleteModel? + var deleteCancellable: AnyCancellable? + + public func archiveRequestToViewModel(_ list: ArchiveModel) { + self.archiveModel = list + } + + public func archiveRequest( + userId: String, + completion: @escaping () -> Void + + ) async { + if let cancellable = archiveCancellable { + cancellable.cancel() + } + + let provider = MoyaProvider(plugins: [MoyaLoggingPlugin()]) + archiveCancellable = provider.requestWithProgressPublisher(.myPageLikes(userId: userId)) + .compactMap { $0.response?.data } + .receive(on: DispatchQueue.main) + .decode(type: ArchiveModel.self, decoder: JSONDecoder()) + .sink(receiveCompletion: { [weak self] result in + switch result { + case .finished: + break + case .failure(let error): + print("네트워크 에러 \(error.localizedDescription)") + } + }, receiveValue: { [weak self] model in + if let status = model.status { + if status == NetworkCode.success.status { + self?.archiveRequestToViewModel(model) + print("마이페이지 좋아요 \(model)") + completion() + } else { + + } + } + }) + + } + + public func deleteToViewModel(_ list: DeleteModel){ + self.deleteModel = list + } + + + public func deleteRequest( + likeId: String, + completion: @escaping () -> Void + ) async { + if let cancellable = deleteCancellable { + cancellable.cancel() + } + + let provider = MoyaProvider(plugins: [MoyaLoggingPlugin()]) + deleteCancellable = provider.requestWithProgressPublisher(.deleteLike(likeId: likeId)) + .compactMap { $0.response?.data } + .receive(on: DispatchQueue.main) + .decode(type: DeleteModel.self, decoder: JSONDecoder()) + .sink(receiveCompletion: { [weak self] result in + switch result { + case .finished: + break + case .failure(let error): + print("네트워크 에러 \(error.localizedDescription)") + } + }, receiveValue: { [weak self] model in + if let status = model.status { + if status == NetworkCode.success.status { + self?.deleteToViewModel(model) + print("마이페이지 좋아요 삭제 \(model)") + completion() + } else { + self?.deleteToViewModel(model) + print("마이페이지 좋아요 삭제 \(model)") + completion() + } + } + }) + + + + } } diff --git a/PingPong/Projects/Feature/Search/Sources/ViewModel/ExploreViewModel.swift b/PingPong/Projects/Feature/Search/Sources/ViewModel/ExploreViewModel.swift index 6d274d09..9502007d 100644 --- a/PingPong/Projects/Feature/Search/Sources/ViewModel/ExploreViewModel.swift +++ b/PingPong/Projects/Feature/Search/Sources/ViewModel/ExploreViewModel.swift @@ -56,11 +56,14 @@ public class ExploreViewModel: ObservableObject { print("네트워크 에러 \(error.localizedDescription)") } }, receiveValue: { [weak self] model in - self?.searchRequestToViewModel(model) - print("검색 결과 \(model)") - completion() + if let status = model.status { + if status == NetworkCode.success.status { + self?.searchRequestToViewModel(model) + print("검색 결과 \(model)") + completion() + } + } }) - } }