NCDataSource.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. 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. if metadatasForSection.count == 0 || indexPath.section >= metadatasForSection.count {
  258. return nil
  259. }
  260. let metadatasForSection = self.metadatasForSection[indexPath.section]
  261. if indexPath.row >= metadatasForSection.metadatas.count {
  262. return nil
  263. }
  264. return metadatasForSection.metadatas[indexPath.row]
  265. }
  266. func getMetadataForSection(_ section: Int) -> NCMetadataForSection? {
  267. if metadatasForSection.count == 0 { return nil }
  268. return self.metadatasForSection[section]
  269. }
  270. func getSectionValue(indexPath: IndexPath) -> String {
  271. if metadatasForSection.count == 0 { return "" }
  272. let metadataForSection = self.metadatasForSection[indexPath.section]
  273. return metadataForSection.sectionValue
  274. }
  275. func getFooterInformation() -> (directories: Int, files: Int, size: Int64) {
  276. var directories: Int = 0
  277. var files: Int = 0
  278. var size: Int64 = 0
  279. for metadataForSection in metadatasForSection {
  280. directories += metadataForSection.numDirectory
  281. files += metadataForSection.numFile
  282. size += metadataForSection.totalSize
  283. }
  284. return (directories, files, size)
  285. }
  286. internal func getSectionValue(metadata: tableMetadata) -> String {
  287. switch self.groupByField {
  288. case "name":
  289. return NSLocalizedString(metadata.name, comment: "").lowercased().firstUppercased
  290. case "classFile":
  291. return NSLocalizedString(metadata.classFile, comment: "").lowercased().firstUppercased
  292. default:
  293. return NSLocalizedString(metadata.name, comment: "").lowercased().firstUppercased
  294. }
  295. }
  296. }
  297. class NCMetadataForSection: NSObject {
  298. var sectionValue: String
  299. var metadatas: [tableMetadata]
  300. var shares: [tableShare]
  301. var localFiles: [tableLocalFile]
  302. var searchResult: NCCSearchResult?
  303. var unifiedSearchInProgress: Bool = false
  304. private var sort : String
  305. private var ascending: Bool
  306. private var directoryOnTop: Bool
  307. private var favoriteOnTop: Bool
  308. private var filterLivePhoto: Bool
  309. private var metadatasSourceSorted: [tableMetadata] = []
  310. private var metadatasFavoriteDirectory: [tableMetadata] = []
  311. private var metadatasFavoriteFile: [tableMetadata] = []
  312. private var metadatasDirectory: [tableMetadata] = []
  313. private var metadatasFile: [tableMetadata] = []
  314. public var numDirectory: Int = 0
  315. public var numFile: Int = 0
  316. public var totalSize: Int64 = 0
  317. public var metadataShare: [String: tableShare] = [:]
  318. public var metadataOffLine: [String] = []
  319. init(sectionValue: String, metadatas: [tableMetadata], shares: [tableShare], localFiles: [tableLocalFile], searchResult: NCCSearchResult?, sort: String, ascending: Bool, directoryOnTop: Bool, favoriteOnTop: Bool, filterLivePhoto: Bool) {
  320. self.sectionValue = sectionValue
  321. self.metadatas = metadatas
  322. self.shares = shares
  323. self.localFiles = localFiles
  324. self.searchResult = searchResult
  325. self.sort = sort
  326. self.ascending = ascending
  327. self.directoryOnTop = directoryOnTop
  328. self.favoriteOnTop = favoriteOnTop
  329. self.filterLivePhoto = filterLivePhoto
  330. super.init()
  331. createMetadatasForSection()
  332. }
  333. func createMetadatasForSection() {
  334. // Clear
  335. //
  336. metadatasSourceSorted.removeAll()
  337. metadatasFavoriteDirectory.removeAll()
  338. metadatasFavoriteFile.removeAll()
  339. metadatasDirectory.removeAll()
  340. metadatasFile.removeAll()
  341. metadataShare.removeAll()
  342. metadataOffLine.removeAll()
  343. numDirectory = 0
  344. numFile = 0
  345. totalSize = 0
  346. // Metadata order
  347. //
  348. if sort != "none" && sort != "" {
  349. metadatasSourceSorted = metadatas.sorted {
  350. switch sort {
  351. case "date":
  352. if ascending {
  353. return ($0.date as Date) < ($1.date as Date)
  354. } else {
  355. return ($0.date as Date) > ($1.date as Date)
  356. }
  357. case "size":
  358. if ascending {
  359. return $0.size < $1.size
  360. } else {
  361. return $0.size > $1.size
  362. }
  363. default:
  364. if ascending {
  365. return $0.fileNameView.lowercased() < $1.fileNameView.lowercased()
  366. } else {
  367. return $0.fileNameView.lowercased() > $1.fileNameView.lowercased()
  368. }
  369. }
  370. }
  371. } else {
  372. metadatasSourceSorted = metadatas
  373. }
  374. // Initialize datasource
  375. //
  376. for metadata in metadatasSourceSorted {
  377. // skipped the root file
  378. if metadata.fileName == "." || metadata.serverUrl == ".." {
  379. continue
  380. }
  381. // skipped livePhoto
  382. if filterLivePhoto && metadata.livePhoto && metadata.ext == "mov" {
  383. continue
  384. }
  385. // share
  386. if let share = self.shares.filter({ $0.serverUrl == metadata.serverUrl && $0.fileName == metadata.fileName }).first {
  387. metadataShare[metadata.ocId] = share
  388. }
  389. // is Local / offline
  390. if !metadata.directory, CCUtility.fileProviderStorageExists(metadata) {
  391. let localFile = self.localFiles.filter({ $0.ocId == metadata.ocId }).first
  392. if localFile == nil {
  393. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  394. }
  395. if localFile?.offline ?? false {
  396. metadataOffLine.append(metadata.ocId)
  397. }
  398. }
  399. // Organized the metadata
  400. if metadata.favorite && favoriteOnTop {
  401. if metadata.directory {
  402. metadatasFavoriteDirectory.append(metadata)
  403. } else {
  404. metadatasFavoriteFile.append(metadata)
  405. }
  406. } else if metadata.directory && directoryOnTop {
  407. metadatasDirectory.append(metadata)
  408. } else {
  409. metadatasFile.append(metadata)
  410. }
  411. //Info
  412. if metadata.directory {
  413. numDirectory += 1
  414. } else {
  415. numFile += 1
  416. totalSize += metadata.size
  417. }
  418. }
  419. metadatas.removeAll()
  420. // Struct view : favorite dir -> favorite file -> directory -> files
  421. metadatas += metadatasFavoriteDirectory
  422. metadatas += metadatasFavoriteFile
  423. metadatas += metadatasDirectory
  424. metadatas += metadatasFile
  425. }
  426. }