NCMenu.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. // Copyright © 2021 Henrik Storch All rights reserved.
  9. //
  10. // Author Philippe Weidmann <philippe.weidmann@infomaniak.com>
  11. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  12. // Author Henrik Storch <henrik.storch@nextcloud.com>
  13. //
  14. // This program is free software: you can redistribute it and/or modify
  15. // it under the terms of the GNU General Public License as published by
  16. // the Free Software Foundation, either version 3 of the License, or
  17. // (at your option) any later version.
  18. //
  19. // This program is distributed in the hope that it will be useful,
  20. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. // GNU General Public License for more details.
  23. //
  24. // You should have received a copy of the GNU General Public License
  25. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. //
  27. import UIKit
  28. import FloatingPanel
  29. class NCMenu: UITableViewController {
  30. var actions = [NCMenuAction]()
  31. static func makeNCMenu(with actions: [NCMenuAction]) -> NCMenu {
  32. let menuViewController = UIStoryboard(name: "NCMenu", bundle: nil).instantiateInitialViewController() as! NCMenu
  33. menuViewController.actions = actions
  34. return menuViewController
  35. }
  36. // MARK: - View Life Cycle
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. }
  40. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  41. let menuAction = actions[indexPath.row]
  42. if let action = menuAction.action {
  43. self.dismiss(animated: true, completion: nil)
  44. action(menuAction)
  45. }
  46. }
  47. // MARK: - Table view data source
  48. override func numberOfSections(in tableView: UITableView) -> Int {
  49. return 1
  50. }
  51. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  52. return actions.count
  53. }
  54. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  55. let cell = tableView.dequeueReusableCell(withIdentifier: "menuActionCell", for: indexPath)
  56. cell.tintColor = NCBrandColor.shared.customer
  57. let action = actions[indexPath.row]
  58. let actionIconView = cell.viewWithTag(1) as! UIImageView
  59. let actionNameLabel = cell.viewWithTag(2) as! UILabel
  60. if action.action == nil {
  61. cell.selectionStyle = .none
  62. }
  63. if (action.isOn) {
  64. actionIconView.image = action.onIcon
  65. actionNameLabel.text = action.onTitle
  66. } else {
  67. actionIconView.image = action.icon
  68. actionNameLabel.text = action.title
  69. }
  70. cell.accessoryType = action.selectable && action.selected ? .checkmark : .none
  71. return cell
  72. }
  73. // MARK: - Tabel View Layout
  74. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  75. return 60
  76. }
  77. }
  78. extension NCMenu: FloatingPanelControllerDelegate {
  79. func floatingPanel(_ fpc: FloatingPanelController, layoutFor size: CGSize) -> FloatingPanelLayout {
  80. return NCMenuFloatingPanelLayout(numberOfActions: self.actions.count)
  81. }
  82. func floatingPanel(_ fpc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout {
  83. return NCMenuFloatingPanelLayout(numberOfActions: self.actions.count)
  84. }
  85. func floatingPanel(_ fpc: FloatingPanelController, animatorForDismissingWith velocity: CGVector) -> UIViewPropertyAnimator {
  86. return UIViewPropertyAnimator(duration: 0.1, curve: .easeInOut)
  87. }
  88. func floatingPanel(_ fpc: FloatingPanelController, animatorForPresentingTo state: FloatingPanelState) -> UIViewPropertyAnimator {
  89. return UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut)
  90. }
  91. func floatingPanelWillEndDragging(_ fpc: FloatingPanelController, withVelocity velocity: CGPoint, targetState: UnsafeMutablePointer<FloatingPanelState>) {
  92. guard velocity.y > 750 else { return }
  93. fpc.dismiss(animated: true, completion: nil)
  94. }
  95. }
  96. class NCMenuFloatingPanelLayout: FloatingPanelLayout {
  97. var position: FloatingPanelPosition = .bottom
  98. var initialState: FloatingPanelState = .full
  99. var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] {
  100. [
  101. .full: FloatingPanelLayoutAnchor(absoluteInset: topInset, edge: .top, referenceGuide: .superview)
  102. ]
  103. }
  104. let topInset: CGFloat
  105. init(numberOfActions: Int) {
  106. // sometimes UIScreen.main.bounds.size.height is not updated correctly
  107. // this ensures we use the correct height value
  108. // can't use `layoutFor size` since menu is dieplayed on top of the whole screen not just the VC
  109. let screenHeight = UIApplication.shared.isLandscape
  110. ? min(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
  111. : max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
  112. let bottomInset = UIApplication.shared.keyWindow?.rootViewController?.view.safeAreaInsets.bottom ?? 0
  113. let panelHeight = CGFloat(numberOfActions * 60) + bottomInset
  114. topInset = max(48, screenHeight - panelHeight)
  115. }
  116. func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] {
  117. return [
  118. surfaceView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0),
  119. surfaceView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0),
  120. ]
  121. }
  122. func backdropAlpha(for state: FloatingPanelState) -> CGFloat {
  123. return 0.2
  124. }
  125. }
  126. class NCMenuPanelController: FloatingPanelController {
  127. var parentPresenter: UIViewController?
  128. // MARK: - View Life Cycle
  129. override func viewDidLoad() {
  130. super.viewDidLoad()
  131. self.surfaceView.backgroundColor = NCBrandColor.shared.systemBackground
  132. self.isRemovalInteractionEnabled = true
  133. self.backdropView.dismissalTapGestureRecognizer.isEnabled = true
  134. self.surfaceView.layer.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. }