-
Notifications
You must be signed in to change notification settings - Fork 0
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 #25 from cuappdev/jaewon/menu-view
Add collection view for menu view
- Loading branch information
Showing
5 changed files
with
269 additions
and
7 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// MenuCollectionViewCell.swift | ||
// acai-ios | ||
// | ||
// Created by Jaewon Sim on 3/17/19. | ||
// Copyright © 2019 Cornell AppDev. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import SnapKit | ||
|
||
class MenuCollectionViewCell: UICollectionViewCell { | ||
|
||
// MARK: View vars | ||
var imageView: UIImageView! | ||
var titleLabel: UILabel! | ||
var ingredientsLabel: UILabel! | ||
var detailStackView: UIStackView! // include title and ingredients | ||
var bottomSeparator: UIView! | ||
|
||
// MARK: Lifecycle | ||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
backgroundColor = .white | ||
|
||
imageView = UIImageView() | ||
contentView.addSubview(imageView) | ||
|
||
titleLabel = UILabel() | ||
titleLabel.font = UIFont.avenirNextBold.withSize(17) | ||
titleLabel.numberOfLines = 1 | ||
titleLabel.textColor = .acaiBlack | ||
|
||
ingredientsLabel = UILabel() | ||
ingredientsLabel.font = UIFont.avenirNextMedium.withSize(12) | ||
ingredientsLabel.numberOfLines = 2 | ||
ingredientsLabel.lineBreakMode = .byWordWrapping | ||
ingredientsLabel.textColor = .acaiBlack | ||
|
||
detailStackView = UIStackView(arrangedSubviews: [titleLabel, ingredientsLabel]) | ||
detailStackView.axis = .vertical | ||
detailStackView.alignment = .fill | ||
detailStackView.distribution = .equalSpacing | ||
contentView.addSubview(detailStackView) | ||
|
||
bottomSeparator = UIView() | ||
bottomSeparator.backgroundColor = .lineGray | ||
contentView.addSubview(bottomSeparator) | ||
|
||
setUpConstraints() | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
// MARK: Constraint setup | ||
private func setUpConstraints() { | ||
let leadingOffset: CGFloat = 24 | ||
let trailingInset: CGFloat = 72 | ||
let imageViewWidthHeight: CGFloat = 64 | ||
let imageViewStackViewHorizontalSpacing: CGFloat = 24 | ||
let bottomSeparatorHeight: CGFloat = 1.0 | ||
|
||
imageView.snp.makeConstraints { make in | ||
make.centerY.equalToSuperview() | ||
make.leading.equalToSuperview().inset(leadingOffset) | ||
make.width.height.equalTo(imageViewWidthHeight) | ||
} | ||
|
||
detailStackView.snp.makeConstraints { make in | ||
make.leading.equalTo(imageView.snp.trailing).offset(imageViewStackViewHorizontalSpacing) | ||
make.centerY.equalToSuperview() | ||
make.trailing.equalToSuperview().inset(trailingInset) | ||
} | ||
|
||
bottomSeparator.snp.makeConstraints { make in | ||
make.leading.trailing.bottom.equalToSuperview() | ||
make.height.equalTo(bottomSeparatorHeight) | ||
} | ||
} | ||
} |
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,117 @@ | ||
// | ||
// MenuViewController.swift | ||
// acai-ios | ||
// | ||
// Created by Jaewon Sim on 3/13/19. | ||
// Copyright © 2019 Cornell AppDev. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import IGListKit | ||
import SnapKit | ||
|
||
protocol MenuSelectionDelegate: class { | ||
func didSelect(_ item: MenuItem) | ||
} | ||
|
||
class MenuViewController: UIViewController { | ||
|
||
// MARK: Models | ||
var menu: [String: [MenuItem]] = [:] | ||
|
||
// MARK: IGListKit Vars | ||
var collectionView: UICollectionView! | ||
var listAdapter: ListAdapter! | ||
|
||
// MARK: Lifecycle | ||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
view.backgroundColor = .white | ||
|
||
title = "Good Morning, Jamie ☀️" | ||
navigationController?.navigationBar.prefersLargeTitles = true | ||
navigationController?.navigationBar.largeTitleTextAttributes = [ | ||
.font: UIFont.avenirNextMedium.withSize(24) | ||
] | ||
navigationController?.navigationBar.titleTextAttributes = [ | ||
.font: UIFont.avenirNextMedium.withSize(16) | ||
] | ||
|
||
let layout = UICollectionViewFlowLayout() | ||
layout.scrollDirection = .vertical | ||
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) | ||
collectionView.backgroundColor = .white | ||
collectionView.showsVerticalScrollIndicator = false | ||
collectionView.alwaysBounceVertical = true | ||
view.addSubview(collectionView) | ||
|
||
listAdapter = ListAdapter(updater: ListAdapterUpdater(), viewController: self) | ||
listAdapter.collectionView = collectionView | ||
listAdapter.dataSource = self | ||
|
||
setUpConstraints() | ||
} | ||
|
||
override func viewDidAppear(_ animated: Bool) { | ||
super.viewDidAppear(animated) | ||
|
||
loadMenu() | ||
} | ||
|
||
private func loadMenu() { | ||
// TODO: get menu from endpoint | ||
|
||
menu = [ | ||
"Bowls": [ | ||
MenuItem(title: "Crunchy", price: 10.00, orderCustomizationOptions: [], image: UIImage(named: "acaiBowl")!), | ||
MenuItem(title: "Miami", price: 10.00, orderCustomizationOptions: [], image: UIImage(named: "acaiBowl")!) | ||
], | ||
|
||
"Smoothies": [ | ||
], | ||
|
||
"Coffee": [ | ||
] | ||
] | ||
|
||
listAdapter.performUpdates(animated: false, completion: nil) | ||
} | ||
|
||
// MARK: Constraint setup | ||
private func setUpConstraints() { | ||
collectionView.snp.makeConstraints { make in | ||
make.leading.trailing.equalToSuperview() | ||
make.top.equalTo(view.safeAreaLayoutGuide.snp.top) | ||
make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom) | ||
} | ||
} | ||
} | ||
|
||
// MARK: List adapter data source | ||
extension MenuViewController: ListAdapterDataSource { | ||
func objects(for listAdapter: ListAdapter) -> [ListDiffable] { | ||
|
||
let currentList = menu["Bowls"] ?? [] | ||
|
||
return currentList | ||
} | ||
|
||
func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController { | ||
let menuController = MenuListSectionController() | ||
menuController.delegate = self | ||
return menuController | ||
} | ||
|
||
func emptyView(for listAdapter: ListAdapter) -> UIView? { | ||
return nil | ||
} | ||
} | ||
|
||
extension MenuViewController: MenuSelectionDelegate { | ||
|
||
func didSelect(_ item: MenuItem) { | ||
print("Selected \(item.title)") | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
acai-ios/SectionControllers/MenuListSectionController.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,37 @@ | ||
// | ||
// MenuListSectionController.swift | ||
// acai-ios | ||
// | ||
// Created by Jaewon Sim on 3/18/19. | ||
// Copyright © 2019 Cornell AppDev. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import IGListKit | ||
|
||
class MenuListSectionController: ListSectionController { | ||
|
||
var currentMenuItem: MenuItem! | ||
|
||
weak var delegate: MenuSelectionDelegate? | ||
|
||
override func sizeForItem(at index: Int) -> CGSize { | ||
return CGSize(width: collectionContext!.containerSize.width, height: 107) | ||
} | ||
|
||
override func cellForItem(at index: Int) -> UICollectionViewCell { | ||
let cell = collectionContext!.dequeueReusableCell(of: MenuCollectionViewCell.self, for: self, at: index) as! MenuCollectionViewCell | ||
cell.imageView.image = currentMenuItem.image | ||
cell.titleLabel.text = currentMenuItem.title | ||
cell.ingredientsLabel.text = ["Granola", "Banana", "Kiwi", "Coconut Flaxseed"].joined(separator: ", ") | ||
return cell | ||
} | ||
|
||
override func didUpdate(to object: Any) { | ||
currentMenuItem = object as? MenuItem | ||
} | ||
|
||
override func didSelectItem(at index: Int) { | ||
delegate?.didSelect(currentMenuItem) | ||
} | ||
} |