NCDataSource.swift 9.6 KB

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