NCDataSource.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. @objc func cellForItemAt(indexPath: IndexPath) -> tableMetadata? {
  92. let row = indexPath.row
  93. if row > self.metadatas.count - 1 {
  94. return nil
  95. } else {
  96. return metadatas[row]
  97. }
  98. }
  99. @objc func numberOfItemsInSection(section: Int) -> Int {
  100. return metadatas.count
  101. }
  102. func getFilesInformation() -> (directories: Int, files: Int, size: Double) {
  103. var directories: Int = 0
  104. var files: Int = 0
  105. var size: Double = 0
  106. for metadata in metadatas {
  107. if metadata.directory {
  108. directories += 1
  109. } else {
  110. files += 1
  111. }
  112. size = size + metadata.size
  113. }
  114. return (directories, files, size)
  115. }
  116. @objc func getIndexPathAt(metadata: tableMetadata) -> IndexPath? {
  117. var row: Int = 0
  118. for metadataCount in metadatas {
  119. if metadataCount.ocId == metadata.ocId {
  120. return IndexPath(row: row, section: 0)
  121. }
  122. row += 1
  123. }
  124. return nil
  125. }
  126. @objc func reloadItemAt(indexPath: IndexPath) -> [tableMetadata] {
  127. let row = indexPath.row
  128. if row > self.metadatas.count - 1 {
  129. let metadata = metadatas[row]
  130. if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  131. metadatas[row] = metadata
  132. }
  133. }
  134. return metadatas
  135. }
  136. @objc func deleteItemAt(indexPath: IndexPath) -> [tableMetadata] {
  137. let row = indexPath.row
  138. if row > self.metadatas.count - 1 {
  139. metadatas.remove(at: row)
  140. }
  141. return metadatas
  142. }
  143. }