NCSortMenu.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 NextcloudKit
  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, account: String, 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. guard let layoutForView = NCManageDatabase.shared.getLayoutForView(account: account, key: key, serverUrl: serverUrl) else { return }
  37. var actions = [NCMenuAction]()
  38. var title = ""
  39. var icon = UIImage()
  40. if layoutForView.ascending {
  41. title = NSLocalizedString("_order_by_name_z_a_", comment: "")
  42. icon = UIImage(named: "sortFileNameZA")!.image(color: UIColor.systemGray, size: 50)
  43. } else {
  44. title = NSLocalizedString("_order_by_name_a_z_", comment: "")
  45. icon = UIImage(named: "sortFileNameAZ")!.image(color: UIColor.systemGray, size: 50)
  46. }
  47. actions.append(
  48. NCMenuAction(
  49. title: title,
  50. icon: icon,
  51. selected: layoutForView.sort == "fileName",
  52. on: layoutForView.sort == "fileName",
  53. action: { _ in
  54. layoutForView.sort = "fileName"
  55. layoutForView.ascending = !layoutForView.ascending
  56. self.actionMenu(layoutForView: layoutForView)
  57. }
  58. )
  59. )
  60. if layoutForView.ascending {
  61. title = NSLocalizedString("_order_by_date_more_recent_", comment: "")
  62. icon = UIImage(named: "sortDateMoreRecent")!.image(color: UIColor.systemGray, size: 50)
  63. } else {
  64. title = NSLocalizedString("_order_by_date_less_recent_", comment: "")
  65. icon = UIImage(named: "sortDateLessRecent")!.image(color: UIColor.systemGray, size: 50)
  66. }
  67. actions.append(
  68. NCMenuAction(
  69. title: title,
  70. icon: icon,
  71. selected: layoutForView.sort == "date",
  72. on: layoutForView.sort == "date",
  73. action: { _ in
  74. layoutForView.sort = "date"
  75. layoutForView.ascending = !layoutForView.ascending
  76. self.actionMenu(layoutForView: layoutForView)
  77. }
  78. )
  79. )
  80. if layoutForView.ascending {
  81. title = NSLocalizedString("_order_by_size_largest_", comment: "")
  82. icon = UIImage(named: "sortLargest")!.image(color: UIColor.systemGray, size: 50)
  83. } else {
  84. title = NSLocalizedString("_order_by_size_smallest_", comment: "")
  85. icon = UIImage(named: "sortSmallest")!.image(color: UIColor.systemGray, size: 50)
  86. }
  87. actions.append(
  88. NCMenuAction(
  89. title: title,
  90. icon: icon,
  91. selected: layoutForView.sort == "size",
  92. on: layoutForView.sort == "size",
  93. action: { _ in
  94. layoutForView.sort = "size"
  95. layoutForView.ascending = !layoutForView.ascending
  96. self.actionMenu(layoutForView: layoutForView)
  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: UIColor.systemGray, size: 50),
  105. selected: layoutForView.directoryOnTop,
  106. on: layoutForView.directoryOnTop,
  107. action: { _ in
  108. layoutForView.directoryOnTop = !layoutForView.directoryOnTop
  109. self.actionMenu(layoutForView: layoutForView)
  110. }
  111. )
  112. )
  113. }
  114. viewController.presentMenu(with: actions)
  115. }
  116. func actionMenu(layoutForView: NCDBLayoutForView) {
  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. NCManageDatabase.shared.setLayoutForView(layoutForView: layoutForView)
  129. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource)
  130. }
  131. }