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. 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. if let providers = self.providers , !providers.isEmpty {
  76. var sectionsDictionary: [String:Int] = [:]
  77. for section in self.sectionsValue {
  78. if let provider = providers.filter({ $0.name.lowercased() == section.lowercased()}).first {
  79. sectionsDictionary[section] = provider.order
  80. }
  81. }
  82. self.sectionsValue.removeAll()
  83. let sectionsDictionarySorted = sectionsDictionary.sorted(by: { $0.value < $1.value } )
  84. for section in sectionsDictionarySorted {
  85. if section.key == NCGlobal.shared.appName {
  86. self.sectionsValue.insert(section.key, at: 0)
  87. } else {
  88. self.sectionsValue.append(section.key)
  89. }
  90. }
  91. } else {
  92. self.sectionsValue = self.sectionsValue.sorted {
  93. if directoryOnTop && $0.lowercased() == "directory" {
  94. return true
  95. } else if directoryOnTop && $1.lowercased() == "directory" {
  96. return false
  97. }
  98. if self.ascending {
  99. return $0 < $1
  100. } else {
  101. return $0 > $1
  102. }
  103. }
  104. }
  105. }
  106. internal func createMetadataForSection(sectionValue: String) {
  107. let metadatas = metadatasSource.filter({ getSectionValue(metadata: $0) == sectionValue})
  108. let metadataForSection = NCMetadatasForSection.init(sectionValue: sectionValue,
  109. metadatas: metadatas,
  110. shares: self.shares,
  111. localFiles: self.localFiles,
  112. sort: self.sort,
  113. ascending: self.ascending,
  114. directoryOnTop: self.directoryOnTop,
  115. favoriteOnTop: self.favoriteOnTop,
  116. filterLivePhoto: self.filterLivePhoto)
  117. metadatasForSection.append(metadataForSection)
  118. }
  119. // MARK: -
  120. @discardableResult
  121. func addMetadata(_ metadata: tableMetadata) -> (indexPath: IndexPath?, sameSections: Bool) {
  122. let numberOfSections = self.numberOfSections()
  123. // ADD metadatasSource
  124. if let rowIndex = self.metadatasSource.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
  125. self.metadatasSource[rowIndex] = metadata
  126. } else {
  127. self.metadatasSource.append(metadata)
  128. }
  129. // ADD metadataForSection
  130. if let sectionIndex = self.sectionsValue.firstIndex(where: {$0 == self.getSectionValue(metadata: metadata) }) {
  131. let metadataForSection = metadatasForSection[sectionIndex]
  132. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
  133. metadataForSection.metadatas[rowIndex] = metadata
  134. return (IndexPath(row: rowIndex, section: sectionIndex), self.isSameNumbersOfSections(numberOfSections: numberOfSections))
  135. } else {
  136. metadataForSection.metadatas.append(metadata)
  137. metadataForSection.createMetadatasForSection()
  138. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) {
  139. return (IndexPath(row: rowIndex, section: sectionIndex), self.isSameNumbersOfSections(numberOfSections: numberOfSections))
  140. }
  141. return (nil, self.isSameNumbersOfSections(numberOfSections: numberOfSections))
  142. }
  143. } else {
  144. // NEW section
  145. createSections()
  146. let sectionValue = getSectionValue(metadata: metadata)
  147. createMetadataForSection(sectionValue: sectionValue)
  148. // get IndexPath of new section
  149. if let sectionIndex = self.sectionsValue.firstIndex(where: {$0 == sectionValue }) {
  150. let metadataForSection = metadatasForSection[sectionIndex]
  151. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
  152. return (IndexPath(row: rowIndex, section: sectionIndex), self.isSameNumbersOfSections(numberOfSections: numberOfSections))
  153. }
  154. }
  155. }
  156. return (nil, self.isSameNumbersOfSections(numberOfSections: numberOfSections))
  157. }
  158. func deleteMetadata(ocId: String) -> (indexPath: IndexPath?, sameSections: Bool) {
  159. let numberOfSections = self.numberOfSections()
  160. var indexPathReturn: IndexPath?
  161. var removeMetadataForSection = false
  162. var sectionValue = ""
  163. // DELETE metadataForSection (IMPORTANT FIRST)
  164. let (indexPath, metadataForSection) = self.getIndexPathMetadata(ocId: ocId)
  165. if let indexPath = indexPath, let metadataForSection = metadataForSection {
  166. metadataForSection.metadatas.remove(at: indexPath.row)
  167. if metadataForSection.metadatas.count == 0 {
  168. sectionValue = metadataForSection.sectionValue
  169. removeMetadataForSection = true
  170. } else {
  171. metadataForSection.createMetadatasForSection()
  172. }
  173. indexPathReturn = indexPath
  174. }
  175. // DELETE metadatasSource (IMPORTANT LAST)
  176. if let rowIndex = self.metadatasSource.firstIndex(where: {$0.ocId == ocId}) {
  177. self.metadatasSource.remove(at: rowIndex)
  178. }
  179. // REMOVE sectionsValue / metadatasForSection
  180. if removeMetadataForSection {
  181. if let index = self.sectionsValue.firstIndex(where: {$0 == sectionValue }) {
  182. self.sectionsValue.remove(at: index)
  183. }
  184. if let index = self.metadatasForSection.firstIndex(where: {$0.sectionValue == sectionValue }) {
  185. self.metadatasForSection.remove(at: index)
  186. }
  187. }
  188. return (indexPathReturn, self.isSameNumbersOfSections(numberOfSections: numberOfSections))
  189. }
  190. @discardableResult
  191. func reloadMetadata(ocId: String, ocIdTemp: String? = nil) -> (indexPath: IndexPath?, sameSections: Bool) {
  192. let numberOfSections = self.numberOfSections()
  193. var ocIdSearch = ocId
  194. var indexPath: IndexPath?
  195. var metadataForSection: NCMetadatasForSection?
  196. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return (nil, self.isSameNumbersOfSections(numberOfSections: numberOfSections)) }
  197. if let ocIdTemp = ocIdTemp {
  198. ocIdSearch = ocIdTemp
  199. }
  200. // UPDATE metadataForSection (IMPORTANT FIRST)
  201. (indexPath, metadataForSection) = self.getIndexPathMetadata(ocId: ocIdSearch)
  202. if let indexPath = indexPath, let metadataForSection = metadataForSection {
  203. metadataForSection.metadatas[indexPath.row] = metadata
  204. metadataForSection.createMetadatasForSection()
  205. }
  206. // UPDATE metadatasSource (IMPORTANT LAST)
  207. if let rowIndex = self.metadatasSource.firstIndex(where: {$0.ocId == ocIdSearch}) {
  208. self.metadatasSource[rowIndex] = metadata
  209. }
  210. return (indexPath, self.isSameNumbersOfSections(numberOfSections: numberOfSections))
  211. }
  212. // MARK: -
  213. func getIndexPathMetadata(ocId: String) -> (indexPath: IndexPath?, metadataForSection: NCMetadatasForSection?) {
  214. if let metadata = metadatasSource.filter({ $0.ocId == ocId}).first {
  215. let sectionValue = getSectionValue(metadata: metadata)
  216. if let sectionIndex = self.sectionsValue.firstIndex(where: {$0 == sectionValue}) {
  217. for metadataForSection in self.metadatasForSection {
  218. if metadataForSection.sectionValue == sectionValue {
  219. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.ocId == ocId}) {
  220. return (IndexPath(row: rowIndex, section: sectionIndex), metadataForSection)
  221. }
  222. }
  223. }
  224. }
  225. }
  226. return (nil, nil)
  227. }
  228. func isSameNumbersOfSections(numberOfSections: Int) -> Bool {
  229. if self.metadatasForSection.count == 0 { return false }
  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. }