diff --git a/frontend/SideDish/SideDish.xcodeproj/project.pbxproj b/frontend/SideDish/SideDish.xcodeproj/project.pbxproj index 82de7546f..a55d670e1 100644 --- a/frontend/SideDish/SideDish.xcodeproj/project.pbxproj +++ b/frontend/SideDish/SideDish.xcodeproj/project.pbxproj @@ -9,6 +9,8 @@ /* Begin PBXBuildFile section */ BD090658262E9B450094421D /* MenuCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD090656262E9B450094421D /* MenuCell.swift */; }; BD090659262E9B450094421D /* MenuCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = BD090657262E9B450094421D /* MenuCell.xib */; }; + BD09065E262EBD310094421D /* Menu.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD09065D262EBD310094421D /* Menu.swift */; }; + BD09066D262EC5120094421D /* Menus.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD09066C262EC5120094421D /* Menus.swift */; }; BD930B58262D6F5800906633 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD930B57262D6F5800906633 /* AppDelegate.swift */; }; BD930B5A262D6F5800906633 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD930B59262D6F5800906633 /* SceneDelegate.swift */; }; BD930B5C262D6F5800906633 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD930B5B262D6F5800906633 /* ViewController.swift */; }; @@ -17,6 +19,9 @@ BD930B64262D6F5900906633 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BD930B62262D6F5900906633 /* LaunchScreen.storyboard */; }; BD930B6F262D6F5900906633 /* SideDishTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD930B6E262D6F5900906633 /* SideDishTests.swift */; }; BD930B7A262D6F5900906633 /* SideDishUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD930B79262D6F5900906633 /* SideDishUITests.swift */; }; + BDFABA96262FF9FC00803154 /* Parsing.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDFABA95262FF9FC00803154 /* Parsing.swift */; }; + BDFABA9B262FFA0600803154 /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDFABA9A262FFA0600803154 /* Request.swift */; }; + BDFABAA0262FFA1C00803154 /* Network.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDFABA9F262FFA1C00803154 /* Network.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -39,6 +44,8 @@ /* Begin PBXFileReference section */ BD090656262E9B450094421D /* MenuCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuCell.swift; sourceTree = ""; }; BD090657262E9B450094421D /* MenuCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = MenuCell.xib; sourceTree = ""; }; + BD09065D262EBD310094421D /* Menu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Menu.swift; sourceTree = ""; }; + BD09066C262EC5120094421D /* Menus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Menus.swift; sourceTree = ""; }; BD930B54262D6F5800906633 /* SideDish.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SideDish.app; sourceTree = BUILT_PRODUCTS_DIR; }; BD930B57262D6F5800906633 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; BD930B59262D6F5800906633 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -53,6 +60,9 @@ BD930B75262D6F5900906633 /* SideDishUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SideDishUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; BD930B79262D6F5900906633 /* SideDishUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SideDishUITests.swift; sourceTree = ""; }; BD930B7B262D6F5900906633 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + BDFABA95262FF9FC00803154 /* Parsing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Parsing.swift; sourceTree = ""; }; + BDFABA9A262FFA0600803154 /* Request.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Request.swift; sourceTree = ""; }; + BDFABA9F262FFA1C00803154 /* Network.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Network.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -80,6 +90,51 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + BD090662262EBD350094421D /* Model */ = { + isa = PBXGroup; + children = ( + BD090674262ED1CA0094421D /* DTO */, + BD09065D262EBD310094421D /* Menu.swift */, + BD09066C262EC5120094421D /* Menus.swift */, + ); + path = Model; + sourceTree = ""; + }; + BD090666262EBD7A0094421D /* Controller */ = { + isa = PBXGroup; + children = ( + ); + path = Controller; + sourceTree = ""; + }; + BD090667262EBD820094421D /* View */ = { + isa = PBXGroup; + children = ( + BD090656262E9B450094421D /* MenuCell.swift */, + BD090657262E9B450094421D /* MenuCell.xib */, + ); + path = View; + sourceTree = ""; + }; + BD090668262EBDC80094421D /* AppCycle */ = { + isa = PBXGroup; + children = ( + BD930B57262D6F5800906633 /* AppDelegate.swift */, + BD930B59262D6F5800906633 /* SceneDelegate.swift */, + ); + path = AppCycle; + sourceTree = ""; + }; + BD090674262ED1CA0094421D /* DTO */ = { + isa = PBXGroup; + children = ( + BDFABA95262FF9FC00803154 /* Parsing.swift */, + BDFABA9A262FFA0600803154 /* Request.swift */, + BDFABA9F262FFA1C00803154 /* Network.swift */, + ); + path = DTO; + sourceTree = ""; + }; BD930B4B262D6F5800906633 = { isa = PBXGroup; children = ( @@ -103,14 +158,14 @@ BD930B56262D6F5800906633 /* SideDish */ = { isa = PBXGroup; children = ( - BD930B57262D6F5800906633 /* AppDelegate.swift */, - BD930B59262D6F5800906633 /* SceneDelegate.swift */, + BD090668262EBDC80094421D /* AppCycle */, + BD090667262EBD820094421D /* View */, + BD090666262EBD7A0094421D /* Controller */, + BD090662262EBD350094421D /* Model */, BD930B5B262D6F5800906633 /* ViewController.swift */, BD930B5D262D6F5800906633 /* Main.storyboard */, - BD090656262E9B450094421D /* MenuCell.swift */, - BD090657262E9B450094421D /* MenuCell.xib */, - BD930B60262D6F5900906633 /* Assets.xcassets */, BD930B62262D6F5900906633 /* LaunchScreen.storyboard */, + BD930B60262D6F5900906633 /* Assets.xcassets */, BD930B65262D6F5900906633 /* Info.plist */, ); path = SideDish; @@ -265,10 +320,15 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + BDFABA96262FF9FC00803154 /* Parsing.swift in Sources */, + BDFABA9B262FFA0600803154 /* Request.swift in Sources */, BD930B5C262D6F5800906633 /* ViewController.swift in Sources */, BD930B58262D6F5800906633 /* AppDelegate.swift in Sources */, BD090658262E9B450094421D /* MenuCell.swift in Sources */, + BDFABAA0262FFA1C00803154 /* Network.swift in Sources */, BD930B5A262D6F5800906633 /* SceneDelegate.swift in Sources */, + BD09066D262EC5120094421D /* Menus.swift in Sources */, + BD09065E262EBD310094421D /* Menu.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/frontend/SideDish/SideDish.xcodeproj/project.xcworkspace/xcuserdata/sonjuhyeong.xcuserdatad/UserInterfaceState.xcuserstate b/frontend/SideDish/SideDish.xcodeproj/project.xcworkspace/xcuserdata/sonjuhyeong.xcuserdatad/UserInterfaceState.xcuserstate index 313534b79..01735c39c 100644 Binary files a/frontend/SideDish/SideDish.xcodeproj/project.xcworkspace/xcuserdata/sonjuhyeong.xcuserdatad/UserInterfaceState.xcuserstate and b/frontend/SideDish/SideDish.xcodeproj/project.xcworkspace/xcuserdata/sonjuhyeong.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/frontend/SideDish/SideDish/AppDelegate.swift b/frontend/SideDish/SideDish/AppCycle/AppDelegate.swift similarity index 100% rename from frontend/SideDish/SideDish/AppDelegate.swift rename to frontend/SideDish/SideDish/AppCycle/AppDelegate.swift diff --git a/frontend/SideDish/SideDish/SceneDelegate.swift b/frontend/SideDish/SideDish/AppCycle/SceneDelegate.swift similarity index 100% rename from frontend/SideDish/SideDish/SceneDelegate.swift rename to frontend/SideDish/SideDish/AppCycle/SceneDelegate.swift diff --git a/frontend/SideDish/SideDish/Model/DTO/JSONModel.swift b/frontend/SideDish/SideDish/Model/DTO/JSONModel.swift new file mode 100644 index 000000000..66a9641af --- /dev/null +++ b/frontend/SideDish/SideDish/Model/DTO/JSONModel.swift @@ -0,0 +1,7 @@ + +import Foundation + +struct getMenu: Codable { + let statusCode: Int + let body: [Menu] +} diff --git a/frontend/SideDish/SideDish/Model/DTO/Network.swift b/frontend/SideDish/SideDish/Model/DTO/Network.swift new file mode 100644 index 000000000..752c03a96 --- /dev/null +++ b/frontend/SideDish/SideDish/Model/DTO/Network.swift @@ -0,0 +1,26 @@ +// +// Network.swift +// SideDish +// +// Created by sonjuhyeong on 2021/04/21. +// + +import Foundation + +class DataTaskManager { + + static let session = URLSession(configuration: .default) + + static func get(completion: @escaping (Result) -> Void) { + session.dataTask(with: RequestManager.getRequest()) { data, response, error in + if let data = data { + guard let menuList = ParsingManager.decodeData(type: getMenu.self, data: data) else { print("d"); return } + completion(.success(menuList)) + } else { + guard let error = error?.localizedDescription as? Error else { return } + completion(.failure(error)) + } + }.resume() + } + +} diff --git a/frontend/SideDish/SideDish/Model/DTO/Parsing.swift b/frontend/SideDish/SideDish/Model/DTO/Parsing.swift new file mode 100644 index 000000000..df70240bd --- /dev/null +++ b/frontend/SideDish/SideDish/Model/DTO/Parsing.swift @@ -0,0 +1,31 @@ +// +// Parsing.swift +// SideDish +// +// Created by sonjuhyeong on 2021/04/21. +// + +import Foundation + +class ParsingManager { + + static func encodeData(data: T) -> Data? { + let encoder = JSONEncoder() + do { + return try encoder.encode(data) + }catch{ + print("Encode error") + return nil + } + } + + static func decodeData(type: T.Type, data: Data) -> T? { + let decoder = JSONDecoder() + do { + return try decoder.decode(type, from: data) + }catch{ + print("Decode error") + return nil + } + } +} diff --git a/frontend/SideDish/SideDish/Model/DTO/Request.swift b/frontend/SideDish/SideDish/Model/DTO/Request.swift new file mode 100644 index 000000000..3cd8da5d4 --- /dev/null +++ b/frontend/SideDish/SideDish/Model/DTO/Request.swift @@ -0,0 +1,23 @@ +// +// Request.swift +// SideDish +// +// Created by sonjuhyeong on 2021/04/21. +// + +import Foundation + +class RequestManager { + + static let urlString = "https://h3rb9c0ugl.execute-api.ap-northeast-2.amazonaws.com/develop/baminchan/main" + //GET + static func getRequest() -> URLRequest { + guard let url = URL(string: urlString) else { + print("The URL is inappropriate.") + return URLRequest(url: URL(string: "")!) + } + return URLRequest(url: url) + } + + +} diff --git a/frontend/SideDish/SideDish/Model/Menu.swift b/frontend/SideDish/SideDish/Model/Menu.swift new file mode 100644 index 000000000..9a51344de --- /dev/null +++ b/frontend/SideDish/SideDish/Model/Menu.swift @@ -0,0 +1,62 @@ +// +// Menu.swift +// SideDish +// +// Created by sonjuhyeong on 2021/04/20. +// + +import Foundation + +class Menu: Codable { + + private let detail_hash: String + private let image: String + private let alt: String + private let delivery_type: [String] + private let title: String + private let description: String + private let n_price: String? + private let s_price: String + private let badge: [String]? + + init(detail_hash: String, image: String, alt: String, delivery_type: [String], title: String, description: String, n_price: String?, s_price: String, badge: [String]?) { + self.detail_hash = detail_hash + self.image = image + self.alt = alt + self.delivery_type = delivery_type + self.title = title + self.description = description + self.n_price = n_price + self.s_price = s_price + self.badge = badge + } + + func configure(cell: MenuCell) { + cell.titleLabel.text = self.title + cell.bodyLabel.text = self.description + cell.currentPriceLabel.text = self.s_price + } +} + + + +extension Menu: Hashable, Equatable { + static func == (lhs: Menu, rhs: Menu) -> Bool { + return lhs.detail_hash == rhs.detail_hash || lhs.image == rhs.image || + lhs.alt == rhs.alt || lhs.delivery_type == rhs.delivery_type || + lhs.title == rhs.title || lhs.description == rhs.description || + lhs.n_price == rhs.n_price || lhs.s_price == rhs.s_price || lhs.badge == rhs.badge + } + + func hash(into hasher: inout Hasher) { + hasher.combine(detail_hash) + hasher.combine(image) + hasher.combine(alt) + hasher.combine(delivery_type) + hasher.combine(title) + hasher.combine(description) + hasher.combine(n_price) + hasher.combine(s_price) + hasher.combine(badge) + } +} diff --git a/frontend/SideDish/SideDish/Model/Menus.swift b/frontend/SideDish/SideDish/Model/Menus.swift new file mode 100644 index 000000000..33b9c8a4a --- /dev/null +++ b/frontend/SideDish/SideDish/Model/Menus.swift @@ -0,0 +1,26 @@ +// +// VariousMenu.swift +// SideDish +// +// Created by sonjuhyeong on 2021/04/20. +// + +import Foundation + +class Menus { + + private var menus: [Menu]! + + init() { + self.menus = nil + } + + func add(menuList: [Menu]) { + self.menus = menuList + } + + func giveMenu() -> [Menu] { + return self.menus + } +} + diff --git a/frontend/SideDish/SideDish/View/MainDiffableDataSource.swift b/frontend/SideDish/SideDish/View/MainDiffableDataSource.swift new file mode 100644 index 000000000..5142d3a57 --- /dev/null +++ b/frontend/SideDish/SideDish/View/MainDiffableDataSource.swift @@ -0,0 +1,26 @@ + +import Foundation +import UIKit + +class MainDiffableDataSource { + + private var dataSource : UICollectionViewDiffableDataSource! + + func setupDataSource(collectionView: UICollectionView) { + self.dataSource = UICollectionViewDiffableDataSource(collectionView: collectionView) { + (collectionView, indexPath, menu) -> UICollectionViewCell? in + guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "menuCell", for: indexPath) as? MenuCell else { preconditionFailure() } + + menu.configure(cell: cell) + return cell + } + } + + func applySnapshot(data: [Menu]) { + let menus = data + var snapshot = NSDiffableDataSourceSnapshot() + snapshot.appendSections([1]) + snapshot.appendItems(menus) + self.dataSource.apply(snapshot, animatingDifferences: true) + } +} diff --git a/frontend/SideDish/SideDish/MenuCell.swift b/frontend/SideDish/SideDish/View/MenuCell.swift similarity index 89% rename from frontend/SideDish/SideDish/MenuCell.swift rename to frontend/SideDish/SideDish/View/MenuCell.swift index 4c84a5862..29b57c492 100644 --- a/frontend/SideDish/SideDish/MenuCell.swift +++ b/frontend/SideDish/SideDish/View/MenuCell.swift @@ -27,7 +27,6 @@ class MenuCell: UICollectionViewCell { self.eventLabel.layer.cornerRadius = 5 self.launchingLabel.layer.masksToBounds = true self.launchingLabel.layer.cornerRadius = 5 - self.titleLabel.text = "[미노리키친] 일본식 우엉조림 (킨피라고보) 80g" } } diff --git a/frontend/SideDish/SideDish/MenuCell.xib b/frontend/SideDish/SideDish/View/MenuCell.xib similarity index 99% rename from frontend/SideDish/SideDish/MenuCell.xib rename to frontend/SideDish/SideDish/View/MenuCell.xib index 70965c973..621169e22 100644 --- a/frontend/SideDish/SideDish/MenuCell.xib +++ b/frontend/SideDish/SideDish/View/MenuCell.xib @@ -51,7 +51,7 @@ - + diff --git a/frontend/SideDish/SideDish/ViewController.swift b/frontend/SideDish/SideDish/ViewController.swift index d33a9b09a..f9e1a1567 100644 --- a/frontend/SideDish/SideDish/ViewController.swift +++ b/frontend/SideDish/SideDish/ViewController.swift @@ -11,30 +11,39 @@ class ViewController: UIViewController { @IBOutlet weak var sideDishCollectionView: UICollectionView! + private var dataSource = MainDiffableDataSource() + private var menus = Menus() + + override func viewDidLoad() { super.viewDidLoad() + + configureCollectionView() + loadCard() + } + + private func configureCollectionView() { let nibName = UINib(nibName: "MenuCell", bundle: .none) sideDishCollectionView.register(nibName, forCellWithReuseIdentifier: "menuCell") - sideDishCollectionView.dataSource = self - sideDishCollectionView.delegate = self + self.dataSource.setupDataSource(collectionView: self.sideDishCollectionView) } - - -} - -extension ViewController: UICollectionViewDelegate { -} - -extension ViewController: UICollectionViewDataSource { - func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { - return 5 + private func loadCard() { + DataTaskManager.get(completion: { (result) in + DispatchQueue.global().async { + switch result { + case .success(let data): + self.menus.add(menuList: data.body) + self.putData() + case.failure(let error): + print(error.localizedDescription) + } + } + }) } - func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { - guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "menuCell", for: indexPath) as? MenuCell else { - return UICollectionViewCell() - } - return cell + private func putData() { + self.dataSource.applySnapshot(data: menus.giveMenu()) } + }