NCDataSource.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. class NCDataSource: NSObject {
  25. public var metadatas: [tableMetadata] = []
  26. public var metadataShare: [String:tableShare] = [:]
  27. public var metadataLocalImage: [String:String] = [:]
  28. private var ascending: Bool = true
  29. private var sort: String = ""
  30. private var directoryOnTop: Bool = true
  31. private var favoriteOnTop: Bool = true
  32. private var filterLivePhoto: Bool = true
  33. override init() {
  34. super.init()
  35. }
  36. init(metadatasSource: [tableMetadata], sort: String, ascending: Bool, directoryOnTop: Bool, favoriteOnTop: Bool, filterLivePhoto: Bool) {
  37. super.init()
  38. self.sort = sort
  39. self.ascending = ascending
  40. self.directoryOnTop = directoryOnTop
  41. self.favoriteOnTop = favoriteOnTop
  42. self.filterLivePhoto = filterLivePhoto
  43. createMetadatas(metadatasSource: metadatasSource)
  44. }
  45. // MARK: -
  46. private func createMetadatas(metadatasSource: [tableMetadata]) {
  47. var metadatasSourceSorted: [tableMetadata] = []
  48. var metadataFavoriteDirectory: [tableMetadata] = []
  49. var metadataFavoriteFile: [tableMetadata] = []
  50. var metadataDirectory: [tableMetadata] = []
  51. var metadataFile: [tableMetadata] = []
  52. if sort == "fileName" || sort == "fileNameView" {
  53. metadatasSourceSorted = metadatasSource.sorted { (metadata1:tableMetadata, metadata2:tableMetadata) -> Bool in
  54. if ascending {
  55. return metadata1.fileNameView.localizedStandardCompare(metadata2.fileNameView) == ComparisonResult.orderedAscending
  56. } else {
  57. return metadata1.fileNameView.localizedStandardCompare(metadata2.fileNameView) == ComparisonResult.orderedDescending
  58. }
  59. }
  60. } else {
  61. metadatasSourceSorted = metadatasSource
  62. }
  63. for metadata in metadatasSourceSorted {
  64. // skipped the root file
  65. if metadata.fileName == "." || metadata.serverUrl == ".." {
  66. continue
  67. }
  68. // skipped livePhoto
  69. if metadata.ext == "mov" && metadata.livePhoto && filterLivePhoto {
  70. continue
  71. }
  72. // share
  73. let shares = NCManageDatabase.sharedInstance.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  74. if shares.count > 0 {
  75. metadataShare[metadata.ocId] = shares.first
  76. }
  77. // is Local / offline
  78. if !metadata.directory {
  79. let size = CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView)
  80. if size > 0 {
  81. let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  82. if tableLocalFile == nil && size == metadata.size {
  83. NCManageDatabase.sharedInstance.addLocalFile(metadata: metadata)
  84. }
  85. if tableLocalFile?.offline ?? false {
  86. metadataLocalImage[metadata.ocId] = "offlineFlag"
  87. } else {
  88. metadataLocalImage[metadata.ocId] = "local"
  89. }
  90. }
  91. }
  92. // Organized the metadata
  93. if metadata.favorite && favoriteOnTop {
  94. if metadata.directory {
  95. metadataFavoriteDirectory.append(metadata)
  96. } else {
  97. metadataFavoriteFile.append(metadata)
  98. }
  99. } else if metadata.directory && directoryOnTop {
  100. metadataDirectory.append(metadata)
  101. } else {
  102. metadataFile.append(metadata)
  103. }
  104. }
  105. metadatas.removeAll()
  106. metadatas += metadataFavoriteDirectory
  107. metadatas += metadataFavoriteFile
  108. metadatas += metadataDirectory
  109. metadatas += metadataFile
  110. }
  111. // MARK: -
  112. func getFilesInformation() -> (directories: Int, files: Int, size: Double) {
  113. var directories: Int = 0
  114. var files: Int = 0
  115. var size: Double = 0
  116. for metadata in metadatas {
  117. if metadata.directory {
  118. directories += 1
  119. } else {
  120. files += 1
  121. }
  122. size = size + metadata.size
  123. }
  124. return (directories, files, size)
  125. }
  126. func deleteMetadata(ocId: String) -> Int? {
  127. if let index = self.getIndexMetadata(ocId: ocId) {
  128. metadatas.remove(at: index)
  129. return index
  130. }
  131. return nil
  132. }
  133. @discardableResult
  134. func reloadMetadata(ocId: String, ocIdTemp: String? = nil) -> Int? {
  135. var index: Int?
  136. if ocIdTemp != nil {
  137. index = self.getIndexMetadata(ocId: ocIdTemp!)
  138. } else {
  139. index = self.getIndexMetadata(ocId: ocId)
  140. }
  141. if index != nil {
  142. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  143. metadatas[index!] = metadata
  144. }
  145. }
  146. return index
  147. }
  148. func addMetadata(_ metadata: tableMetadata) -> Int? {
  149. var index: Int = 0
  150. // Already exists
  151. for metadataCount in metadatas {
  152. if metadataCount.fileNameView == metadata.fileNameView || metadataCount.ocId == metadata.ocId {
  153. metadatas[index] = metadata
  154. return index
  155. }
  156. index += 1
  157. }
  158. // Append & rebuild
  159. metadatas.append(metadata)
  160. createMetadatas(metadatasSource: metadatas)
  161. return getIndexMetadata(ocId: metadata.ocId)
  162. }
  163. func getIndexMetadata(ocId: String) -> Int? {
  164. var index: Int = 0
  165. for metadataCount in metadatas {
  166. if metadataCount.ocId == ocId {
  167. return index
  168. }
  169. index += 1
  170. }
  171. return nil
  172. }
  173. func numberOfItems() -> Int {
  174. return metadatas.count
  175. }
  176. func cellForItemAt(indexPath: IndexPath) -> tableMetadata? {
  177. let row = indexPath.row
  178. if row > metadatas.count - 1 {
  179. return nil
  180. } else {
  181. return metadatas[row]
  182. }
  183. }
  184. }