NCDataSource.swift 20 KB

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