NCMenu.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. }
  32. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  33. let menuAction = actions[indexPath.row]
  34. if let action = menuAction.action {
  35. self.dismiss(animated: true, completion: nil)
  36. action(menuAction)
  37. }
  38. }
  39. // MARK: - Table view data source
  40. override func numberOfSections(in tableView: UITableView) -> Int {
  41. return 1
  42. }
  43. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  44. return actions.count
  45. }
  46. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  47. let cell = tableView.dequeueReusableCell(withIdentifier: "menuActionCell", for: indexPath)
  48. cell.tintColor = NCBrandColor.shared.customer
  49. let action = actions[indexPath.row]
  50. let actionIconView = cell.viewWithTag(1) as! UIImageView
  51. let actionNameLabel = cell.viewWithTag(2) as! UILabel
  52. if action.action == nil {
  53. cell.selectionStyle = .none
  54. }
  55. if (action.isOn) {
  56. actionIconView.image = action.onIcon
  57. actionNameLabel.text = action.onTitle
  58. } else {
  59. actionIconView.image = action.icon
  60. actionNameLabel.text = action.title
  61. }
  62. cell.accessoryType = action.selectable && action.selected ? .checkmark : .none
  63. return cell
  64. }
  65. // MARK: - Accessibility
  66. open override func accessibilityPerformEscape() -> Bool {
  67. dismiss(animated: true)
  68. return true
  69. }
  70. }
  71. extension NCMenu: FloatingPanelControllerDelegate {
  72. func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout? {
  73. return NCMenuFloatingPanelLayout(height: self.actions.count * 60 + Int((UIApplication.shared.keyWindow?.rootViewController!.view.safeAreaInsets.bottom)!))
  74. }
  75. func floatingPanel(_ vc: FloatingPanelController, behaviorFor newCollection: UITraitCollection) -> FloatingPanelBehavior? {
  76. return NCMenuFloatingPanelBehavior()
  77. }
  78. func floatingPanelDidEndDecelerating(_ vc: FloatingPanelController) {
  79. if vc.position == .hidden {
  80. vc.dismiss(animated: false, completion: nil)
  81. }
  82. }
  83. }
  84. class NCMenuFloatingPanelLayout: FloatingPanelLayout {
  85. let height: CGFloat
  86. init(height: Int) {
  87. self.height = CGFloat(height)
  88. }
  89. var initialPosition: FloatingPanelPosition {
  90. return .full
  91. }
  92. var supportedPositions: Set<FloatingPanelPosition> {
  93. return [.full, .hidden]
  94. }
  95. func insetFor(position: FloatingPanelPosition) -> CGFloat? {
  96. if (position == .full) {
  97. return max(48, UIScreen.main.bounds.size.height - height)
  98. } else {
  99. return nil
  100. }
  101. }
  102. var positionReference: FloatingPanelLayoutReference {
  103. return .fromSuperview
  104. }
  105. public func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] {
  106. return [
  107. surfaceView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0),
  108. surfaceView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0),
  109. ]
  110. }
  111. func backdropAlphaFor(position: FloatingPanelPosition) -> CGFloat {
  112. return 0.2
  113. }
  114. }
  115. public class NCMenuFloatingPanelBehavior: FloatingPanelBehavior {
  116. public func addAnimator(_ fpc: FloatingPanelController, to: FloatingPanelPosition) -> UIViewPropertyAnimator {
  117. return UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut)
  118. }
  119. public func removeAnimator(_ fpc: FloatingPanelController, from: FloatingPanelPosition) -> UIViewPropertyAnimator {
  120. return UIViewPropertyAnimator(duration: 0.1, curve: .easeInOut)
  121. }
  122. public func moveAnimator(_ fpc: FloatingPanelController, from: FloatingPanelPosition, to: FloatingPanelPosition) -> UIViewPropertyAnimator {
  123. return UIViewPropertyAnimator(duration: 0.1, curve: .easeInOut)
  124. }
  125. }
  126. class NCMenuPanelController: FloatingPanelController {
  127. var parentPresenter: UIViewController?
  128. override func viewDidLoad() {
  129. super.viewDidLoad()
  130. if #available(iOS 13.0, *) {
  131. self.surfaceView.backgroundColor = .systemBackground
  132. }
  133. self.isRemovalInteractionEnabled = true
  134. self.surfaceView.cornerRadius = 16
  135. }
  136. }
  137. class NCMenuAction {
  138. let title: String
  139. let icon: UIImage
  140. let selectable: Bool
  141. var onTitle: String?
  142. var onIcon: UIImage?
  143. var selected: Bool = false
  144. var isOn: Bool = false
  145. var action: ((_ menuAction: NCMenuAction) -> Void)?
  146. init(title: String, icon: UIImage, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  147. self.title = title
  148. self.icon = icon
  149. self.action = action
  150. self.selectable = false
  151. }
  152. init(title: String, icon: UIImage, onTitle: String? = nil, onIcon: UIImage? = nil, selected: Bool, on: Bool, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  153. self.title = title
  154. self.icon = icon
  155. self.onTitle = onTitle ?? title
  156. self.onIcon = onIcon ?? icon
  157. self.action = action
  158. self.selected = selected
  159. self.isOn = on
  160. self.selectable = true
  161. }
  162. }