VLCActionSheet.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. @objc 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. NotificationCenter.default.addObserver(self, selector: #selector(updateTheme), name: .VLCThemeDidChangeNotification, object: nil)
  146. view.addSubview(backgroundView)
  147. view.addSubview(mainStackView)
  148. mainStackView.addArrangedSubview(headerView)
  149. mainStackView.addArrangedSubview(collectionView)
  150. mainStackView.addArrangedSubview(bottomBackgroundView)
  151. backgroundView.frame = UIScreen.main.bounds
  152. setupMainStackViewConstraints()
  153. setupCollectionViewConstraints()
  154. setuplHeaderViewConstraints()
  155. setupBottomBackgroundView()
  156. }
  157. override func viewWillAppear(_ animated: Bool) {
  158. super.viewWillAppear(animated)
  159. mainStackView.isHidden = true
  160. collectionView.reloadData()
  161. updateViewConstraints()
  162. }
  163. override func viewDidAppear(_ animated: Bool) {
  164. super.viewDidAppear(animated)
  165. // This is to avoid a horrible visual glitch!
  166. mainStackView.isHidden = false
  167. UIView.transition(with: backgroundView, duration: 0.2, options: .transitionCrossDissolve, animations: { [weak self] in
  168. self?.backgroundView.isHidden = false
  169. }, completion: nil)
  170. let realMainStackView = mainStackView.frame
  171. mainStackView.frame.origin.y += mainStackView.frame.origin.y
  172. UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: {
  173. [mainStackView] in
  174. mainStackView.frame = realMainStackView
  175. }, completion: nil)
  176. }
  177. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  178. super.viewWillTransition(to: size, with: coordinator)
  179. coordinator.animate(alongsideTransition: { [weak self] _ in
  180. self?.maxCollectionViewHeightConstraint.constant = size.height / 2
  181. self?.collectionView.layoutIfNeeded()
  182. })
  183. }
  184. override func viewWillLayoutSubviews() {
  185. super.viewWillLayoutSubviews()
  186. collectionView.collectionViewLayout.invalidateLayout()
  187. }
  188. @objc func setAction(closure action: @escaping (_ item: Any) -> Void) {
  189. self.action = action
  190. }
  191. @objc fileprivate func updateTheme() {
  192. collectionView.backgroundColor = PresentationTheme.current.colors.background
  193. headerView.backgroundColor = PresentationTheme.current.colors.background
  194. headerView.title.textColor = PresentationTheme.current.colors.cellTextColor
  195. bottomBackgroundView.backgroundColor = PresentationTheme.current.colors.background
  196. for cell in collectionView.visibleCells {
  197. if let cell = cell as? VLCActionSheetCell {
  198. cell.backgroundColor = PresentationTheme.current.colors.background
  199. cell.name.textColor = PresentationTheme.current.colors.cellTextColor
  200. }
  201. }
  202. collectionView.layoutIfNeeded()
  203. }
  204. }
  205. // MARK: UICollectionViewDelegateFlowLayout
  206. extension VLCActionSheet: UICollectionViewDelegateFlowLayout {
  207. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  208. return CGSize(width: collectionView.frame.width, height: cellHeight)
  209. }
  210. }
  211. // MARK: UICollectionViewDelegate
  212. extension VLCActionSheet: UICollectionViewDelegate {
  213. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  214. if let delegate = delegate, let item = delegate.itemAtIndexPath(indexPath) {
  215. delegate.actionSheet(collectionView: collectionView, didSelectItem: item, At: indexPath)
  216. action?(item)
  217. }
  218. removeActionSheet()
  219. }
  220. }
  221. // MARK: UICollectionViewDataSource
  222. extension VLCActionSheet: UICollectionViewDataSource {
  223. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  224. if let dataSource = dataSource {
  225. return dataSource.numberOfRows()
  226. }
  227. preconditionFailure("VLCActionSheet: No data source")
  228. }
  229. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  230. if let dataSource = dataSource {
  231. return dataSource.actionSheet(collectionView: collectionView, cellForItemAt: indexPath)
  232. }
  233. preconditionFailure("VLCActionSheet: No data source")
  234. }
  235. }