-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Polynomeer/feature/5/model
feat: Create DataSource and Check MainView (#5)
- Loading branch information
Showing
14 changed files
with
293 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+22 KB
(160%)
...roj/project.xcworkspace/xcuserdata/sonjuhyeong.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
import Foundation | ||
|
||
struct getMenu: Codable { | ||
let statusCode: Int | ||
let body: [Menu] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<getMenu, Error>) -> 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() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// Parsing.swift | ||
// SideDish | ||
// | ||
// Created by sonjuhyeong on 2021/04/21. | ||
// | ||
|
||
import Foundation | ||
|
||
class ParsingManager { | ||
|
||
static func encodeData<T: Codable>(data: T) -> Data? { | ||
let encoder = JSONEncoder() | ||
do { | ||
return try encoder.encode(data) | ||
}catch{ | ||
print("Encode error") | ||
return nil | ||
} | ||
} | ||
|
||
static func decodeData<T: Codable>(type: T.Type, data: Data) -> T? { | ||
let decoder = JSONDecoder() | ||
do { | ||
return try decoder.decode(type, from: data) | ||
}catch{ | ||
print("Decode error") | ||
return nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
|
26 changes: 26 additions & 0 deletions
26
frontend/SideDish/SideDish/View/MainDiffableDataSource.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
class MainDiffableDataSource { | ||
|
||
private var dataSource : UICollectionViewDiffableDataSource<Int, Menu>! | ||
|
||
func setupDataSource(collectionView: UICollectionView) { | ||
self.dataSource = UICollectionViewDiffableDataSource<Int, Menu>(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<Int, Menu>() | ||
snapshot.appendSections([1]) | ||
snapshot.appendItems(menus) | ||
self.dataSource.apply(snapshot, animatingDifferences: true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.