Skip to content

Commit

Permalink
Feat: ViewModel 데이터 바인딩 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
HoonHaChoi committed Apr 20, 2021
1 parent 526b4af commit 199e3d6
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 23 deletions.
8 changes: 4 additions & 4 deletions Side-dish/Side-dish.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
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 */; };
Expand All @@ -34,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 Down Expand Up @@ -112,7 +112,6 @@
BFCE49B4262E74FF006C0882 /* DomainLayer */,
BFCE49BB262E7742006C0882 /* PresentationLayer */,
8844B7C1262D6CEC00FA49E9 /* View */,
BFCE4956262D4891006C0882 /* ViewController.swift */,
BFCE496E262D883F006C0882 /* Main.storyboard */,
BFCE495B262D4895006C0882 /* Assets.xcassets */,
BFCE495D262D4895006C0882 /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -152,6 +151,7 @@
isa = PBXGroup;
children = (
BFCE49BC262E7759006C0882 /* SideDishViewModel.swift */,
BFCE4956262D4891006C0882 /* SideDishViewController.swift */,
);
path = PresentationLayer;
sourceTree = "<group>";
Expand Down Expand Up @@ -228,7 +228,7 @@
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 */,
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion Side-dish/Side-dish/DataLayer/Network/EndPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enum Endpoint {
}
}

enum Path: String {
enum Path: String, CaseIterable {
case main
case soup
case side
Expand Down
6 changes: 3 additions & 3 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">
<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 storyboardIdentifier="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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import UIKit
import Combine

class ViewController: UIViewController {
class SideDishViewController: UIViewController {

@IBOutlet weak var SideDishCollectionView: UICollectionView!
private var cancellable = Set<AnyCancellable>()
Expand All @@ -20,15 +20,23 @@ class ViewController: UIViewController {
SideDishCollectionView.register(FoodCardCell.nib, forCellWithReuseIdentifier: FoodCardCell.identifier)
SideDishCollectionView.dataSource = self
SideDishCollectionView.delegate = self

sideDishViewModel.test { (t) in
t.forEach { (item) in
print(item.title)
}
}
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 @@ -39,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
33 changes: 23 additions & 10 deletions Side-dish/Side-dish/PresentationLayer/SideDishViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,35 @@ class SideDishViewModel {

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

func bind(handler: @escaping (Result<[Item], NetworkError>) -> ()) {
sideDishUseCase.execute(path: .main).sink { (complete) in
if case .failure(let error) = complete {
handler(.failure(error))
}
} receiveValue: { (SideDishes) in
self.item = SideDishes.body
handler(.success(SideDishes.body))
}.store(in: &cancellable)
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.sink { (item) in
$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)
}
}
2 changes: 1 addition & 1 deletion Side-dish/Side-dish/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let screen = (scene as? UIWindowScene) else { return }

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

Expand Down

0 comments on commit 199e3d6

Please sign in to comment.