NCMenu.swift 4.3 KB

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