NCMainMenuTableViewController.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. //
  9. // Author Philippe Weidmann <philippe.weidmann@infomaniak.com>
  10. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU General Public License as published by
  14. // the Free Software Foundation, either version 3 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. import UIKit
  26. import FloatingPanel
  27. class NCMainMenuTableViewController: UITableViewController {
  28. var actions = [NCMenuAction]()
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. }
  32. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  33. let menuAction = actions[indexPath.row]
  34. self.dismiss(animated: true, completion: nil)
  35. menuAction.action?(menuAction)
  36. }
  37. // MARK: - Table view data source
  38. override func numberOfSections(in tableView: UITableView) -> Int {
  39. return 1
  40. }
  41. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  42. return actions.count
  43. }
  44. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  45. let cell = tableView.dequeueReusableCell(withIdentifier: "menuActionCell", for: indexPath)
  46. cell.tintColor = NCBrandColor.sharedInstance.customer
  47. let action = actions[indexPath.row]
  48. let actionIconView = cell.viewWithTag(1) as! UIImageView
  49. let actionNameLabel = cell.viewWithTag(2) as! UILabel
  50. if (action.isOn) {
  51. actionIconView.image = action.onIcon
  52. actionNameLabel.text = action.onTitle
  53. } else {
  54. actionIconView.image = action.icon
  55. actionNameLabel.text = action.title
  56. }
  57. cell.accessoryType = action.selectable && action.selected ? .checkmark : .none
  58. return cell
  59. }
  60. }
  61. extension NCMainMenuTableViewController: FloatingPanelControllerDelegate {
  62. func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout? {
  63. if #available(iOS 11.0, *) {
  64. return NCMainMenuFloatingPanelLayout(height: self.actions.count * 60 + Int((UIApplication.shared.keyWindow?.rootViewController!.view.safeAreaInsets.bottom)!))
  65. } else {
  66. return NCMainMenuFloatingPanelLayout(height: self.actions.count * 60)
  67. }
  68. }
  69. func floatingPanel(_ vc: FloatingPanelController, behaviorFor newCollection: UITraitCollection) -> FloatingPanelBehavior? {
  70. return NCMainMenuFloatingPanelBehavior()
  71. }
  72. }
  73. class NCMainMenuFloatingPanelLayout: FloatingPanelLayout {
  74. let height: CGFloat
  75. init(height: Int) {
  76. self.height = CGFloat(height)
  77. }
  78. var initialPosition: FloatingPanelPosition {
  79. return .full
  80. }
  81. var supportedPositions: Set<FloatingPanelPosition> {
  82. return [.full]
  83. }
  84. func insetFor(position: FloatingPanelPosition) -> CGFloat? {
  85. if (position == .full) {
  86. return max(48, UIScreen.main.bounds.size.height - height)
  87. } else {
  88. return nil
  89. }
  90. }
  91. var positionReference: FloatingPanelLayoutReference {
  92. return .fromSuperview
  93. }
  94. public func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] {
  95. return [
  96. surfaceView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0),
  97. surfaceView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0),
  98. ]
  99. }
  100. func backdropAlphaFor(position: FloatingPanelPosition) -> CGFloat {
  101. return 0.2
  102. }
  103. }
  104. public class NCMainMenuFloatingPanelBehavior: FloatingPanelBehavior {
  105. public func addAnimator(_ fpc: FloatingPanelController, to: FloatingPanelPosition) -> UIViewPropertyAnimator {
  106. return UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut)
  107. }
  108. public func removeAnimator(_ fpc: FloatingPanelController, from: FloatingPanelPosition) -> UIViewPropertyAnimator {
  109. return UIViewPropertyAnimator(duration: 0.1, curve: .easeInOut)
  110. }
  111. public func moveAnimator(_ fpc: FloatingPanelController, from: FloatingPanelPosition, to: FloatingPanelPosition) -> UIViewPropertyAnimator {
  112. return UIViewPropertyAnimator(duration: 0.1, curve: .easeInOut)
  113. }
  114. }