NCDataSource.swift 18 KB

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