123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- /*****************************************************************************
- * VLCActionSheet.swift
- *
- * Copyright © 2018 VLC authors and VideoLAN
- * Copyright © 2018 Videolabs
- *
- * Authors: Soomin Lee <bubu@mikan.io>
- *
- * Refer to the COPYING file of the official project for license.
- *****************************************************************************/
- import Foundation
- import UIKit
- @objc protocol VLCActionSheetDataSource {
- @objc func numberOfRows() -> Int
- @objc func actionSheet(collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
- }
- @objc protocol VLCActionSheetDelegate {
- @objc optional func headerViewTitle() -> String?
- @objc func itemAtIndexPath(_ indexPath: IndexPath) -> Any?
- @objc func actionSheet(collectionView: UICollectionView, didSelectItem item: Any, At indexPath: IndexPath)
- }
- // MARK: VLCActionSheet
- class VLCActionSheet: UIViewController {
- private let cellHeight: CGFloat = 50
- @objc weak var dataSource: VLCActionSheetDataSource?
- @objc weak var delegate: VLCActionSheetDelegate?
- var action: ((_ item: Any) -> Void)?
- lazy var backgroundView: UIView = {
- let backgroundView = UIView()
- backgroundView.isHidden = true
- backgroundView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
- backgroundView.backgroundColor = UIColor.black.withAlphaComponent(0.6)
- backgroundView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.removeActionSheet)))
- return backgroundView
- }()
- lazy var collectionViewLayout: UICollectionViewFlowLayout = {
- let collectionViewLayout = UICollectionViewFlowLayout()
- collectionViewLayout.minimumLineSpacing = 1
- collectionViewLayout.minimumInteritemSpacing = 0
- return collectionViewLayout
- }()
- lazy var collectionView: UICollectionView = {
- let collectionView = UICollectionView(frame: UIScreen.main.bounds, collectionViewLayout: collectionViewLayout)
- collectionView.delegate = self
- collectionView.dataSource = self
- collectionView.backgroundColor = PresentationTheme.current.colors.background
- collectionView.alwaysBounceVertical = true
- collectionView.showsVerticalScrollIndicator = false
- collectionView.register(VLCActionSheetCell.self, forCellWithReuseIdentifier: VLCActionSheetCell.identifier)
- collectionView.translatesAutoresizingMaskIntoConstraints = false
- return collectionView
- }()
- lazy var headerView: VLCActionSheetSectionHeader = {
- let headerView = VLCActionSheetSectionHeader()
- headerView.title.text = delegate?.headerViewTitle?() ?? "Default header title"
- headerView.title.textColor = PresentationTheme.current.colors.cellTextColor
- headerView.backgroundColor = PresentationTheme.current.colors.background
- headerView.translatesAutoresizingMaskIntoConstraints = false
- return headerView
- }()
- lazy var bottomBackgroundView: UIView = {
- let bottomBackgroundView = UIView()
- bottomBackgroundView.backgroundColor = PresentationTheme.current.colors.background
- bottomBackgroundView.translatesAutoresizingMaskIntoConstraints = false
- return bottomBackgroundView
- }()
- lazy var mainStackView: UIStackView = {
- let mainStackView = UIStackView()
- mainStackView.spacing = 0
- mainStackView.axis = .vertical
- mainStackView.alignment = .center
- mainStackView.translatesAutoresizingMaskIntoConstraints = false
- return mainStackView
- }()
- fileprivate lazy var maxCollectionViewHeightConstraint: NSLayoutConstraint = {
- let maxCollectionViewHeightConstraint = collectionView.heightAnchor.constraint(
- lessThanOrEqualToConstant: (view.bounds.height / 2) - cellHeight)
- return maxCollectionViewHeightConstraint
- }()
- fileprivate lazy var collectionViewHeightConstraint: NSLayoutConstraint = {
- guard let dataSource = dataSource else {
- preconditionFailure("VLCActionSheet: Data source not set correctly!")
- }
- let collectionViewHeightConstraint = collectionView.heightAnchor.constraint(
- equalToConstant: CGFloat(dataSource.numberOfRows()) * cellHeight)
- collectionViewHeightConstraint.priority = .required - 1
- return collectionViewHeightConstraint
- }()
- fileprivate lazy var bottomBackgroundViewHeightConstraint: NSLayoutConstraint = {
- let bottomBackgroundViewHeightConstraint = bottomBackgroundView.heightAnchor.constraint(equalToConstant: 0)
- return bottomBackgroundViewHeightConstraint
- }()
- @objc func removeActionSheet() {
- UIView.transition(with: backgroundView, duration: 0.01, options: .transitionCrossDissolve, animations: {
- [weak self] in
- self?.backgroundView.isHidden = true
- }, completion: { [weak self] finished in
- self?.presentingViewController?.dismiss(animated: true, completion: nil)
- })
- }
- // MARK: Private methods
- fileprivate func setuplHeaderViewConstraints() {
- NSLayoutConstraint.activate([
- headerView.heightAnchor.constraint(equalToConstant: cellHeight),
- headerView.widthAnchor.constraint(equalTo: view.widthAnchor),
- ])
- }
- fileprivate func setupCollectionViewConstraints() {
- NSLayoutConstraint.activate([
- collectionViewHeightConstraint,
- maxCollectionViewHeightConstraint,
- collectionView.widthAnchor.constraint(equalTo: view.widthAnchor),
- ])
- }
- fileprivate func setupBottomBackgroundView() {
- NSLayoutConstraint.activate([
- bottomBackgroundViewHeightConstraint,
- bottomBackgroundView.widthAnchor.constraint(equalTo: view.widthAnchor)
- ])
- }
- fileprivate func setupMainStackViewConstraints() {
- NSLayoutConstraint.activate([
- mainStackView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
- mainStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
- mainStackView.widthAnchor.constraint(equalTo: view.widthAnchor),
- mainStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
- ])
- }
- override func updateViewConstraints() {
- super.updateViewConstraints()
- if let presentingViewController = presentingViewController, let dataSource = dataSource {
- collectionViewHeightConstraint.constant = CGFloat(dataSource.numberOfRows()) * cellHeight
- maxCollectionViewHeightConstraint.constant = presentingViewController.view.frame.size.height / 2
- collectionView.setNeedsLayout()
- collectionView.layoutIfNeeded()
- }
- }
- @available(iOS 11.0, *)
- override func viewSafeAreaInsetsDidChange() {
- bottomBackgroundViewHeightConstraint.constant = view.safeAreaInsets.bottom
- }
- // MARK: UIViewController
- override func viewDidLoad() {
- super.viewDidLoad()
- view.addSubview(backgroundView)
- view.addSubview(mainStackView)
- mainStackView.addArrangedSubview(headerView)
- mainStackView.addArrangedSubview(collectionView)
- mainStackView.addArrangedSubview(bottomBackgroundView)
- backgroundView.frame = UIScreen.main.bounds
- setupMainStackViewConstraints()
- setupCollectionViewConstraints()
- setuplHeaderViewConstraints()
- setupBottomBackgroundView()
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- mainStackView.isHidden = true
- collectionView.reloadData()
- updateViewConstraints()
- }
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- // This is to avoid a horrible visual glitch!
- mainStackView.isHidden = false
- UIView.transition(with: backgroundView, duration: 0.2, options: .transitionCrossDissolve, animations: { [weak self] in
- self?.backgroundView.isHidden = false
- }, completion: nil)
- let realMainStackView = mainStackView.frame
- mainStackView.frame.origin.y += mainStackView.frame.origin.y
- UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: {
- [mainStackView] in
- mainStackView.frame = realMainStackView
- }, completion: nil)
- }
- override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
- super.viewWillTransition(to: size, with: coordinator)
- coordinator.animate(alongsideTransition: { [weak self] _ in
- self?.maxCollectionViewHeightConstraint.constant = size.height / 2
- self?.collectionView.layoutIfNeeded()
- })
- }
- override func viewWillLayoutSubviews() {
- super.viewWillLayoutSubviews()
- collectionView.collectionViewLayout.invalidateLayout()
- }
- @objc func setAction(closure action: @escaping (_ item: Any) -> Void) {
- self.action = action
- }
- }
- // MARK: UICollectionViewDelegateFlowLayout
- extension VLCActionSheet: UICollectionViewDelegateFlowLayout {
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
- return CGSize(width: collectionView.frame.width, height: cellHeight)
- }
- }
- // MARK: UICollectionViewDelegate
- extension VLCActionSheet: UICollectionViewDelegate {
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- if let delegate = delegate, let item = delegate.itemAtIndexPath(indexPath) {
- delegate.actionSheet(collectionView: collectionView, didSelectItem: item, At: indexPath)
- action?(item)
- }
- removeActionSheet()
- }
- }
- // MARK: UICollectionViewDataSource
- extension VLCActionSheet: UICollectionViewDataSource {
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- if let dataSource = dataSource {
- return dataSource.numberOfRows()
- }
- preconditionFailure("VLCActionSheet: No data source")
- }
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- if let dataSource = dataSource {
- return dataSource.actionSheet(collectionView: collectionView, cellForItemAt: indexPath)
- }
- preconditionFailure("VLCActionSheet: No data source")
- }
- }
|