NCDataSource.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 UIKit
  24. class NCDataSource: NSObject {
  25. public var metadatasForSection: [NCMetadatasForSection] = []
  26. public var metadatasSource: [tableMetadata] = []
  27. public var metadataShare: [String: tableShare] = [:]
  28. public var metadataOffLine: [String] = []
  29. public var sections: [String] = []
  30. private var ascending: Bool = true
  31. private var sort: String = ""
  32. private var directoryOnTop: Bool = true
  33. private var favoriteOnTop: Bool = true
  34. private var filterLivePhoto: Bool = true
  35. private var groupByField: String = ""
  36. override init() {
  37. super.init()
  38. }
  39. init(metadatasSource: [tableMetadata], sort: String? = "none", ascending: Bool? = false, directoryOnTop: Bool? = true, favoriteOnTop: Bool? = true, filterLivePhoto: Bool? = true, groupByField: String = "name") {
  40. super.init()
  41. self.metadatasSource = metadatasSource
  42. self.sort = sort ?? "none"
  43. self.ascending = ascending ?? false
  44. self.directoryOnTop = directoryOnTop ?? true
  45. self.favoriteOnTop = favoriteOnTop ?? true
  46. self.filterLivePhoto = filterLivePhoto ?? true
  47. self.groupByField = groupByField
  48. // Create sections && sorted
  49. self.sections = self.metadatasSource.map { getSectionField(metadata: $0) }
  50. self.sections = self.sections.sorted {
  51. if self.ascending {
  52. return $0 < $1
  53. } else {
  54. return $0 > $1
  55. }
  56. }
  57. // Create metadataForSection
  58. for section in self.sections {
  59. let metadatas = self.metadatasSource.filter({ getSectionField(metadata: $0) == section})
  60. let metadataForSection = NCMetadatasForSection.init(section: section, metadatas: metadatas, sort: self.sort, ascending: self.ascending, directoryOnTop: self.directoryOnTop, favoriteOnTop: self.favoriteOnTop, filterLivePhoto: self.filterLivePhoto)
  61. metadatasForSection.append(metadataForSection)
  62. }
  63. }
  64. // MARK: -
  65. func getFilesInformation() -> (directories: Int, files: Int, size: Int64) {
  66. var directories: Int = 0
  67. var files: Int = 0
  68. var size: Int64 = 0
  69. for metadata in self.metadatasSource {
  70. if metadata.directory {
  71. directories += 1
  72. } else {
  73. files += 1
  74. }
  75. size += metadata.size
  76. }
  77. return (directories, files, size)
  78. }
  79. func deleteMetadata(ocId: String) -> IndexPath? {
  80. if let indexPath = self.getIndexPathMetadata(ocId: ocId) {
  81. self.metadatasSource.remove(at: indexPath.row)
  82. return indexPath
  83. }
  84. return nil
  85. }
  86. @discardableResult
  87. func reloadMetadata(ocId: String, ocIdTemp: String? = nil) -> IndexPath? {
  88. var indexPath: IndexPath?
  89. if let ocIdTemp = ocIdTemp {
  90. indexPath = self.getIndexPathMetadata(ocId: ocIdTemp)
  91. } else {
  92. indexPath = self.getIndexPathMetadata(ocId: ocId)
  93. }
  94. guard let indexPath = indexPath, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return nil }
  95. self.metadatasSource[indexPath.row] = metadata
  96. if CCUtility.fileProviderStorageExists(metadata) {
  97. let tableLocalFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  98. if tableLocalFile?.offline ?? false {
  99. metadataOffLine.append(metadata.ocId)
  100. }
  101. }
  102. return indexPath
  103. }
  104. @discardableResult
  105. func addMetadata(_ metadata: tableMetadata) -> IndexPath? {
  106. var row: Int = 0
  107. // Already exists
  108. for metadataCount in self.metadatasSource {
  109. if metadataCount.fileNameView == metadata.fileNameView || metadataCount.ocId == metadata.ocId {
  110. self.metadatasSource[row] = metadata
  111. let section = getSection(metadata: metadata)
  112. return IndexPath(row: row, section: section)
  113. }
  114. row += 1
  115. }
  116. // Append & rebuild
  117. self.metadatasSource.append(metadata)
  118. //createMetadatas()
  119. return getIndexPathMetadata(ocId: metadata.ocId)
  120. }
  121. func getIndexPathMetadata(ocId: String) -> IndexPath? {
  122. var row: Int = 0
  123. for metadata in self.metadatasSource {
  124. if metadata.ocId == ocId {
  125. let section = getSection(metadata: metadata)
  126. return IndexPath(row: row, section: section)
  127. }
  128. row += 1
  129. }
  130. return nil
  131. }
  132. func numberOfSections() -> Int {
  133. if sections.count == 0 {
  134. return 1
  135. } else {
  136. return sections.count
  137. }
  138. }
  139. func numberOfItemsInSection(_ section: Int) -> Int {
  140. if self.sections.count == 0 || self.metadatasSource.count == 0 { return 0 }
  141. let sectionName = self.sections[section]
  142. let metadatas = self.metadatasSource.filter({ getSectionField(metadata: $0) == sectionName})
  143. return metadatas.count
  144. }
  145. func cellForItemAt(indexPath: IndexPath) -> tableMetadata? {
  146. let row = indexPath.row
  147. let sectionName = self.sections[indexPath.section]
  148. let metadatas = self.metadatasSource.filter({ getSectionField(metadata: $0) == sectionName})
  149. if row > metadatas.count - 1 {
  150. return nil
  151. } else {
  152. return metadatas[row]
  153. }
  154. }
  155. internal func getSection(metadata: tableMetadata) -> Int {
  156. let section = self.sections.firstIndex(where: {$0 == getSectionField(metadata: metadata)}) ?? 0
  157. return section
  158. }
  159. internal func getSectionField(metadata: tableMetadata) -> String {
  160. switch self.groupByField {
  161. case "name":
  162. return metadata.name
  163. default:
  164. return metadata.name
  165. }
  166. }
  167. }
  168. class NCMetadatasForSection: NSObject {
  169. var section: String
  170. var metadatas: [tableMetadata]
  171. var sort : String
  172. var ascending: Bool
  173. var directoryOnTop: Bool
  174. var favoriteOnTop: Bool
  175. var filterLivePhoto: Bool
  176. var metadatasSourceSorted: [tableMetadata] = []
  177. var metadatasFavoriteDirectory: [tableMetadata] = []
  178. var metadatasFavoriteFile: [tableMetadata] = []
  179. var metadatasDirectory: [tableMetadata] = []
  180. var metadatasFile: [tableMetadata] = []
  181. var metadataShare: [String: tableShare] = [:]
  182. var metadataOffLine: [String] = []
  183. init(section: String, metadatas: [tableMetadata], sort: String, ascending: Bool, directoryOnTop: Bool, favoriteOnTop: Bool, filterLivePhoto: Bool) {
  184. self.section = section
  185. self.metadatas = metadatas
  186. self.sort = sort
  187. self.ascending = ascending
  188. self.directoryOnTop = directoryOnTop
  189. self.favoriteOnTop = favoriteOnTop
  190. self.filterLivePhoto = filterLivePhoto
  191. super.init()
  192. createMetadatasForSection()
  193. }
  194. private func createMetadatasForSection() {
  195. /*
  196. Metadata order
  197. */
  198. if sort != "none" && sort != "" {
  199. metadatasSourceSorted = metadatas.sorted {
  200. switch sort {
  201. case "date":
  202. if ascending {
  203. return ($0.date as Date) < ($1.date as Date)
  204. } else {
  205. return ($0.date as Date) > ($1.date as Date)
  206. }
  207. case "size":
  208. if ascending {
  209. return $0.size < $1.size
  210. } else {
  211. return $0.size > $1.size
  212. }
  213. default:
  214. if ascending {
  215. return $0.fileNameView.lowercased() < $1.fileNameView.lowercased()
  216. } else {
  217. return $0.fileNameView.lowercased() > $1.fileNameView.lowercased()
  218. }
  219. }
  220. }
  221. } else {
  222. metadatasSourceSorted = metadatas
  223. }
  224. /*
  225. Initialize datasource
  226. */
  227. for metadata in metadatasSourceSorted {
  228. // skipped the root file
  229. if metadata.fileName == "." || metadata.serverUrl == ".." {
  230. continue
  231. }
  232. // skipped livePhoto
  233. if metadata.ext == "mov" && metadata.livePhoto && filterLivePhoto {
  234. continue
  235. }
  236. // share
  237. let shares = NCManageDatabase.shared.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  238. if shares.count > 0 {
  239. metadataShare[metadata.ocId] = shares.first
  240. }
  241. // is Local / offline
  242. if !metadata.directory, CCUtility.fileProviderStorageExists(metadata) {
  243. let tableLocalFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  244. if tableLocalFile == nil {
  245. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  246. }
  247. if tableLocalFile?.offline ?? false {
  248. metadataOffLine.append(metadata.ocId)
  249. }
  250. }
  251. // Organized the metadata
  252. if metadata.favorite && favoriteOnTop {
  253. if metadata.directory {
  254. metadatasFavoriteDirectory.append(metadata)
  255. } else {
  256. metadatasFavoriteFile.append(metadata)
  257. }
  258. } else if metadata.directory && directoryOnTop {
  259. metadatasDirectory.append(metadata)
  260. } else {
  261. metadatasFile.append(metadata)
  262. }
  263. }
  264. metadatas.removeAll()
  265. // Struct view : favorite dir -> favorite file -> directory -> files
  266. metadatas += metadatasFavoriteDirectory
  267. metadatas += metadatasFavoriteFile
  268. metadatas += metadatasDirectory
  269. metadatas += metadatasFile
  270. }
  271. }