NCMenu.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. extension Array where Element == NCMenuAction {
  30. var listHeight: CGFloat { reduce(0, { $0 + $1.rowHeight }) }
  31. }
  32. class NCMenu: UITableViewController {
  33. var actions = [NCMenuAction]()
  34. static func makeNCMenu(with actions: [NCMenuAction]) -> NCMenu? {
  35. let menuViewController = UIStoryboard(name: "NCMenu", bundle: nil).instantiateInitialViewController() as? NCMenu
  36. menuViewController?.actions = actions
  37. return menuViewController
  38. }
  39. // MARK: - View Life Cycle
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. }
  43. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  44. let menuAction = actions[indexPath.row]
  45. if let action = menuAction.action {
  46. self.dismiss(animated: true, completion: nil)
  47. action(menuAction)
  48. }
  49. }
  50. // MARK: - Table view data source
  51. override func numberOfSections(in tableView: UITableView) -> Int {
  52. return 1
  53. }
  54. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  55. return actions.count
  56. }
  57. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  58. let action = actions[indexPath.row]
  59. guard action.title != NCMenuAction.seperatorIdentifier else {
  60. let cell = UITableViewCell()
  61. cell.backgroundColor = NCBrandColor.shared.separator
  62. return cell
  63. }
  64. let cell = tableView.dequeueReusableCell(withIdentifier: "menuActionCell", for: indexPath)
  65. cell.tintColor = NCBrandColor.shared.customer
  66. let actionIconView = cell.viewWithTag(1) as? UIImageView
  67. let actionNameLabel = cell.viewWithTag(2) as? UILabel
  68. if action.action == nil {
  69. cell.selectionStyle = .none
  70. }
  71. if action.isOn {
  72. actionIconView?.image = action.onIcon
  73. actionNameLabel?.text = action.onTitle
  74. } else {
  75. actionIconView?.image = action.icon
  76. actionNameLabel?.text = action.title
  77. }
  78. cell.accessoryType = action.selectable && action.selected ? .checkmark : .none
  79. return cell
  80. }
  81. // MARK: - Tabel View Layout
  82. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  83. actions[indexPath.row].rowHeight
  84. }
  85. }
  86. extension NCMenu: FloatingPanelControllerDelegate {
  87. func floatingPanel(_ fpc: FloatingPanelController, layoutFor size: CGSize) -> FloatingPanelLayout {
  88. return NCMenuFloatingPanelLayout(actionsHeight: self.actions.listHeight)
  89. }
  90. func floatingPanel(_ fpc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout {
  91. return NCMenuFloatingPanelLayout(actionsHeight: self.actions.listHeight)
  92. }
  93. func floatingPanel(_ fpc: FloatingPanelController, animatorForDismissingWith velocity: CGVector) -> UIViewPropertyAnimator {
  94. return UIViewPropertyAnimator(duration: 0.1, curve: .easeInOut)
  95. }
  96. func floatingPanel(_ fpc: FloatingPanelController, animatorForPresentingTo state: FloatingPanelState) -> UIViewPropertyAnimator {
  97. return UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut)
  98. }
  99. func floatingPanelWillEndDragging(_ fpc: FloatingPanelController, withVelocity velocity: CGPoint, targetState: UnsafeMutablePointer<FloatingPanelState>) {
  100. guard velocity.y > 750 else { return }
  101. fpc.dismiss(animated: true, completion: nil)
  102. }
  103. }