NCDataSource.swift 19 KB

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