From 74671ed986bebe5a70b8eeaf0c2cfd22e9bb5730 Mon Sep 17 00:00:00 2001 From: EunsuSeo01 Date: Mon, 30 Dec 2024 11:00:19 +0900 Subject: [PATCH] =?UTF-8?q?[Feat]=20#319=20-=20ViewModel=EC=97=90=20API=20?= =?UTF-8?q?=ED=98=B8=EC=B6=9C=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1=20?= =?UTF-8?q?=EB=B0=8F=20=EC=A3=BC=EC=86=8C=20=ED=8F=AC=EB=A7=B7=ED=8C=85=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModel/HankkiDetailViewModel.swift | 54 +++++++++++++++++-- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/ViewModel/HankkiDetailViewModel.swift b/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/ViewModel/HankkiDetailViewModel.swift index b3d306d1..a3d1b8c7 100644 --- a/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/ViewModel/HankkiDetailViewModel.swift +++ b/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/ViewModel/HankkiDetailViewModel.swift @@ -18,11 +18,17 @@ final class HankkiDetailViewModel { setHankkiDetailData?() } } + var address: String? { + didSet { + setHankkiDetailData?() + } + } var removeOptions: [String] = [ StringLiterals.RemoveHankki.optionDisappeared, StringLiterals.RemoveHankki.optionNoMore8000, StringLiterals.RemoveHankki.optionImproperHankki ] + weak var delegate: NetworkResultDelegate? var setHankkiDetailData: (() -> Void)? @@ -36,7 +42,6 @@ final class HankkiDetailViewModel { extension HankkiDetailViewModel { - /// 식당 세부 조회 func getHankkiDetailAPI() { NetworkService.shared.hankkiService.getHankkiDetail(id: hankkiId) { [weak self] result in guard let self = self else { return } @@ -47,13 +52,12 @@ extension HankkiDetailViewModel { default: result.handleNetworkResult(delegate: self.delegate) { response in self.hankkiDetailData = response.data + self.getHankkiAddressAPI() // 상세 조회 후 주소도 같이 불러옴 } } - } } - /// 식당 좋아요 추가 func postHankkiHeartAPI() { NetworkService.shared.hankkiService.postHankkiHeart(id: hankkiId) { result in result.handleNetworkResult { _ in @@ -63,7 +67,6 @@ extension HankkiDetailViewModel { } } - /// 식당 좋아요 삭제 func deleteHankkiHeartAPI() { NetworkService.shared.hankkiService.deleteHankkiHeart(id: hankkiId) { result in result.handleNetworkResult { _ in @@ -72,10 +75,51 @@ extension HankkiDetailViewModel { } } - /// 식당 삭제 func deleteHankkiAPI(completion: @escaping () -> Void) { NetworkService.shared.hankkiService.deleteHankki(id: hankkiId) { result in result.handleNetworkResult(onSuccessVoid: completion) } } } + +private extension HankkiDetailViewModel { + + func getHankkiAddressAPI() { + guard let detailData = hankkiDetailData else { return } + NetworkService.shared.naverMapService.getHankkiAddress(latitude: detailData.latitude, longitude: detailData.longitude) { result in + switch result { + case .badRequest: + UIApplication.showBlackToast(message: StringLiterals.Toast.serverError) + case .serverError: + UIApplication.showBlackToast(message: StringLiterals.Toast.serverError) + default: + result.handleNetworkResult { response in + guard let data = response.results.first, + let data = data else { + self.address = "-" + return + } + + self.address = self.formatAddress(from: data) + } + } + } + } + + func formatAddress(from data: GetHankkiAddressResult) -> String { + let address: [String?] = [ + data.region?.area1?.name, + data.region?.area2?.name, + data.region?.area3?.name, + data.region?.area4?.name, + data.land?.name, + data.land?.number1, + data.land?.number2 + ] + + return address + .compactMap { $0 } + .filter { !$0.isEmpty } + .joined(separator: " ") + } +}