NCMenu.swift 6.4 KB

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