Skip to content

Commit 17bd5f9

Browse files
authored
Merge pull request #222 from 513sojin/#207-리팩토링
[Refactor] #207 - 서버 통신 함수 리팩토링
2 parents 87db050 + 8c6f1fd commit 17bd5f9

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

Runnect-iOS/Runnect-iOS.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
0AEBD608F3973389E8E1C6D6 /* Pods_Runnect_iOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 015778D02D5CDE0838284CD7 /* Pods_Runnect_iOS.framework */; };
11+
232686362B03AF4400675A17 /* NetworkProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 232686352B03AF4400675A17 /* NetworkProvider.swift */; };
1112
23EE06C12AC1AD5200CB3FF8 /* LocationSelectView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 23EE06C02AC1AD5200CB3FF8 /* LocationSelectView.swift */; };
1213
23EE06C52AC1AE1900CB3FF8 /* BaseView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 23EE06C42AC1AE1900CB3FF8 /* BaseView.swift */; };
1314
23EE06C92AC1DED100CB3FF8 /* GesturePublisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 23EE06C82AC1DED100CB3FF8 /* GesturePublisher.swift */; };
@@ -177,6 +178,7 @@
177178

178179
/* Begin PBXFileReference section */
179180
015778D02D5CDE0838284CD7 /* Pods_Runnect_iOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runnect_iOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
181+
232686352B03AF4400675A17 /* NetworkProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkProvider.swift; sourceTree = "<group>"; };
180182
23EE06C02AC1AD5200CB3FF8 /* LocationSelectView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSelectView.swift; sourceTree = "<group>"; };
181183
23EE06C42AC1AE1900CB3FF8 /* BaseView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseView.swift; sourceTree = "<group>"; };
182184
23EE06C82AC1DED100CB3FF8 /* GesturePublisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GesturePublisher.swift; sourceTree = "<group>"; };
@@ -1003,6 +1005,7 @@
10031005
CE6655BE295D82E200C64E12 /* .gitkeep */,
10041006
CE102C4929DBAD3D00E23E69 /* AuthInterceptor.swift */,
10051007
CEE59FD929DD6F7D00C791F1 /* Providers.swift */,
1008+
232686352B03AF4400675A17 /* NetworkProvider.swift */,
10061009
);
10071010
path = Service;
10081011
sourceTree = "<group>";
@@ -1534,6 +1537,7 @@
15341537
CE40BB2D296808B00030ABCA /* DepartureSearchingRouter.swift in Sources */,
15351538
CE15F5A4296C932E0023827C /* RunningModel.swift in Sources */,
15361539
CE17F02D2961BBA100E1DED0 /* ColorLiterals.swift in Sources */,
1540+
232686362B03AF4400675A17 /* NetworkProvider.swift in Sources */,
15371541
71BAD06C2B24D1F70061E31D /* UserProfileVC.swift in Sources */,
15381542
CEC2A68E2962AF2C00160BF7 /* RNMarker.swift in Sources */,
15391543
CE6655D2295D862A00C64E12 /* Publisher+Driver.swift in Sources */,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// NetworkProvider.swift
3+
// Runnect-iOS
4+
//
5+
// Created by Sojin Lee on 2023/11/14.
6+
//
7+
8+
import Moya
9+
10+
enum ResponseResult<T> {
11+
case success(T)
12+
}
13+
14+
class NetworkProvider<Provider : TargetType> : MoyaProvider<Provider> {
15+
func request<Model : Codable>(target : Provider, instance : Model.Type , vc: UIViewController, completion : @escaping(ResponseResult<Model>) -> ()){
16+
self.request(target) { result in
17+
switch result {
18+
/// 서버 통신 성공
19+
case .success(let response):
20+
if (200..<300).contains(response.statusCode) {
21+
if let decodeData = try? JSONDecoder().decode(instance, from: response.data) {
22+
completion(.success(decodeData))
23+
} else{
24+
/// decoding error
25+
vc.showNetworkFailureToast()
26+
}
27+
} else {
28+
/// 응답 코드 400인 경우
29+
vc.showNetworkFailureToast()
30+
}
31+
/// 서버 통신 실패
32+
case .failure(let error):
33+
if let response = error.response {
34+
if let responseData = String(data: response.data, encoding: .utf8) {
35+
print(responseData)
36+
} else {
37+
print(error.localizedDescription)
38+
}
39+
} else {
40+
print(error.localizedDescription)
41+
}
42+
vc.showNetworkFailureToast()
43+
}
44+
}
45+
}
46+
}

Runnect-iOS/Runnect-iOS/Presentation/CourseDrawing/VC/CourseDrawingVC.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ final class CourseDrawingVC: UIViewController {
1717
private let courseProvider = Providers.courseProvider
1818
private let departureSearchingProvider = Providers.departureSearchingProvider
1919

20+
private let networkProvider = NetworkProvider<DepartureSearchingRouter>(withAuth: false)
21+
2022
private var departureLocationModel: DepartureLocationModel?
2123

2224
var pathImage: UIImage?
@@ -194,7 +196,8 @@ extension CourseDrawingVC {
194196

195197
mapView.eventSubject.sink { [weak self] arr in
196198
guard let self = self else { return }
197-
self.searchLocationTmapAddress(latitude: arr[0], longitude: arr[1])
199+
// self.searchLocationTmapAddress(latitude: arr[0], longitude: arr[1])
200+
self.searchTest(latitude: arr[0], longitude: arr[1])
198201
}.store(in: cancelBag)
199202
}
200203

@@ -496,4 +499,13 @@ extension CourseDrawingVC {
496499
}
497500
}
498501
}
502+
503+
private func searchTest(latitude: Double, longitude: Double) {
504+
networkProvider.request(target: .getLocationTmapAddress(latitude: latitude, longitude: longitude), instance: TmapAddressSearchingResponseDto.self, vc: self) { result in
505+
switch result {
506+
case .success(let data):
507+
self.updateData(model: data.toDepartureLocationModel(latitude: latitude, longitude: longitude))
508+
}
509+
}
510+
}
499511
}

0 commit comments

Comments
 (0)