MainMenuTableViewController.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // MainMenuTableViewController.swift
  3. // Nextcloud
  4. //
  5. // Created by Philippe Weidmann on 16.01.20.
  6. // Copyright © 2020 TWS. All rights reserved.
  7. //
  8. import UIKit
  9. import FloatingPanel
  10. class MainMenuTableViewController: UITableViewController {
  11. var actions = [MenuAction]()
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. }
  15. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  16. let menuAction = actions[indexPath.row]
  17. self.dismiss(animated: true, completion: nil)
  18. menuAction.action?(menuAction)
  19. }
  20. // MARK: - Table view data source
  21. override func numberOfSections(in tableView: UITableView) -> Int {
  22. return 1
  23. }
  24. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  25. return actions.count
  26. }
  27. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  28. let cell = tableView.dequeueReusableCell(withIdentifier: "menuActionCell", for: indexPath)
  29. cell.tintColor = NCBrandColor.sharedInstance.customer
  30. let action = actions[indexPath.row]
  31. let actionIconView = cell.viewWithTag(1) as! UIImageView
  32. let actionNameLabel = cell.viewWithTag(2) as! UILabel
  33. if (action.isOn) {
  34. actionIconView.image = action.onIcon
  35. actionNameLabel.text = action.onTitle
  36. } else {
  37. actionIconView.image = action.icon
  38. actionNameLabel.text = action.title
  39. }
  40. cell.accessoryType = action.selectable && action.selected ? .checkmark : .none
  41. return cell
  42. }
  43. }
  44. extension MainMenuTableViewController: FloatingPanelControllerDelegate {
  45. func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout? {
  46. return MainMenuFloatingPanelLayout(height: self.actions.count * 60)
  47. }
  48. func floatingPanel(_ vc: FloatingPanelController, behaviorFor newCollection: UITraitCollection) -> FloatingPanelBehavior? {
  49. return MainMenuFloatingPanelBehavior()
  50. }
  51. }
  52. class MainMenuFloatingPanelLayout: FloatingPanelLayout {
  53. let height: CGFloat
  54. init(height: Int) {
  55. self.height = CGFloat(height)
  56. }
  57. var initialPosition: FloatingPanelPosition {
  58. return .tip
  59. }
  60. var supportedPositions: Set<FloatingPanelPosition> {
  61. return [.half]
  62. }
  63. func insetFor(position: FloatingPanelPosition) -> CGFloat? {
  64. switch position {
  65. case .half: return height
  66. case .tip: return height
  67. default: return nil
  68. }
  69. }
  70. func backdropAlphaFor(position: FloatingPanelPosition) -> CGFloat {
  71. return 0.5
  72. }
  73. }
  74. public class MainMenuFloatingPanelBehavior: FloatingPanelBehavior {
  75. public func addAnimator(_ fpc: FloatingPanelController, to: FloatingPanelPosition) -> UIViewPropertyAnimator {
  76. return UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut)
  77. }
  78. public func removeAnimator(_ fpc: FloatingPanelController, from: FloatingPanelPosition) -> UIViewPropertyAnimator {
  79. return UIViewPropertyAnimator(duration: 0.1, curve: .easeInOut)
  80. }
  81. public func moveAnimator(_ fpc: FloatingPanelController, from: FloatingPanelPosition, to: FloatingPanelPosition) -> UIViewPropertyAnimator {
  82. return UIViewPropertyAnimator(duration: 0.1, curve: .easeInOut)
  83. }
  84. }