Skip to content

Commit

Permalink
feat: ✨price post 요청 및 receive 구현(#42)
Browse files Browse the repository at this point in the history
서버 에러 및 badURL으로 미완성채 커밋함
  • Loading branch information
Lia316 committed Jun 2, 2021
1 parent 6cc6e86 commit eb6456b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 52 deletions.
4 changes: 4 additions & 0 deletions iOS/Airbnb/Airbnb.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
AEC52F712663FF3600D8F802 /* ConditionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEC52F6F2663FF3600D8F802 /* ConditionCell.swift */; };
AEC52F742664041200D8F802 /* ConditionViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEC52F732664041200D8F802 /* ConditionViewModel.swift */; };
AEF03BA526674E8200050531 /* PriceViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEF03BA426674E8200050531 /* PriceViewController.swift */; };
AEF03BA726675AB200050531 /* PriceUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEF03BA626675AB200050531 /* PriceUseCase.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -95,6 +96,7 @@
AEC52F6F2663FF3600D8F802 /* ConditionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConditionCell.swift; sourceTree = "<group>"; };
AEC52F732664041200D8F802 /* ConditionViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConditionViewModel.swift; sourceTree = "<group>"; };
AEF03BA426674E8200050531 /* PriceViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PriceViewController.swift; sourceTree = "<group>"; };
AEF03BA626675AB200050531 /* PriceUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PriceUseCase.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -273,6 +275,7 @@
isa = PBXGroup;
children = (
AEF03BA426674E8200050531 /* PriceViewController.swift */,
AEF03BA626675AB200050531 /* PriceUseCase.swift */,
);
path = Price;
sourceTree = "<group>";
Expand Down Expand Up @@ -411,6 +414,7 @@
buildActionMask = 2147483647;
files = (
AE9AC6A7265CCCBF0038C198 /* CalendarViewController.swift in Sources */,
AEF03BA726675AB200050531 /* PriceUseCase.swift in Sources */,
AE11F5AE265F3D920034779F /* Date+extension.swift in Sources */,
AE0B5DE326560287000B35E7 /* MainPageUseCase.swift in Sources */,
AE0B5DEF26563BE4000B35E7 /* RecommandedDestinationViewController.swift in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -84,53 +84,5 @@
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "EFDC7A0B-304B-4050-82F1-F0A8509261FC"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Airbnb/MainPage/Controller/MainPageViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "23"
endingLineNumber = "23"
landmarkName = "viewDidLoad()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "6D414F91-BA3A-4A0F-92D9-2526DC57B69D"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Airbnb/MainPage/Controller/MainPageViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "42"
endingLineNumber = "42"
landmarkName = "bind()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "DDBB8F6A-5C7E-44D5-B2A0-64045AA8C544"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Airbnb/MainPage/Controller/MainPageViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "43"
endingLineNumber = "43"
landmarkName = "bind()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
43 changes: 43 additions & 0 deletions iOS/Airbnb/Airbnb/ConditionPage/Price/PriceUseCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// PriceUseCase.swift
// Airbnb
//
// Created by Lia on 2021/06/02.
//

import Foundation
import Combine

class PriceUseCase {

@Published var prices: Prices!
@Published var error: Error!

private var networkManager: NetworkManageable
private var cancelBag = Set<AnyCancellable>()

init(networkManager: NetworkManageable = NetworkManager()) {
self.networkManager = networkManager
}

}

extension PriceUseCase {

func requestMainPage(condition: Condition) {

networkManager.post(url: EndPoint.url(path: "/rooms/price")!, data: condition, result: Prices.self)
.receive(on: DispatchQueue.main)
.sink { error in
print("😡📮error", error)
self.error = error as? Error
} receiveValue: { games in
self.prices = games.self
}.store(in: &cancelBag)
}

}

struct Prices: Decodable {
var prices: [Int]
}
24 changes: 24 additions & 0 deletions iOS/Airbnb/Airbnb/ConditionPage/Price/PriceViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
//

import UIKit
import Combine

class PriceViewController: UIViewController {


private var conditionViewModel: ConditionViewModel
private var priceUseCase = PriceUseCase()
private var cancelBag = Set<AnyCancellable>()
private var containerView: UIView!
let label = UILabel()

Expand All @@ -26,6 +29,8 @@ class PriceViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
priceUseCase.requestMainPage(condition: Condition(cityId: conditionViewModel.city, schedule: conditionViewModel.schedule, price: conditionViewModel.price, people: conditionViewModel.people))
bind()
configure()
configureContainer()
configureNavigation()
Expand All @@ -35,6 +40,25 @@ class PriceViewController: UIViewController {

}

extension PriceViewController {

private func bind() {
priceUseCase.$prices.receive(on: DispatchQueue.main)
.sink { prices in
guard let prices = prices else { return }
print(prices)
}
.store(in: &cancelBag)

priceUseCase.$error
.receive(on: DispatchQueue.main)
.sink { error in
guard let error = error else { return }
print(error) ///사용자에게 에러 표시하는 부분 미구현
}.store(in: &cancelBag)
}
}


extension PriceViewController {

Expand Down
9 changes: 5 additions & 4 deletions iOS/Airbnb/Airbnb/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum EndPoint {

protocol NetworkManageable {
func get<T: Decodable>(type: T.Type, url: URL) -> AnyPublisher<T, Error>
func post<T: Codable>(url: URL, data: T) -> AnyPublisher<Void, Error>
func post<T: Encodable, R: Decodable>(url: URL, data: T, result: R.Type) -> AnyPublisher<R, Error>
}


Expand Down Expand Up @@ -66,7 +66,7 @@ extension NetworkManager: NetworkManageable {
.eraseToAnyPublisher()
}

func post<T: Codable>(url: URL, data: T) -> AnyPublisher<Void, Error> {
func post<T: Encodable, R: Decodable>(url: URL, data: T, result: R.Type) -> AnyPublisher<R, Error> {

return Just(data)
.encode(encoder: JSONEncoder())
Expand All @@ -85,12 +85,13 @@ extension NetworkManager: NetworkManageable {
}
.flatMap { request in
return self.session.dataTaskPublisher(for: request)
.tryMap { element -> Void in
.tryMap { element -> R in
guard let httpResponse = element.response as? HTTPURLResponse,
httpResponse.statusCode == 200 else {
throw NetworkError.BadURL
}
return
let result = try JSONDecoder().decode(R.self, from: element.data)
return result
}
}
.eraseToAnyPublisher()
Expand Down

0 comments on commit eb6456b

Please sign in to comment.