NCDataSource.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. import NCCommunication
  25. class NCDataSource: NSObject {
  26. public var metadatasSource: [tableMetadata] = []
  27. public var metadatasForSection: [NCMetadatasForSection] = []
  28. private var sectionsValue: [String] = []
  29. private var providers: [NCCSearchProvider]?
  30. private var shares: [tableShare] = []
  31. private var localFiles: [tableLocalFile] = []
  32. private var ascending: Bool = true
  33. private var sort: String = ""
  34. private var directoryOnTop: Bool = true
  35. private var favoriteOnTop: Bool = true
  36. private var filterLivePhoto: Bool = true
  37. private var groupByField: String = ""
  38. override init() {
  39. super.init()
  40. }
  41. init(metadatasSource: [tableMetadata], account: String, sort: String? = "none", ascending: Bool? = false, directoryOnTop: Bool? = true, favoriteOnTop: Bool? = true, filterLivePhoto: Bool? = true, groupByField: String = "name", providers: [NCCSearchProvider]? = nil) {
  42. super.init()
  43. self.metadatasSource = metadatasSource
  44. self.shares = NCManageDatabase.shared.getTableShares(account: account)
  45. self.localFiles = NCManageDatabase.shared.getTableLocalFile(account: account)
  46. self.sort = sort ?? "none"
  47. self.ascending = ascending ?? false
  48. self.directoryOnTop = directoryOnTop ?? true
  49. self.favoriteOnTop = favoriteOnTop ?? true
  50. self.filterLivePhoto = filterLivePhoto ?? true
  51. self.groupByField = groupByField
  52. self.providers = providers
  53. createSections()
  54. for sectionValue in self.sectionsValue {
  55. createMetadataForSection(sectionValue: sectionValue)
  56. }
  57. }
  58. // MARK: -
  59. func clearDataSource() {
  60. self.metadatasSource.removeAll()
  61. self.metadatasForSection.removeAll()
  62. self.sectionsValue.removeAll()
  63. }
  64. internal func createSections() {
  65. self.sectionsValue = metadatasSource.map { getSectionValue(metadata: $0) }
  66. self.sectionsValue = Array(Set(self.sectionsValue))
  67. if let providers = self.providers , !providers.isEmpty {
  68. var sectionsDictionary: [String:Int] = [:]
  69. for section in self.sectionsValue {
  70. if let provider = providers.filter({ $0.name.lowercased() == section.lowercased()}).first {
  71. sectionsDictionary[section] = provider.order
  72. }
  73. }
  74. self.sectionsValue.removeAll()
  75. let sectionsDictionarySorted = sectionsDictionary.sorted(by: { $0.value < $1.value } )
  76. for section in sectionsDictionarySorted {
  77. if section.key == NCGlobal.shared.appName {
  78. self.sectionsValue.insert(section.key, at: 0)
  79. } else {
  80. self.sectionsValue.append(section.key)
  81. }
  82. }
  83. } else {
  84. self.sectionsValue = self.sectionsValue.sorted {
  85. if directoryOnTop && $0.lowercased() == "directory" {
  86. return true
  87. }
  88. if self.ascending {
  89. return $0 < $1
  90. } else {
  91. return $0 > $1
  92. }
  93. }
  94. }
  95. }
  96. internal func createMetadataForSection(sectionValue: String) {
  97. let metadatas = metadatasSource.filter({ getSectionValue(metadata: $0) == sectionValue})
  98. let metadataForSection = NCMetadatasForSection.init(sectionValue: sectionValue,
  99. metadatas: metadatas,
  100. shares: self.shares,
  101. localFiles: self.localFiles,
  102. sort: self.sort,
  103. ascending: self.ascending,
  104. directoryOnTop: self.directoryOnTop,
  105. favoriteOnTop: self.favoriteOnTop,
  106. filterLivePhoto: self.filterLivePhoto)
  107. metadatasForSection.append(metadataForSection)
  108. }
  109. // MARK: -
  110. @discardableResult
  111. func addMetadata(_ metadata: tableMetadata) -> (indexPath: IndexPath?, sameSections: Bool) {
  112. let numberOfSections = self.numberOfSections()
  113. // ADD metadatasSource
  114. if let rowIndex = self.metadatasSource.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
  115. self.metadatasSource[rowIndex] = metadata
  116. } else {
  117. self.metadatasSource.append(metadata)
  118. }
  119. // ADD metadataForSection
  120. if let sectionIndex = self.sectionsValue.firstIndex(where: {$0 == self.getSectionValue(metadata: metadata) }) {
  121. let metadataForSection = metadatasForSection[sectionIndex]
  122. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
  123. metadataForSection.metadatas[rowIndex] = metadata
  124. return (IndexPath(row: rowIndex, section: sectionIndex), self.isSameNumbersOfSections(numberOfSections: numberOfSections))
  125. } else {
  126. metadataForSection.metadatas.append(metadata)
  127. metadataForSection.createMetadatasForSection()
  128. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) {
  129. return (IndexPath(row: rowIndex, section: sectionIndex), self.isSameNumbersOfSections(numberOfSections: numberOfSections))
  130. }
  131. return (nil, self.isSameNumbersOfSections(numberOfSections: numberOfSections))
  132. }
  133. } else {
  134. // NEW section
  135. createSections()
  136. let sectionValue = getSectionValue(metadata: metadata)
  137. createMetadataForSection(sectionValue: sectionValue)
  138. // get IndexPath of new section
  139. if let sectionIndex = self.sectionsValue.firstIndex(where: {$0 == sectionValue }) {
  140. let metadataForSection = metadatasForSection[sectionIndex]
  141. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
  142. return (IndexPath(row: rowIndex, section: sectionIndex), self.isSameNumbersOfSections(numberOfSections: numberOfSections))
  143. }
  144. }
  145. }
  146. return (nil, self.isSameNumbersOfSections(numberOfSections: numberOfSections))
  147. }
  148. func deleteMetadata(ocId: String) -> (indexPath: IndexPath?, sameSections: Bool) {
  149. let numberOfSections = self.numberOfSections()
  150. var indexPathReturn: IndexPath?
  151. var removeMetadataForSection = false
  152. var sectionValue = ""
  153. // DELETE metadataForSection (IMPORTANT FIRST)
  154. let (indexPath, metadataForSection) = self.getIndexPathMetadata(ocId: ocId)
  155. if let indexPath = indexPath, let metadataForSection = metadataForSection {
  156. metadataForSection.metadatas.remove(at: indexPath.row)
  157. if metadataForSection.metadatas.count == 0 {
  158. sectionValue = metadataForSection.sectionValue
  159. removeMetadataForSection = true
  160. } else {
  161. metadataForSection.createMetadatasForSection()
  162. }
  163. indexPathReturn = indexPath
  164. }
  165. // DELETE metadatasSource (IMPORTANT LAST)
  166. if let rowIndex = self.metadatasSource.firstIndex(where: {$0.ocId == ocId}) {
  167. self.metadatasSource.remove(at: rowIndex)
  168. }
  169. // REMOVE sectionsValue / metadatasForSection
  170. if removeMetadataForSection {
  171. if let index = self.sectionsValue.firstIndex(where: {$0 == sectionValue }) {
  172. self.sectionsValue.remove(at: index)
  173. }
  174. if let index = self.metadatasForSection.firstIndex(where: {$0.sectionValue == sectionValue }) {
  175. self.metadatasForSection.remove(at: index)
  176. }
  177. }
  178. return (indexPathReturn, self.isSameNumbersOfSections(numberOfSections: numberOfSections))
  179. }
  180. @discardableResult
  181. func reloadMetadata(ocId: String, ocIdTemp: String? = nil) -> (indexPath: IndexPath?, sameSections: Bool) {
  182. let numberOfSections = self.numberOfSections()
  183. var ocIdSearch = ocId
  184. var indexPath: IndexPath?
  185. var metadataForSection: NCMetadatasForSection?
  186. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return (nil, self.isSameNumbersOfSections(numberOfSections: numberOfSections)) }
  187. if let ocIdTemp = ocIdTemp {
  188. ocIdSearch = ocIdTemp
  189. }
  190. // UPDATE metadataForSection (IMPORTANT FIRST)
  191. (indexPath, metadataForSection) = self.getIndexPathMetadata(ocId: ocIdSearch)
  192. if let indexPath = indexPath, let metadataForSection = metadataForSection {
  193. metadataForSection.metadatas[indexPath.row] = metadata
  194. metadataForSection.createMetadatasForSection()
  195. }
  196. // UPDATE metadatasSource (IMPORTANT LAST)
  197. if let rowIndex = self.metadatasSource.firstIndex(where: {$0.ocId == ocIdSearch}) {
  198. self.metadatasSource[rowIndex] = metadata
  199. }
  200. return (indexPath, self.isSameNumbersOfSections(numberOfSections: numberOfSections))
  201. }
  202. // MARK: -
  203. func getIndexPathMetadata(ocId: String) -> (indexPath: IndexPath?, metadataForSection: NCMetadatasForSection?) {
  204. if let metadata = metadatasSource.filter({ $0.ocId == ocId}).first {
  205. let sectionValue = getSectionValue(metadata: metadata)
  206. if let sectionIndex = self.sectionsValue.firstIndex(where: {$0 == sectionValue}) {
  207. for metadataForSection in self.metadatasForSection {
  208. if metadataForSection.sectionValue == sectionValue {
  209. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.ocId == ocId}) {
  210. return (IndexPath(row: rowIndex, section: sectionIndex), metadataForSection)
  211. }
  212. }
  213. }
  214. }
  215. }
  216. return (nil, nil)
  217. }
  218. func isSameNumbersOfSections(numberOfSections: Int) -> Bool {
  219. return numberOfSections == self.numberOfSections()
  220. }
  221. func numberOfSections() -> Int {
  222. if self.metadatasForSection.count == 0 {
  223. return 1
  224. } else {
  225. return self.metadatasForSection.count
  226. }
  227. }
  228. func numberOfItemsInSection(_ section: Int) -> Int {
  229. if self.metadatasForSection.count == 0 || self.metadatasSource.count == 0 { return 0 }
  230. return self.metadatasForSection[section].metadatas.count
  231. }
  232. func cellForItemAt(indexPath: IndexPath) -> tableMetadata? {
  233. let metadatasForSection = self.metadatasForSection[indexPath.section]
  234. return metadatasForSection.metadatas[indexPath.row]
  235. }
  236. func getSectionValue(indexPath: IndexPath) -> String {
  237. if metadatasForSection.count == 0 { return "" }
  238. let metadataForSection = self.metadatasForSection[indexPath.section]
  239. return metadataForSection.sectionValue
  240. }
  241. func getFooterInformation() -> (directories: Int, files: Int, size: Int64) {
  242. var directories: Int = 0
  243. var files: Int = 0
  244. var size: Int64 = 0
  245. for metadataForSection in metadatasForSection {
  246. directories += metadataForSection.numDirectory
  247. files += metadataForSection.numFile
  248. size += metadataForSection.totalSize
  249. }
  250. return (directories, files, size)
  251. }
  252. internal func getSectionValue(metadata: tableMetadata) -> String {
  253. switch self.groupByField {
  254. case "name":
  255. return metadata.name
  256. case "classFile":
  257. return metadata.classFile
  258. default:
  259. return metadata.name
  260. }
  261. }
  262. }
  263. class NCMetadatasForSection: NSObject {
  264. var sectionValue: String
  265. var metadatas: [tableMetadata]
  266. var shares: [tableShare]
  267. var localFiles: [tableLocalFile]
  268. private var sort : String
  269. private var ascending: Bool
  270. private var directoryOnTop: Bool
  271. private var favoriteOnTop: Bool
  272. private var filterLivePhoto: Bool
  273. private var metadatasSourceSorted: [tableMetadata] = []
  274. private var metadatasFavoriteDirectory: [tableMetadata] = []
  275. private var metadatasFavoriteFile: [tableMetadata] = []
  276. private var metadatasDirectory: [tableMetadata] = []
  277. private var metadatasFile: [tableMetadata] = []
  278. public var numDirectory: Int = 0
  279. public var numFile: Int = 0
  280. public var totalSize: Int64 = 0
  281. public var metadataShare: [String: tableShare] = [:]
  282. public var metadataOffLine: [String] = []
  283. init(sectionValue: String, metadatas: [tableMetadata], shares: [tableShare], localFiles: [tableLocalFile], sort: String, ascending: Bool, directoryOnTop: Bool, favoriteOnTop: Bool, filterLivePhoto: Bool) {
  284. self.sectionValue = sectionValue
  285. self.metadatas = metadatas
  286. self.shares = shares
  287. self.localFiles = localFiles
  288. self.sort = sort
  289. self.ascending = ascending
  290. self.directoryOnTop = directoryOnTop
  291. self.favoriteOnTop = favoriteOnTop
  292. self.filterLivePhoto = filterLivePhoto
  293. super.init()
  294. createMetadatasForSection()
  295. }
  296. func createMetadatasForSection() {
  297. // Clear
  298. //
  299. metadatasSourceSorted.removeAll()
  300. metadatasFavoriteDirectory.removeAll()
  301. metadatasFavoriteFile.removeAll()
  302. metadatasDirectory.removeAll()
  303. metadatasFile.removeAll()
  304. metadataShare.removeAll()
  305. metadataOffLine.removeAll()
  306. numDirectory = 0
  307. numFile = 0
  308. totalSize = 0
  309. // Metadata order
  310. //
  311. if sort != "none" && sort != "" {
  312. metadatasSourceSorted = metadatas.sorted {
  313. switch sort {
  314. case "date":
  315. if ascending {
  316. return ($0.date as Date) < ($1.date as Date)
  317. } else {
  318. return ($0.date as Date) > ($1.date as Date)
  319. }
  320. case "size":
  321. if ascending {
  322. return $0.size < $1.size
  323. } else {
  324. return $0.size > $1.size
  325. }
  326. default:
  327. if ascending {
  328. return $0.fileNameView.lowercased() < $1.fileNameView.lowercased()
  329. } else {
  330. return $0.fileNameView.lowercased() > $1.fileNameView.lowercased()
  331. }
  332. }
  333. }
  334. } else {
  335. metadatasSourceSorted = metadatas
  336. }
  337. // Initialize datasource
  338. //
  339. for metadata in metadatasSourceSorted {
  340. // skipped the root file
  341. if metadata.fileName == "." || metadata.serverUrl == ".." {
  342. continue
  343. }
  344. // skipped livePhoto
  345. if metadata.ext == "mov" && metadata.livePhoto && filterLivePhoto {
  346. continue
  347. }
  348. // share
  349. if let share = self.shares.filter({ $0.serverUrl == metadata.serverUrl && $0.fileName == metadata.fileName }).first {
  350. metadataShare[metadata.ocId] = share
  351. }
  352. // is Local / offline
  353. if !metadata.directory, CCUtility.fileProviderStorageExists(metadata) {
  354. let localFile = self.localFiles.filter({ $0.ocId == metadata.ocId }).first
  355. if localFile == nil {
  356. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  357. }
  358. if localFile?.offline ?? false {
  359. metadataOffLine.append(metadata.ocId)
  360. }
  361. }
  362. // Organized the metadata
  363. if metadata.favorite && favoriteOnTop {
  364. if metadata.directory {
  365. metadatasFavoriteDirectory.append(metadata)
  366. } else {
  367. metadatasFavoriteFile.append(metadata)
  368. }
  369. } else if metadata.directory && directoryOnTop {
  370. metadatasDirectory.append(metadata)
  371. } else {
  372. metadatasFile.append(metadata)
  373. }
  374. //Info
  375. if metadata.directory {
  376. numDirectory += 1
  377. } else {
  378. numFile += 1
  379. totalSize += metadata.size
  380. }
  381. }
  382. metadatas.removeAll()
  383. // Struct view : favorite dir -> favorite file -> directory -> files
  384. metadatas += metadatasFavoriteDirectory
  385. metadatas += metadatasFavoriteFile
  386. metadatas += metadatasDirectory
  387. metadatas += metadatasFile
  388. }
  389. }