Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cristainlika/46a41bf69a231f7218aa34ebf6dee857 to your computer and use it in GitHub Desktop.
Save cristainlika/46a41bf69a231f7218aa34ebf6dee857 to your computer and use it in GitHub Desktop.
import UIKit
class DisplayAsOptionViewController: UIViewController {
fileprivate let cartHeaderCell = "cartHeaderCell"
let optionCell = "optionCell"
let addonCell = "addonCell"
let addonGroupCell = "addonGroupCell"
var dish: Dish?
fileprivate let addToCartCell = "addToCartCell"
var totalSection = 7
//var optionListViewController : OptionListViewController?
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
setupNavigationBar(title: "Your Order")
print("\(self.navigationController?.navigationBar.frame.size.height)")
self.loadDataToTheView()
}
func doneGroupSelect(){
}
var data : [Any]?
var dishOptionList : [DishOption]?
var choiceList : [Choice]?
var addonList : [Addon]?
var addonGroupList : [AddonGroup]?
func loadDataToTheView() {
data = [Any]()
//print(dish)
dishOptionList = [DishOption]()
choiceList = [Choice]()
if let objs = dish?.optionList {
for obj in objs {
if obj.displayAsButton == "0"{
dishOptionList?.append(obj)
data?.append(obj)
} ///... optionOBJ.displayAsButton == "0"
} //... for optionOBJ in optionOBJs
}//end ... if let optionOBJs = dish?.optionList
addonList = [Addon]()
if let objs = dish?.addonList {
for obj in objs {
addonList?.append(obj)
data?.append(obj)
} //... for optionOBJ in optionOBJs
}//end ... if let optionOBJs = dish?.optionList
addonGroupList = [AddonGroup]()
if let objs = dish?.addonGroupList {
for obj in objs {
print(obj.name)
addonGroupList?.append(obj)
data?.append(obj)
} //... for optionOBJ in optionOBJs
}//end ... if let optionOBJs = dish?.optionList
// print(data)
// print(data?.count)
tableView.reloadData()
}
lazy var tableView : UITableView = {
var tv = UITableView()
tv.delegate = self
tv.dataSource = self
//tv.backgroundColor = UIColor.yellow
tv.separatorStyle = UITableViewCellSeparatorStyle.none
tv.allowsMultipleSelectionDuringEditing = false
tv.translatesAutoresizingMaskIntoConstraints = false
return tv
}()
var separatorLineView : UIView = {
var view = UIView()
view.backgroundColor = UIColor.gray
return view
}()
func setupViews() {
view.addSubview(tableView)
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : tableView]))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : tableView]))
registerCells()
}
func showNextViewForApp(indexPath : IndexPath ) {
let viewController = OptionListViewController()
viewController.obj = data?[indexPath.row]
viewController.delegate = self
//print(viewController.delegate)
self.navigationController?.pushViewController(viewController, animated: true)
}
fileprivate func registerCells(){
self.tableView.register(CartHeaderCell.self, forCellReuseIdentifier: cartHeaderCell)
self.tableView.register(OptionCell.self, forCellReuseIdentifier: optionCell)
self.tableView.register(AddonCell.self, forCellReuseIdentifier: addonCell)
self.tableView.register(AddonGroupCell.self, forCellReuseIdentifier: addonGroupCell)
self.tableView.register(AddToCartCell.self, forCellReuseIdentifier: addToCartCell)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension DisplayAsOptionViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
if (data?.count) != nil{
return 3
}
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 1
case 1:
if let count = data?.count{
return count
}
return 0
default:
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: cartHeaderCell, for: indexPath) as! CartHeaderCell
cell.configureCell(indexPath.item)
return cell
case 1:
let obj = data?[indexPath.row]
var cell = UITableViewCell()
switch obj {
case is Addon:
cell = tableView.dequeueReusableCell(withIdentifier: addonCell, for: indexPath) as! AddonCell
cell.textLabel?.text = "bad moring"
let switchView = UISwitch(frame: .zero)
switchView.setOn(false, animated: true)
cell.accessoryView = switchView
//cell.isUserInteractionEnabled = false
//please ignore the "display in popup" for mobile app it is showing switch .
guard let addon = obj as? Addon else {
return cell
}
cell.textLabel?.text = "\(addon.name) + €\(addon.price)"
// cell.detailTextLabel?.text = "+\(addon.price)"
case is AddonGroup:
cell = tableView.dequeueReusableCell(withIdentifier: addonGroupCell, for: indexPath) as! AddonGroupCell
cell.textLabel?.text = "good moring"
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
guard let addonGroup = obj as? AddonGroup else {
return cell
}
if let addons = addonGroup.addonList {
cell.detailTextLabel?.text = ""
var selectedAddons = ""
for _addon in addons
{
if _addon.isSelect == true {
selectedAddons = selectedAddons + "\(_addon.name)"
}
}
cell.detailTextLabel?.text = selectedAddons
}
cell.textLabel?.text = addonGroup.name
case is DishOption:
cell = tableView.dequeueReusableCell(withIdentifier: optionCell, for: indexPath) as! OptionCell
cell.textLabel?.text = "Lucky moring"
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
guard let dishOption = obj as? DishOption else {
return cell
}
if dishOption.isMandatory == "1"{
if let objs = dishOption.choiceList {
let obj = objs[0]
cell.detailTextLabel?.text = obj.name
// obj.isSelect = true
if let data = objs.first(where:{$0.isSelect == true}) {
cell.detailTextLabel?.text = data.name
}else{
obj.isSelect = true
}
}
}
else {
cell.detailTextLabel?.text = ""
}
if let choices = dishOption.choiceList {
for _choice in choices
{
if _choice.isSelect == true {
cell.detailTextLabel?.text = _choice.name
}
}
}
cell.textLabel?.text = dishOption.name
default:
print("i have an unexpected case ....")
}
let lineView = UIView(frame: CGRect(x: 17, y: cell.contentView.frame.size.height - 1.0, width: cell.contentView.frame.size.width , height: 1))
lineView.backgroundColor = UIColor(red: 56/255, green: 66/255, blue: 72/255, alpha: 0.149)
cell.contentView.addSubview(lineView)
return cell
case 2 :
let cell = tableView.dequeueReusableCell(withIdentifier: addToCartCell, for: indexPath) as! AddToCartCell
cell.selectionStyle = UITableViewCellSelectionStyle.none
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: cartHeaderCell, for: indexPath) as! CartHeaderCell
return cell
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: -5, width: tableView.frame.size.width , height: 20))
view.layer.borderWidth = 1
view.layer.borderColor = UIColor(red: 224/255, green: 224/255, blue: 224/255, alpha: 1).cgColor
view.backgroundColor = UIColor(red: 56/255, green: 66/255, blue: 72/255, alpha: 0.149)
return view
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.section {
case 0:
return 70
case 2 :
return 180
default:
return 44
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
switch section {
case 1:
return 20
case 2 :
return 20
default:
return 0
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "LOve em "
}
}
extension DisplayAsOptionViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 1 {
showNextViewForApp(indexPath: indexPath)
}
}
}
extension DisplayAsOptionViewController : SelectedInfo {
func selectedInfo(didSelectedInfo: Any) {
print(didSelectedInfo)
switch didSelectedInfo {
case is AddonGroup:
print("i have an AddonGroup case ....")
self.tableView.reloadData()
case is DishOption:
print("i have an AddonGroup case ....")
self.tableView.reloadData()
default:
print("i have an unexpected case ....")
}
}
}
// MARK:- CELL TABLEVIEW
fileprivate class CartHeaderCell : baseTableCell {
var titleLable : UILabel = {
var view = UILabel()
view.text = "Any Burrito and Soft Drink"
view.textColor = UIColor.black
view.textAlignment = .center
view.font = UIFont.systemFont(ofSize: 18)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
var descriptionLabel : UILabel = {
var view = UILabel()
view.text = "trit prevent you from losing history, non-fast-forward updates were rejected "
view.textColor = UIColor.gray
view.textAlignment = .center
view.font = UIFont.systemFont(ofSize: 13)
view.numberOfLines = 0
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func setupViews(){
//Mark:- For CartHeader
self.contentView.addSubview(titleLable)
self.contentView.addSubview(descriptionLabel)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[v0]-10-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : titleLable]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-5-[v0(22)]-2-[v1]-2-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : titleLable ,"v1" : descriptionLabel]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[v0]-10-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : descriptionLabel]))
}
func configureCell(_ index: Int) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment