NCMenu.swift 6.1 KB

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