NCDataSource.swift 9.8 KB

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