Skip to content

Commit

Permalink
Merge pull request #6 from HoonHaChoi/feat-3-EndPoint
Browse files Browse the repository at this point in the history
EndPoint Merge
  • Loading branch information
jung-yun committed Apr 20, 2021
2 parents b0313b5 + 199e3d6 commit 4a57969
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 60 deletions.
46 changes: 41 additions & 5 deletions Side-dish/Side-dish.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
8844B7D4262DBA3F00FA49E9 /* SideDishes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8844B7D3262DBA3F00FA49E9 /* SideDishes.swift */; };
BFCE4953262D4891006C0882 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE4952262D4891006C0882 /* AppDelegate.swift */; };
BFCE4955262D4891006C0882 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE4954262D4891006C0882 /* SceneDelegate.swift */; };
BFCE4957262D4891006C0882 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE4956262D4891006C0882 /* ViewController.swift */; };
BFCE4957262D4891006C0882 /* SideDishViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE4956262D4891006C0882 /* SideDishViewController.swift */; };
BFCE495C262D4895006C0882 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = BFCE495B262D4895006C0882 /* Assets.xcassets */; };
BFCE495F262D4895006C0882 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BFCE495D262D4895006C0882 /* LaunchScreen.storyboard */; };
BFCE496F262D883F006C0882 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BFCE496E262D883F006C0882 /* Main.storyboard */; };
BFCE4976262DBFE2006C0882 /* EndPoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE4975262DBFE2006C0882 /* EndPoint.swift */; };
BFCE497A262DC205006C0882 /* HTTPMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE4979262DC205006C0882 /* HTTPMethod.swift */; };
BFCE497D262DC22A006C0882 /* NetworkError.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE497C262DC22A006C0882 /* NetworkError.swift */; };
BFCE4980262DC3F2006C0882 /* NetworkManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE497F262DC3F2006C0882 /* NetworkManager.swift */; };
BFCE49B7262E751F006C0882 /* SideDishUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE49B6262E751E006C0882 /* SideDishUseCase.swift */; };
BFCE49BD262E7759006C0882 /* SideDishViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE49BC262E7759006C0882 /* SideDishViewModel.swift */; };
BFCE49C2262E8918006C0882 /* DIContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE49C1262E8918006C0882 /* DIContainer.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -31,7 +34,7 @@
BFCE494F262D4891006C0882 /* Side-dish.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Side-dish.app"; sourceTree = BUILT_PRODUCTS_DIR; };
BFCE4952262D4891006C0882 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
BFCE4954262D4891006C0882 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
BFCE4956262D4891006C0882 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
BFCE4956262D4891006C0882 /* SideDishViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SideDishViewController.swift; sourceTree = "<group>"; };
BFCE495B262D4895006C0882 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
BFCE495E262D4895006C0882 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
BFCE4960262D4895006C0882 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand All @@ -40,6 +43,9 @@
BFCE4979262DC205006C0882 /* HTTPMethod.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPMethod.swift; sourceTree = "<group>"; };
BFCE497C262DC22A006C0882 /* NetworkError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkError.swift; sourceTree = "<group>"; };
BFCE497F262DC3F2006C0882 /* NetworkManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkManager.swift; sourceTree = "<group>"; };
BFCE49B6262E751E006C0882 /* SideDishUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SideDishUseCase.swift; sourceTree = "<group>"; };
BFCE49BC262E7759006C0882 /* SideDishViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SideDishViewModel.swift; sourceTree = "<group>"; };
BFCE49C1262E8918006C0882 /* DIContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DIContainer.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -99,11 +105,13 @@
BFCE4951262D4891006C0882 /* Side-dish */ = {
isa = PBXGroup;
children = (
8844B7CD262DB62C00FA49E9 /* DataLayer */,
BFCE4952262D4891006C0882 /* AppDelegate.swift */,
BFCE4954262D4891006C0882 /* SceneDelegate.swift */,
BFCE49C1262E8918006C0882 /* DIContainer.swift */,
8844B7CD262DB62C00FA49E9 /* DataLayer */,
BFCE49B4262E74FF006C0882 /* DomainLayer */,
BFCE49BB262E7742006C0882 /* PresentationLayer */,
8844B7C1262D6CEC00FA49E9 /* View */,
BFCE4956262D4891006C0882 /* ViewController.swift */,
BFCE496E262D883F006C0882 /* Main.storyboard */,
BFCE495B262D4895006C0882 /* Assets.xcassets */,
BFCE495D262D4895006C0882 /* LaunchScreen.storyboard */,
Expand All @@ -123,6 +131,31 @@
path = Network;
sourceTree = "<group>";
};
BFCE49B4262E74FF006C0882 /* DomainLayer */ = {
isa = PBXGroup;
children = (
BFCE49B5262E7513006C0882 /* UseCase */,
);
path = DomainLayer;
sourceTree = "<group>";
};
BFCE49B5262E7513006C0882 /* UseCase */ = {
isa = PBXGroup;
children = (
BFCE49B6262E751E006C0882 /* SideDishUseCase.swift */,
);
path = UseCase;
sourceTree = "<group>";
};
BFCE49BB262E7742006C0882 /* PresentationLayer */ = {
isa = PBXGroup;
children = (
BFCE49BC262E7759006C0882 /* SideDishViewModel.swift */,
BFCE4956262D4891006C0882 /* SideDishViewController.swift */,
);
path = PresentationLayer;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -195,14 +228,17 @@
buildActionMask = 2147483647;
files = (
8844B7D0262DB6A500FA49E9 /* Item.swift in Sources */,
BFCE4957262D4891006C0882 /* ViewController.swift in Sources */,
BFCE4957262D4891006C0882 /* SideDishViewController.swift in Sources */,
BFCE4980262DC3F2006C0882 /* NetworkManager.swift in Sources */,
BFCE4953262D4891006C0882 /* AppDelegate.swift in Sources */,
BFCE4955262D4891006C0882 /* SceneDelegate.swift in Sources */,
BFCE49B7262E751F006C0882 /* SideDishUseCase.swift in Sources */,
BFCE49C2262E8918006C0882 /* DIContainer.swift in Sources */,
BFCE497A262DC205006C0882 /* HTTPMethod.swift in Sources */,
8844B7BF262D6C3000FA49E9 /* FoodCardCell.swift in Sources */,
BFCE497D262DC22A006C0882 /* NetworkError.swift in Sources */,
BFCE4976262DBFE2006C0882 /* EndPoint.swift in Sources */,
BFCE49BD262E7759006C0882 /* SideDishViewModel.swift in Sources */,
8844B7D4262DBA3F00FA49E9 /* SideDishes.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Binary file not shown.
17 changes: 17 additions & 0 deletions Side-dish/Side-dish/DIContainer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// DIContainer.swift
// Side-dish
//
// Created by HOONHA CHOI on 2021/04/20.
//

import Foundation

class DIContainer {
static func createDI() -> SideDishViewModel {
let networkManage = NetworkManager()
let useCase = SideDishUseCase(networkManager: networkManage)
let viewModel = SideDishViewModel(sideDishUseCase: useCase)
return viewModel
}
}
8 changes: 4 additions & 4 deletions Side-dish/Side-dish/DataLayer/Network/EndPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ enum Endpoint {
}
}

enum Path: String {
case main = "1main1"
case soup = "soup"
case side = "side"
enum Path: String, CaseIterable {
case main
case soup
case side
}
30 changes: 15 additions & 15 deletions Side-dish/Side-dish/DataLayer/Network/NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@
import Foundation
import Combine

class NetworkManager {
protocol NetworkManageable {
func requestResource(path: Path, method: HTTPMethod) -> AnyPublisher<SideDishes, NetworkError>
}

class NetworkManager: NetworkManageable {

func getResource(path: Path, method: HTTPMethod) -> AnyPublisher<SideDishes, NetworkError> {
func requestResource(path: Path, method: HTTPMethod) -> AnyPublisher<SideDishes, NetworkError> {
guard let urlRequest = makeURLRequest(path: path, method: method) else {
return Fail(error: NetworkError.invalidURL).eraseToAnyPublisher()
}
return URLSession.shared.dataTaskPublisher(for: urlRequest)
.mapError { _ in
NetworkError.invalidRequest
}
.tryMap{ data , response -> Data in
.flatMap { data, response -> AnyPublisher<SideDishes, NetworkError> in
guard let httpResponse = response as? HTTPURLResponse else {
throw NetworkError.invalidResponse
return Fail(error: NetworkError.invalidResponse).eraseToAnyPublisher()
}
guard 200..<300 ~= httpResponse.statusCode else {
throw NetworkError.invalidStatusCode(httpResponse.statusCode)
}
guard !data.isEmpty else {
throw NetworkError.emptyData
return Fail(error:NetworkError.invalidStatusCode(httpResponse.statusCode)).eraseToAnyPublisher()
}
return data
}
.decode(type: SideDishes.self, decoder: JSONDecoder())
.mapError { _ in
NetworkError.failParsing
}
.eraseToAnyPublisher()
return Just(data)
.decode(type: SideDishes.self, decoder: JSONDecoder())
.mapError { _ in
NetworkError.failParsing
}.eraseToAnyPublisher()
}.eraseToAnyPublisher()
}

private func makeURLRequest(path: Path, method: HTTPMethod) -> URLRequest? {
Expand Down
26 changes: 26 additions & 0 deletions Side-dish/Side-dish/DomainLayer/UseCase/SideDishUseCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// UseCase.swift
// Side-dish
//
// Created by HOONHA CHOI on 2021/04/20.
//

import Foundation
import Combine

protocol SideDishProtocol {
func execute(path: Path) -> AnyPublisher<SideDishes, NetworkError>
}

class SideDishUseCase: SideDishProtocol {

private let networkManager: NetworkManageable

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

func execute(path: Path) -> AnyPublisher<SideDishes, NetworkError> {
return networkManager.requestResource(path: path, method: .get)
}
}
25 changes: 4 additions & 21 deletions Side-dish/Side-dish/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17506" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="ETQ-co-NCv">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17506" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="hGp-I0-H3m">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17505"/>
Expand All @@ -9,10 +9,10 @@
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<!--Side Dish View Controller-->
<scene sceneID="d8q-YB-ASa">
<objects>
<viewController id="hGp-I0-H3m" customClass="ViewController" customModule="Side_dish" customModuleProvider="target" sceneMemberID="viewController">
<viewController storyboardIdentifier="ViewController" id="hGp-I0-H3m" customClass="SideDishViewController" customModule="Side_dish" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="fGK-fr-HD1">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
Expand Down Expand Up @@ -54,24 +54,7 @@
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="ySh-Mg-ZVv" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="983" y="141"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="SKe-cE-aiu">
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" navigationBarHidden="YES" id="ETQ-co-NCv" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" id="N5n-Eh-W6D">
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
<connections>
<segue destination="hGp-I0-H3m" kind="relationship" relationship="rootViewController" id="zth-7w-ZeX"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Eqb-zu-rEU" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="278" y="129"/>
<point key="canvasLocation" x="810" y="88"/>
</scene>
</scenes>
<resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,35 @@
import UIKit
import Combine

class ViewController: UIViewController {
class SideDishViewController: UIViewController {

@IBOutlet weak var SideDishCollectionView: UICollectionView!
private var cancellable = Set<AnyCancellable>()
private var sideDishViewModel: SideDishViewModel!

override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.isHidden = true
SideDishCollectionView.register(FoodCardCell.nib, forCellWithReuseIdentifier: FoodCardCell.identifier)
SideDishCollectionView.dataSource = self
SideDishCollectionView.delegate = self

NetworkManager().getResource(path: .main, method: .get).sink { (complete) in
if case .failure(let error) = complete {
print(error)
sideDishViewModel.test { (t) in
t.forEach { (item) in
print(item.title)
}
} receiveValue: { (category) in
print(category)
}.store(in: &cancellable)

}
sideDishViewModel.occur { (t) in
print("test : \(t)")
}
}

func depend(sideDishViewModel: SideDishViewModel) {
self.sideDishViewModel = sideDishViewModel
}

}

extension ViewController: UICollectionViewDataSource {
extension SideDishViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
Expand All @@ -42,7 +47,7 @@ extension ViewController: UICollectionViewDataSource {
}
}

extension ViewController: UICollectionViewDelegateFlowLayout {
extension SideDishViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 130)
}
Expand Down
52 changes: 52 additions & 0 deletions Side-dish/Side-dish/PresentationLayer/SideDishViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// ViewModel.swift
// Side-dish
//
// Created by HOONHA CHOI on 2021/04/20.
//

import Foundation
import Combine

class SideDishViewModel {

private let sideDishUseCase: SideDishProtocol
private var cancellable = Set<AnyCancellable>()

@Published var errorMessage = ""
@Published var item: [Item] = []

init(sideDishUseCase: SideDishProtocol) {
self.sideDishUseCase = sideDishUseCase
request()
}

private func request() {
Path.allCases.forEach { (path) in
sideDishUseCase.execute(path: path).sink { (complete) in
if case .failure(let error) = complete {
self.errorMessage = error.message
}
} receiveValue: { (SideDishes) in
self.item = SideDishes.body
}.store(in: &cancellable)
}

}

func test(completion: @escaping ([Item]) -> ()){
$item
.dropFirst()
.sink { (item) in
completion(item)
}.store(in: &cancellable)
}

func occur(completion: @escaping ((String) ->())) {
$errorMessage
.dropFirst()
.sink { (message) in
completion(message)
}.store(in: &cancellable)
}
}
16 changes: 13 additions & 3 deletions Side-dish/Side-dish/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@
import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
guard let screen = (scene as? UIWindowScene) else { return }

guard let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as? SideDishViewController else {
return
}

viewController.depend(sideDishViewModel: DIContainer.createDI())
window = UIWindow(frame: screen.coordinateSpace.bounds)
window?.windowScene = screen
window?.rootViewController = UINavigationController(rootViewController: viewController)
window?.makeKeyAndVisible()
}
}

Expand Down

0 comments on commit 4a57969

Please sign in to comment.