VLCActionSheet.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*****************************************************************************
  2. * VLCActionSheet.swift
  3. *
  4. * Copyright © 2018 VLC authors and VideoLAN
  5. * Copyright © 2018 Videolabs
  6. *
  7. * Authors: Soomin Lee <bubu@mikan.io>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. import Foundation
  12. import UIKit
  13. @objc protocol VLCActionSheetDataSource {
  14. @objc func numberOfRows() -> Int
  15. @objc func actionSheet(collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
  16. }
  17. @objc protocol VLCActionSheetDelegate {
  18. @objc optional func headerViewTitle() -> String?
  19. @objc func itemAtIndexPath(_ indexPath: IndexPath) -> Any?
  20. @objc func actionSheet(collectionView: UICollectionView, didSelectItem item: Any, At indexPath: IndexPath)
  21. }
  22. // MARK: VLCActionSheet
  23. class VLCActionSheet: UIViewController {
  24. private let cellHeight: CGFloat = 50
  25. @objc weak var dataSource: VLCActionSheetDataSource?
  26. @objc weak var delegate: VLCActionSheetDelegate?
  27. var action: ((_ item: Any) -> Void)?
  28. lazy var backgroundView: UIView = {
  29. let backgroundView = UIView()
  30. backgroundView.isHidden = true
  31. backgroundView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
  32. backgroundView.backgroundColor = UIColor.black.withAlphaComponent(0.6)
  33. backgroundView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.removeActionSheet)))
  34. return backgroundView
  35. }()
  36. lazy var collectionViewLayout: UICollectionViewFlowLayout = {
  37. let collectionViewLayout = UICollectionViewFlowLayout()
  38. collectionViewLayout.minimumLineSpacing = 1
  39. collectionViewLayout.minimumInteritemSpacing = 0
  40. return collectionViewLayout
  41. }()
  42. lazy var collectionView: UICollectionView = {
  43. let collectionView = UICollectionView(frame: UIScreen.main.bounds, collectionViewLayout: collectionViewLayout)
  44. collectionView.delegate = self
  45. collectionView.dataSource = self
  46. collectionView.backgroundColor = PresentationTheme.current.colors.background
  47. collectionView.alwaysBounceVertical = true
  48. collectionView.showsVerticalScrollIndicator = false
  49. collectionView.register(VLCActionSheetCell.self, forCellWithReuseIdentifier: VLCActionSheetCell.identifier)
  50. collectionView.translatesAutoresizingMaskIntoConstraints = false
  51. return collectionView
  52. }()
  53. lazy var headerView: VLCActionSheetSectionHeader = {
  54. let headerView = VLCActionSheetSectionHeader()
  55. headerView.title.text = delegate?.headerViewTitle?() ?? "Default header title"
  56. headerView.title.textColor = PresentationTheme.current.colors.cellTextColor
  57. headerView.backgroundColor = PresentationTheme.current.colors.background
  58. headerView.translatesAutoresizingMaskIntoConstraints = false
  59. return headerView
  60. }()
  61. lazy var bottomBackgroundView: UIView = {
  62. let bottomBackgroundView = UIView()
  63. bottomBackgroundView.backgroundColor = PresentationTheme.current.colors.background
  64. bottomBackgroundView.translatesAutoresizingMaskIntoConstraints = false
  65. return bottomBackgroundView
  66. }()
  67. lazy var mainStackView: UIStackView = {
  68. let mainStackView = UIStackView()
  69. mainStackView.spacing = 0
  70. mainStackView.axis = .vertical
  71. mainStackView.alignment = .center
  72. mainStackView.translatesAutoresizingMaskIntoConstraints = false
  73. return mainStackView
  74. }()
  75. fileprivate lazy var maxCollectionViewHeightConstraint: NSLayoutConstraint = {
  76. let maxCollectionViewHeightConstraint = collectionView.heightAnchor.constraint(
  77. lessThanOrEqualToConstant: (view.bounds.height / 2) - cellHeight)
  78. return maxCollectionViewHeightConstraint
  79. }()
  80. fileprivate lazy var collectionViewHeightConstraint: NSLayoutConstraint = {
  81. guard let dataSource = dataSource else {
  82. preconditionFailure("VLCActionSheet: Data source not set correctly!")
  83. }
  84. let collectionViewHeightConstraint = collectionView.heightAnchor.constraint(
  85. equalToConstant: CGFloat(dataSource.numberOfRows()) * cellHeight)
  86. collectionViewHeightConstraint.priority = .required - 1
  87. return collectionViewHeightConstraint
  88. }()
  89. fileprivate lazy var bottomBackgroundViewHeightConstraint: NSLayoutConstraint = {
  90. let bottomBackgroundViewHeightConstraint = bottomBackgroundView.heightAnchor.constraint(equalToConstant: 0)
  91. return bottomBackgroundViewHeightConstraint
  92. }()
  93. @objc func removeActionSheet() {
  94. UIView.transition(with: backgroundView, duration: 0.01, options: .transitionCrossDissolve, animations: {
  95. [weak self] in
  96. self?.backgroundView.isHidden = true
  97. }, completion: { [weak self] finished in
  98. self?.presentingViewController?.dismiss(animated: true, completion: nil)
  99. })
  100. }
  101. // MARK: Private methods
  102. fileprivate func setuplHeaderViewConstraints() {
  103. NSLayoutConstraint.activate([
  104. headerView.heightAnchor.constraint(equalToConstant: cellHeight),
  105. headerView.widthAnchor.constraint(equalTo: view.widthAnchor),
  106. ])
  107. }
  108. fileprivate func setupCollectionViewConstraints() {
  109. NSLayoutConstraint.activate([
  110. collectionViewHeightConstraint,
  111. maxCollectionViewHeightConstraint,
  112. collectionView.widthAnchor.constraint(equalTo: view.widthAnchor),
  113. ])
  114. }
  115. fileprivate func setupBottomBackgroundView() {
  116. NSLayoutConstraint.activate([
  117. bottomBackgroundViewHeightConstraint,
  118. bottomBackgroundView.widthAnchor.constraint(equalTo: view.widthAnchor)
  119. ])
  120. }
  121. fileprivate func setupMainStackViewConstraints() {
  122. NSLayoutConstraint.activate([
  123. mainStackView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  124. mainStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  125. mainStackView.widthAnchor.constraint(equalTo: view.widthAnchor),
  126. mainStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
  127. ])
  128. }
  129. override func updateViewConstraints() {
  130. super.updateViewConstraints()
  131. if let presentingViewController = presentingViewController, let dataSource = dataSource {
  132. collectionViewHeightConstraint.constant = CGFloat(dataSource.numberOfRows()) * cellHeight
  133. maxCollectionViewHeightConstraint.constant = presentingViewController.view.frame.size.height / 2
  134. collectionView.setNeedsLayout()
  135. collectionView.layoutIfNeeded()
  136. }
  137. }
  138. @available(iOS 11.0, *)
  139. override func viewSafeAreaInsetsDidChange() {
  140. bottomBackgroundViewHeightConstraint.constant = view.safeAreaInsets.bottom
  141. }
  142. // MARK: UIViewController
  143. override func viewDidLoad() {
  144. super.viewDidLoad()
  145. view.addSubview(backgroundView)
  146. view.addSubview(mainStackView)
  147. mainStackView.addArrangedSubview(headerView)
  148. mainStackView.addArrangedSubview(collectionView)
  149. mainStackView.addArrangedSubview(bottomBackgroundView)
  150. backgroundView.frame = UIScreen.main.bounds
  151. setupMainStackViewConstraints()
  152. setupCollectionViewConstraints()
  153. setuplHeaderViewConstraints()
  154. setupBottomBackgroundView()
  155. }
  156. override func viewWillAppear(_ animated: Bool) {
  157. super.viewWillAppear(animated)
  158. mainStackView.isHidden = true
  159. collectionView.reloadData()
  160. updateViewConstraints()
  161. }
  162. override func viewDidAppear(_ animated: Bool) {
  163. super.viewDidAppear(animated)
  164. // This is to avoid a horrible visual glitch!
  165. mainStackView.isHidden = false
  166. UIView.transition(with: backgroundView, duration: 0.2, options: .transitionCrossDissolve, animations: { [weak self] in
  167. self?.backgroundView.isHidden = false
  168. }, completion: nil)
  169. let realMainStackView = mainStackView.frame
  170. mainStackView.frame.origin.y += mainStackView.frame.origin.y
  171. UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: {
  172. [mainStackView] in
  173. mainStackView.frame = realMainStackView
  174. }, completion: nil)
  175. }
  176. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  177. super.viewWillTransition(to: size, with: coordinator)
  178. coordinator.animate(alongsideTransition: { [weak self] _ in
  179. self?.maxCollectionViewHeightConstraint.constant = size.height / 2
  180. self?.collectionView.layoutIfNeeded()
  181. })
  182. }
  183. override func viewWillLayoutSubviews() {
  184. super.viewWillLayoutSubviews()
  185. collectionView.collectionViewLayout.invalidateLayout()
  186. }
  187. @objc func setAction(closure action: @escaping (_ item: Any) -> Void) {
  188. self.action = action
  189. }
  190. }
  191. // MARK: UICollectionViewDelegateFlowLayout
  192. extension VLCActionSheet: UICollectionViewDelegateFlowLayout {
  193. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  194. return CGSize(width: collectionView.frame.width, height: cellHeight)
  195. }
  196. }
  197. // MARK: UICollectionViewDelegate
  198. extension VLCActionSheet: UICollectionViewDelegate {
  199. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  200. if let delegate = delegate, let item = delegate.itemAtIndexPath(indexPath) {
  201. delegate.actionSheet(collectionView: collectionView, didSelectItem: item, At: indexPath)
  202. action?(item)
  203. }
  204. removeActionSheet()
  205. }
  206. }
  207. // MARK: UICollectionViewDataSource
  208. extension VLCActionSheet: UICollectionViewDataSource {
  209. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  210. if let dataSource = dataSource {
  211. return dataSource.numberOfRows()
  212. }
  213. preconditionFailure("VLCActionSheet: No data source")
  214. }
  215. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  216. if let dataSource = dataSource {
  217. return dataSource.actionSheet(collectionView: collectionView, cellForItemAt: indexPath)
  218. }
  219. preconditionFailure("VLCActionSheet: No data source")
  220. }
  221. }