NCMenu.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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, size: size)
  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, size: CGSize = UIScreen.main.bounds.size) {
  106. let bottomInset = UIApplication.shared.keyWindow?.rootViewController?.view.safeAreaInsets.bottom ?? 0
  107. let screenHeight = size.height
  108. let panelHeight = CGFloat(numberOfActions * 60) + bottomInset
  109. topInset = max(48, screenHeight - panelHeight)
  110. }
  111. func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] {
  112. return [
  113. surfaceView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0),
  114. surfaceView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0),
  115. ]
  116. }
  117. func backdropAlpha(for state: FloatingPanelState) -> CGFloat {
  118. return 0.2
  119. }
  120. }
  121. class NCMenuPanelController: FloatingPanelController {
  122. var parentPresenter: UIViewController?
  123. // MARK: - View Life Cycle
  124. override func viewDidLoad() {
  125. super.viewDidLoad()
  126. self.surfaceView.backgroundColor = NCBrandColor.shared.systemBackground
  127. self.isRemovalInteractionEnabled = true
  128. self.backdropView.dismissalTapGestureRecognizer.isEnabled = true
  129. self.surfaceView.layer.cornerRadius = 16
  130. }
  131. }
  132. class NCMenuAction {
  133. let title: String
  134. let icon: UIImage
  135. let selectable: Bool
  136. var onTitle: String?
  137. var onIcon: UIImage?
  138. var selected: Bool = false
  139. var isOn: Bool = false
  140. var action: ((_ menuAction: NCMenuAction) -> Void)?
  141. init(title: String, icon: UIImage, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  142. self.title = title
  143. self.icon = icon
  144. self.action = action
  145. self.selectable = false
  146. }
  147. init(title: String, icon: UIImage, onTitle: String? = nil, onIcon: UIImage? = nil, selected: Bool, on: Bool, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  148. self.title = title
  149. self.icon = icon
  150. self.onTitle = onTitle ?? title
  151. self.onIcon = onIcon ?? icon
  152. self.action = action
  153. self.selected = selected
  154. self.isOn = on
  155. self.selectable = true
  156. }
  157. }