NCMenu.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. tableView.estimatedRowHeight = 60
  43. tableView.rowHeight = UITableView.automaticDimension
  44. }
  45. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  46. let menuAction = actions[indexPath.row]
  47. if let action = menuAction.action {
  48. self.dismiss(animated: true, completion: nil)
  49. action(menuAction)
  50. }
  51. }
  52. // MARK: - Table view data source
  53. override func numberOfSections(in tableView: UITableView) -> Int {
  54. return 1
  55. }
  56. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  57. return actions.count
  58. }
  59. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  60. let action = actions[indexPath.row]
  61. guard action.title != NCMenuAction.seperatorIdentifier else {
  62. let cell = UITableViewCell()
  63. cell.backgroundColor = .separator
  64. return cell
  65. }
  66. let cell = tableView.dequeueReusableCell(withIdentifier: "menuActionCell", for: indexPath)
  67. cell.tintColor = NCBrandColor.shared.customer
  68. let actionIconView = cell.viewWithTag(1) as? UIImageView
  69. let actionNameLabel = cell.viewWithTag(2) as? UILabel
  70. let actionDetailLabel = cell.viewWithTag(3) as? UILabel
  71. if action.action == nil {
  72. cell.selectionStyle = .none
  73. }
  74. if let details = action.details {
  75. actionDetailLabel?.text = details
  76. actionNameLabel?.isHidden = false
  77. } else { actionDetailLabel?.isHidden = true }
  78. if action.isOn {
  79. actionIconView?.image = action.onIcon
  80. actionNameLabel?.text = action.onTitle
  81. } else {
  82. actionIconView?.image = action.icon
  83. actionNameLabel?.text = action.title
  84. }
  85. cell.accessoryType = action.selectable && action.selected ? .checkmark : .none
  86. return cell
  87. }
  88. // MARK: - Tabel View Layout
  89. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  90. actions[indexPath.row].title == NCMenuAction.seperatorIdentifier ? NCMenuAction.seperatorHeight : UITableView.automaticDimension
  91. }
  92. }
  93. extension NCMenu: FloatingPanelControllerDelegate {
  94. func floatingPanel(_ fpc: FloatingPanelController, layoutFor size: CGSize) -> FloatingPanelLayout {
  95. return NCMenuFloatingPanelLayout(actionsHeight: self.actions.listHeight)
  96. }
  97. func floatingPanel(_ fpc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout {
  98. return NCMenuFloatingPanelLayout(actionsHeight: self.actions.listHeight)
  99. }
  100. func floatingPanel(_ fpc: FloatingPanelController, animatorForDismissingWith velocity: CGVector) -> UIViewPropertyAnimator {
  101. return UIViewPropertyAnimator(duration: 0.1, curve: .easeInOut)
  102. }
  103. func floatingPanel(_ fpc: FloatingPanelController, animatorForPresentingTo state: FloatingPanelState) -> UIViewPropertyAnimator {
  104. return UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut)
  105. }
  106. func floatingPanelWillEndDragging(_ fpc: FloatingPanelController, withVelocity velocity: CGPoint, targetState: UnsafeMutablePointer<FloatingPanelState>) {
  107. guard velocity.y > 750 else { return }
  108. fpc.dismiss(animated: true, completion: nil)
  109. }
  110. }