2019-07-10 19:52:14 +02:00
|
|
|
//
|
|
|
|
|
// MenuViewController.swift
|
|
|
|
|
// AvH Plan
|
|
|
|
|
//
|
2019-07-13 09:48:20 +02:00
|
|
|
// Created by Deniz Duezgoeren on 11.07.19.
|
2019-07-10 19:52:14 +02:00
|
|
|
// Copyright © 2019 Deniz Duezgoeren. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
2019-07-16 11:51:50 +02:00
|
|
|
import MagazineLayout
|
2019-07-10 19:52:14 +02:00
|
|
|
|
2019-09-16 19:05:06 +02:00
|
|
|
class MenuViewController : UICollectionViewController, UICollectionViewDelegateMagazineLayout, UITabBarControllerDelegate {
|
2019-07-10 19:52:14 +02:00
|
|
|
|
2019-07-13 09:48:20 +02:00
|
|
|
var items = [String]()
|
|
|
|
|
private let refreshControl = UIRefreshControl()
|
2019-07-21 21:14:38 +02:00
|
|
|
let df = DataFetcher.sharedInstance
|
2019-07-16 11:51:50 +02:00
|
|
|
let layout = MagazineLayout()
|
|
|
|
|
let identifier = "menu_cell"
|
|
|
|
|
|
|
|
|
|
required init?(coder decoder: NSCoder) {
|
|
|
|
|
super.init(coder: decoder)
|
2019-09-10 19:22:32 +02:00
|
|
|
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
|
2019-07-16 11:51:50 +02:00
|
|
|
}
|
2019-07-13 09:48:20 +02:00
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
|
super.viewWillAppear(animated)
|
2019-07-16 11:51:50 +02:00
|
|
|
|
|
|
|
|
view.addSubview(collectionView)
|
2019-09-10 19:22:32 +02:00
|
|
|
collectionView.translatesAutoresizingMaskIntoConstraints = true
|
|
|
|
|
|
2019-07-16 11:51:50 +02:00
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
|
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
|
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
2019-09-10 19:22:32 +02:00
|
|
|
collectionView.topAnchor.constraint(equalTo: view.superview!.topAnchor),
|
2019-07-16 11:51:50 +02:00
|
|
|
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
|
|
|
])
|
|
|
|
|
collectionView.register(UINib(nibName: "MenuViewCell", bundle: nil), forCellWithReuseIdentifier: identifier)
|
|
|
|
|
collectionView.dataSource = self
|
|
|
|
|
collectionView.delegate = self
|
2019-07-17 21:54:38 +02:00
|
|
|
collectionView.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
|
2019-07-16 11:51:50 +02:00
|
|
|
|
2019-07-13 09:48:20 +02:00
|
|
|
if #available(iOS 10.0, *) {
|
|
|
|
|
collectionView.refreshControl = refreshControl
|
|
|
|
|
} else {
|
2019-08-23 13:04:36 +02:00
|
|
|
collectionView.addSubview(refreshControl)
|
2019-07-13 09:48:20 +02:00
|
|
|
}
|
|
|
|
|
refreshControl.addTarget(self, action: #selector(objDoAsync(_:)), for: .valueChanged)
|
2019-07-17 21:54:38 +02:00
|
|
|
refreshControl.tintColor = #colorLiteral(red: 0.07843137255, green: 0.5568627451, blue: 1, alpha: 1)
|
|
|
|
|
refreshControl.attributedTitle = NSAttributedString(string: NSLocalizedString("fetch_menu", comment: ""))
|
2019-09-16 19:05:06 +02:00
|
|
|
self.tabBarController?.delegate = self
|
2019-07-17 21:54:38 +02:00
|
|
|
|
|
|
|
|
self.items = self.df.readMenu()
|
|
|
|
|
self.collectionView.reloadData()
|
2019-07-13 09:48:20 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-03 11:01:56 +02:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
2019-09-10 19:22:32 +02:00
|
|
|
self.extendedLayoutIncludesOpaqueBars = true
|
|
|
|
|
self.collectionView.contentInsetAdjustmentBehavior = .always
|
2019-08-03 11:01:56 +02:00
|
|
|
self.navigationController?.navigationBar.topItem?.title = NSLocalizedString("food_menu", comment: "")
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-13 09:48:20 +02:00
|
|
|
@objc private func objDoAsync(_ sender: Any) {
|
|
|
|
|
df.doAsync(do: "menu") { menuItems in
|
|
|
|
|
self.items = menuItems as! [String]
|
|
|
|
|
self.collectionView.reloadData()
|
|
|
|
|
self.refreshControl.endRefreshing()
|
2019-08-23 13:04:36 +02:00
|
|
|
self.df.setTabBarBadge(for: self.tabBarController?.tabBar.items)
|
2019-07-13 09:48:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-16 19:05:06 +02:00
|
|
|
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
|
|
|
|
|
if let alert = self.df.presentInformationAlert(for: tabBarController, at: 3) {
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-10 19:22:32 +02:00
|
|
|
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
2019-07-13 09:48:20 +02:00
|
|
|
return self.items.count
|
|
|
|
|
}
|
2019-07-16 11:51:50 +02:00
|
|
|
|
2019-09-10 19:22:32 +02:00
|
|
|
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
2019-07-16 11:51:50 +02:00
|
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath as IndexPath) as! MenuViewCell
|
2019-08-15 20:46:30 +02:00
|
|
|
|
|
|
|
|
cell.content.text = self.items[indexPath.item]
|
|
|
|
|
cell.tintView.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
|
|
|
|
|
let layer = cell.tintView.layer
|
2019-09-13 13:07:20 +02:00
|
|
|
df.setCardFormatting(for: layer)
|
2019-07-13 09:48:20 +02:00
|
|
|
return cell
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 11:51:50 +02:00
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeModeForItemAt indexPath: IndexPath) -> MagazineLayoutItemSizeMode {
|
|
|
|
|
let widthMode = MagazineLayoutItemWidthMode.fullWidth(respectsHorizontalInsets: true)
|
|
|
|
|
let heightMode = MagazineLayoutItemHeightMode.dynamic
|
|
|
|
|
return MagazineLayoutItemSizeMode(widthMode: widthMode, heightMode: heightMode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, visibilityModeForHeaderInSectionAtIndex index: Int) -> MagazineLayoutHeaderVisibilityMode {
|
|
|
|
|
return .hidden
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, visibilityModeForFooterInSectionAtIndex index: Int) -> MagazineLayoutFooterVisibilityMode {
|
|
|
|
|
return .hidden
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, visibilityModeForBackgroundInSectionAtIndex index: Int) -> MagazineLayoutBackgroundVisibilityMode {
|
|
|
|
|
return .hidden
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, horizontalSpacingForItemsInSectionAtIndex index: Int) -> CGFloat {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, verticalSpacingForElementsInSectionAtIndex index: Int) -> CGFloat {
|
2019-07-17 21:54:38 +02:00
|
|
|
return 0
|
2019-07-16 11:51:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetsForSectionAtIndex index: Int) -> UIEdgeInsets {
|
|
|
|
|
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetsForItemsInSectionAtIndex index: Int) -> UIEdgeInsets {
|
|
|
|
|
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
|
2019-07-13 09:48:20 +02:00
|
|
|
}
|
2019-07-10 19:52:14 +02:00
|
|
|
}
|
2019-07-13 09:48:20 +02:00
|
|
|
|