NCSortMenu.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 UIKit
  24. import FloatingPanel
  25. import NCCommunication
  26. class NCSortMenu: NSObject {
  27. private var sortButton: UIButton?
  28. private var serverUrl: String = ""
  29. private var hideDirectoryOnTop: Bool?
  30. private var key = ""
  31. func toggleMenu(viewController: UIViewController, key: String, sortButton: UIButton?, serverUrl: String, hideDirectoryOnTop: Bool = false) {
  32. self.key = key
  33. self.sortButton = sortButton
  34. self.serverUrl = serverUrl
  35. self.hideDirectoryOnTop = hideDirectoryOnTop
  36. var layoutForView = NCUtility.shared.getLayoutForView(key: key, serverUrl: serverUrl)
  37. let menuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateInitialViewController() as! NCMenu
  38. var actions = [NCMenuAction]()
  39. var title = ""
  40. var icon = UIImage()
  41. if layoutForView.ascending {
  42. title = NSLocalizedString("_order_by_name_z_a_", comment: "")
  43. icon = UIImage(named: "sortFileNameZA")!.image(color: NCBrandColor.shared.gray, size: 50)
  44. } else {
  45. title = NSLocalizedString("_order_by_name_a_z_", comment: "")
  46. icon = UIImage(named: "sortFileNameAZ")!.image(color: NCBrandColor.shared.gray, size: 50)
  47. }
  48. actions.append(
  49. NCMenuAction(
  50. title: title,
  51. icon: icon,
  52. selected: layoutForView.sort == "fileName",
  53. on: layoutForView.sort == "fileName",
  54. action: { menuAction in
  55. layoutForView.sort = "fileName"
  56. layoutForView.ascending = !layoutForView.ascending
  57. self.actionMenu(layoutForView: layoutForView)
  58. }
  59. )
  60. )
  61. if layoutForView.ascending {
  62. title = NSLocalizedString("_order_by_date_more_recent_", comment: "")
  63. icon = UIImage(named: "sortDateMoreRecent")!.image(color: NCBrandColor.shared.gray, size: 50)
  64. } else {
  65. title = NSLocalizedString("_order_by_date_less_recent_", comment: "")
  66. icon = UIImage(named: "sortDateLessRecent")!.image(color: NCBrandColor.shared.gray, size: 50)
  67. }
  68. actions.append(
  69. NCMenuAction(
  70. title: title,
  71. icon: icon,
  72. selected: layoutForView.sort == "date",
  73. on: layoutForView.sort == "date",
  74. action: { menuAction in
  75. layoutForView.sort = "date"
  76. layoutForView.ascending = !layoutForView.ascending
  77. self.actionMenu(layoutForView: layoutForView)
  78. }
  79. )
  80. )
  81. if layoutForView.ascending {
  82. title = NSLocalizedString("_order_by_size_largest_", comment: "")
  83. icon = UIImage(named: "sortLargest")!.image(color: NCBrandColor.shared.gray, size: 50)
  84. } else {
  85. title = NSLocalizedString("_order_by_size_smallest_", comment: "")
  86. icon = UIImage(named: "sortSmallest")!.image(color: NCBrandColor.shared.gray, size: 50)
  87. }
  88. actions.append(
  89. NCMenuAction(
  90. title: title,
  91. icon: icon,
  92. selected: layoutForView.sort == "size",
  93. on: layoutForView.sort == "size",
  94. action: { menuAction in
  95. layoutForView.sort = "size"
  96. layoutForView.ascending = !layoutForView.ascending
  97. self.actionMenu(layoutForView: layoutForView)
  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: layoutForView.directoryOnTop,
  107. on: layoutForView.directoryOnTop,
  108. action: { menuAction in
  109. layoutForView.directoryOnTop = !layoutForView.directoryOnTop
  110. self.actionMenu(layoutForView: layoutForView)
  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(layoutForView: NCGlobal.layoutForViewType) {
  124. var layoutForView = layoutForView
  125. switch layoutForView.sort {
  126. case "fileName":
  127. layoutForView.titleButtonHeader = layoutForView.ascending ? "_sorted_by_name_a_z_" : "_sorted_by_name_z_a_"
  128. case "date":
  129. layoutForView.titleButtonHeader = layoutForView.ascending ? "_sorted_by_date_less_recent_" : "_sorted_by_date_more_recent_"
  130. case "size":
  131. layoutForView.titleButtonHeader = layoutForView.ascending ? "_sorted_by_size_smallest_" : "_sorted_by_size_largest_"
  132. default:
  133. break
  134. }
  135. self.sortButton?.setTitle(NSLocalizedString(layoutForView.titleButtonHeader, comment: ""), for: .normal)
  136. NCUtility.shared.setLayoutForView(key: key, serverUrl: serverUrl, layoutForView: layoutForView)
  137. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl":self.serverUrl])
  138. }
  139. }