Skip to content

Commit

Permalink
Merge pull request #23 from ghis22130/feature-diffable
Browse files Browse the repository at this point in the history
feat: Mod BanchanListViewMdoel, BanchanListViewController
  • Loading branch information
ghis22130 authored Apr 22, 2021
2 parents 00059ea + 7cf0ddc commit b9b9498
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 110 deletions.
8 changes: 4 additions & 4 deletions iOS/SideDish/SideDish.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
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 */; };
D4BFBB5626301B6B00D68297 /* Section.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4BFBB5526301B6B00D68297 /* Section.swift */; };
FFA10EB1262FC6CD00D584B6 /* BanchanCollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFA10EB0262FC6CD00D584B6 /* BanchanCollectionView.swift */; };
FFEF70F926310FB400189376 /* NetworkService.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEF70F826310FB400189376 /* NetworkService.swift */; };
FFEF70FF2631104B00189376 /* BanchanListDTO+Mapping.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEF70FE2631104B00189376 /* BanchanListDTO+Mapping.swift */; };
FFEF71042631107200189376 /* FetchBanchanListUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEF71032631107200189376 /* FetchBanchanListUseCase.swift */; };
FFEF71092631128900189376 /* FetchImageUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEF71082631128900189376 /* FetchImageUseCase.swift */; };
FFEF7123263119C500189376 /* BanchanListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEF7122263119C500189376 /* BanchanListViewModel.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -81,14 +81,14 @@
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>"; };
D4BFBB5526301B6B00D68297 /* Section.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Section.swift; sourceTree = "<group>"; };
D7433C6120AE2C2B5B290532 /* Pods_SideDishTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SideDishTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
E5121CE39396F4ECB1937C15 /* Pods-SideDish-SideDishUITests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SideDish-SideDishUITests.debug.xcconfig"; path = "Target Support Files/Pods-SideDish-SideDishUITests/Pods-SideDish-SideDishUITests.debug.xcconfig"; sourceTree = "<group>"; };
FFA10EB0262FC6CD00D584B6 /* BanchanCollectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BanchanCollectionView.swift; sourceTree = "<group>"; };
FFEF70F826310FB400189376 /* NetworkService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkService.swift; sourceTree = "<group>"; };
FFEF70FE2631104B00189376 /* BanchanListDTO+Mapping.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "BanchanListDTO+Mapping.swift"; sourceTree = "<group>"; };
FFEF71032631107200189376 /* FetchBanchanListUseCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchBanchanListUseCase.swift; sourceTree = "<group>"; };
FFEF71082631128900189376 /* FetchImageUseCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchImageUseCase.swift; sourceTree = "<group>"; };
FFEF7122263119C500189376 /* BanchanListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BanchanListViewModel.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -255,7 +255,7 @@
D4BFBB21262EA7BC00D68297 /* ViewModel */ = {
isa = PBXGroup;
children = (
D4BFBB5526301B6B00D68297 /* Section.swift */,
FFEF7122263119C500189376 /* BanchanListViewModel.swift */,
D4BFBB20262EA7B600D68297 /* View */,
);
path = ViewModel;
Expand Down Expand Up @@ -575,7 +575,6 @@
D4BFBAD7262E989000D68297 /* BanchanListViewController.swift in Sources */,
D4BFBAD3262E989000D68297 /* AppDelegate.swift in Sources */,
D4BFBAD5262E989000D68297 /* SceneDelegate.swift in Sources */,
D4BFBB5626301B6B00D68297 /* Section.swift in Sources */,
FFA10EB1262FC6CD00D584B6 /* BanchanCollectionView.swift in Sources */,
D4BFBB0A262E9A8200D68297 /* DetailBanchanViewController.swift in Sources */,
D4BFBB2F262EAD1C00D68297 /* DetailBanchan.swift in Sources */,
Expand All @@ -584,6 +583,7 @@
D4BFBB35262EB89800D68297 /* BanchanCustomCell.swift in Sources */,
D4BFBB2A262EA93700D68297 /* Banchan.swift in Sources */,
FFEF71092631128900189376 /* FetchImageUseCase.swift in Sources */,
FFEF7123263119C500189376 /* BanchanListViewModel.swift in Sources */,
D4BFBB3C262EC8DB00D68297 /* BanchanCustomCellHeader.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ extension BanchanListDTO {

extension BanchanListDTO.BanchanListItemDTO {
func toDomain() -> Banchan {
return .init(hash: hash, image: image, alt: alt, title: title, description: description, netPrice: normalPrice, salePrice: salePrice, badge: badge, deliveryType: deliveryType)
var newData: Data?
FetchImageUseCase.fetch(network: NetworkSerivce.shared, imgURL: image) { data in
DispatchQueue.main.async {
newData = data
}
}
return .init(hash: hash, image: newData, alt: alt, title: title, description: description, netPrice: normalPrice, salePrice: salePrice, badge: badge, delivery_type: deliveryType)
}
}
5 changes: 5 additions & 0 deletions iOS/SideDish/SideDish/Data/Network/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ protocol NetworkRequest {

class NetworkSerivce: NetworkRequest {

static let shared = NetworkSerivce()

private init() {
}

func request(url strUrl: String, httpMethod: HTTPMethod, completion: @escaping (AFDataResponse<Data>) -> Void) {
guard let url = URL(string: strUrl) else { return }

Expand Down
6 changes: 3 additions & 3 deletions iOS/SideDish/SideDish/Domain/Entities/Banchan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ enum PriceType {

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

func hash(into hasher: inout Hasher) {
hasher.combine(hash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import Foundation

struct FetchBanchanListUseCase {
private static let url: String = "https://h3rb9c0ugl.execute-api.ap-northeast-2.amazonaws.com/develop/baminchan/side"
private static let baseURL: String = "https://h3rb9c0ugl.execute-api.ap-northeast-2.amazonaws.com/develop/baminchan/"

static func fetchBanchanList(network: NetworkRequest, completion: @escaping ([Banchan]?) -> Void) {
network.request(url: self.url, httpMethod: .get) { dataDummy in
static func fetchBanchanList(network: NetworkRequest,section: String, completion: @escaping ([Banchan]?) -> Void) {
let url = baseURL+section
network.request(url: url, httpMethod: .get) { dataDummy in
guard let data = dataDummy.data else { return }

guard let banchans = try? JSONDecoder().decode(BanchanListDTO.self, from: data) else { return }
Expand Down
5 changes: 5 additions & 0 deletions iOS/SideDish/SideDish/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,53 @@
// BanchanListViewModel.swift
// SideDish
//
// Created by 지북 on 2021/04/20.
// Created by 지북 on 2021/04/22.
//

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

var menu: Dictionary<Section, [Banchan]> {
didSet {
NotificationCenter.default.post(name: Notification.Name("updateMenu"), object: self)
}
}
var network = NetworkSerivce.shared

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

func fetchMenu() {
FetchBanchanListUseCase.fetchBanchanList(network: network, section: "main", completion: { banchans in
guard let banchans = banchans else { return }
self.menu[.main] = banchans
})

FetchBanchanListUseCase.fetchBanchanList(network: network, section: "soup", completion: { banchans in
guard let banchans = banchans else { return }
self.menu[.soup] = banchans
})

FetchBanchanListUseCase.fetchBanchanList(network: network, section: "side", completion: { banchans in
guard let banchans = banchans else { return }
self.menu[.side] = banchans
})
}

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

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import UIKit

class BanchanCustomCell: UICollectionViewCell {

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
Expand All @@ -28,18 +28,12 @@ class BanchanCustomCell: UICollectionViewCell {

var banchan: Banchan? {
didSet{
guard let url = URL(string: "\(banchan!.image)"),
let data = try? Data(contentsOf: url) else {
return
}

imageView.image = UIImage(data: data)
imageView.image = UIImage(data: banchan!.image ?? Data())
titleLabel.text = banchan?.title
descriptionLabel.text = banchan?.description
netPriceLabel.text = "\(banchan?.netPrice ?? "")"
salePriceLabel.text = "\(banchan?.salePrice ?? "")"
priceTypeLabel.text = "\(banchan?.badge ?? [])"
netPriceLabel.text = "1000"
salePriceLabel.text = "2000"
priceTypeLabel.text = "3000"
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,54 @@ class BanchanListViewController: UIViewController {

@IBOutlet weak var banchanCollectionView: BanchanCollectionView!

//var viewModel = BanchanListViewModel()
private var sections = Section.allSections
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)
applySnapshot(animatingDifferences: true)
banchanCollectionView.dataSource = self.dataSource
banchanCollectionView.delegate = self

NotificationCenter.default.addObserver(self, selector: #selector(applySnapshot(animatingDifferences:)), name: Notification.Name("updateMenu"), object: viewModel)
}
}

// 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
}
cell.banchan = banchan
return cell
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BanchanCustomCell.identifer, for: indexPath) as? BanchanCustomCell else { return nil }
cell.banchan = banchan
return cell
}

dataSource.supplementaryViewProvider = { (collectionView, kind, indexPath) in
guard kind == UICollectionView.elementKindSectionHeader else {
return nil
}

guard let view = collectionView.dequeueReusableSupplementaryView(
ofKind: kind,
withReuseIdentifier: BanchanCustomCellHeader.identifier,
for: indexPath) as? BanchanCustomCellHeader else {
return nil
}
guard kind == UICollectionView.elementKindSectionHeader else { return nil }
let section = self.dataSource.snapshot().sectionIdentifiers[indexPath.section]
view.titleLabel.text = section.title.rawValue
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
}

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

snapshot.appendSections(sections)

sections.forEach { section in
snapshot.appendItems(section.banchans, toSection: section)
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)
}
}
Expand Down

0 comments on commit b9b9498

Please sign in to comment.