NCMenu.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 action = actions[indexPath.row]
  56. guard action.title != NCMenuAction.seperatorIdentifier else {
  57. let cell = UITableViewCell()
  58. cell.backgroundColor = NCBrandColor.shared.separator
  59. return cell
  60. }
  61. let cell = tableView.dequeueReusableCell(withIdentifier: "menuActionCell", for: indexPath)
  62. cell.tintColor = NCBrandColor.shared.customer
  63. let actionIconView = cell.viewWithTag(1) as? UIImageView
  64. let actionNameLabel = cell.viewWithTag(2) as? UILabel
  65. if action.action == nil {
  66. cell.selectionStyle = .none
  67. }
  68. if action.isOn {
  69. actionIconView?.image = action.onIcon
  70. actionNameLabel?.text = action.onTitle
  71. } else {
  72. actionIconView?.image = action.icon
  73. actionNameLabel?.text = action.title
  74. }
  75. cell.accessoryType = action.selectable && action.selected ? .checkmark : .none
  76. return cell
  77. }
  78. // MARK: - Tabel View Layout
  79. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  80. actions[indexPath.row].title == NCMenuAction.seperatorIdentifier ? 3 : 60
  81. }
  82. }
  83. extension NCMenu: FloatingPanelControllerDelegate {
  84. func floatingPanel(_ fpc: FloatingPanelController, layoutFor size: CGSize) -> FloatingPanelLayout {
  85. return NCMenuFloatingPanelLayout(numberOfActions: self.actions.count)
  86. }
  87. func floatingPanel(_ fpc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout {
  88. return NCMenuFloatingPanelLayout(numberOfActions: self.actions.count)
  89. }
  90. func floatingPanel(_ fpc: FloatingPanelController, animatorForDismissingWith velocity: CGVector) -> UIViewPropertyAnimator {
  91. return UIViewPropertyAnimator(duration: 0.1, curve: .easeInOut)
  92. }
  93. func floatingPanel(_ fpc: FloatingPanelController, animatorForPresentingTo state: FloatingPanelState) -> UIViewPropertyAnimator {
  94. return UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut)
  95. }
  96. func floatingPanelWillEndDragging(_ fpc: FloatingPanelController, withVelocity velocity: CGPoint, targetState: UnsafeMutablePointer<FloatingPanelState>) {
  97. guard velocity.y > 750 else { return }
  98. fpc.dismiss(animated: true, completion: nil)
  99. }
  100. }