NCSortMenu.swift 6.5 KB

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