Skip to content

Commit

Permalink
Merge pull request #24 from ghis22130/feature-Combine
Browse files Browse the repository at this point in the history
Feature combine
  • Loading branch information
youngminshim-de authored Apr 22, 2021
2 parents b9b9498 + 30af0d2 commit a5f141d
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 36 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion iOS/SideDish/SideDish.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
D4BFBB1E262EA7A100D68297 /* BanchanList */ = {
isa = PBXGroup;
children = (
D4BFBB20262EA7B600D68297 /* View */,
D4BFBB21262EA7BC00D68297 /* ViewModel */,
);
path = BanchanList;
Expand Down Expand Up @@ -256,7 +257,6 @@
isa = PBXGroup;
children = (
FFEF7122263119C500189376 /* BanchanListViewModel.swift */,
D4BFBB20262EA7B600D68297 /* View */,
);
path = ViewModel;
sourceTree = "<group>";
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "AA19519A-5D83-41CC-BC5D-146FCCFF6D38"
type = "0"
version = "2.0">
</Bucket>
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ extension BanchanListDTO {

extension BanchanListDTO.BanchanListItemDTO {
func toDomain() -> Banchan {
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)
return .init(hash: hash, image: image, alt: alt, title: title, description: description, netPrice: normalPrice, salePrice: salePrice, badge: badge, delivery_type: deliveryType)
}
}
2 changes: 1 addition & 1 deletion iOS/SideDish/SideDish/Domain/Entities/Banchan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum PriceType {

struct Banchan: Hashable {
private (set) var hash: String
private (set) var image: Data?
private (set) var image: String
private (set) var alt: String
private (set) var title: String
private (set) var description: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class BanchanCustomCell: UICollectionViewCell {

var banchan: Banchan? {
didSet{
imageView.image = UIImage(data: banchan!.image ?? Data())
titleLabel.text = banchan?.title
descriptionLabel.text = banchan?.description
netPriceLabel.text = "1000"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@
//

import UIKit
import Toaster

class BanchanCustomCellHeader: UICollectionReusableView {

@IBOutlet weak var titleLabel: UILabel!
static let identifier = "BanchanCustomCellHeader"
var cellCount = 0

override func awakeFromNib() {
super.awakeFromNib()
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(headerTouched(_:)))
self.addGestureRecognizer(tapRecognizer)
}

static var nib: UINib {
return UINib(nibName: identifier, bundle: nil)
}

@objc func headerTouched(_ sender: UITapGestureRecognizer) {
Toast(text: "\(cellCount)개 상품이 등록되어 있습니다.").show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
//

import UIKit
import Combine

class BanchanListViewController: UIViewController {

@IBOutlet weak var banchanCollectionView: BanchanCollectionView!

var viewModel = BanchanListViewModel()
lazy var dataSource = configureDataSource()
var subscriptions = Set<AnyCancellable>()

typealias DataSource = UICollectionViewDiffableDataSource<Section, Banchan>
typealias Snapshot = NSDiffableDataSourceSnapshot<Section, Banchan>
Expand All @@ -21,11 +23,26 @@ class BanchanListViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

applySnapshot(animatingDifferences: true)
applySnapshot(animatingDifferences: false)
banchanCollectionView.dataSource = self.dataSource
banchanCollectionView.delegate = self

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

private func bind() {
viewModel.$menu
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { (result) in
switch result {
case .failure(let error):
print(error)
case .finished:
break
}
}, receiveValue: { (value) in
self.applySnapshot()
})
.store(in: &subscriptions)
}
}

Expand All @@ -34,21 +51,32 @@ 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 }

FetchImageUseCase.fetch(network: NetworkSerivce.shared, imgURL: banchan.image) { (data) in
guard let data = data else {
return
}
cell.imageView.image = UIImage(data: data)
}
cell.banchan = banchan
return cell
}

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
view.cellCount = self.viewModel.count(section: section)
return view

}
return dataSource
}

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

Expand All @@ -63,23 +91,6 @@ extension BanchanListViewController {
}
}

// 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, UICollectionViewDelegateFlowLayout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import Combine

class BanchanListViewModel {
enum Section: String, CaseIterable {
Expand All @@ -14,18 +15,18 @@ class BanchanListViewModel {
case main = "모두가 좋아하는 든든한 메인요리"
}

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

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

func count(section: Section) -> Int {
return menu[section]?.count ?? 0
}

func fetchMenu() {
FetchBanchanListUseCase.fetchBanchanList(network: network, section: "main", completion: { banchans in
guard let banchans = banchans else { return }
Expand Down

0 comments on commit a5f141d

Please sign in to comment.