NCDataSource.swift 19 KB

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