NCSortMenu.swift 6.6 KB

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