NCMenu.swift 5.5 KB

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