|
| 1 | +// |
| 2 | +// GymDetailViewController.swift |
| 3 | +// Uplift |
| 4 | +// |
| 5 | +// Created by Yana Sang on 5/22/19. |
| 6 | +// Copyright © 2019 Cornell AppDev. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Crashlytics |
| 10 | +import UIKit |
| 11 | + |
| 12 | +class GymDetailViewController: UIViewController { |
| 13 | + |
| 14 | + // MARK: - Public view vars |
| 15 | + let collectionView = UICollectionView(frame: .zero, collectionViewLayout: StretchyHeaderLayout()) |
| 16 | + |
| 17 | + // MARK: - Private data vars |
| 18 | + private var sections: [ItemType]! |
| 19 | + |
| 20 | + // MARK: - Public data vars |
| 21 | + var gymDetail: Gym! |
| 22 | + |
| 23 | + // MARK: - Private classes/enums |
| 24 | + |
| 25 | + private enum ItemType { |
| 26 | + case hours |
| 27 | + case tabbedController |
| 28 | + } |
| 29 | + |
| 30 | + // MARK: - Custom Initializer |
| 31 | + init(gym: Gym) { |
| 32 | + super.init(nibName: nil, bundle: nil) |
| 33 | + gymDetail = gym |
| 34 | + sections = [.tabbedController] |
| 35 | + } |
| 36 | + |
| 37 | + required init?(coder aDecoder: NSCoder) { |
| 38 | + fatalError("init(coder:) has not been implemented") |
| 39 | + } |
| 40 | + |
| 41 | + override func viewDidLoad() { |
| 42 | + super.viewDidLoad() |
| 43 | + updatesStatusBarAppearanceAutomatically = true |
| 44 | + view.backgroundColor = .white |
| 45 | + |
| 46 | + // MARK: - Fabric |
| 47 | +// Answers.logCustomEvent(withName: "Checking Gym Details") |
| 48 | + |
| 49 | + let edgeSwipe = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(back)) |
| 50 | + edgeSwipe.edges = .left |
| 51 | + view.addGestureRecognizer(edgeSwipe) |
| 52 | + |
| 53 | + setupViews() |
| 54 | + setupConstraints() |
| 55 | + } |
| 56 | + |
| 57 | + override func viewWillAppear(_ animated: Bool) { |
| 58 | + navigationController?.isNavigationBarHidden = true |
| 59 | + } |
| 60 | + |
| 61 | + // MARK: - Targets |
| 62 | + @objc func back() { |
| 63 | + navigationController?.popViewController(animated: true) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// MARK: - CollectionViewDataSource, CollectionViewDelegate, CollectionViewDelegateFlowLayout |
| 68 | +extension GymDetailViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { |
| 69 | + |
| 70 | + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { |
| 71 | + return self.sections.count |
| 72 | + } |
| 73 | + |
| 74 | + func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { |
| 75 | + let itemType = sections[indexPath.item] |
| 76 | + |
| 77 | + switch itemType { |
| 78 | + case .hours: |
| 79 | + // swiftlint:disable:next force_cast |
| 80 | + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GymDetailHoursCell.reuseId, for: indexPath) as! GymDetailHoursCell |
| 81 | + cell.configure(for: self, hours: gymDetail.hours, isDisclosed: gymDetail.hoursIsDisclosed) |
| 82 | + return cell |
| 83 | + |
| 84 | + case .tabbedController: |
| 85 | + // swiftlint:disable:next force_cast |
| 86 | + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GymDetailTabbedControllerCell.reuseId, for: indexPath) as! GymDetailTabbedControllerCell |
| 87 | + cell.configure(fitnessCenters: gymDetail.fitnessCenters) |
| 88 | + cell.delegate = self |
| 89 | + return cell |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { |
| 94 | + let height: CGFloat |
| 95 | + |
| 96 | + switch sections[indexPath.item] { |
| 97 | + case .hours: |
| 98 | + height = GymDetailHoursCell.getHeight(isDisclosed: gymDetail.hoursIsDisclosed, numLines: gymDetail.hours.getNumHoursLines()) |
| 99 | + case .tabbedController: |
| 100 | + height = GymDetailTabbedControllerCell.getHeight(fitnessCenters: gymDetail.fitnessCenters[gymDetail.currentDisplayedFitnessCenter], viewWidt: self.view.frame.width) |
| 101 | + } |
| 102 | + |
| 103 | + return CGSize(width: collectionView.frame.width, height: height) |
| 104 | + } |
| 105 | + |
| 106 | + func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { |
| 107 | + let headerView = collectionView.dequeueReusableSupplementaryView( |
| 108 | + ofKind: UICollectionView.elementKindSectionHeader, |
| 109 | + withReuseIdentifier: GymDetailHeaderView.reuseId, |
| 110 | + // swiftlint:disable:next force_cast |
| 111 | + for: indexPath) as! GymDetailHeaderView |
| 112 | + headerView.configure(for: self, for: gymDetail) |
| 113 | + return headerView |
| 114 | + } |
| 115 | + |
| 116 | + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { |
| 117 | + return CGSize(width: collectionView.frame.width, height: 300) |
| 118 | + } |
| 119 | + |
| 120 | + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { |
| 121 | + return 0 |
| 122 | + } |
| 123 | + |
| 124 | +} |
| 125 | + |
| 126 | +// MARK: - Layout |
| 127 | +extension GymDetailViewController { |
| 128 | + |
| 129 | + private func setupViews() { |
| 130 | + collectionView.backgroundColor = .white |
| 131 | + collectionView.showsVerticalScrollIndicator = false |
| 132 | + |
| 133 | + collectionView.register(GymDetailHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: GymDetailHeaderView.reuseId) |
| 134 | + |
| 135 | + collectionView.register(GymDetailHoursCell.self, forCellWithReuseIdentifier: GymDetailHoursCell.reuseId) |
| 136 | + collectionView.register(GymDetailTabbedControllerCell.self, forCellWithReuseIdentifier: GymDetailTabbedControllerCell.reuseId) |
| 137 | + |
| 138 | + collectionView.delegate = self |
| 139 | + collectionView.dataSource = self |
| 140 | + view.addSubview(collectionView) |
| 141 | + } |
| 142 | + |
| 143 | + private func setupConstraints() { |
| 144 | + collectionView.snp.makeConstraints { make in |
| 145 | + make.top.leading.trailing.equalToSuperview() |
| 146 | + make.bottom.equalTo(view.safeAreaLayoutGuide) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | +} |
| 151 | + |
| 152 | +// MARK: - Extensions |
| 153 | +extension GymDetailViewController: GymDetailHoursCellDelegate { |
| 154 | + |
| 155 | + func didDropHours(isDropped: Bool, completion: @escaping () -> Void) { |
| 156 | + gymDetail.hoursIsDisclosed.toggle() |
| 157 | + collectionView.performBatchUpdates({}, completion: nil) |
| 158 | + completion() |
| 159 | + } |
| 160 | + |
| 161 | +} |
| 162 | + |
| 163 | +extension GymDetailViewController: GymDetailTabbedControllerCellDelegate { |
| 164 | + |
| 165 | + func didMoveTo(index: Int, completion: @escaping () -> Void) { |
| 166 | + gymDetail.currentDisplayedFitnessCenter = index |
| 167 | + collectionView.performBatchUpdates({}, completion: nil) |
| 168 | + completion() |
| 169 | + } |
| 170 | + |
| 171 | + func didChangeSize(completion: @escaping () -> Void) { |
| 172 | + collectionView.performBatchUpdates({}, completion: nil) |
| 173 | + completion() |
| 174 | + } |
| 175 | + |
| 176 | +} |
| 177 | + |
| 178 | +extension GymDetailViewController: GymDetailHeaderViewDelegate { |
| 179 | + |
| 180 | + func gymDetailHeaderViewBackButtonTapped() { |
| 181 | + back() |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments