NCDataSource.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // NCDataSource.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/09/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 Foundation
  24. @objc class NCDataSource: NSObject {
  25. @objc var metadatas: [tableMetadata] = []
  26. @objc var sections: Int = 1
  27. @objc init(metadatasSource: [tableMetadata], sort: String, ascending: Bool, groupBy: String? = nil, directoryOnTop: Bool, filterLivePhoto: Bool) {
  28. var metadatasFavorite: [tableMetadata] = []
  29. var numDirectory: Int = 0
  30. var numDirectoryFavorite:Int = 0
  31. /*
  32. Metadata order
  33. */
  34. let metadatasSourceSorted = metadatasSource.sorted { (obj1:tableMetadata, obj2:tableMetadata) -> Bool in
  35. if sort == "date" {
  36. if ascending {
  37. return obj1.date.compare(obj2.date as Date) == ComparisonResult.orderedAscending
  38. } else {
  39. return obj1.date.compare(obj2.date as Date) == ComparisonResult.orderedDescending
  40. }
  41. } else if sort == "sessionTaskIdentifier" {
  42. if ascending {
  43. return obj1.sessionTaskIdentifier < obj2.sessionTaskIdentifier
  44. } else {
  45. return obj1.sessionTaskIdentifier > obj2.sessionTaskIdentifier
  46. }
  47. } else if sort == "size" {
  48. if ascending {
  49. return obj1.size < obj2.size
  50. } else {
  51. return obj1.size > obj2.size
  52. }
  53. } else {
  54. let range = Range(NSMakeRange(0, obj1.fileNameView.count), in: obj1.fileNameView)
  55. if ascending {
  56. return obj1.fileNameView.compare(obj2.fileNameView, options: .caseInsensitive, range: range, locale: .current) == ComparisonResult.orderedAscending
  57. } else {
  58. return obj1.fileNameView.compare(obj2.fileNameView, options: .caseInsensitive, range: range, locale: .current) == ComparisonResult.orderedDescending
  59. }
  60. }
  61. }
  62. /*
  63. Initialize datasource
  64. */
  65. for metadata in metadatasSourceSorted {
  66. // skipped livePhoto
  67. if metadata.ext == "mov" && metadata.livePhoto {
  68. continue
  69. }
  70. if metadata.directory && directoryOnTop {
  71. if metadata.favorite {
  72. metadatas.insert(metadata, at: numDirectoryFavorite)
  73. numDirectoryFavorite += 1
  74. numDirectory += 1
  75. } else {
  76. metadatas.insert(metadata, at: numDirectory)
  77. numDirectory += 1
  78. }
  79. } else {
  80. if metadata.favorite && directoryOnTop {
  81. metadatasFavorite.append(metadata)
  82. } else {
  83. metadatas.append(metadata)
  84. }
  85. }
  86. }
  87. if directoryOnTop && metadatasFavorite.count > 0 {
  88. metadatas.insert(contentsOf: metadatasFavorite, at: numDirectory)
  89. }
  90. /*
  91. if (directoryOnTop && metadataFilesFavorite.count > 0) {
  92. [sectionDataSource.metadatas insertObjects:metadataFilesFavorite atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(numDirectoryFavorite, metadataFilesFavorite.count)]]; // Add Favorite files at end of favorite folders
  93. }
  94. */
  95. }
  96. @objc func cellForItemAt(indexPath: IndexPath) -> tableMetadata? {
  97. let row = indexPath.row
  98. if row > self.metadatas.count - 1 {
  99. return nil
  100. } else {
  101. return metadatas[row]
  102. }
  103. }
  104. @objc func numberOfItemsInSection(section: Int) -> Int {
  105. return metadatas.count
  106. }
  107. @objc func getSize() -> Double {
  108. var size: Double = 0
  109. for metadata in metadatas {
  110. size = size + metadata.size
  111. }
  112. return size
  113. }
  114. }