NCMenu.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // NCMainMenuTableViewController.swift
  3. // Nextcloud
  4. //
  5. // Created by Philippe Weidmann on 16.01.20.
  6. // Copyright © 2020 Philippe Weidmann. All rights reserved.
  7. // Copyright © 2020 Marino Faggiana All rights reserved.
  8. //
  9. // Author Philippe Weidmann <philippe.weidmann@infomaniak.com>
  10. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU General Public License as published by
  14. // the Free Software Foundation, either version 3 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. import UIKit
  26. import FloatingPanel
  27. class NCMenu: UITableViewController {
  28. var actions = [NCMenuAction]()
  29. static func makeNCMenu(with actions: [NCMenuAction]) -> NCMenu {
  30. let menuViewController = UIStoryboard(name: "NCMenu", bundle: nil).instantiateInitialViewController() as! NCMenu
  31. menuViewController.actions = actions
  32. return menuViewController
  33. }
  34. // MARK: - View Life Cycle
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. }
  38. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  39. let menuAction = actions[indexPath.row]
  40. if let action = menuAction.action {
  41. self.dismiss(animated: true, completion: nil)
  42. action(menuAction)
  43. }
  44. }
  45. // MARK: - Table view data source
  46. override func numberOfSections(in tableView: UITableView) -> Int {
  47. return 1
  48. }
  49. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  50. return actions.count
  51. }
  52. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  53. let cell = tableView.dequeueReusableCell(withIdentifier: "menuActionCell", for: indexPath)
  54. cell.tintColor = NCBrandColor.shared.customer
  55. let action = actions[indexPath.row]
  56. let actionIconView = cell.viewWithTag(1) as! UIImageView
  57. let actionNameLabel = cell.viewWithTag(2) as! UILabel
  58. if action.action == nil {
  59. cell.selectionStyle = .none
  60. }
  61. if (action.isOn) {
  62. actionIconView.image = action.onIcon
  63. actionNameLabel.text = action.onTitle
  64. } else {
  65. actionIconView.image = action.icon
  66. actionNameLabel.text = action.title
  67. }
  68. cell.accessoryType = action.selectable && action.selected ? .checkmark : .none
  69. return cell
  70. }
  71. // MARK: - Accessibility
  72. open override func accessibilityPerformEscape() -> Bool {
  73. dismiss(animated: true)
  74. return true
  75. }
  76. }
  77. extension NCMenu: FloatingPanelControllerDelegate {
  78. func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout? {
  79. let safeAreaInsetsBottom = Int(UIApplication.shared.keyWindow?.rootViewController?.view.safeAreaInsets.bottom ?? 0)
  80. return NCMenuFloatingPanelLayout(height: self.actions.count * 60 + safeAreaInsetsBottom)
  81. }
  82. func floatingPanel(_ vc: FloatingPanelController, behaviorFor newCollection: UITraitCollection) -> FloatingPanelBehavior? {
  83. return NCMenuFloatingPanelBehavior()
  84. }
  85. func floatingPanelDidEndDecelerating(_ vc: FloatingPanelController) {
  86. if vc.position == .hidden {
  87. vc.dismiss(animated: false, completion: nil)
  88. }
  89. }
  90. }
  91. class NCMenuFloatingPanelLayout: FloatingPanelLayout {
  92. let height: CGFloat
  93. init(height: Int) {
  94. self.height = CGFloat(height)
  95. }
  96. var initialPosition: FloatingPanelPosition {
  97. return .full
  98. }
  99. var supportedPositions: Set<FloatingPanelPosition> {
  100. return [.full, .hidden]
  101. }
  102. func insetFor(position: FloatingPanelPosition) -> CGFloat? {
  103. if (position == .full) {
  104. return max(48, UIScreen.main.bounds.size.height - height)
  105. } else {
  106. return nil
  107. }
  108. }
  109. var positionReference: FloatingPanelLayoutReference {
  110. return .fromSuperview
  111. }
  112. public func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] {
  113. return [
  114. surfaceView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0),
  115. surfaceView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0),
  116. ]
  117. }
  118. func backdropAlphaFor(position: FloatingPanelPosition) -> CGFloat {
  119. return 0.2
  120. }
  121. }
  122. public class NCMenuFloatingPanelBehavior: FloatingPanelBehavior {
  123. public func addAnimator(_ fpc: FloatingPanelController, to: FloatingPanelPosition) -> UIViewPropertyAnimator {
  124. return UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut)
  125. }
  126. public func removeAnimator(_ fpc: FloatingPanelController, from: FloatingPanelPosition) -> UIViewPropertyAnimator {
  127. return UIViewPropertyAnimator(duration: 0.1, curve: .easeInOut)
  128. }
  129. public func moveAnimator(_ fpc: FloatingPanelController, from: FloatingPanelPosition, to: FloatingPanelPosition) -> UIViewPropertyAnimator {
  130. return UIViewPropertyAnimator(duration: 0.1, curve: .easeInOut)
  131. }
  132. }
  133. class NCMenuPanelController: FloatingPanelController {
  134. var parentPresenter: UIViewController?
  135. // MARK: - View Life Cycle
  136. override func viewDidLoad() {
  137. super.viewDidLoad()
  138. self.surfaceView.backgroundColor = NCBrandColor.shared.systemBackground
  139. self.isRemovalInteractionEnabled = true
  140. self.surfaceView.cornerRadius = 16
  141. }
  142. }
  143. class NCMenuAction {
  144. let title: String
  145. let icon: UIImage
  146. let selectable: Bool
  147. var onTitle: String?
  148. var onIcon: UIImage?
  149. var selected: Bool = false
  150. var isOn: Bool = false
  151. var action: ((_ menuAction: NCMenuAction) -> Void)?
  152. init(title: String, icon: UIImage, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  153. self.title = title
  154. self.icon = icon
  155. self.action = action
  156. self.selectable = false
  157. }
  158. init(title: String, icon: UIImage, onTitle: String? = nil, onIcon: UIImage? = nil, selected: Bool, on: Bool, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  159. self.title = title
  160. self.icon = icon
  161. self.onTitle = onTitle ?? title
  162. self.onIcon = onIcon ?? icon
  163. self.action = action
  164. self.selected = selected
  165. self.isOn = on
  166. self.selectable = true
  167. }
  168. }