NCSortMenu.swift 6.8 KB

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