NCDataSource.swift 21 KB

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