NCSortMenu.swift 6.5 KB

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