NCSortMenu.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // NCSortMenu.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 27/08/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import FloatingPanel
  24. import NCCommunication
  25. class NCSortMenu: NSObject {
  26. private var sortButton: UIButton?
  27. private var serverUrl: String = ""
  28. private var hideDirectoryOnTop: Bool?
  29. private var key = ""
  30. func toggleMenu(viewController: UIViewController, key: String, sortButton: UIButton?, serverUrl: String, hideDirectoryOnTop: Bool = false) {
  31. self.key = key
  32. self.sortButton = sortButton
  33. self.serverUrl = serverUrl
  34. self.hideDirectoryOnTop = hideDirectoryOnTop
  35. var layoutForView = NCUtility.shared.getLayoutForView(key: key, serverUrl: serverUrl)
  36. let menuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateInitialViewController() as! NCMenu
  37. var actions = [NCMenuAction]()
  38. actions.append(
  39. NCMenuAction(
  40. title: NSLocalizedString("_order_by_name_a_z_", comment: ""),
  41. icon: UIImage(named: "sortFileNameAZ")!.image(color: NCBrandColor.shared.gray, size: 50),
  42. onTitle: NSLocalizedString("_order_by_name_z_a_", comment: ""),
  43. onIcon: UIImage(named: "sortFileNameZA")!.image(color: NCBrandColor.shared.gray, size: 50),
  44. selected: layoutForView.sort == "fileName",
  45. on: layoutForView.sort == "fileName",
  46. action: { menuAction in
  47. if layoutForView.sort == "fileName" {
  48. layoutForView.ascending = !layoutForView.ascending
  49. } else {
  50. layoutForView.sort = "fileName"
  51. }
  52. self.actionMenu(layoutForView: layoutForView)
  53. }
  54. )
  55. )
  56. actions.append(
  57. NCMenuAction(
  58. title: NSLocalizedString("_order_by_date_more_recent_", comment: ""),
  59. icon: UIImage(named: "sortDateMoreRecent")!.image(color: NCBrandColor.shared.gray, size: 50),
  60. onTitle: NSLocalizedString("_order_by_date_less_recent_", comment: ""),
  61. onIcon: UIImage(named: "sortDateLessRecent")!.image(color: NCBrandColor.shared.gray, size: 50),
  62. selected: layoutForView.sort == "date",
  63. on: layoutForView.sort == "date",
  64. action: { menuAction in
  65. if layoutForView.sort == "date" {
  66. layoutForView.ascending = !layoutForView.ascending
  67. } else {
  68. layoutForView.sort = "date"
  69. }
  70. self.actionMenu(layoutForView: layoutForView)
  71. }
  72. )
  73. )
  74. actions.append(
  75. NCMenuAction(
  76. title: NSLocalizedString("_order_by_size_smallest_", comment: ""),
  77. icon: UIImage(named: "sortSmallest")!.image(color: NCBrandColor.shared.gray, size: 50),
  78. onTitle: NSLocalizedString("_order_by_size_largest_", comment: ""),
  79. onIcon: UIImage(named: "sortLargest")!.image(color: NCBrandColor.shared.gray, size: 50),
  80. selected: layoutForView.sort == "size",
  81. on: layoutForView.sort == "size",
  82. action: { menuAction in
  83. if layoutForView.sort == "size" {
  84. layoutForView.ascending = !layoutForView.ascending
  85. } else {
  86. layoutForView.sort = "size"
  87. }
  88. self.actionMenu(layoutForView: layoutForView)
  89. }
  90. )
  91. )
  92. if !hideDirectoryOnTop {
  93. actions.append(
  94. NCMenuAction(
  95. title: NSLocalizedString("_directory_on_top_no_", comment: ""),
  96. icon: UIImage(named: "foldersOnTop")!.image(color: NCBrandColor.shared.gray, size: 50),
  97. selected: layoutForView.directoryOnTop,
  98. on: layoutForView.directoryOnTop,
  99. action: { menuAction in
  100. layoutForView.directoryOnTop = !layoutForView.directoryOnTop
  101. self.actionMenu(layoutForView: layoutForView)
  102. }
  103. )
  104. )
  105. }
  106. menuViewController.actions = actions
  107. let menuPanelController = NCMenuPanelController()
  108. menuPanelController.parentPresenter = viewController
  109. menuPanelController.delegate = menuViewController
  110. menuPanelController.set(contentViewController: menuViewController)
  111. menuPanelController.track(scrollView: menuViewController.tableView)
  112. viewController.present(menuPanelController, animated: true, completion: nil)
  113. }
  114. func actionMenu(layoutForView: NCGlobal.layoutForViewType) {
  115. var layoutForView = layoutForView
  116. switch layoutForView.sort {
  117. case "fileName":
  118. layoutForView.titleButtonHeader = layoutForView.ascending ? "_sorted_by_name_a_z_" : "_sorted_by_name_z_a_"
  119. case "date":
  120. layoutForView.titleButtonHeader = layoutForView.ascending ? "_sorted_by_date_less_recent_" : "_sorted_by_date_more_recent_"
  121. case "size":
  122. layoutForView.titleButtonHeader = layoutForView.ascending ? "_sorted_by_size_smallest_" : "_sorted_by_size_largest_"
  123. default:
  124. break
  125. }
  126. self.sortButton?.setTitle(NSLocalizedString(layoutForView.titleButtonHeader, comment: ""), for: .normal)
  127. NCUtility.shared.setLayoutForView(key: key, serverUrl: serverUrl, layoutForView: layoutForView)
  128. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl":self.serverUrl])
  129. }
  130. }