Skip to content

Commit

Permalink
[Feat] #319 - ViewModel에 API 호출 코드 작성 및 주소 포맷팅 함수 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
EunsuSeo01 committed Dec 30, 2024
1 parent 1aedc2a commit 74671ed
Showing 1 changed file with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand All @@ -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 }
Expand All @@ -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
Expand All @@ -63,7 +67,6 @@ extension HankkiDetailViewModel {
}
}

/// 식당 좋아요 삭제
func deleteHankkiHeartAPI() {
NetworkService.shared.hankkiService.deleteHankkiHeart(id: hankkiId) { result in
result.handleNetworkResult { _ in
Expand All @@ -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: " ")
}
}

0 comments on commit 74671ed

Please sign in to comment.