NCSectionHeaderFooter.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // NCSectionHeaderFooter.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 09/10/2018.
  6. // Copyright © 2018 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 Foundation
  24. class NCSectionHeaderMenu: UICollectionReusableView {
  25. @IBOutlet weak var buttonMore: UIButton!
  26. @IBOutlet weak var buttonSwitch: UIButton!
  27. @IBOutlet weak var buttonOrder: UIButton!
  28. @IBOutlet weak var buttonOrderWidthConstraint: NSLayoutConstraint!
  29. @IBOutlet weak var viewLabelSection: UIView!
  30. @IBOutlet weak var labelSection: UILabel!
  31. @IBOutlet weak var labelSectionHeightConstraint: NSLayoutConstraint!
  32. @IBOutlet weak var separator: UIView!
  33. var delegate: NCSectionHeaderMenuDelegate?
  34. override func awakeFromNib() {
  35. super.awakeFromNib()
  36. buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  37. buttonOrder.setTitle("", for: .normal)
  38. buttonOrder.setTitleColor(NCBrandColor.sharedInstance.icon, for: .normal)
  39. buttonMore.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "more"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  40. viewLabelSection.backgroundColor = NCBrandColor.sharedInstance.select
  41. separator.backgroundColor = NCBrandColor.sharedInstance.separator
  42. self.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  43. }
  44. func setTitleOrder(datasourceSorted: String, datasourceAscending: Bool) {
  45. // Order (∨∧▽△)
  46. var title = ""
  47. switch datasourceSorted {
  48. case "fileName":
  49. if datasourceAscending == true { title = NSLocalizedString("_order_by_name_a_z_", comment: "") }
  50. if datasourceAscending == false { title = NSLocalizedString("_order_by_name_z_a_", comment: "") }
  51. case "date":
  52. if datasourceAscending == false { title = NSLocalizedString("_order_by_date_more_recent_", comment: "") }
  53. if datasourceAscending == true { title = NSLocalizedString("_order_by_date_less_recent_", comment: "") }
  54. case "size":
  55. if datasourceAscending == true { title = NSLocalizedString("_order_by_size_smallest_", comment: "") }
  56. if datasourceAscending == false { title = NSLocalizedString("_order_by_size_largest_", comment: "") }
  57. default:
  58. title = NSLocalizedString("_order_by_", comment: "") + " " + datasourceSorted
  59. }
  60. title = title + " ▽"
  61. let size = title.size(withAttributes:[.font: buttonOrder.titleLabel?.font as Any])
  62. buttonOrder.setTitle(title, for: .normal)
  63. buttonOrderWidthConstraint.constant = size.width + 5
  64. }
  65. func setTitleLabel(sectionDatasource: CCSectionDataSourceMetadata, section: Int) {
  66. var title = ""
  67. if sectionDatasource.sections.object(at: section) is String {
  68. title = sectionDatasource.sections.object(at: section) as! String
  69. }
  70. if sectionDatasource.sections.object(at: section) is Date {
  71. let titleDate = sectionDatasource.sections.object(at: section) as! Date
  72. title = CCUtility.getTitleSectionDate(titleDate)
  73. }
  74. if title.contains("download") {
  75. labelSection.text = NSLocalizedString("_title_section_download_", comment: "")
  76. } else if title.contains("upload") {
  77. labelSection.text = NSLocalizedString("_title_section_upload_", comment: "")
  78. } else {
  79. labelSection.text = NSLocalizedString(title, comment: "")
  80. }
  81. }
  82. func setStatusButton(count: Int) {
  83. if count == 0 {
  84. buttonSwitch.isEnabled = false
  85. buttonOrder.isEnabled = false
  86. buttonMore.isEnabled = false
  87. } else {
  88. buttonSwitch.isEnabled = true
  89. buttonOrder.isEnabled = true
  90. buttonMore.isEnabled = true
  91. }
  92. }
  93. @IBAction func touchUpInsideMore(_ sender: Any) {
  94. delegate?.tapMoreHeader(sender: sender)
  95. }
  96. @IBAction func touchUpInsideSwitch(_ sender: Any) {
  97. delegate?.tapSwitchHeader(sender: sender)
  98. }
  99. @IBAction func touchUpInsideOrder(_ sender: Any) {
  100. delegate?.tapOrderHeader(sender: sender)
  101. }
  102. }
  103. protocol NCSectionHeaderMenuDelegate {
  104. func tapSwitchHeader(sender: Any)
  105. func tapMoreHeader(sender: Any)
  106. func tapOrderHeader(sender: Any)
  107. }
  108. class NCSectionHeader: UICollectionReusableView {
  109. @IBOutlet weak var labelSection: UILabel!
  110. override func awakeFromNib() {
  111. super.awakeFromNib()
  112. self.backgroundColor = NCBrandColor.sharedInstance.select
  113. }
  114. func setTitleLabel(sectionDatasource: CCSectionDataSourceMetadata, section: Int) {
  115. var title = ""
  116. if sectionDatasource.sections.object(at: section) is String {
  117. title = sectionDatasource.sections.object(at: section) as! String
  118. }
  119. if sectionDatasource.sections.object(at: section) is Date {
  120. let titleDate = sectionDatasource.sections.object(at: section) as! Date
  121. title = CCUtility.getTitleSectionDate(titleDate)
  122. }
  123. if title.contains("download") {
  124. labelSection.text = NSLocalizedString("_title_section_download_", comment: "")
  125. } else if title.contains("upload") {
  126. labelSection.text = NSLocalizedString("_title_section_upload_", comment: "")
  127. } else {
  128. labelSection.text = NSLocalizedString(title, comment: "")
  129. }
  130. }
  131. }
  132. class NCSectionMediaHeader: UICollectionReusableView {
  133. @IBOutlet weak var labelSection: UILabel!
  134. @IBOutlet weak var labelLeadingConstraint: NSLayoutConstraint!
  135. @IBOutlet weak var labelHeightConstraint: NSLayoutConstraint!
  136. @IBOutlet weak var labelWidthConstraint: NSLayoutConstraint!
  137. override func awakeFromNib() {
  138. super.awakeFromNib()
  139. }
  140. func setTitleLabel(sectionDatasource: CCSectionDataSourceMetadata, section: Int) {
  141. var title = ""
  142. if sectionDatasource.sections.count > section {
  143. if sectionDatasource.sections.object(at: section) is String {
  144. title = sectionDatasource.sections.object(at: section) as! String
  145. }
  146. if sectionDatasource.sections.object(at: section) is Date {
  147. let titleDate = sectionDatasource.sections.object(at: section) as! Date
  148. title = CCUtility.getTitleSectionDate(titleDate)
  149. }
  150. }
  151. labelSection.text = NSLocalizedString(title, comment: "")
  152. }
  153. }
  154. class NCSectionFooter: UICollectionReusableView {
  155. @IBOutlet weak var labelSection: UILabel!
  156. override func awakeFromNib() {
  157. super.awakeFromNib()
  158. self.backgroundColor = UIColor.clear
  159. labelSection.textColor = NCBrandColor.sharedInstance.icon
  160. }
  161. func setTitleLabel(sectionDatasource: CCSectionDataSourceMetadata) {
  162. var foldersText = ""
  163. var filesText = ""
  164. if sectionDatasource.directories > 1 {
  165. foldersText = "\(sectionDatasource.directories) " + NSLocalizedString("_folders_", comment: "")
  166. } else if sectionDatasource.directories == 1 {
  167. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  168. }
  169. if sectionDatasource.files > 1 {
  170. filesText = "\(sectionDatasource.files) " + NSLocalizedString("_files_", comment: "") + " " + CCUtility.transformedSize(sectionDatasource.totalSize)
  171. } else if sectionDatasource.files == 1 {
  172. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + CCUtility.transformedSize(sectionDatasource.totalSize)
  173. }
  174. if foldersText == "" {
  175. labelSection.text = filesText
  176. } else if filesText == "" {
  177. labelSection.text = foldersText
  178. } else {
  179. labelSection.text = foldersText + ", " + filesText
  180. }
  181. }
  182. }