NCDataSource.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. for section in sections {
  135. if !self.sections.contains(section) {
  136. self.sections.append(section)
  137. }
  138. }
  139. }
  140. // MARK: -
  141. func getFilesInformation() -> (directories: Int, files: Int, size: Int64) {
  142. var directories: Int = 0
  143. var files: Int = 0
  144. var size: Int64 = 0
  145. for metadata in metadatas {
  146. if metadata.directory {
  147. directories += 1
  148. } else {
  149. files += 1
  150. }
  151. size += metadata.size
  152. }
  153. return (directories, files, size)
  154. }
  155. func deleteMetadata(ocId: String) -> IndexPath? {
  156. if let indexPath = self.getIndexPathMetadata(ocId: ocId) {
  157. metadatas.remove(at: indexPath.row)
  158. return indexPath
  159. }
  160. return nil
  161. }
  162. @discardableResult
  163. func reloadMetadata(ocId: String, ocIdTemp: String? = nil) -> IndexPath? {
  164. var indexPath: IndexPath?
  165. if let ocIdTemp = ocIdTemp {
  166. indexPath = self.getIndexPathMetadata(ocId: ocIdTemp)
  167. } else {
  168. indexPath = self.getIndexPathMetadata(ocId: ocId)
  169. }
  170. guard let indexPath = indexPath, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return nil }
  171. metadatas[indexPath.row] = metadata
  172. if CCUtility.fileProviderStorageExists(metadata) {
  173. let tableLocalFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  174. if tableLocalFile?.offline ?? false {
  175. metadataOffLine.append(metadata.ocId)
  176. }
  177. }
  178. return indexPath
  179. }
  180. @discardableResult
  181. func addMetadata(_ metadata: tableMetadata) -> IndexPath? {
  182. var row: Int = 0
  183. // Already exists
  184. for metadataCount in self.metadatas {
  185. if metadataCount.fileNameView == metadata.fileNameView || metadataCount.ocId == metadata.ocId {
  186. metadatas[row] = metadata
  187. let section = getSection(metadata: metadata)
  188. return IndexPath(row: row, section: section)
  189. }
  190. row += 1
  191. }
  192. // Append & rebuild
  193. metadatas.append(metadata)
  194. createMetadatas(metadatasSource: metadatas)
  195. return getIndexPathMetadata(ocId: metadata.ocId)
  196. }
  197. func getIndexPathMetadata(ocId: String) -> IndexPath? {
  198. var row: Int = 0
  199. for metadata in self.metadatas {
  200. if metadata.ocId == ocId {
  201. let section = getSection(metadata: metadata)
  202. return IndexPath(row: row, section: section)
  203. }
  204. row += 1
  205. }
  206. return nil
  207. }
  208. func numberOfSections() -> Int {
  209. if sections.count == 0 {
  210. return 1
  211. } else {
  212. return sections.count
  213. }
  214. }
  215. func numberOfItemsInSection(_ section: Int) -> Int {
  216. if self.sections.count == 0 || metadatas.count == 0 { return 0 }
  217. let sectionName = self.sections[section]
  218. let metadatas = self.metadatas.filter({ getSectionField(metadata: $0) == sectionName})
  219. return metadatas.count
  220. }
  221. func cellForItemAt(indexPath: IndexPath) -> tableMetadata? {
  222. let row = indexPath.row
  223. let sectionName = self.sections[indexPath.section]
  224. let metadatas = self.metadatas.filter({ getSectionField(metadata: $0) == sectionName})
  225. if row > metadatas.count - 1 {
  226. return nil
  227. } else {
  228. return metadatas[row]
  229. }
  230. }
  231. internal func getSection(metadata: tableMetadata) -> Int {
  232. let section = self.sections.firstIndex(where: {$0 == getSectionField(metadata: metadata)}) ?? 0
  233. return section
  234. }
  235. internal func getSectionField(metadata: tableMetadata) -> String {
  236. switch self.groupByField {
  237. case "name":
  238. return metadata.name
  239. default:
  240. return metadata.name
  241. }
  242. }
  243. }