NCDataSource.swift 19 KB

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