NCDataSource.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. private var sort: String = ""
  28. private var ascending: Bool = true
  29. private var directoryOnTop: Bool = true
  30. private var filterLivePhoto: Bool = true
  31. @objc init(metadatasSource: [tableMetadata], sort: String, ascending: Bool, groupBy: String? = nil, directoryOnTop: Bool, filterLivePhoto: Bool) {
  32. super.init()
  33. self.sort = sort
  34. self.ascending = ascending
  35. self.directoryOnTop = directoryOnTop
  36. self.filterLivePhoto = filterLivePhoto
  37. createMetadatas(metadatasSource: metadatasSource)
  38. }
  39. // MARK: -
  40. private func createMetadatas(metadatasSource: [tableMetadata]) {
  41. var metadatasFavorite: [tableMetadata] = []
  42. var numDirectory: Int = 0
  43. var numDirectoryFavorite:Int = 0
  44. /*
  45. Metadata order
  46. */
  47. let metadatasSourceSorted = metadatasSource.sorted { (obj1:tableMetadata, obj2:tableMetadata) -> Bool in
  48. if sort == "date" {
  49. if ascending {
  50. return obj1.date.compare(obj2.date as Date) == ComparisonResult.orderedAscending
  51. } else {
  52. return obj1.date.compare(obj2.date as Date) == ComparisonResult.orderedDescending
  53. }
  54. } else if sort == "sessionTaskIdentifier" {
  55. if ascending {
  56. return obj1.sessionTaskIdentifier > obj2.sessionTaskIdentifier
  57. } else {
  58. return obj1.sessionTaskIdentifier < obj2.sessionTaskIdentifier
  59. }
  60. } else if sort == "size" {
  61. if ascending {
  62. return obj1.size > obj2.size
  63. } else {
  64. return obj1.size < obj2.size
  65. }
  66. } else {
  67. let range = Range(NSMakeRange(0, obj1.fileNameView.count), in: obj1.fileNameView)
  68. if ascending {
  69. return obj1.fileNameView.compare(obj2.fileNameView, options: .caseInsensitive, range: range, locale: .current) == ComparisonResult.orderedAscending
  70. } else {
  71. return obj1.fileNameView.compare(obj2.fileNameView, options: .caseInsensitive, range: range, locale: .current) == ComparisonResult.orderedDescending
  72. }
  73. }
  74. }
  75. /*
  76. Initialize datasource
  77. */
  78. for metadata in metadatasSourceSorted {
  79. // skipped livePhoto
  80. if metadata.ext == "mov" && metadata.livePhoto {
  81. continue
  82. }
  83. if metadata.directory && directoryOnTop {
  84. if metadata.favorite {
  85. metadatas.insert(metadata, at: numDirectoryFavorite)
  86. numDirectoryFavorite += 1
  87. numDirectory += 1
  88. } else {
  89. metadatas.insert(metadata, at: numDirectory)
  90. numDirectory += 1
  91. }
  92. } else {
  93. if metadata.favorite && directoryOnTop {
  94. metadatasFavorite.append(metadata)
  95. } else {
  96. metadatas.append(metadata)
  97. }
  98. }
  99. }
  100. if directoryOnTop && metadatasFavorite.count > 0 {
  101. metadatas.insert(contentsOf: metadatasFavorite, at: numDirectory)
  102. }
  103. }
  104. func getFilesInformation() -> (directories: Int, files: Int, size: Double) {
  105. var directories: Int = 0
  106. var files: Int = 0
  107. var size: Double = 0
  108. for metadata in metadatas {
  109. if metadata.directory {
  110. directories += 1
  111. } else {
  112. files += 1
  113. }
  114. size = size + metadata.size
  115. }
  116. return (directories, files, size)
  117. }
  118. @objc func deleteMetadata(ocId: String) -> Bool {
  119. var found = false
  120. if let index = self.getIndexMetadata(ocId: ocId) {
  121. metadatas.remove(at: index)
  122. found = true
  123. }
  124. return found
  125. }
  126. @objc func reloadMetadata(ocId: String) -> Bool {
  127. var found = false
  128. if let index = self.getIndexMetadata(ocId: ocId) {
  129. if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", ocId)) {
  130. metadatas[index] = metadata
  131. found = true
  132. }
  133. }
  134. return found
  135. }
  136. @objc func addMetadata(_ metadata: tableMetadata) {
  137. for metadataCount in metadatas {
  138. if metadataCount.ocId == metadata.ocId {
  139. return
  140. }
  141. }
  142. self.metadatas.append(metadata)
  143. createMetadatas(metadatasSource: metadatas)
  144. }
  145. func getIndexMetadata(ocId: String) -> Int? {
  146. var index: Int = 0
  147. for metadataCount in metadatas {
  148. if metadataCount.ocId == ocId {
  149. return index
  150. }
  151. index += 1
  152. }
  153. return nil
  154. }
  155. // MARK: -
  156. @objc func cellForItemAt(indexPath: IndexPath) -> tableMetadata? {
  157. let row = indexPath.row
  158. if row > self.metadatas.count - 1 {
  159. return nil
  160. } else {
  161. return metadatas[row]
  162. }
  163. }
  164. @objc func numberOfItemsInSection(section: Int) -> Int {
  165. return metadatas.count
  166. }
  167. }