NCSortMenu.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 layout = ""
  30. private var typeLayout = ""
  31. private var datasourceSorted = ""
  32. private var datasourceAscending = true
  33. private var datasourceGroupBy = ""
  34. private var datasourceDirectoryOnTop = false
  35. private var datasourceTitleButton = ""
  36. @objc func toggleMenu(viewController: UIViewController, layout: String, sortButton: UIButton?, serverUrl: String?, hideDirectoryOnTop: Bool = false) {
  37. self.layout = layout
  38. self.sortButton = sortButton
  39. self.serverUrl = serverUrl
  40. self.hideDirectoryOnTop = hideDirectoryOnTop
  41. (typeLayout, datasourceSorted, datasourceAscending, datasourceGroupBy, datasourceDirectoryOnTop, datasourceTitleButton) = NCUtility.shared.getLayoutForView(key: layout)
  42. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  43. mainMenuViewController.actions = self.initSortMenu()
  44. let menuPanelController = NCMenuPanelController()
  45. menuPanelController.parentPresenter = viewController
  46. menuPanelController.delegate = mainMenuViewController
  47. menuPanelController.set(contentViewController: mainMenuViewController)
  48. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  49. viewController.present(menuPanelController, animated: true, completion: nil)
  50. }
  51. @objc func actionMenu() {
  52. switch datasourceSorted {
  53. case "fileName":
  54. self.datasourceTitleButton = datasourceAscending ? "_sorted_by_name_a_z_" : "_sorted_by_name_z_a_"
  55. case "date":
  56. self.datasourceTitleButton = datasourceAscending ? "_sorted_by_date_less_recent_" : "_sorted_by_date_more_recent_"
  57. case "size":
  58. self.datasourceTitleButton = datasourceAscending ? "_sorted_by_size_largest_" : "_sorted_by_size_smallest_"
  59. default:
  60. break
  61. }
  62. self.sortButton?.setTitle(NSLocalizedString(self.datasourceTitleButton, comment: ""), for: .normal)
  63. NCUtility.shared.setLayoutForView(key: self.layout, layout: self.typeLayout, sort: self.datasourceSorted, ascending: self.datasourceAscending, groupBy: self.datasourceGroupBy, directoryOnTop: self.datasourceDirectoryOnTop, titleButton: self.datasourceTitleButton)
  64. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["serverUrl":self.serverUrl ?? ""])
  65. }
  66. private func initSortMenu() -> [NCMenuAction] {
  67. var actions = [NCMenuAction]()
  68. actions.append(
  69. NCMenuAction(
  70. title: NSLocalizedString("_order_by_name_a_z_", comment: ""),
  71. icon: CCGraphics.changeThemingColorImage(UIImage(named: "sortFileNameAZ"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  72. onTitle: NSLocalizedString("_order_by_name_z_a_", comment: ""),
  73. onIcon: CCGraphics.changeThemingColorImage(UIImage(named: "sortFileNameZA"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  74. selected: self.datasourceSorted == "fileName",
  75. on: self.datasourceSorted == "fileName",
  76. action: { menuAction in
  77. if self.datasourceSorted == "fileName" {
  78. self.datasourceAscending = !self.datasourceAscending
  79. } else {
  80. self.datasourceSorted = "fileName"
  81. }
  82. self.actionMenu()
  83. }
  84. )
  85. )
  86. actions.append(
  87. NCMenuAction(
  88. title: NSLocalizedString("_order_by_date_more_recent_", comment: ""),
  89. icon: CCGraphics.changeThemingColorImage(UIImage(named: "sortDateMoreRecent"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  90. onTitle: NSLocalizedString("_order_by_date_less_recent_", comment: ""),
  91. onIcon: CCGraphics.changeThemingColorImage(UIImage(named: "sortDateLessRecent"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  92. selected: self.datasourceSorted == "date",
  93. on: self.datasourceSorted == "date",
  94. action: { menuAction in
  95. if self.datasourceSorted == "date" {
  96. self.datasourceAscending = !self.datasourceAscending
  97. } else {
  98. self.datasourceSorted = "date"
  99. }
  100. self.actionMenu()
  101. }
  102. )
  103. )
  104. actions.append(
  105. NCMenuAction(
  106. title: NSLocalizedString("_order_by_size_smallest_", comment: ""),
  107. icon: CCGraphics.changeThemingColorImage(UIImage(named: "sortSmallest"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  108. onTitle: NSLocalizedString("_order_by_size_largest_", comment: ""),
  109. onIcon: CCGraphics.changeThemingColorImage(UIImage(named: "sortLargest"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  110. selected: self.datasourceSorted == "size",
  111. on: self.datasourceSorted == "size",
  112. action: { menuAction in
  113. if self.datasourceSorted == "size" {
  114. self.datasourceAscending = !self.datasourceAscending
  115. } else {
  116. self.datasourceSorted = "size"
  117. }
  118. self.actionMenu()
  119. }
  120. )
  121. )
  122. if !(hideDirectoryOnTop ?? false) {
  123. actions.append(
  124. NCMenuAction(
  125. title: NSLocalizedString("_directory_on_top_no_", comment: ""),
  126. icon: CCGraphics.changeThemingColorImage(UIImage(named: "foldersOnTop"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  127. selected: self.datasourceDirectoryOnTop,
  128. on: self.datasourceDirectoryOnTop,
  129. action: { menuAction in
  130. self.datasourceDirectoryOnTop = !self.datasourceDirectoryOnTop
  131. self.actionMenu()
  132. }
  133. )
  134. )
  135. }
  136. return actions
  137. }
  138. }