NCDataSource.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. return numberOfSections == self.numberOfSections()
  230. }
  231. func numberOfSections() -> Int {
  232. if self.metadatasForSection.count == 0 {
  233. return 1
  234. } else {
  235. return self.metadatasForSection.count
  236. }
  237. }
  238. func numberOfItemsInSection(_ section: Int) -> Int {
  239. if self.metadatasForSection.count == 0 || self.metadatasSource.count == 0 { return 0 }
  240. return self.metadatasForSection[section].metadatas.count
  241. }
  242. func cellForItemAt(indexPath: IndexPath) -> tableMetadata? {
  243. let metadatasForSection = self.metadatasForSection[indexPath.section]
  244. return metadatasForSection.metadatas[indexPath.row]
  245. }
  246. func getSectionValue(indexPath: IndexPath) -> String {
  247. if metadatasForSection.count == 0 { return "" }
  248. let metadataForSection = self.metadatasForSection[indexPath.section]
  249. return metadataForSection.sectionValue
  250. }
  251. func getFooterInformation() -> (directories: Int, files: Int, size: Int64) {
  252. var directories: Int = 0
  253. var files: Int = 0
  254. var size: Int64 = 0
  255. for metadataForSection in metadatasForSection {
  256. directories += metadataForSection.numDirectory
  257. files += metadataForSection.numFile
  258. size += metadataForSection.totalSize
  259. }
  260. return (directories, files, size)
  261. }
  262. internal func getSectionValue(metadata: tableMetadata) -> String {
  263. switch self.groupByField {
  264. case "name":
  265. return metadata.name
  266. case "classFile":
  267. return metadata.classFile
  268. default:
  269. return metadata.name
  270. }
  271. }
  272. }
  273. class NCMetadatasForSection: NSObject {
  274. var sectionValue: String
  275. var metadatas: [tableMetadata]
  276. var shares: [tableShare]
  277. var localFiles: [tableLocalFile]
  278. private var sort : String
  279. private var ascending: Bool
  280. private var directoryOnTop: Bool
  281. private var favoriteOnTop: Bool
  282. private var filterLivePhoto: Bool
  283. private var metadatasSourceSorted: [tableMetadata] = []
  284. private var metadatasFavoriteDirectory: [tableMetadata] = []
  285. private var metadatasFavoriteFile: [tableMetadata] = []
  286. private var metadatasDirectory: [tableMetadata] = []
  287. private var metadatasFile: [tableMetadata] = []
  288. public var numDirectory: Int = 0
  289. public var numFile: Int = 0
  290. public var totalSize: Int64 = 0
  291. public var metadataShare: [String: tableShare] = [:]
  292. public var metadataOffLine: [String] = []
  293. init(sectionValue: String, metadatas: [tableMetadata], shares: [tableShare], localFiles: [tableLocalFile], sort: String, ascending: Bool, directoryOnTop: Bool, favoriteOnTop: Bool, filterLivePhoto: Bool) {
  294. self.sectionValue = sectionValue
  295. self.metadatas = metadatas
  296. self.shares = shares
  297. self.localFiles = localFiles
  298. self.sort = sort
  299. self.ascending = ascending
  300. self.directoryOnTop = directoryOnTop
  301. self.favoriteOnTop = favoriteOnTop
  302. self.filterLivePhoto = filterLivePhoto
  303. super.init()
  304. createMetadatasForSection()
  305. }
  306. func createMetadatasForSection() {
  307. // Clear
  308. //
  309. metadatasSourceSorted.removeAll()
  310. metadatasFavoriteDirectory.removeAll()
  311. metadatasFavoriteFile.removeAll()
  312. metadatasDirectory.removeAll()
  313. metadatasFile.removeAll()
  314. metadataShare.removeAll()
  315. metadataOffLine.removeAll()
  316. numDirectory = 0
  317. numFile = 0
  318. totalSize = 0
  319. // Metadata order
  320. //
  321. if sort != "none" && sort != "" {
  322. metadatasSourceSorted = metadatas.sorted {
  323. switch sort {
  324. case "date":
  325. if ascending {
  326. return ($0.date as Date) < ($1.date as Date)
  327. } else {
  328. return ($0.date as Date) > ($1.date as Date)
  329. }
  330. case "size":
  331. if ascending {
  332. return $0.size < $1.size
  333. } else {
  334. return $0.size > $1.size
  335. }
  336. default:
  337. if ascending {
  338. return $0.fileNameView.lowercased() < $1.fileNameView.lowercased()
  339. } else {
  340. return $0.fileNameView.lowercased() > $1.fileNameView.lowercased()
  341. }
  342. }
  343. }
  344. } else {
  345. metadatasSourceSorted = metadatas
  346. }
  347. // Initialize datasource
  348. //
  349. for metadata in metadatasSourceSorted {
  350. // skipped the root file
  351. if metadata.fileName == "." || metadata.serverUrl == ".." {
  352. continue
  353. }
  354. // skipped livePhoto
  355. if filterLivePhoto && metadata.livePhoto && metadata.ext == "mov" {
  356. continue
  357. }
  358. // share
  359. if let share = self.shares.filter({ $0.serverUrl == metadata.serverUrl && $0.fileName == metadata.fileName }).first {
  360. metadataShare[metadata.ocId] = share
  361. }
  362. // is Local / offline
  363. if !metadata.directory, CCUtility.fileProviderStorageExists(metadata) {
  364. let localFile = self.localFiles.filter({ $0.ocId == metadata.ocId }).first
  365. if localFile == nil {
  366. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  367. }
  368. if localFile?.offline ?? false {
  369. metadataOffLine.append(metadata.ocId)
  370. }
  371. }
  372. // Organized the metadata
  373. if metadata.favorite && favoriteOnTop {
  374. if metadata.directory {
  375. metadatasFavoriteDirectory.append(metadata)
  376. } else {
  377. metadatasFavoriteFile.append(metadata)
  378. }
  379. } else if metadata.directory && directoryOnTop {
  380. metadatasDirectory.append(metadata)
  381. } else {
  382. metadatasFile.append(metadata)
  383. }
  384. //Info
  385. if metadata.directory {
  386. numDirectory += 1
  387. } else {
  388. numFile += 1
  389. totalSize += metadata.size
  390. }
  391. }
  392. metadatas.removeAll()
  393. // Struct view : favorite dir -> favorite file -> directory -> files
  394. metadatas += metadatasFavoriteDirectory
  395. metadatas += metadatasFavoriteFile
  396. metadatas += metadatasDirectory
  397. metadatas += metadatasFile
  398. }
  399. }