Skip to content

Commit

Permalink
feat: Add BanchanListViewModel, UICollectionViewDiffableDataSource
Browse files Browse the repository at this point in the history
섹션별로 업데이트를 하기위해 UICollectionViewDiffableDataSource를 도입하였습니다.
이에 따른 NSDiffableDataSourceSnapshot을 적용해 주었고 가독성을 높이기 위해 typealias를 통하여 재정의 해주었습니다.
DataSource에 정보를 주기 위한 BanchanListViewModel을 정의해주었습니다.

issue: #13
  • Loading branch information
ghis22130 committed Apr 21, 2021
1 parent f416793 commit a5f36c4
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 25 deletions.
16 changes: 12 additions & 4 deletions iOS/SideDish/SideDish.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
D4BFBB36262EB89800D68297 /* BanchanCustomCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D4BFBB34262EB89800D68297 /* BanchanCustomCell.xib */; };
D4BFBB3C262EC8DB00D68297 /* BanchanCustomCellHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4BFBB3A262EC8DB00D68297 /* BanchanCustomCellHeader.swift */; };
D4BFBB3D262EC8DB00D68297 /* BanchanCustomCellHeader.xib in Resources */ = {isa = PBXBuildFile; fileRef = D4BFBB3B262EC8DB00D68297 /* BanchanCustomCellHeader.xib */; };
FFA10EAA262FBBD200D584B6 /* BanchanListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFA10EA9262FBBD200D584B6 /* BanchanListViewModel.swift */; };
FFA10EB1262FC6CD00D584B6 /* BanchanCollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFA10EB0262FC6CD00D584B6 /* BanchanCollectionView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -65,6 +67,8 @@
D4BFBB34262EB89800D68297 /* BanchanCustomCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = BanchanCustomCell.xib; sourceTree = "<group>"; };
D4BFBB3A262EC8DB00D68297 /* BanchanCustomCellHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BanchanCustomCellHeader.swift; sourceTree = "<group>"; };
D4BFBB3B262EC8DB00D68297 /* BanchanCustomCellHeader.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = BanchanCustomCellHeader.xib; sourceTree = "<group>"; };
FFA10EA9262FBBD200D584B6 /* BanchanListViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BanchanListViewModel.swift; sourceTree = "<group>"; };
FFA10EB0262FC6CD00D584B6 /* BanchanCollectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BanchanCollectionView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -192,13 +196,19 @@
isa = PBXGroup;
children = (
D4BFBAD6262E989000D68297 /* BanchanListViewController.swift */,
FFA10EB0262FC6CD00D584B6 /* BanchanCollectionView.swift */,
D4BFBB33262EB89800D68297 /* BanchanCustomCell.swift */,
D4BFBB34262EB89800D68297 /* BanchanCustomCell.xib */,
D4BFBB3A262EC8DB00D68297 /* BanchanCustomCellHeader.swift */,
D4BFBB3B262EC8DB00D68297 /* BanchanCustomCellHeader.xib */,
);
path = View;
sourceTree = "<group>";
};
D4BFBB21262EA7BC00D68297 /* ViewModel */ = {
isa = PBXGroup;
children = (
FFA10EA9262FBBD200D584B6 /* BanchanListViewModel.swift */,
);
path = ViewModel;
sourceTree = "<group>";
Expand All @@ -208,10 +218,6 @@
children = (
D4BFBB08262E9A8100D68297 /* DetailBanchanViewController.swift */,
D4BFBB09262E9A8200D68297 /* DetailBanchanViewController.xib */,
D4BFBB33262EB89800D68297 /* BanchanCustomCell.swift */,
D4BFBB34262EB89800D68297 /* BanchanCustomCell.xib */,
D4BFBB3A262EC8DB00D68297 /* BanchanCustomCellHeader.swift */,
D4BFBB3B262EC8DB00D68297 /* BanchanCustomCellHeader.xib */,
);
path = VIew;
sourceTree = "<group>";
Expand Down Expand Up @@ -399,7 +405,9 @@
files = (
D4BFBAD7262E989000D68297 /* BanchanListViewController.swift in Sources */,
D4BFBAD3262E989000D68297 /* AppDelegate.swift in Sources */,
FFA10EAA262FBBD200D584B6 /* BanchanListViewModel.swift in Sources */,
D4BFBAD5262E989000D68297 /* SceneDelegate.swift in Sources */,
FFA10EB1262FC6CD00D584B6 /* BanchanCollectionView.swift in Sources */,
D4BFBB0A262E9A8200D68297 /* DetailBanchanViewController.swift in Sources */,
D4BFBB2F262EAD1C00D68297 /* DetailBanchan.swift in Sources */,
D4BFBB35262EB89800D68297 /* BanchanCustomCell.swift in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>SideDish.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
20 changes: 13 additions & 7 deletions iOS/SideDish/SideDish/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,29 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="JdN-M1-kEg">
<rect key="frame" x="0.0" y="88" width="414" height="774"/>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="JdN-M1-kEg" customClass="BanchanCollectionView" customModule="SideDish" customModuleProvider="target">
<rect key="frame" x="0.0" y="44" width="414" height="818"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<collectionViewFlowLayout key="collectionViewLayout" automaticEstimatedItemSize="YES" minimumLineSpacing="10" minimumInteritemSpacing="10" id="Fhs-Nj-eY3">
<size key="itemSize" width="128" height="128"/>
<size key="headerReferenceSize" width="0.0" height="0.0"/>
<size key="headerReferenceSize" width="50" height="50"/>
<size key="footerReferenceSize" width="0.0" height="0.0"/>
<inset key="sectionInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
</collectionViewFlowLayout>
<cells>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" id="P0E-7d-Beb">
<rect key="frame" x="0.0" y="0.0" width="128" height="128"/>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="BanchanCustomCell" id="P0E-7d-Beb" customClass="BanchanCustomCell" customModule="SideDish" customModuleProvider="target">
<rect key="frame" x="0.0" y="50" width="128" height="128"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<collectionViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO" id="7aV-8e-Rej">
<rect key="frame" x="0.0" y="0.0" width="128" height="128"/>
<autoresizingMask key="autoresizingMask"/>
</collectionViewCellContentView>
</collectionViewCell>
</cells>
<collectionReusableView key="sectionHeaderView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="BanchanCustomCellHeader" id="Nu8-H2-72q" customClass="BanchanCustomCellHeader" customModule="SideDish" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="414" height="50"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</collectionReusableView>
</collectionView>
</subviews>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
Expand All @@ -48,6 +52,9 @@
</constraints>
</view>
<navigationItem key="navigationItem" id="EJy-m7-z9G"/>
<connections>
<outlet property="banchanCollectionView" destination="JdN-M1-kEg" id="FLu-Wr-7CB"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
Expand All @@ -56,10 +63,9 @@
<!--Navigation Controller-->
<scene sceneID="fFg-F6-nkd">
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="AcM-5S-3KN" sceneMemberID="viewController">
<navigationController automaticallyAdjustsScrollViewInsets="NO" navigationBarHidden="YES" id="AcM-5S-3KN" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" id="Sit-P2-pFh">
<rect key="frame" x="0.0" y="44" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
Expand Down
27 changes: 17 additions & 10 deletions iOS/SideDish/SideDish/Domain/Entities/Banchan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ enum PriceType {
static let event = "이벤트특가"
}

struct Banchan {
private var hash: String
private var image: URL
private var alt: String
private var title: String
private var description: String
private var netPrice: Int
private var salePrice: Int?
private var badge: [PriceType]
private var delivery_type: [Delivery]
struct Banchan: Hashable {
private (set) var hash: String
private (set) var image: URL
private (set) var alt: String
private (set) var title: String
private (set) var description: String
private (set) var netPrice: Int
private (set) var salePrice: Int?
private (set) var badge: [PriceType]
private (set) var delivery_type: [Delivery]

func hash(into hasher: inout Hasher) {
hasher.combine(hash)
}
static func == (lhs: Banchan, rhs: Banchan) -> Bool {
lhs.hash == rhs.hash
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// BanchanCollectionView.swift
// SideDish
//
// Created by 지북 on 2021/04/21.
//

import UIKit

class BanchanCollectionView: UICollectionView {
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout: layout)
configure()
}

required init?(coder: NSCoder) {
super.init(coder: coder)
configure()
}

private func configure() {
self.register(BanchanCustomCell.nib, forCellWithReuseIdentifier: BanchanCustomCell.identifer)
self.register(BanchanCustomCellHeader.nib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: BanchanCustomCellHeader.identifier)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Y4o-0b-QQA">
<rect key="frame" x="0.0" y="0.0" width="343" height="32"/>
<rect key="frame" x="16" y="0.0" width="311" height="32"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="22"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
Expand All @@ -23,9 +23,9 @@
<viewLayoutGuide key="safeArea" id="VXr-Tz-HHm"/>
<constraints>
<constraint firstItem="Y4o-0b-QQA" firstAttribute="top" secondItem="U6b-Vx-4bR" secondAttribute="top" id="DqX-UG-rjs"/>
<constraint firstAttribute="trailing" secondItem="Y4o-0b-QQA" secondAttribute="trailing" id="O6a-at-ypl"/>
<constraint firstAttribute="trailing" secondItem="Y4o-0b-QQA" secondAttribute="trailing" constant="16" id="O6a-at-ypl"/>
<constraint firstItem="VXr-Tz-HHm" firstAttribute="bottom" secondItem="Y4o-0b-QQA" secondAttribute="bottom" id="TuJ-Ku-8Wc"/>
<constraint firstItem="Y4o-0b-QQA" firstAttribute="leading" secondItem="U6b-Vx-4bR" secondAttribute="leading" id="eYy-qZ-XAQ"/>
<constraint firstItem="Y4o-0b-QQA" firstAttribute="leading" secondItem="U6b-Vx-4bR" secondAttribute="leading" constant="16" id="eYy-qZ-XAQ"/>
</constraints>
<connections>
<outlet property="titleLabel" destination="Y4o-0b-QQA" id="I1o-TD-iuc"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,76 @@
import UIKit

class BanchanListViewController: UIViewController {


@IBOutlet weak var banchanCollectionView: BanchanCollectionView!

var viewModel = BanchanListViewModel()
lazy var dataSource = configureDataSource()

typealias DataSource = UICollectionViewDiffableDataSource<Section, Banchan>
typealias Snapshot = NSDiffableDataSourceSnapshot<Section, Banchan>
typealias Section = BanchanListViewModel.Section

override func viewDidLoad() {
super.viewDidLoad()
applySnapshot(animatingDifferences: false)
banchanCollectionView.dataSource = self.dataSource
banchanCollectionView.delegate = self
}
}

// MARK: - DataSource
extension BanchanListViewController {
func configureDataSource() -> DataSource {
let dataSource = DataSource(collectionView: banchanCollectionView) { (collectionView, indexPath, banchan) -> UICollectionViewCell? in
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BanchanCustomCell.identifer, for: indexPath) as? BanchanCustomCell else { return nil }
return cell
}
print("2")
dataSource.supplementaryViewProvider = { (collectionView, kind, indexPath) in
guard kind == UICollectionView.elementKindSectionHeader else { return nil }
let section = self.dataSource.snapshot().sectionIdentifiers[indexPath.section]
guard let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: BanchanCustomCellHeader.identifier, for: indexPath) as? BanchanCustomCellHeader else { return nil }
view.titleLabel.text = section.rawValue
return view

}
return dataSource
}

func applySnapshot(animatingDifferences: Bool = true) {
var snapshot = Snapshot()

snapshot.appendSections(Section.allCases)

Section.allCases.forEach { section in
guard let banchans = viewModel.getBanchans(section: section) else { return }
snapshot.appendItems(banchans, toSection: section)
}

dataSource.apply(snapshot, animatingDifferences: animatingDifferences)
}
}

// MARK: - UICollectionViewDataSource Implementation
//extension VideosViewController {
// override func collectionView(
// _ collectionView: UICollectionView,
// didSelectItemAt indexPath: IndexPath
// ) {
// guard let video = dataSource.itemIdentifier(for: indexPath) else {
// return
// }
// guard let link = video.link else {
// print("Invalid link")
// return
// }
// let safariViewController = SFSafariViewController(url: link)
// present(safariViewController, animated: true, completion: nil)
// }
//}

// MARK: - Delegate
extension BanchanListViewController: UICollectionViewDelegate {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// BanchanListViewModel.swift
// SideDish
//
// Created by 지북 on 2021/04/20.
//

import Foundation
import UIKit

class BanchanListViewModel {
enum Section: String, CaseIterable {
case soup = "정성이 담긴 뜨끈뜨끈 국물요리"
case side = "식탁을 풍성하게 하는 정갈한 밑반찬"
case main = "모두가 좋아하는 든든한 메인요리"
}

var menu: Dictionary<Section, [Banchan]>

init() {
self.menu = [:]
}

func sectionCount() -> Int {
return Section.allCases.count
}

func getBanchans(section: Section) -> [Banchan]? {
return menu[section]
}
}

0 comments on commit a5f36c4

Please sign in to comment.