NCSortMenu.swift 6.4 KB

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