NCDataSource.swift 18 KB

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